NaN

Hi, Im converting a double to a string which is output to an applet window and I am getting the value NaN (Not-aNumber)

here is what i am doing...

distance=distance+(Math.sqrt((x2-x1)^2))+(Math.sqrt((y2-y1)^2));//calculate distance by taking the positive of coord2-coord1 (using square root of a square)

Distance=Double.toString(distance);//export distance as string for printing

[466 byte] By [thedigita] at [2007-10-2 9:39:28]
# 1
^2 is not "raised to the power 2". It's "bitwise XOR with the number 2".Presumably that's giving a negative value to pass to sqrt.You could have found this out with a couple of print statements.
jverda at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 2
Doh, that'll teach me for asking people how to do squares on irc
thedigita at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 3

> Doh, that'll teach me for asking people how to do

> squares on irc

LOL :-)

Seriously, though, in situations like this, it's very important to test your assumptions. You assumed x^2 was a squaring operation. But yet, you got a result that suggested that that or one of your other assumptions was wrong. It would have been very easy to print out x^2 and a couple of other values to see what was going on.

You'd have seen some weird number and probably not deduced that ^ is XOR, but you'd have known it's NOT raising to a power.

People here are happy to help, but in the future, please use a debugger or some print statements to at least check the main values and flow of your code.

jverda at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks!

ive found out that i need to use..pow(x,2) but ci ant get this to recognise it...

xdouble=x2-x1;

ydouble=y2-y1;

distance=distance+(Math.sqrt(pow (xdouble, 2)))+(Math.sqrt(pow (ydouble,2)));//calculate distance by taking the positive of coord2-coord1 (using square root of a square)

Distance=Double.toString(distance);//export distance as string for printing

thedigita at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 5
pow should be Math.pow
jschella at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 6
LoL yep, i figured it out just before you posted. Thanks for the help!!!
thedigita at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 7
And you're taking the square root of the squares. Pythagoras is oftenmore useful (unless you live in Manhatten).double distance = Math.sqrt(Math.pow(xdouble, 2) + Math.pow(ydouble, 2));
pbrockway2a at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 8
once my float is converted to a string it is displayed for example as 1002.0, and the number should never have a decimal value (so is an integer). Is there any way I can display my string as an integer value? i.e without the .0 at the end?
thedigita at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 9
java.text.DecimalFormat
jverda at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 10
String s = Integer.toString((int)distance);
james972a at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...
# 11
> pow should be Math.powNot if s/he did previously:import static java.lang.Math.pow;:-)
jfbrierea at 2007-7-16 23:45:30 > top of Java-index,Java Essentials,Java Programming...