Re: Comparing floating point values in Java



Philipp wrote:
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?

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

Most C++ implementations use IEEE floating point.

Java uses IEEE floating point.

You should expect the same floating point issues in
Java as in C++.

Your example is not equivalent to the example in your link.

I believe that the above should always write true in Java.

But in general you should not use == to compare floating
points in Java either.

Your example are just more simple than real life code.

Arne



.



Relevant Pages

  • 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)
  • Re: java performance on machines
    ... Primarily the goal is to compare cpu/memory power on different ... The idea is to write a series of tests in java, both integer and float ... If synthetic benchmarks are ok then you could look at the SPEC benchmarks ...
    (comp.lang.java.help)
  • Re: Comparing floating point values in Java
    ... when comparing floating point values in computers. ... In Java, will unchanged values compare strictly true if equal? ... In strictfp mode, the exact, bit-by-bit, result is predictable from the ...
    (comp.lang.java.programmer)
  • Re: Comparing floating point values in Java
    ... when comparing floating point values in computers. ... In Java, will unchanged values compare strictly true if equal? ... In strictfp mode, the exact, bit-by-bit, result is predictable from the ...
    (comp.lang.java.programmer)
  • Re: Comparing floating point values in Java
    ... In Java, will unchanged values compare strictly true if equal? ... float a = 1; ... I was calculating an array of float values from some user input. ...
    (comp.lang.java.programmer)

Quantcast