posted 11 years ago

See the code below

class Jcp377

{

public static void main(String[] args)

{

float f7 = -2147483645.5f;

System.out.println("Round f7 is " + Math.round(f7));

}

}

Math.round() is returning incorrect values..

like for -2147483645.5f it should return -2147483645 but it is returning

-2147483648 and its returning unpredictable values... Please Explain!!!

class Jcp377

{

public static void main(String[] args)

{

float f7 = -2147483645.5f;

System.out.println("Round f7 is " + Math.round(f7));

}

}

Math.round() is returning incorrect values..

like for -2147483645.5f it should return -2147483645 but it is returning

-2147483648 and its returning unpredictable values... Please Explain!!!

posted 11 years ago

you should understand that the precision of the types float and double is limited. You cannot store numbers with an arbitrary number of digits in a float or double and expect them to hold the value exactly.

So, the behaviour you see is normal.

The precision of a float is about 6 decimal digits, so if you put a number with 11 decimal digits like -2147483645.5 in a float, it will not work as you expect.

So, the behaviour you see is normal.

The precision of a float is about 6 decimal digits, so if you put a number with 11 decimal digits like -2147483645.5 in a float, it will not work as you expect.