| View previous topic :: View next topic |
| Author |
Message |
Andy Guest
|
Posted: Fri May 07, 2004 2:44 pm Post subject: need code to convert float format to internal java float for |
|
|
JVM structure CONSTANT_Float_info keeps float value
in 4 bytes integer type. I need code to convert float to 4 bytes
according to JVM spec
Thanks a lot
|
|
| Back to top |
|
 |
Thomas Fritsch Guest
|
Posted: Fri May 07, 2004 3:23 pm Post subject: Re: need code to convert float format to internal java float |
|
|
Andy wrote:
| Quote: | JVM structure CONSTANT_Float_info keeps float value
in 4 bytes integer type. I need code to convert float to 4 bytes
according to JVM spec
Thanks a lot
float f = ...; |
int i = Float.floatToIntBits(f);
See
[url]http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html#floatToIntBits(float[/url])
--
Thomas<dot>Fritsch<squiggle>ops<dot>de
|
|
| Back to top |
|
 |
Andy Guest
|
Posted: Sat May 08, 2004 3:00 pm Post subject: Re: need code to convert float format to internal java float |
|
|
Thanks
But I need piece of code written in C.
| Quote: |
JVM structure CONSTANT_Float_info keeps float value
in 4 bytes integer type. I need code to convert float to 4 bytes
according to JVM spec
Thanks a lot
float f = ...;
int i = Float.floatToIntBits(f);
See
[url]http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html#floatToIntBits([/url] |
float)
| Quote: |
--
Thomas<dot>Fritsch<squiggle>ops<dot>de
|
|
|
| Back to top |
|
 |
Andy Guest
|
Posted: Mon May 10, 2004 1:26 pm Post subject: Re: need code to convert float format to internal java float |
|
|
This is code to convert bytes to float.
I need the reverse of this
bytes
The bytes item of the CONSTANT_Integer_info structure represents
the value of the int constant. The bytes of the value are stored in
big-endian (high byte first) order.
The bytes item of the CONSTANT_Float_info structure represents the
value of the float constant in IEEE 754 floating-point single format
(§3.3.2). The bytes of the single format representation are stored in
big-endian (high byte first) order.
The value represented by the CONSTANT_Float_info structure is
determined as follows. The bytes of the value are first converted into
an int constant bits. Then:
* If bits is 0x7f800000, the float value will be positive
infinity.
* If bits is 0xff800000, the float value will be negative
infinity.
* If bits is in the range 0x7f800001 through 0x7fffffff or in
the range 0xff800001 through 0xffffffff, the float value will be NaN.
* In all other cases, let s, e, and m be three values that
might be computed from bits:
int s = ((bits >> 31) == 0) ? 1 : -1;
int e = ((bits >> 23) & 0xff);
int m = (e == 0) ?
(bits & 0x7fffff) << 1 :
(bits & 0x7fffff) | 0x800000;
Then the float value equals the result of the mathematical expression
s·m·2e-150.
|
|
| Back to top |
|
 |
Pete Kirkham Guest
|
Posted: Mon May 10, 2004 7:24 pm Post subject: Re: need code to convert float format to internal java float |
|
|
Andy wrote:
| Quote: | This is code to convert bytes to float.
I need the reverse of this
....
Then the float value equals the result of the mathematical expression
s·m·2e-150.
You have three choices- |
1/ perform the opposite operations to what you posted and hope you don't
get too bad rounding errors.
2/ look up the float representation on your machine, use the cast Daniel
Sjöblom gave you, and maybe have to do some additional bit twiddling to
make your machine's representation big-endian IEEE.
3/ in the unlikely event that your machine cannot support IEEE directly,
find and use a IEEE library.
Pete
|
|
| Back to top |
|
 |
|