| | |
Simple sin() problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
I hate to be spamming the forums with this, but I've been banging my head at the wall for a few hours now, and I can't really understand what is going on. Here's the full program I'm running:
The program is returning 1.22461e-16.
Is this simply because I'm not using enough digits of pi? Then why does cos work flawlessly? Any help is greatly appreciated.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include "math.h" int main() { std::cout << sin (3.141592653589793) << "\n"; }
Is this simply because I'm not using enough digits of pi? Then why does cos work flawlessly? Any help is greatly appreciated.
•
•
Join Date: Mar 2008
Posts: 1,444
Reputation:
Solved Threads: 118
1.22461e-16 is equal to 0.000000000000000122461, which is pretty close to 0 if you ask me. This is just caused by the precision of the double type and how the data is stored. You could also round the number to remove that problem.
Last edited by William Hemsworth; May 28th, 2009 at 9:02 pm.
I need pageviews! most fun profile ever :)
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
It's in the nature of finite precision.
Theres not much you can do about it.
there a basicly two kinds of datatypes.
1. integral
2. float
integral being, int, char, byte etc
floats being, float, double
integral is always precise, floating values are more obscure
What numerical libraries do is to check for equality within a tolerance like
If you need it depends on your context,
if you just want the value, then you shouldn't care.
But if your program has a branchpoint depending on this value,
you should go the "within tolerance" way.
on a sidenote.
floats are only precise to the 7th or 8th digit,
whereas double are to the 23thd
good luck
Theres not much you can do about it.
there a basicly two kinds of datatypes.
1. integral
2. float
integral being, int, char, byte etc
floats being, float, double
integral is always precise, floating values are more obscure
What numerical libraries do is to check for equality within a tolerance like
c++ Syntax (Toggle Plain Text)
float one = 0; float two = 0.0000001 //wrong if(one!=two) puts("not same"); //right #define tole 0.000001 if(fabs(one-two)>tole) puts("not same")
If you need it depends on your context,
if you just want the value, then you shouldn't care.
But if your program has a branchpoint depending on this value,
you should go the "within tolerance" way.
on a sidenote.
floats are only precise to the 7th or 8th digit,
whereas double are to the 23thd
good luck
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
![]() |
Similar Threads
- Simple C++ Class problem (C++)
- Help with Simple Math Problem (Python)
- Simple problem (Java)
- Another counting problem (C)
- so-called "simple" host (Web Hosting Deals)
- Math problem in C (C)
- include file problem (C++)
- DrawHouse Code Problem (Java)
Other Threads in the C++ Forum
- Previous Thread: Very Basic Encryption
- Next Thread: Please Hlpe me, lojec erooe
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






