[java] 이 빼기가 0이 아닌 이유는 무엇입니까?
This is because decimal numbers that "look" round in base 10, are not exactly representable in base 2 (which is what computers use to represent floating point numbers). Please see the article What Every Computer Scientist Should Know About Floating-Point Arithmetic for a detailed explanation of this problem and workarounds.
-------------------Floating-point inaccuracies (there are an infinite number of real numbers and only a finite number of 32- or 64-bit numbers to represent them with).
If you can't handle tiny errors, you should use BigDecimal
instead.
PrecisionEvaluate()
ColdFusion에서 사용 ( BigDecimal
Java에서 사용 )
zero = PrecisionEvaluate(416582.2850 - 411476.8100 - 5105.475);
와 달리 Evaulate()
""가 필요하지 않습니다.
컴퓨터는 숫자를 이진수로 저장하기 때문에 부동 숫자는 정확하지 않습니다. 1E-11은 이러한 십진수를 가장 가까운 표현 가능한 이진수로 반올림하기 때문에 작은 차이입니다.
-------------------이 "버그"는 버그가 아닙니다. 부동 소수점 산술이 작동하는 방식입니다. 참조 : http://docs.sun.com/source/806-3568/ncg_goldberg.html
Java에서 임의의 정밀도를 원하면 BigDecimal
다음을 사용하십시오 .
BigDecimal a = new BigDecimal("416582.2850");
BigDecimal b = new BigDecimal("411476.8100");
BigDecimal c = new BigDecimal("5105.475");
System.out.println(a.subtract(b).subtract(c)); // 0.0
-------------------문제는 부동 소수점 유형의 부정확 한 표현입니다. 이것들은 실수로 정확하게 표현 될 수 없기 때문에 약간의 정밀도 손실이 발생하여 작은 오류가 발생합니다. 일반적으로 부동 소수점을 사용하면 결과가 일부 작은 에피 슬론 (오류 요인) 내의 다른 값과 같은지 비교하려고합니다.
-------------------이들은 부동 소수점 문제이며 사용 BigDecimal
하면 문제가 해결됩니다.
빼기 순서를 변경하면 Google에서도 0이됩니다.
416582.2850 - 5105.475 - 411476.8100 = 0
출처
https://stackoverflow.com/questions/2002849