2

I am having a problem with this code! I am trying to run the code in java but the answer seems pretty weird.

float a=0.1F;
float b=0.2F;
if((a+b)==0.3){
System.out.println("True");
}
else{
System.out.println("False");
}

The answer is : False But theoretically it should be return True. The function returns True if we use values of a and b like 0.15 and 0.15 or 0.05 and 0.25. I am confused. I have read somewhere that languages like Java/JavaScript implements IEEE-754 number formatting! If so, then what is this formatting and what is wrong with the code? Is there anyway to change the number format?

Ankur Sharma
  • 283
  • 2
  • 13

5 Answers5

5

The precision difference between floats (a+b) and doubles (0.3) is causing the condition to be false. You could instead use (a+b)==0.3F. i.e.

float a = 0.1F;
float b = 0.2F;
if ((a + b) == 0.3F) {  // notice the "F"
    System.out.println("True");
} else {
    System.out.println("False");
}
True

EDIT: In this case, even if you use doubles the condition will be false:

double a = 0.1;
double b = 0.2;
if ((a + b) == 0.3) {
    System.out.println("True");
} else {
    System.out.println("False");
}
False

Printing 0.1 + 0.2 will reveal why (the number cannot be represented perfectly):

0.30000000000000004
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

(a + b) is a float, whereas 0.3 is a double. You should compare it with the same type:

if ((a + b) == 0.3F) {
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

simply add f to the end of the number to make the compiler differentiate between a double and a float:

float a=0.1F;
float b=0.2F;
if((a+b)==0.3f){
System.out.println("True");
}
else{
System.out.println("False");
}

UPDATE:

see this similar question too:

where the best answer states you should check loat ewuality using:

if(Math.abs((a+b) - 0.3F) < epsilon)

where epsilon is a very small number like 0.00000001, depending on the desired precision.

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Forget f! run the program with all the variables declared as double! – Ankur Sharma Nov 24 '12 at 17:42
  • @ankurSharma that wont work. As .1 /.2 /.3 they do not have exact binary representation. And it is advisable not to use == on float and double. – Mukul Goel Nov 24 '12 at 17:46
  • Yeah! That's what the problem with IEEE-754 number representing scheme. Its the problem in JavaScript too! Thanks for your time and help! – Ankur Sharma Nov 24 '12 at 17:51
1

Never compare Float/Double with == sign.

This could not work even if you put 'f' after number. The problem here is not format, it is data loss. The data loss happens because of converting 10 base to 2 base can also make condition false.

ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75
0
float a=0.1F;
float b=0.2F;
if((a+b)==0.3){
System.out.println("True");
}
else{
System.out.println("False");
}

.3 by default is a double type. You do see it as .3 ,but for computer its exact value is .2999998... something. Hence the difference.

If you explicitly tell compiler that .3 is float then your analogy will hold.

So

float a=0.1F;
float b=0.2F;
if((a+b)==0.3F){
System.out.println("True");
}
else{
System.out.println("False");
}

Follow this thread to satisfy your thirst for "double" issues

Community
  • 1
  • 1
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • Try compiling the program declaring all of the variables as double instead of float and then tell me what do you get! – Ankur Sharma Nov 24 '12 at 17:44
  • @ankurSharma i know about that .1 / .2 /.3 is not exact in float nor double. Are we playing some pop quiz? – Mukul Goel Nov 24 '12 at 17:47
  • but the program works perfectly for numbers declared as .15! What is the best way to compare the floating/double precision numbers? Or it is better to avoid their comparison! – Ankur Sharma Nov 24 '12 at 18:06
  • @Ankursharma this gives a good overview of approaches to use and things to avoid. Personally I'll always usr BigDecimal if i got to ever do comparison or manipulations on floatings.. Refer http://stackoverflow.com/questions/2896013/manipulating-and-comparing-floating-points-in-java – Mukul Goel Nov 24 '12 at 18:13