문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top