Comparing floating point values in Java



Hello,
I'm aware of problems (rounding, NaN etc) when comparing floating point values in computers.
In C++ this goes a bit further as you cannot compare with certitude floating point numbers even if you have made exactly the same operations on each of them (see eg: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.18 )

My question: In Java, will unchanged values compare strictly true if equal?
Is it _guaranteed_ that the following code does output "true" on all JVMs?

Thanks for answers. Phil

example code:
public class Test {
public static void main(String[] args) {
float a = 1;
float b = 1;
System.out.println("a == b?: " + (a == b));
}
}
.



Relevant Pages

  • Re: Comparing floating point values in Java
    ... when comparing floating point ... will unchanged values compare strictly true if equal? ... float a = 1; ... public static boolean areEqual(float a, float b, float delta) { ...
    (comp.lang.java.programmer)
  • Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
    ... values are stored there will be very small rounding errors. ... Comparing floating point values should be done using a tolerance:- ... Most newbies find this situation shocking yet it's pratically as old as ... the situation using CDbl) than to compare with "almost near ...
    (microsoft.public.scripting.vbscript)
  • Re: pointer and array
    ... impossible to compare the values, ... int 3 and the float 3.14, that are equal when we convert them both ... the "double" 33554433.0 becomes 33554432.0 after conversion to ...
    (comp.lang.c)
  • Re: compare two float values
    ... float t, check; ... Don't compare for equality, won't work for float, rather compare ... compatible implementation, exactly one of these three will be true ... operations are usually not exact, and two calculations that you think ...
    (comp.lang.c)
  • java performance on machines
    ... Primarily the goal is to compare cpu/memory power on different operations, ... The idea is to write a series of tests in java, both integer and float based, which I then run on the machines to compare their performance. ... I was thinking of writing tests based on float operations and on integer operations. ...
    (comp.lang.java.help)