Double precision error in C++ [closed] - c++

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

Related

NaN in C++ why? [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 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().

get real part from fftw_complex in FFTW [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
this is maybe newbei question , but i dont know how to fast acces to real part of fftw_complex with FFFTW, i cant use .real() method,
I need convert this to double array, dynamic array in c++;
From the docs, 2 second Google search:
4.1.1 Complex numbers
The default FFTW interface uses double precision for all floating-point numbers, and defines a fftw_complex type to hold complex numbers as:
typedef double fftw_complex[2];
Here, the [0] element holds the real part and the 1 element holds the imaginary part.

Calculating large numbers in C++ without external libraries [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
I need to write a program that will perform operations on float numbers higher than 10^100.
I can't use any arbitrary precision mathematics libraries that are not included in GCC package by default.
I have NO idea how how to go about it.
Can you point me in the right direction?
You can create a class that can store larger numbers. 12345678 equals to 1234 * 10e4 + 5678.
For large numbers I use string buffers and do manual computation on it. It is much overhead and slow but you get infinite precision.

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.

Basic C++ Pseudo-Random Number Generation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I am learning C++ and something I would like to do is be able to generate pseudo-random numbers. I am aware that you can #include <random> however it seems as if that is still unstable (I could not get it to work.) Any help on the matter is appreciated. Thanks.
According to this question, #include <random> is a C++11 header. You might want to check if your compiler supports it, and that the proper flags are set.
Otherwise, have you looked at rand()? It should be enough for basic random numbers.