가능한 중복이 :
Precision of Floating Point자바 배정 밀도
double a=0.000001,b=50000;
b=a*b;
System.out.println("double:"+b); // -> double:0.049999999999999996
float a=0.000001f,b=50000;
b=a*b;
System.out.println("float"+b); // -> float0.05
나는 내 코드의 대부분에서 두 번 사용하고 오늘은이 문제를 발견했다. 어떻게 처리해야합니까?
상황 :
double []refValues= {100,75,50,25,15,5,1};
double bInMilliGram=b*1000;
double pickedVal=0;
for(double ref:refValues)
if(ref<=bInMilliGram) {
pickedVal=ref;
break;
}
System.out.println("bInMilliGram:"+bInMilliGram+", pickedVal:"+pickedVal);
O/P : -> bInMilliGram : 49.99999999999999, pickedVal : 25.0
http://floating-point-gui.de/ – BalusC
이것은 예상되는 부동 소수점 유형의 동작입니다. http://stackoverflow.com/questions/872544/precision-of-floating-point – Piskvor