floating point is not fun

#include <stdio.h>
#include <math.h>

int main()
{
        double a= 10.0;
        double b= 1e+308;
        printf("%dn",isinf(a * b));
        return 0;
}

Prints 1 on: 64bit intel, 32bit PowerPC, 32bit SPARC, 64bit Sparc. But prints zero on 32bit intel.

Oh, but if you build that with g++ instead of gcc on 32bit Intel, you get 1.

Tags: , ,

One Response to “floating point is not fun”

  1. Thomas says:

    Ergh. I read another fun example of that just this afternoon in this C++ FAQ
    http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.18

    On my i386 system at least, the following does in fact print “Huh?!?”. I really really appreciate integers.

    #include

    void foo(double x, double y)
    {
    if (cos(x) != cos(y)) {
    std::cout << “Huh?!?\n”;
    }
    }

    int main()
    {
    foo(1.0, 1.0);
    return 0;
    }

Leave a Reply