Re: Comparing floating point values in Java
- From: Thomas Schodt <spamtrap0606@xxxxxxxxxxxxxxxxx>
- Date: Sat, 30 Dec 2006 17:51:55 +0000
Philipp wrote:
I'm aware of problems (rounding, NaN etc) when comparing floating point values in computers.
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));
}
}
I would suggest that
any JVM where it would output false
is probably non-compliant.
Your example may be somewhat lacking
as many scalar values
and many fractions where the denominator is a cardinal exponent of 2
correspond exactly
to a floating point representation.
Personally I like to think of floating point values
as representing bounded infinite sets of real values
(or not bounded - for positive and negative infinity).
There are some that take a dim view on the liberties Sun / Gosling took in the way Java deals with IEEE754 values, eg.;
<http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf>
.
- References:
- Comparing floating point values in Java
- From: Philipp
- Comparing floating point values in Java
- Prev by Date: Re: Comparing floating point values in Java
- Next by Date: Re: How to check variables for uniqueness ?
- Previous by thread: Re: Comparing floating point values in Java
- Next by thread: Re: Comparing floating point values in Java
- Index(es):
Relevant Pages
|
|