Sin and cos function in C++ vs MATLAB [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 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.

Related

What is the most efficient way to calculate PI in C? [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 5 years ago.
Improve this question
I'm have to write a C program, what it does is takes a integer as input. And gives output to the input(th) number of PI after decimal. Like if input is 100, output will be 9(100th digit of pi, after decimal is 9).
What will be the best algorithm for that?
p.s I don't want to save the value of pi into the string, and do it.
Your question is more a math question than a C programming one (so perhaps off-topic). Read first the wikipage on Pi and Approximations of π
If you need to compute only a few hundred (or even hundred thousands) digits, you just need to use some algorithm and code it using some bignum library (e.g. GMPlib, which has mpfr_const_pî ass commented by chtz).
Things become interesting for many billions of digits. I'm not expert on Pi, but look into Fabrice Bellard work on it (read the technical notes mentioning Chudnovsky's algorithm).

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().

Is there any further steps to calculate phase after dft? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to find magnitude and phase of fourier transform. There is an tutorial Opencv.
After using this formula, we are going to switch to a logarithmic scale and shifting normalizing. But I could not find for phase. Phase formule is :
Here is the question after arctan calculation, do I need to do extra stuff like magnitude(log scale,shifting,normalizing)? Or what is the logic behind it I could not understand? I am programmer guy and I am very far from these Math stuff.
The arctan range is (−π, π]. Hint: use std::atan2. You may indeed shift this to [0, 2*π) if you like. This is in no way necessary, it just avoids negative numbers.
Scaling to 360 degrees is also possible, but very rare - math is always done in radians, degrees are only for human consumption, and which human is going to look at FFT magnitudes?
Log scales are utterly pointless for angles, as they are modulo 2π.

Why dose cos(90) not equal zero? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I understand that cos(); in c++ uses radians right.. and you can get radians with..
(angle * PI ) / 180;
So why does
float value = cos( (90 * PI / 180 ); // == 6.1 etc... and not 0?
If I use the scientific calculator in windows for cos(90) I get zero. Yet as an experiment, when I push cosh(90), I get that same 6.1 etc... value that calling the function in C++ gave me.
Any ideas what is going on? Here is my code as it is now...
http://ideone.com/YQgLz
What I am asking basically is why is cos(90 degrees) in C++ coming back with the same number as doing cosh(90) on the windows calculator. Isn't cos(90 degrees) supposed to be zero anyway?
So you didn't really get 6.1 (a cosine/sine value that is greater than 1 is only possible for certain complex numbers), but 6.1 * 10^-17. The thing is that floating-point numbers aren't exact values (by nature - that's how the base-2 representation works), nor do the maths functions return precise values - they use various approximation formulæ to calculate a value - don't ever expect them to be exact.

Normalizing histograms? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
What is normalizing histograms? When and why would I use it? What are its advantages?
I don't understand the concept at all- when I try to apply it to my histogram, when I use back projection, I don't get any results.
Could someone give me a non-technical explanation of normalization?
I am using OpenCV
PS: Don't send me to wikipedia- I don't understand the Wikipedia Page
Thanks
It's very simple, actually. A normalized histogram is one in which the sum of the frequencies is exactly 1. Therefore, if you express each frequency as a percentage of the total, you get a normalized histogram.
What is the use of a normalized histogram? Well, if you studied probability and/or statistics, you might know that one property required for a function to be a probability distribution for a random variable is that the total area under the curve is 1. That's for continuous-variable functions. For discrete functions, the requirements is that the sum of all values of the function is 1. So a normalized histogram can be thought of a probability distribution function which shows how probable each of the values of your random variable is.