Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 08:53, 25 September 2007.

How to use floating point numbers in CLDC 1.0

From Forum Nokia Wiki


If you are working for targets with MIDP 1.0, you'll probably have to deal with CLDC 1.0, with the lack of floating point support. So, you can't use float or double types in your code.

This tip helps you in these situation with an Open Source library from David Clausen called MicroFloat. The use of this library is very slow (because of the lack of floating hardware on the device) but it will work.

MicroFloat is a Java software library for doing IEEE-754 floating-point math on small devices which don't have native support for floating-point types. Basically this means Java-powered mobile phones (J2ME CLDC 1.0).

In this package you'll get support for 32-bit "float" and 64-bit "double" data types, including all primitive operations supported by Java SE (add, subtract, multiply, divide, mod, comparisons, typecasts) as well as a full reproduction of all methods in java.lang.Math (sin, cos, exp, pow, log, etc.). In theory, these operations should return results which are fully compliant with the IEEE-754 and Java SE specs.

You can download it from http://www.dclausen.net/projects/microfloat and browse the documentation in JavaDoc format in http://www.dclausen.net/projects/microfloat/javadoc/index.html

Then, you've just change your CLDC 1.1 code like this:

// old function using native floating point arithmetic in CLDC 1.1
public double averageThreeNumbers(double a, double b, double c) {
  return (a + b + c) / 3;
}
 
// new function using MicroFloat arithmetic in CLDC 1.0
import net.dclausen.microfloat.*;
 
private static final long THREE = 0x4008000000000000L; 
public long averageThreeNumbers(long a, long b, long c) {
  return MicroDouble.div(MicroDouble.add(MicroDouble.add(a, b), c), THREE);
}

You can obtain constants like THREE above by having a simple J2SE helper class with a main method like this:

// Desktop Console Java SE application to get a MicroFloat constant
public static void main(String[] args) {
  System.out.println(Long.toHexString(Double.doubleToLongBits(3)));
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
6630: float:left problem srezic Browsing and Mark-ups 2 2004-11-29 11:19
I need a developer marcushoverby Mobile Java General 1 2002-09-06 20:14
Why CLDC Bhargi Mobile Java General 1 2004-04-06 10:12
Trap Incomming Call piksale PC Suite API and PC Connectivity SDK 1 2005-08-04 13:06
CLDC 1.1 for Nokia 3650 Sebarry General Discussion 0 2003-08-18 12:32
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZjavaQ
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX
     
    
            
            RDF Facets:
            
            
                        qfnZuserE5ftagQSxmathX