Point2D.Distance method returns NaN, how is this possible?

D

DiscoStu

Hello Everyone,

Im having some really weird problems, Im using jdk1.4.1 and Im
using the java.awt.geom.Point2D's distance method to calculate the
distance between two ships in the game im writing. Well, the first
time I call the Point2D.distance method it returns NaN, then with
subsequent calls of the SAME valid arguments it returns a valid
number.

When I try and do a if(value == Double.NaN) on the NaN value it
doesent equate to true..... Well, Its either the Point2D.Distance()
returning NaN or the System.out.println is not printing out the value
properly and printing NaN instead.

Has anyone else encountered this problem? I use the static
Point2D.Distance method extensivly and it works 98% of the time, and
then returning NaN the other two.

Thanks, Greg.
 
J

Jos A. Horsmeier

Roedy Green said:
those weird NaN Infinity etc. don't behave in any way reassembling
sanity. See http://mindprod.com/gotchas.html#NAN

What is so weir about a NaN? It is exactly what it says -- Not A Number.
NaN values are the result of a/b or a*b where both a and b equal plus or
minus zero or plus or minus infinity or NaN itself, or unsuitable
combinations thereof, thats' all there is to it.

The IEEE754 format allows for only one notion of +/- infinity, so
infinities can be succesfully compared. NaNs are the result of
different (failing) operations so they can never be compared.

Normally, if an operation results in a NaN, consider it a bug in
the code.

kind regards,

Jos
 
D

DiscoStu

Normally, if an operation results in a NaN, consider it a bug in
the code.

Normall I would let that one go but your kicking me where it hurts :)
Maybe it is a bug in the code but it would have to be in the
Point2D.Distance() API code, because Im calling it each time with
valid numbers. It fails the first time, works the second time.... with
EXACTLY the same arguments. And it seems to be the first in a "stack"
of calls I make to Distance(), that first call always returns NaN for
some reason.
 
W

William Brogden

DiscoStu said:
Normall I would let that one go but your kicking me where it hurts :)
Maybe it is a bug in the code but it would have to be in the
Point2D.Distance() API code, because Im calling it each time with
valid numbers. It fails the first time, works the second time.... with
EXACTLY the same arguments. And it seems to be the first in a "stack"
of calls I make to Distance(), that first call always returns NaN for
some reason.

I'm betting one of your inputs is NaN.
 
B

Babu Kalakrishnan

Im having some really weird problems, Im using jdk1.4.1 and Im
using the java.awt.geom.Point2D's distance method to calculate the
distance between two ships in the game im writing. Well, the first
time I call the Point2D.distance method it returns NaN, then with
subsequent calls of the SAME valid arguments it returns a valid
number.

The only reason I can think of where the distance method can return a
NaN is if one of the input numbers is a NaN. Just print out the
numbers before you make the call and verify (and fix the reason for
the same).
When I try and do a if(value == Double.NaN) on the NaN value it
doesent equate to true..... Well, Its either the Point2D.Distance()
returning NaN or the System.out.println is not printing out the value
properly and printing NaN instead.

This is documented in the API. The == operator will not return true if
the numbers on the two sides of the expression are NaNs (because two
NaNs cannot be said to be "equal"). Try the Double.isNan(value) method
instead to test if "value" is NaN.

BK
 
M

Mark Thornton

Roedy said:
I don't dispute that. However, like quantum mechanics, the behaviours
they have to exhibit sound deranged.

I am a mathematician ... :)

Mark Thornton
 
D

Dale King

Harald Hein said:
That's the way it is defined. NaN == NaN always gives false, not true.
Somewhere in Double there is a method to test for NaN. Something like
isNaN().

And that is actually how isNaN is defined. It checks that the number is not
equal to itself.
 
R

Roedy Green

I am a mathematician ... :)

Officially so am I, though I studied organic chemistry and math.
Computer science has always been my first love though.

I always felt that mathematical proofs were cheating. You never
clearly enough laid down the ground rules. So often the technique used
to prove something seemed more iffy than the proposition itself.

With computers came the possibility of a very clearly defined set of
rules that could be mechanically applied.

The sorts of proof I favoured had many small steps, each of which had
no leap of faith associated with it. My profs detested them. They
liked clever short proofs.

I am pleased to hear that mathematical discoveries are now being made
by my sort of proof, mechanically generated.

I would bring back problems from my summer work. The UBC
mathematicians were not interested in them at all. The problems were
"intractable" or "finite" and hence potentially solved by trying
trillions of possibilities. Instead they preferred problems involving
things like Lebesque measure theory, trusting that someday someone
would find an application for it.

I remember presenting one problem, to the visiting famous
mathematician Anthony Ralston: how to connect points to form a similar
curve that a Statler Mars flex rule. (We had been doing cartoons on
the pen plotter, and needed better behaved interpolation.) He publicly
bawled me out for presenting such a silly problem. The question was
mathematically meaningless. Adobe's John Warnock did not think the
idea was so silly, and used Bezier curves to help interpolate.

The course I enjoyed most was the one on combinations and permutations
and other finite math. It seemed so much cleaner. The definitions
needed no handwaving.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top