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

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.

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.

Best way to store coords: struct of uints or double? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have geo coordinates in format N000.11.22.333 E444.55.66.777. Milliseconds are necessary for precision. I need to perform calculations like calculate coord given coord0, angle and distance. Sure I want to keep precision not less than milliseconds. This and other calculation algorithms often use trigonometric functions to get a result.
Which solution is better: 1) use struct that contains degrees, minutes, seconds and milliseconds as uints and overload operators for manipulate them; 2) use double type and convert coords to decimal view for calculations. As I think float type is not enough stable on these calculations.
Be used for qt 5.9 x64 project, msvc 2017, win platform only
Pick whichever is easiest (almost definitely a double). A double is more accurate than a ruler or a careful surveyor. Lots of navigation software uses a single 32-bit integer for lat and one for long.

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

How to get common factors with 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 8 years ago.
Improve this question
I'm having trouble starting with the code i have already read some other questions here but im still stuck on how am i even gonna start this one :/.
So i made 3 inputs to fill the expression (ax^2+bx+c)
.......
cout<<"This Program runs onlyy the expression (ax^2+bx+c)"<<endl;
cout<<"\nEnter The first Integer[a]";
cin>>ina;
cout<<"Enter the second Integer[b]";
cin>>inb;
cout<<"Enter The third Integer[c]";
cin>>inc;
cout<<"Your Expression is"<<endl;
cout<<ina<<"x^2 + "<<inb<<"x + "<<inc<<endl;
........
Now how would i make my program show its common factor..
example is (x^2+4x+3) how can i make it show that its common factor is (x+3) and (x+1)?
Well how to factor a polynomial has very little to do with C++.
If Ax^2 + Bx + C can be expressed as A*(x - x1)*(x - x2) (and it can always be so expressed), then clearly plugging the value x1 in for x makes the original equation zero, since the first term of (x1 - x1)*(x1 - x2) is then 0. And ditto for x2. And conversely, if you have two values that make the equation zero, then they are x1 and x2.
There is a standard formula for solving quadratic equations. Computing that in a program one should be aware that subtracting a number from a roughly equal size number can produce a less precise result. So how that formula is expressed in the code, can make a difference wrt. to the accuracy of the results. You can find more information about that on the net. Including examples for quadratic equation formula.

how to differ rational and irrational number 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 9 years ago.
Improve this question
how to tell my float variable store an irrational number?
I'm a kind of newbie in C++
and I dont know many library function to be implemented
I want to make an exception for every calculation that end up being an irrational number
C++ doesn't have general arbitrary-precision rational numbers implemented. The available numbers are size-limited integers and floating point numbers.
A floating point number (in the common IEEE format) is however an integer multiplied by an exact power of two (positive or negative).
Even numbers like 0.1 = 1/10 are impossible to represent exactly because the denominator is not a power of two.
So the answer is simple :-) ... any number you will face with C++ is rational, more than that is an integer multiplied by a (possibly negative) power of two.
There are libraries implementing arbitrary precision integers and rational numbers, but they're not part of standard C++.
C++, by default, can only manage rational numbers. Moreover it's a very specific subset of the rationals where
The numerator is not too big in absolute value
The denominator is a power of two and it's not too big
When you write
double x = 1.0;
x = x / 10.0;
you get a result that is already outside of the capability of the C++ language because the denominator is not a power of two.
What the computer will do is storing into x a close approximation because 0.1 it's a number that cannot be stored exactly in IEEE double format.
Floating point numbers are an approximation of the number. It is accurate as best that it can do with the limited amount of room to play in.
So the best bet is to limit the effect of both. It is called algebra. Also enables one to reduce round errors.