# Precision

• 07-26-2005
acc988
Precision
I am making a program (a zernike equation function) that needs to be accurate. Most things are accurate enough, but when I use sin or cos from the math.h file, it only returns 6 decimal places. I need more than that. How can I fix this?

cout << sin(10*(3.141592653589793/6)) << endl;

returns:

-0.866025

-acc
• 07-26-2005
swoopy
Make sure your variable is a double and use setprecision() from <iomanip> when you print. For example:
Code:

`  cout << setprecision(14) << sin(10*(3.141592653589793/6)) << endl;`
Code:

`  cout << setprecision(14) << sin(10*(M_PI/6)) << endl;`
Code:

```  const double pi = 4. * atan(1.);   cout << setprecision(14) << sin(10*(pi/6)) << endl;```
• 07-26-2005
hk_mp5kpdw
It returns more than 6 decimal places of precision, it is simply that the default display of floating point values is set to be 6 digits of precision. To change the precision when it comes time to display, you must either use cout.precision(x) or the setprecision(x) manipulator:

Code:

```cout.precision(10); cout << sin(10*(3.141592653589793/6)) << endl;```
or...

Code:

```#include <iomanip> using namespace std; cout << setprecision(10) << sin(10*(3.141592653589793/6)) << endl;```
• 07-26-2005
acc988
When c++ actually does the calcuations, does it use maximum precision? If not, then does setprecision work without cout? I'm making a DLL file for a program called Mathcad. When I compared my answer for the entire zernike equation to the same equation in Mathcad, the answers differ. I thought it might be because c++ wasn't being as precise as Mathcad.
• 07-26-2005
Rashakil Fol
It uses as much precision as it can. If you're doing calculations with doubles, it will use all the decimal places (or binary places) that doubles can hold. Doubles always calculate using the same amount of precision -- setprecision only affects the behavior of the function that converts the floating point number into a string of characters.

Naturally, some floating point error might build up with repeated calculations. It may be that Mathcad is using a different amount of precision -- or maybe it seems to use the same amount of precision but jumps to extra precision for difficult calculations that bring in floating point error (some other math software does this -- I don't know anything about mathcad).

• 07-26-2005
acc988
The difference in the answers increases as I increase the value of a variable. I'll attach an image that shows the difference.
• 07-26-2005
Zach L.
A slighty better idea, perhaps:
Code:

```cout.precision(numeric_limits<double>::digits10); cout << sin(10*(3.141592653589793/6)) << endl;```
And you have to include <limits>. This keeps the guesswork out of how many decimal digits your double is holding.

The difference in your values may not be due to the precision of the variables used, but rather, the order in which operations are performed, or subtle differences in the algorithms used. Floating point values are very finicky.
• 07-26-2005
swoopy
You could also try using a long double versus a double .

But judging from you calculations, the problem may be one of those Zach mentioned.
• 07-26-2005
Stoned_Coder