Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Machine Epsilon precision discrepancy

I am attempting to calculate the machine epsilon value for doubles and floats in C++ as part of a school assignment. I'm using Cygwin in Windows 7, 64 bit, here is the code:

#include <iostream>

int main() {
    double epsilon = 1;
    while(1 + epsilon > 1)
        epsilon = epsilon / 2;
    epsilon = 2*epsilon;

    std::cout << epsilon << std::endl;

    float epsilon_f = 1;
    while(1 + epsilon_f > 1)
        epsilon_f = epsilon_f / 2;
    epsilon_f = 2*epsilon_f;

    std::cout << epsilon_f << std::endl;

    return 1;
}

When I run the code, I receive 1.0842e-019 for both values. I looked it up and should be getting 2.22e-16 for the double, and 1.19e-07 for the float value. When I run the exact same code on a Macbook, the code returns the correct values. What could be causing the discrepancy on my Windows machine?

like image 498
JennaLewis Avatar asked Nov 09 '22 19:11

JennaLewis


1 Answers

The CPU's floating-point registers typically contain 80 bits, and it looks like the Cygwin compiler chooses to perform the loop calculations entirely in registers (only truncating the result to 32/64 bits when printing the results).

As @Potatoswatter points out, this is entirely legal for the compiler, and your program actually exhibits undefined behavior because it assumes that there is a precision limit. Since there's undefined behavior, the compiler may choose to transform your program into anything it wants (including one that deletes all of your files, but that's fortunately not a common solution...)

P.S. Welcome to StackOverflow, and kudos for a question that (if you read up on the concepts in the answers) will probably make you learn more about processor architecture and compilers than anyone else in your class! :-)

like image 109
Aasmund Eldhuset Avatar answered Nov 14 '22 23:11

Aasmund Eldhuset