[groovy-user] Shouldn't the Groovy truth value of NaN be false?

66 views
Skip to first unread message

Steve Tekell

unread,
Jan 27, 2010, 5:52:20 PM1/27/10
to us...@groovy.codehaus.org

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.

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


Roshan Dawrani

unread,
Jan 27, 2010, 11:20:28 PM1/27/10
to us...@groovy.codehaus.org
The javadoc of the number to boolean conversion in groovy says:

    /**
     * Coerce a number to a boolean value.
     * A number is coerced to false if its double value is equal to 0, and to true otherwise,
     **/

It seems to be consistent in doing it. So, it is at least by design and not a bug.

Should NaN values be specially handled here?

rgds,
Roshan

Jochen Theodorou

unread,
Jan 28, 2010, 5:07:42 AM1/28/10
to us...@groovy.codehaus.org
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?

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

Jim White

unread,
Jan 28, 2010, 10:42:53 AM1/28/10
to us...@groovy.codehaus.org
Jochen Theodorou wrote:

> 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

Steve Tekell

unread,
Jan 28, 2010, 11:31:56 AM1/28/10
to us...@groovy.codehaus.org

something like this

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.

Jochen Theodorou

unread,
Jan 28, 2010, 11:53:01 AM1/28/10
to us...@groovy.codehaus.org
Steve Tekell wrote:
[...]

> 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)

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/

Matthew...@thomsonreuters.com

unread,
Jan 28, 2010, 1:39:49 PM1/28/10
to us...@groovy.codehaus.org
FWIW I agree with the original post. It would seem more Groovy to me
(i.e. it would just make sense to me) to have NaN evaluate to false for
Groovy truth (in addition to the existing values that evaluate to
false).

Matt

Tom Nichols

unread,
Jan 28, 2010, 4:42:00 PM1/28/10
to us...@groovy.codehaus.org
For the record, Javascript treats NaN as false:

var test = NaN; // false
test = 0; // false
test = -1; // true

Steve Tekell

unread,
Jan 28, 2010, 5:32:26 PM1/28/10
to us...@groovy.codehaus.org

yes and since it's a dynamically typed
false values of x could be
null, empty-string, 0, false, and NaN

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.

Steve Tekell

unread,
Jan 28, 2010, 5:49:52 PM1/28/10
to us...@groovy.codehaus.org

I went ahead and created a request for this
http://jira.codehaus.org/browse/GROOVY-4018

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.

Reply all
Reply to author
Forward
0 new messages