473,413 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

not a number double

oll3i
679 512MB
why
distance+=Math.sqrt(Math.pow((double)wiersz_analiz owany[i],(double)2)-Math.pow((double)czyNajblizszySasiad[i],(double)2));

returns NaN

thank YOU
Jul 8 '07 #1
14 3359
JosAH
11,448 Expert 8TB
why
distance+=Math.sqrt(Math.pow((double)wiersz_analiz owany[i],(double)2)-Math.pow((double)czyNajblizszySasiad[i],(double)2));

returns NaN

thank YOU
Remember the poor men's debugger System.out.println()? Apply it.
(hint: negative values don't go well for the Math.sqrt() method).

kind regards,

Jos
Jul 8 '07 #2
odefta
18
Correct.
make a loop and ...
System,out.println(czyNajblizszySasiad[i])

Remember the poor men's debugger System.out.println()? Apply it.
(hint: negative values don't go well for the Math.sqrt() method).

kind regards,

Jos
Jul 9 '07 #3
oll3i
679 512MB
i did system.out.printlns and the values are correct they are 0 ,1 ,2 or 3
because test data are rows consisted of 0 1 2 or 3 values
Jul 9 '07 #4
JosAH
11,448 Expert 8TB
i did system.out.printlns and the values are correct they are 0 ,1 ,2 or 3
because test data are rows consisted of 0 1 2 or 3 values
For all the operands of your expression print out all the operands and the
values of the evaluated (sub) expressions. a NaN (Not a Number) doesn't come
from nowhere.

kind regards,

Jos
Jul 9 '07 #5
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. public double liczDystans(Integer[] czyNajblizszySasiad){
  2.     for(int i=0;i<liczbaColumn;i++){
  3.     dystans+=Math.sqrt(Math.pow((double)wiersz_analizowany[i],(double)2)-Math.pow((double)czyNajblizszySasiad[i],(double)2));
  4.  
  5.     System.out.println("wiersz_analizowany[i]="+wiersz_analizowany[i]);
  6.     System.out.println("Math.pow((double)wiersz_analizowany[i],(double)2)"+Math.pow((double)wiersz_analizowany[i],(double)2));
  7.  
  8.     System.out.println("czyNajblizszySasiad[i]="+czyNajblizszySasiad[i]);
  9.     System.out.println("Math.pow((double)czyNajblizszySasiad[i],(double)2)"+Math.pow((double)czyNajblizszySasiad[i],(double)2));
  10.  
  11.     System.out.println("dystans="+dystans);
  12.     }
  13.     return dystans;
  14. }
  15.  

System.out.println("Math.pow((double)wiersz_analiz owany[i],(double)2)"+Math.pow((double)wiersz_analizowany[i],(double)2));

and
System.out.println("Math.pow((double)czyNajblizszy Sasiad[i],(double)2)"+Math.pow((double)czyNajblizszySasiad[i],(double)2));

return a double sqrt

Math.sqrt(Math.pow((double)wiersz_analizowany[i],(double)2)-Math.pow((double)czyNajblizszySasiad[i],(double)2));

returns nan ?
Jul 10 '07 #6
oll3i
679 512MB
it may happen that in my for loop dystans is equal to NaN but what happens if i add a double to NaN does it still return NaN ?
Jul 10 '07 #7
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. public double liczDystans(Integer[] czyNajblizszySasiad){
  2.     for(int i=0;i<liczbaColumn;i++){
  3.     dystans+=Math.sqrt(Math.pow((double)wiersz_analizowany[i],(double)2)-Math.pow((double)czyNajblizszySasiad[i],(double)2));
  4.  
  5.     System.out.println("wiersz_analizowany[i]="+wiersz_analizowany[i]);
  6.     System.out.println("Math.pow((double)wiersz_analizowany[i],(double)2)"+Math.pow((double)wiersz_analizowany[i],(double)2));
  7.  
  8.     System.out.println("czyNajblizszySasiad[i]="+czyNajblizszySasiad[i]);
  9.     System.out.println("Math.pow((double)czyNajblizszySasiad[i],(double)2)"+Math.pow((double)czyNajblizszySasiad[i],(double)2));
  10.  
  11.     System.out.println("dystans="+dystans);
  12.     }
  13.     return dystans;
  14. }
  15.  

System.out.println("Math.pow((double)wiersz_analiz owany[i],(double)2)"+Math.pow((double)wiersz_analizowany[i],(double)2));

and
System.out.println("Math.pow((double)czyNajblizszy Sasiad[i],(double)2)"+Math.pow((double)czyNajblizszySasiad[i],(double)2));

return a double sqrt

Math.sqrt(Math.pow((double)wiersz_analizowany[i],(double)2)-Math.pow((double)czyNajblizszySasiad[i],(double)2));

returns nan ?
Print the arguments you're passing to Math.pow as well.
I'm sure you have this open while doing this.
Jul 10 '07 #8
JosAH
11,448 Expert 8TB
it may happen that in my for loop dystans is equal to NaN but what happens if i add a double to NaN does it still return NaN ?
Yes, any operator yields NaN or false when at least one of the operands equals
Nan. Even NaN == NaN is false as well as NaN != NaN. It's Not a Number ;-)

kind regards,

Jos
Jul 10 '07 #9
oll3i
679 512MB
i changed it to


dystans=Math.sqrt(Math.pow((double)wiersz_analizow any[0],(double)2)-Math.pow((double)czyNajblizszySasiad[0],(double)2)+
Math.pow((double)wiersz_analizowany[1],(double)2)-Math.pow((double)czyNajblizszySasiad[1],(double)2) +
Math.pow((double)wiersz_analizowany[2],(double)2)-Math.pow((double)czyNajblizszySasiad[2],(double)2)+
Math.pow((double)wiersz_analizowany[3],(double)2)-Math.pow((double)czyNajblizszySasiad[3],(double)2)+
Math.pow((double)wiersz_analizowany[4],(double)2)-Math.pow((double)czyNajblizszySasiad[4],(double)2)+
Math.pow((double)wiersz_analizowany[5],(double)2)-Math.pow((double)czyNajblizszySasiad[5],(double)2)+
Math.pow((double)wiersz_analizowany[6],(double)2)-Math.pow((double)czyNajblizszySasiad[6],(double)2)+
Math.pow((double)wiersz_analizowany[7],(double)2)-Math.pow((double)czyNajblizszySasiad[7],(double)2)+
Math.pow((double)wiersz_analizowany[8],(double)2)-Math.pow((double)czyNajblizszySasiad[8],(double)2)+
Math.pow((double)wiersz_analizowany[9],(double)2)-Math.pow((double)czyNajblizszySasiad[9],(double)2)+
Math.pow((double)wiersz_analizowany[10],(double)2)-Math.pow((double)czyNajblizszySasiad[10],(double)2)+
Math.pow((double)wiersz_analizowany[11],(double)2)-Math.pow((double)czyNajblizszySasiad[11],(double)2)+
Math.pow((double)wiersz_analizowany[12],(double)2)-Math.pow((double)czyNajblizszySasiad[12],(double)2)+
Math.pow((double)wiersz_analizowany[13],(double)2)-Math.pow((double)czyNajblizszySasiad[13],(double)2)+
Math.pow((double)wiersz_analizowany[14],(double)2)-Math.pow((double)czyNajblizszySasiad[14],(double)2)+
Math.pow((double)wiersz_analizowany[15],(double)2)-Math.pow((double)czyNajblizszySasiad[15],(double)2)+
Math.pow((double)wiersz_analizowany[16],(double)2)-Math.pow((double)czyNajblizszySasiad[16],(double)2)+
Math.pow((double)wiersz_analizowany[17],(double)2)-Math.pow((double)czyNajblizszySasiad[17],(double)2)+
Math.pow((double)wiersz_analizowany[18],(double)2)-Math.pow((double)czyNajblizszySasiad[18],(double)2)+
Math.pow((double)wiersz_analizowany[19],(double)2)-Math.pow((double)czyNajblizszySasiad[19],(double)2)
);


but still distance is 0.0 or NaN Math.pow returns correct numbers i checked that it's sqrt that returns NaN or 0.0 where the result should be
eg 6.2 (first row of my test data)i counted
Jul 10 '07 #10
JosAH
11,448 Expert 8TB
i changed it to


dystans=Math.sqrt(Math.pow((double)wiersz_analizow any[0],(double)2)-Math.pow((double)czyNajblizszySasiad[0],(double)2)+
Math.pow((double)wiersz_analizowany[1],(double)2)-Math.pow((double)czyNajblizszySasiad[1],(double)2) +
Math.pow((double)wiersz_analizowany[2],(double)2)-Math.pow((double)czyNajblizszySasiad[2],(double)2)+
Math.pow((double)wiersz_analizowany[3],(double)2)-Math.pow((double)czyNajblizszySasiad[3],(double)2)+
Math.pow((double)wiersz_analizowany[4],(double)2)-Math.pow((double)czyNajblizszySasiad[4],(double)2)+
Math.pow((double)wiersz_analizowany[5],(double)2)-Math.pow((double)czyNajblizszySasiad[5],(double)2)+
Math.pow((double)wiersz_analizowany[6],(double)2)-Math.pow((double)czyNajblizszySasiad[6],(double)2)+
Math.pow((double)wiersz_analizowany[7],(double)2)-Math.pow((double)czyNajblizszySasiad[7],(double)2)+
Math.pow((double)wiersz_analizowany[8],(double)2)-Math.pow((double)czyNajblizszySasiad[8],(double)2)+
Math.pow((double)wiersz_analizowany[9],(double)2)-Math.pow((double)czyNajblizszySasiad[9],(double)2)+
Math.pow((double)wiersz_analizowany[10],(double)2)-Math.pow((double)czyNajblizszySasiad[10],(double)2)+
Math.pow((double)wiersz_analizowany[11],(double)2)-Math.pow((double)czyNajblizszySasiad[11],(double)2)+
Math.pow((double)wiersz_analizowany[12],(double)2)-Math.pow((double)czyNajblizszySasiad[12],(double)2)+
Math.pow((double)wiersz_analizowany[13],(double)2)-Math.pow((double)czyNajblizszySasiad[13],(double)2)+
Math.pow((double)wiersz_analizowany[14],(double)2)-Math.pow((double)czyNajblizszySasiad[14],(double)2)+
Math.pow((double)wiersz_analizowany[15],(double)2)-Math.pow((double)czyNajblizszySasiad[15],(double)2)+
Math.pow((double)wiersz_analizowany[16],(double)2)-Math.pow((double)czyNajblizszySasiad[16],(double)2)+
Math.pow((double)wiersz_analizowany[17],(double)2)-Math.pow((double)czyNajblizszySasiad[17],(double)2)+
Math.pow((double)wiersz_analizowany[18],(double)2)-Math.pow((double)czyNajblizszySasiad[18],(double)2)+
Math.pow((double)wiersz_analizowany[19],(double)2)-Math.pow((double)czyNajblizszySasiad[19],(double)2)
);


but still distance is 0.0 or NaN Math.pow returns correct numbers i checked that it's sqrt that returns NaN or 0.0 where the result should be
eg 6.2 (first row of my test data)i counted
Basically you're doing:

Expand|Select|Wrap|Line Numbers
  1. Math.sqrt(Math.pow(x, 2)-Math.pow(y, 2))
  2.  
(shouldn't that - be a +?)

Note that when |x| < |y| the square root doesn't exist.
Also note that the IEEE/459 floating point format knows about *two* zeros: +0
and -0; the square root of -0 is also a NaN if I'm not mistaken.

kind regards,

Jos

ps. no insult intended, but when you want your software to be maintained,
worked on etc. in an international environment, English variable names are to
be preferred. Really, I've been there, done that.
Jul 10 '07 #11
r035198x
13,262 8TB
Basically you're doing:

Expand|Select|Wrap|Line Numbers
  1. Math.sqrt(Math.pow(x, 2)-Math.pow(y, 2))
  2.  
(shouldn't that - be a +?)

Note that when |x| < |y| the square root doesn't exist.
Also note that the IEEE/459 floating point format knows about *two* zeros: +0
and -0; the square root of -0 is also a NaN if I'm not mistaken.

kind regards,

Jos

ps. no insult intended, but when you want your software to be maintained,
worked on etc. in an international environment, English variable names are to
be preferred. Really, I've been there, done that.
You've written software in (what's the Dutch language called) before?
Jul 10 '07 #12
oll3i
679 512MB
usually i use english names for variables but it's only college project so ...
Jul 10 '07 #13
oll3i
679 512MB
it's Euclidean Distance i'm counting should be -
Jul 10 '07 #14
JosAH
11,448 Expert 8TB
You've written software in (what's the Dutch language called) before?
Yep, and my instructor and professor kicked that habit out of me when I was a
youngster ;-)

The Dutch speak Dutch (who'd have thought it!) Afrikaans and Flemish and
Surinaams are all related. (the Flemish even claim that their language *is*
Dutch and the Dutch speak a dialect, which I think is true).

kind regards,

Jos
Jul 10 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
13
by: Kishor | last post by:
Hi Friends Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32 Output : 5th root of 32 is 2 Ex:(1) Input : 243 Output : 5th root of 243 is...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
6
by: John | last post by:
I'm new to C and I know this is probably one of those "most commonly mis-interpreted as a problem", newcomer type of question, but for this code: double x; printf("Enter number:");...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.