NaN in C++ why? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to implement a K-nn classifier. A part of this problem is getting the euclidean distance from an example to another one. I'm having problems calculating it, because sum finally is NaN.
The problem is in this code block:
for(int i=0;i<fdataset.size();i++){
float sum=0;
for(int k=0;k<fdataset[i].size();k++){
if(mask[k]){
sum+=(fdataset[i][k]-example[k])*(fdataset[i][k]-example[k]);
}
}
results[i]=sqrt(sum);
}
fdataset is a vector< vector<float> > and example is vector<float>. There should be no problems. So, why I'm having this problem?
Thanks!

The most likely explanation is that your data set is "poisoned" with one or more NaNs. It would only take a single NaN in the fdataset or example arrays to corrupt sum.
As an aid to debugging, you could check each input with std::isnan().
Update: As user akavel suggested in a comment, there are other expressions that can also generate NaN in IEEE 754 floating-point arithmetic. Wikipedia lists them here. I believe that the operations relevant to your code are:
Operations where one of the operands is a NaN
0 * inf
inf - inf
So you should also check that your inputs are not inf with std::isinf().

Related

Sin and cos function in C++ vs MATLAB [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a precision problem in C++. I have two angles which their average is pi/2 and they are like pi/2 +- alpha so the absolute values of sine and cosine should be equal. When I find their sine values in MATLAB they are equal, which they should be. Try out : sin(1.25911) & sin(1.88252) and their sum is 3.1416. But when I find these values in C++ the answer is : 0.951818 and 0.951806
How can I increase the accuracy of these numbers so the get equal? I can choose my precision up to 3 decimal numbers but I prefer to keep it up to 6.
3.1416 is a crude approximation to pi. If you use a better value, you'll get a better answer from sin. So,
sin(1.25911)=0.951817787502636
sin(pi-1.25911)=sin(1.88248265358979)=0.951817787502636
Note, that I've used more accurate input values to sin here, their average is closer to pi/2 than your example.

C++ Why convert something to NaN? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My understanding is that NaN (Not a Number) is essentaly a constant that is returned from a mathematical function to indicate something went wrong or the calculation is invalid. So it makes sense that their are functions to check if a number is NaN or better yet, use the CERT Coding Standard to do error checking for mathematical errors ( https://www.securecoding.cert.org/confluence/display/c/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions ).
My question is this; why does std::nan() exist? Why would you ever want to take a valid number/string/value and convert it to NaN? (Refrence: http://en.cppreference.com/w/cpp/numeric/math/nan )
NaN is often used to indicate a null or missing value, especially in data analyisis and data science. So it is common for an application to initialize values to nan, in order to track whether a value has been provided or not without the overhead of using optional<T>-like structures.
Secondarily, it common to create custom math functions that you want to return nan for certain inputs. So it's more than just for completeness.
Suppose you want to implement std::acos. How would you return nan in case of invalid input (|arg| < 1)? It should be possible to implement such functions in C++. Beside that fact, that you may need to write a function which is not provided by STL, one of distinctive charts of C++ is that it's standard library can be written on C++.
IEEE 754 systematically introduced the use of NaN to represented numbers whose definitions could otherwise not be represented on computers.
You'll often see this for 0/0, ±inf / ±inf, 0 * ±inf, etc.

std::min is not returning the smaller of two values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this line of code:
out = std::min(x - 1, y - 1);
But it is returning the larger of the two quantities. How can this be?
This can only happen if x and y are unsigned types and one of them is zero.
Subtracting 1 from an unsigned value 0 will cause an unsigned value to wrap around to the largest possible value for that type. Hence the other value will be smaller.
As a side note: only blame your compiler / STL as a very last resort.

How to design an algorithm that multiplies two floats without '*'? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How do I design an algorithm that takes two floats and multiplies them using only addition, bit shifting and bitwise operations?
I have already found one like this for integers, but that doesn't work with floats.
I have also found another that is much like what I need but log is also prohibited in my case.
The floats are stored according to the IEEE754 standard. I have also tried to keep their exponent part, and bitwise multiply their fractional part with no luck.
According to http://en.wikipedia.org/wiki/IEEE_floating_point, an IEEE754 number x = (-1)^s * c * b^q is represented by s,c,b,q , all are integers. for Two floating point numbers with the same base b is the same.
So the multiplication of two floating point numbers x and y is:
(-1)^(s1+s2)*c1*c2*b^(q1+q2) so the new floating point is represented by: s1+s2, c1*c2, b q1+q2 so you only have left to deal with multiplication of c1 and c2, both are integers so you are done.

Double precision error in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am using openCV C++ libraries and calculated a double. It does the arithmetic but when I read out the number, prints out -1.#QNAN on the command prompt. What does that mean?
I am using a 64-bit i3 processor.
It means you got a quiet NAN, probably by dividing -Inf / Inf or multiplying something with -Inf, or perhaps casing a non-double into a double. It's not so much a precision error as much as it's an arithmetic exception.
EDIT: or adding/substracting Inf ... read more on NaNs here
That's not an error, read more about floating point here