Is the rounding mode of large floats well set?

Consider the following code:

System.out.printf("%f%n", 123456796f);

      

This prints 123456800

on my system, so the value was rounded to 4. Is the mandate rounded in this situation, or is the compiler also opted to round by 4, yielding 123456792

?

+3


source to share


1 answer


The Java Language Specification, ยง3.10.2, Floating Point Literals :

Details on the correct conversion of input from the Unicode string representation of a floating point number to an internal binary floating point representation of IEEE 754 are described for the valueOf

class methods Float

and the class Double

package java.lang

.



Float.valueOf

Javadoc
:

s

regarded as representing an exact decimal value in common "computerized scientific notation" or as an exact hexadecimal value; this exact numeric value is then conceptually converted to an "infinitely precise" binary value, which is then rounded to type Float

using the normal rounding rule to the nearest IEEE 754 floating point arithmetic rule [...]

+3


source







All Articles