Package net.dclausen.microfloat

Provides classes for performing IEEE-754 floating-point arithmetic on JVM implementations which lack native float and double datatypes.


Class Summary
MicroDouble A software implementation of IEEE-754 double precision math which does not rely on the double data type.
MicroFloat A software implementation of IEEE-754 single precision math which does not rely on the float data type.

Package net.dclausen.microfloat Description

Provides classes for performing IEEE-754 floating-point arithmetic on JVM implementations which lack native float and double datatypes. This is intended for use on "micro" devices such as mobile phones, which implement the CLDC 1.0 specification. Note that CLDC 1.1 includes native floating-point support, so you don't need this library when running on a CLDC 1.1 JVM.

The classes in this package overload the int and long data types by storing float and double data in them, respectively. The format is the same one used by Float.floatToIntBits(float) Double.doubleToLongBits(double) When working with these values, you should be careful to avoid integer arithmetic, comparison, and typecasts, all of which can lead to meaningless results. For the sake of simplicity, parameter types and return values are frequently referred to in this documentation as double or float types when their actual type is long or int. Although literally incorrect, this means that the values are treated as a double or float.

Generally speaking, the methods in this package duplicate J2SE native floating point functions exactly. However there are some exceptions:

  1. All arithmetic is performed in FP-Strict mode. Since most J2SE JVMs use extended precision for intermediate calculations, this can cause minor discrepancies in results for primitive operations unless they are in a strictfp block, and for elementary function like sin and log, for which there is no way to disable use of extended precision.
  2. The String conversion methods use a different technique from the implementations in Float and Double, and are sometimes less accurate. I believe the error is no more than one ulp in all cases. Although not identical to their J2SE counterparts, toString and parseFloat / parseDouble should at least be consistent with one another. In other words MicroDouble.parseDouble(MicroDouble.toString(d)) == d.
  3. There are a few bugs in various J2SE implementations. I myself have found bugs in Math.rint and Math.round, and others have reported problems with Math.pow and Math.tan.
  4. There may be bugs in this code. Caveat emptor.
Using this package, you should be able to port most code which uses floating-point numbers to run on a CLDC 1.0 JVM. Here is a simple example:

// old function using native floating point arithmetic
public double averageThreeNumbers(double a, double b, double c) {
  return (a + b + c) / 3;

// new function using microfloat arithmetic
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:

public static void main(String[] args) {
When building this package for a J2ME midlet or other memory-sensitive target, you should use an obfuscator such as RetroGuard or ProGuard. This will strip out code for methods which you aren't using, in addition to doing other size optimizations. Typically this results in a much smaller JAR file.

Visit the project homepage at for more information and updates.

Related Projects


Copyright (C) 2003, 2004 David Clausen

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Portions of this software are derived from FDLIBM, which contained the
following notice:

Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.

Developed at SunSoft, a Sun Microsystems, Inc. business.
Permission to use, copy, modify, and distribute this
software is freely granted, provided that this notice 
is preserved.

See Also:
Float, Double, Math, JLS section 4.2.3, JLS chapter 15, Numerical computation guide