# Floating point number format

Miro Ricco
Ranch Hand
Posts: 40
Hi all
Can someone tell me what am I doing wrong ?
I want to implement an math midlet application, but I can't find double or float class.
Regards
Miro

Christian Wolf
Greenhorn
Posts: 28
Floating point operations are expensive. Therefore the MIDP does not include Float and Double classes and math operations for these classes.
You can emulate floating point by calculating with very big long types. For example, if you need a precision of 0.000001 then multiply all your arguments with 1000000, make your calculations based on Integers, make a string from the result and then insert a comma at the correct position. The resulting string is then the floating point result of your calculation. If you wrap this into a class, you might have a pseudo Float class that you can use for your program itself
Christian Wolf
[ January 20, 2004: Message edited by: Christian Wolf ]

Ko Ko Naing
Ranch Hand
Posts: 3178
Originally posted by Christian Wolf:
For example, if you need a precision of 0.000001 then multiply all your arguments with 1000000, make your calculations based on Integers, make a string from the result and then insert a comma at the correct position.
[ January 20, 2004: Message edited by: Christian Wolf ]

Do u mean "a decimal point", instead of "a comma"? So that it will become like a decimal notation, since MIDP does not contain such Float or Double class...
Correct me, if I am wrong...

Christian Wolf
Greenhorn
Posts: 28
Do u mean "a decimal point", instead of "a comma"? So that it will become like a decimal notation, since MIDP does not contain such Float or Double class...

You are right, of course, I mean "decimal point", the delimiter character between trunc(c) and (c-trunc(c)) *g* It's called "comma" in my language.
So in result you get a class being able to make fix point calculations based on strings as arguments and longs as internal representation of fix point numbers.
Christian Wolf

Ko Ko Naing
Ranch Hand
Posts: 3178
Do u mean that class is something like a long value inside and a string value to the output? It will need more processing... So is it like there is a trade-off between processing time and memory location?

Christian Wolf
Greenhorn
Posts: 28
Do u mean that class is something like a long value inside and a string value to the output? It will need more processing... So is it like there is a trade-off between processing time and memory location?

Yes, that's what I mean. It needs slightly more processing, of course, than just calculating pure integer. But if you need to work with floats then you have to make something like that on your own. Memory is not the big problem with this.
Christian Wolf

Ko Ko Naing
Ranch Hand
Posts: 3178
I got it!!! Thank you very much for your patient explanation...

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
Also note that MIDP 2.0 supports floats.
There is also a package out there called MathFP or Math something or other.
I actually created my own class to handle floats. The only difference is that the user does not enter in the decimal point, they enter the whole number, and it automatically puts in a decimal point at the second place. I am using Currency. Then I do math using the Strings and replacing the Decimal back in to its correct space. It was pretty easy to do with Strings.
Mark

Ko Ko Naing
Ranch Hand
Posts: 3178
Originally posted by Mark Spritzler:
I actually created my own class to handle floats. The only difference is that the user does not enter in the decimal point, they enter the whole number, and it automatically puts in a decimal point at the second place. I am using Currency. Then I do math using the Strings and replacing the Decimal back in to its correct space. It was pretty easy to do with Strings.
Mark

Hi Mark,
Do u feel it that it is some kinda trade-off between processing time and memory space, when dealing with floating point numbers? Since MIDP 2.0 supports floats, I guess it is because the mobile devices nowadays are coming with pretty much memory space then before...
It would be great, if u have time to explain ur opinion on it...

serge masse
Ranch Hand
Posts: 102
CLDC 1.0 does not support float and double primitives. CLDC 1.1 does support float and double primitives, as well as Float and Double classes and other Math floating point methods.
As of today, in a JRE (VM) in a specific device, MIDP 1.x or 2.0 can be with either CLDC 1.0 or 1.1. This is what makes a J2ME JRE support or not support floating point arithmetics.
CLDC 1.1 is new and few devices support it now but many will later in 2004.

serge masse
Ranch Hand
Posts: 102
to be precise, MIDP 2 with CLDC 1.0 does not support floating point arithmetics, but MIDP 2 with CLDC 1.1 does.
and we may never see an implementation of MIDP 1.x with CLDC 1.1.
[ January 20, 2004: Message edited by: serge masse ]