Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java floating-point numbers representation as a hexadecimal numbers

Why does 0x1p3 equal 8.0? Why does 0x1e3 equal 483, whereas 0x1e3d equals 7741? It is confusing since 1e3d equals 1000.0.

like image 296
serious Avatar asked Dec 28 '10 13:12

serious


People also ask

What is the difference between hexadecimal and decimal floating point constants?

As such, hexadecimal floating-point constants have exact representations in binary floating-point, unlike decimal floating-point constants, which in general do not.

What is hexadecimal in Java?

Java supports hexadecimal floating-point constants much like C, with some slight difference in format. For example, it doesn’t print the ‘+’ sign for positive exponents, and it prints powers of two with a radix point followed by a zero (for example, 2 8 prints as 0x1.0p8).

What are the floating point types in Java?

This is specified as part of the Java Language Specification 4.2: The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers. a sign bit: 0 meaning it is positive.

What is the scientific notation for floating point number?

A floating-point number is typically expressed in the scientific notation, with a fraction ( F ), and an exponent ( E) of a certain radix ( r ), in the form of F×r^E. Decimal numbers use radix of 10 ( F×10^E ); while binary numbers use radix of 2 ( F×2^E ). Representation of floating point number is not unique.


2 Answers

0x1e3 and 0x1e3d are hexadecimal integer literals. Note that e and d are hexadecimal digits, not the exponent indicator or double type indicator in this case.

1e3d is a decimal floating-point literal. The e is the exponent indicator, the d says that this is a double rather than a float.

The notation 0x1p3 is a way to express a floating-point literal in hexadecimal, as you can read in section 3.10.2 of the Java Language Specification. It means 1 times 2 to the power 3; the exponent is binary (so, it's 2-to-the-power instead of 10-to-the-power).

like image 164
Jesper Avatar answered Oct 20 '22 04:10

Jesper


0x1e3 is hex for 483, as is 0x1e3d hex for 7741. The e is being read as a hex digit with value 14.

like image 20
moinudin Avatar answered Oct 20 '22 02:10

moinudin