File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes Floating point number format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Floating point number format" Watch "Floating point number format" New topic
Author

Floating point number format

Miro Ricco
Ranch Hand

Joined: Apr 16, 2002
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

Joined: Dec 15, 2003
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

Joined: Jun 08, 2002
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Christian Wolf
Greenhorn

Joined: Dec 15, 2003
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

Joined: Jun 08, 2002
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?
Thank you for your reply...
Christian Wolf
Greenhorn

Joined: Dec 15, 2003
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

Joined: Jun 08, 2002
Posts: 3178
I got it!!! Thank you very much for your patient explanation...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
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...
Thank you in advance...
serge masse
Ranch Hand

Joined: Jul 23, 2003
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 - http://goo.gl/GgF7R - my android apps
serge masse
Ranch Hand

Joined: Jul 23, 2003
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Floating point number format