Question

If I have a double d; if I do:

d = 1/d;
d = 1/d;

Is this possible that I lose precision? In what case for example?

Was it helpful?

Solution

Yes, ofcourse it's possible to loose precision. Floating-point types such as float and double are not infinitely precise.

double d = 123456789.0;

System.out.println(d);
d = 1 / d;
d = 1 / d;
System.out.println(d);

output:

1.23456789E8
1.2345678899999999E8

OTHER TIPS

If you use double or float then yes you may well lose precision.

However, if you use a Rational class such as this one you will avoid precision loss.

Because computer systems use a fixed precision to represent numbers, you will find that d does not always exactly equal ( 1/(1/d)), even though this equation is clearly algebraically equal.

Some reading material: enter link description here you may want to search for more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top