def x = Double.NaN
if (x) println "${x} is True"
else println "${x} is False"
-----------
NaN is True
I expect NaN to be false; this seems to be more consistent with the Groovy
notation of truth.
Steve
--
View this message in context: http://old.nabble.com/Shouldn%27t-the-Groovy-truth-value-of-NaN-be-false--tp27348256p27348256.html
Sent from the groovy - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
that is a bit abstract... assuming you get false for x being null, 0 and
NaN, then what do you do with that information in combination with
Groovy truth?
bye blackdrag
--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
> Steve Tekell wrote:
>
>> Is there a valid reason why NaN is true in Groovy?
>>
>> def x = Double.NaN
>> if (x) println "${x} is True"
>> else println "${x} is False"
>> -----------
>> NaN is True
>>
>> I expect NaN to be false; this seems to be more consistent with the
>> Groovy
>> notation of truth.
>
>
> that is a bit abstract... assuming you get false for x being null, 0 and
> NaN, then what do you do with that information in combination with
> Groovy truth?
Since NaN means "nothing" in terms of being a number just as null means
"nothing" in terms of being an object, it would more make sense for NaN
to be Groovy false rather than true.
The possible usefullness I can imagine would be where you want to do a
test to verify that you have a valid number before proceeding to do a
calculation and that zero would not be valid either.
OTOH, NaN is not going to behave just like null anyhow. For one thing
null is less than zero while NaN is greater than zero. And as things
are now, you can't compare NaN (or numbers) to a string at all. The NaN
being greater than zero (and greater than null too of course) is part of
its definition (Float.compareTo):
> Float.NaN is considered by this method to be equal to itself and
> greater than all other float values (including
> Float.POSITIVE_INFINITY).
The trouble with changing this now, is that using it depends on arcane
Groovy knowledge and making the behavior different across Groovy version
just makes it even more arcane and could wipe out any possible tiny
benefit of knowing it.
In any case, the behavior needs to be documented.
Jim
def x = geom?.calcCentroid()?.x
myDomainClass.DoubleForLongitude = x ?: null;
or
def denominator = calcDenom(....)
if (denominator) { doFormula(...) }
or
def result = mathFormula(....)
if (result) {
//do something with the non-null numeric result
}
If a calculation returns null, the result is false.
But if a calculation returns NaN, the result is true.
It seems in the spirit of Groovy to clean up some of this Java mess.
Groovy turns
if (x != null && x != '')
into
if (x)
so I thought it would also be groovy to turn
if (x != null && !Double.isNaN(x))
into
if (x)
Steve
Jochen Theodorou wrote:
>
> that is a bit abstract... assuming you get false for x being null, 0 and
> NaN, then what do you do with that information in combination with
> Groovy truth?
>
--
View this message in context: http://old.nabble.com/Shouldn%27t-the-Groovy-truth-value-of-NaN-be-false--tp27348256p27358529.html
Sent from the groovy - user mailing list archive at Nabble.com.
but it is not only null and NaN, it is also 0.
bye blackdrag
--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
Matt
var test = NaN; // false
test = 0; // false
test = -1; // true
If one needs to discriminate between any of those values they must go beyond
truth value. I am not saying that NaN == null == 0 == false == ""
Also, 0 and false are already handled by Java's notation of truth. My
example just shows the Groovy improvements over Java that lead one to intuit
the Groovy truth value of NaN being false (like JavaScript as someone
pointed out).
Jochen Theodorou wrote:
>
> but it is not only null and NaN, it is also 0.
>
--
View this message in context: http://old.nabble.com/Shouldn%27t-the-Groovy-truth-value-of-NaN-be-false--tp27348256p27364226.html
Sent from the groovy - user mailing list archive at Nabble.com.
If it can't be done, then at least there's a place to explain why.
--
View this message in context: http://old.nabble.com/Shouldn%27t-the-Groovy-truth-value-of-NaN-be-false--tp27348256p27364492.html
Sent from the groovy - user mailing list archive at Nabble.com.