I just discovered that while this works in dynamic Groovy, in at least in 
Groovy 2.4.3 when using @CompileStatic, it works as Java does and not as 
dynamic Groovy does. Therefore, I will assume it is not safe to rely on ==/!= 
Float.NaN working properly. Even if this is to be considered a bug in static 
compiler, the fact that a released version of Groovy is inconsistent means that 
we should never use this code pattern.

Jason

From: Winnebeck, Jason [mailto:jason.winneb...@windstream.com]
Sent: Monday, September 21, 2015 10:15 AM
To: users@groovy.incubator.apache.org
Subject: Is NaN ==/!= NaN guaranteed to be consistent in all Groovy versions?

I see in Groovy that == and != work to compare variables against NaN. The 
rationale seems to be that compareTo is used instead of primitive comparisons 
or equals. Is this behavior guaranteed to be defined this way in all future 
versions of Groovy?

I've come across some Groovy code that is doing "x != Float.NaN" and in all of 
the test cases it seems to work. I ask because I'm afraid of primopts or static 
compiler in the future perhaps replacing the float to float comparison with a 
primitive !=, which returns false always in Java, but if this is a locked in 
behavior for Groovy, then I won't worry about refactoring existing code.

I see that https://issues.apache.org/jira/browse/GROOVY-3309 addresses this and 
Jochen responded to it but it was left open instead of closed as "won't fix", 
which concerns me that Groovy team may introduce a breaking change in the 
future.

Jason
________________________________
This email message and any attachments are for the sole use of the intended 
recipient(s). Any unauthorized review, use, disclosure or distribution is 
prohibited. If you are not the intended recipient, please contact the sender by 
reply email and destroy all copies of the original message and any attachments.

Reply via email to