Re: Comparing floating point values in Java



Philipp wrote:
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));
}
}

Java does not permit reordering optimizations that could affect a
floating point result.

In strictfp mode, the exact, bit-by-bit, result is predictable from the
source code for any Java implementation.

Without strictfp, the implementation is allowed, but never required, to
use a wider exponent for some intermediate results. That may suppress an
overflow to infinity or underflow to zero (for numbers too tiny to be
represented as denormalized with the correct exponent width).

That would allow some calculations to get different results depending on
issues such as whether a value was kept in a register or stored to a
memory temporary.

If you are just concerned about issues such as whether Java will
arbitrarily reorder expressions, you are fine in default mode. If you
really need exact equality on every identical source calculation, you
need strictfp.

Patricia
.



Relevant Pages

  • 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; ... Java uses IEEE floating point. ...
    (comp.lang.java.programmer)
  • Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
    ... Comparing floating point values should be done using a tolerance:- ... In other cases the resulting 'near enough' value stored after a calculation ... the situation using CDbl) than to compare with "almost near ... CStr may no longer be what you want. ...
    (microsoft.public.scripting.vbscript)
  • Re: Can anyone repeat this?
    ... incrementing and decrementing by .01 (as represented by the compiler), ... Not a good idea to compare floating points like this though. ...
    (microsoft.public.vc.mfc)
  • Re: Strange basic problem with comparision of two varialbes defined as double
    ... > My application does need to compare a lot of values for accounting purposes. ... store data in floating point format to simplify the problem. ... those fractions representable by binary fractions are ... Your alternatives are to either use a representation that deals with the ...
    (microsoft.public.vstudio.development)