Manipulating and comparing floating points in java
Posted
by Praneeth
on Stack Overflow
See other posts from Stack Overflow
or by Praneeth
Published on 2010-05-24T09:42:53Z
Indexed on
2010/05/24
10:21 UTC
Read the original article
Hit count: 300
In Java the floating point arithmetic is not represented precisely. For example following snippet of code
float a = 1.2;
float b= 3.0;
float c = a * b;
if(c == 3.6){
System.out.println("c is 3.6");
} else {
System.out.println("c is not 3.6");
}
actually prints "c is not 3.6".
I'm not interested in precision beyond 3 decimals (#.###). How can I deal with this problem to multiply floats and compare them reliably?
Thanks much
© Stack Overflow or respective owner