How do I write hexadeximal floating point literal in java?

257 views Asked by At

For number -5 x 10^-1 i.e., -0.5, I could write,

-5E-1f or -50E-2f or -0.05E+1F

as "decimal floating point literal" in java.

If the same number is written in "hexadecimal floating literal", I found it as

-0X32p-2f which is wrong. This value is -25.0. I considered 50 as 0x32 and came out with this notation.

How do I learn writing the above value -5 x 10^-1 in hexadecimal floating point literal?

2

There are 2 answers

1
Jesper On BEST ANSWER

See section 3.10.2 of the Java Language Specification:

In case of a hexadecimal floating point literal, the exponent is a binary exponent; it's a power of 2, not a power of 10 as in decimal floating point literals.

So 0x32p-2f means 50 times 2 to the power -2, not times 10 to the power -2.

A hexadecimal float literal that represents 0.5 would be 0x1p-1f.

0
shmosel On

Have a look at the documentation for Double.toHexString().