Thread: Precision

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Question 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

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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;

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Question

    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.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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).

    How much do your answers differ?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    The difference in the answers increases as I increase the value of a variable. I'll attach an image that shows the difference.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting Precision Confusion
    By dnguyen1022 in forum C++ Programming
    Replies: 11
    Last Post: 01-14-2009, 10:38 AM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. Replies: 1
    Last Post: 04-03-2008, 01:17 AM
  4. Precision based floating-point
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2006, 10:35 AM
  5. International Limits on Precision
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-20-2004, 06:32 AM