Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Numerical precision in c++

Options
  • 11-03-2008 2:21am
    #1
    Registered Users Posts: 2,481 ✭✭✭


    I'm using c++ to do some numerical analysis, and I just had a quick question.
    If I write code that looks something like

    double d = 354074884.0/205283203125.0;

    How will d be represented internally? I know it will be represented as a decimal and get truncated at some point. Will the truncation depend on what architecture the code is running on or compiled on?

    Cheers for your help.


Comments

  • Closed Accounts Posts: 4,368 ✭✭✭thelordofcheese


    I'm not 100% sure, but i think the limit on doubles is 16 digits after the point. Also the double type is 8 bytes in size so i don't think it'd change from architecture to architecture.


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    I think in most (if not all) cases* it will be represented according to the standard IEEE 754-1985.

    * Please someone correct me if I'm wrong.


  • Closed Accounts Posts: 10 Zachary


    So what's the difference between a double on a 32-bit machine and a double on a 64-bit machine?

    Can I output a double x in full precision using a cout<<x ?


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Zachary wrote: »
    So what's the difference between a double on a 32-bit machine and a double on a 64-bit machine?

    I don't know the answer to this for sure. I suspect that the answer is "no difference" but some combinations of architecture and compiler may change that answer. My C++ is rusty enough that I don't currently have such arcana in my head.

    Typically, most questions of the form "how is <floating point numeric type> implemented on <platform>" can be answered by one of the following two statements:
    • You don't need to know/care. (If precision doesn't really matter)
    • Use an arbitrary-precision library like GMP. (If precision does really matter)

    Odds are that anyone who needs the speed of the raw native types and needs to worry exactly about the precision will need to research the platforms they are using and the exact nature of the algorithms they're using to avoid unacceptable loss of precision. For the vast majority of cases you do not need to know the implementation behind the type.
    Zachary wrote: »
    Can I output a double x in full precision using a cout<<x ?

    You can play with the following:
    #include <iostream>
    
    int main(void) {
        double d = 123456789.123456789;
        std::cout << d << std::endl;
        printf("%.100f\n", d); // Obviously double will not have 100 decimal places, but you get the point ;-)
        return 0;
    }
    


  • Registered Users Posts: 413 ✭✭ianhobo


    Will the truncation depend on what architecture the code is running on or compiled on?

    It will depend on what architecture the code is compiled to run on, and on compiler optimisations. The same code, but for differing architectures will compile differently. The "how big is an int question" comes into play.
    However for x86(intel/amd/"home" computers) processor architectures is will generally be consistent

    Microsoft define their fundamental double type to be 8 bytes, for BOTH 32 and 64-bit compilers
    but gcc define it as "two words" (unless you define it yourself) therefore indicating twice the range is compiled for a 64bit processor


  • Advertisement
Advertisement