Problems with floating point.

J

John

Hi all,

Check out the following:

public class Test
{
public static void main(String[] args)
{
float x = 10.32f;
float y = 5.81f;
System.out.println(x - y); // Expecting 4.51; Outcome is 4.5099998
}
}

I'm expecting 4.51 as the result of (10.32 - 5.81), but the result being
displayed is 4.5099998. Can someone please explain why this is happening,
and how would I be able to limit the answer to two decimal places.

Thanks,

John.
 
R

Richard F.L.R.Snashall

John said:
Hi all,

Check out the following:

public class Test
{
public static void main(String[] args)
{
float x = 10.32f;
float y = 5.81f;
System.out.println(x - y); // Expecting 4.51; Outcome is 4.5099998
}
}

I'm expecting 4.51 as the result of (10.32 - 5.81), but the result being
displayed is 4.5099998. Can someone please explain why this is happening,
and how would I be able to limit the answer to two decimal places.

Thanks,

John.

Round off errors -- those numbers cannot be perfectly represented
inside the machine. Standard IEEE float would give you that much
precision.
 
D

derek

Check out the following:
public class Test
{
public static void main(String[] args)
{
float x = 10.32f;
float y = 5.81f;
System.out.println(x - y); // Expecting 4.51; Outcome is 4.5099998
}
}

I'm expecting 4.51 as the result of (10.32 - 5.81), but the result being
displayed is 4.5099998. Can someone please explain why this is happening,
and how would I be able to limit the answer to two decimal places.

(Quoted from Patricia) :

On any system, in any language, any data type based on IEEE754 64 bit
binary floating point has 53 bits, about 15.9 decimal digits, of precision.

Just as no decimal fraction can exactly represent 1/3 or 2/3,
so no binary fraction can exactly represent 1/10 or 2/10.

Subtracting numbers of equal magnitude with a matching leading decimal
digit costs a digit of precision, so it should be expected that the result will
be accurate to just under 15 decimal digits.

The unusual thing about Java in this area is the high precision of its default
String conversion for floating point values. Most languages by default round
to some reasonable number of digits.

Java converts precisely enough that if you took the toString result and parsed
it as a double you would recover the original value. This makes the rounding
error distressingly visible. To fix it, use DecimalFormat to limit the number of
digits displayed to what you actually want/need.

Example :

double one = 23.3;
double two = 22.2;
double three = one - two;

System.out.println(three); // Output : 1.1000000000000014
System.out.println( new DecimalFormat(".##").format(three) ); // Output : 1.1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top