# MicroFloat for floating point numbers in CLDC 1.0

Compatibility
Platform(s):
MIDP 1.0

Article
Created: firt (01 Apr 2007)
Last edited: lpvalente (14 Sep 2013)

This article gives a brief introduction of the MicroFloat library, which provides methods for performing arithmetic using floating point numbers, using values stored in long variable types to simulate double and float primitive types. The MicroFloat library is one of the several libraries that exist for getting around the limitation of CLDC 1.0 (no floating point primitive types).

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.

`// old function using native floating point arithmetic in CLDC 1.1public double averageThreeNumbers(double a, double b, double c) {  return (a + b + c) / 3;} // new function using MicroFloat arithmetic in CLDC 1.0import 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);}`
`// Desktop Console Java SE application to get a MicroFloat constantpublic static void main(String[] args) {  System.out.println(Long.toHexString(Double.doubleToLongBits(3)));}`