Haskell <-> Java and floats

[email protected] [email protected]
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.


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?


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