Haskell <-> Java and floats

pachinko@magicaldesk.com pachinko@magicaldesk.com
Sun, 30 Sep 2001 00:37:01 -0700


Has anyone ever tried to pass/receive a float to/from a Java app? If so, how did you manage it?

I need an algorithm for converting an IEEE 754 format float to a 32 bit integer?

I want something that can convert a haskell float into a int that can be stored to disk for later input to a Java app that converts it back to a float with the Java floatToIntBits function.

I think I'm OK in the reverse direction, converting an int into a float (as implemented by the Java intBitsToFloat function). The following link to the Java docs gives the bit masks and expression required to convert an int to a float.

http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Float.html

Unfortunately, although the page gives a brief description of the the floatToIntBits function, specfically how to place the components of the floating point number into a 32 bit int, it doesn't describe how to extract those components. Things seem to be complicated a bit by things such as the hidden bit, and Java interpreting the floating point to the far left of the mantissa, instead of the far right.

I found the following page describing how the floatToIntBits function works, but I think it's wrong, since when I try to run this function it gives a different integer that when I use the standard Java floatToIntBits function. Maybe the page describes a different version of the IEEE format?

http://www.jos.org/redist/kore-0.0.7/java.lang.Float.src.html

I'm not an expert on any of this - so any algorithm, in any language (including english), would be much appreciated.

BTW - all of the above applies double (64 bit integer, doubleToIntBits/intBitsToDouble).


_____________________________________________________________
Global Virtual Desktop
http://www.magicaldesk.com