pow(2) returns slightly different result compared to other platforms' implementation

Matthew Dillon dillon at backplane.com
Sat Sep 24 11:26:58 PDT 2016

Hmmm.  Yah, 10 ^ -20 cannot be exactly represented in any 2s complement FP
format.  So unless there is a requirement that it round up, that result is
perfectly valid.

        long double d;

        d = pow(10.0, -20.0);
        printf("%23.23Lg\n", d);
        d = powl(10.0, -20.0);
        printf("%23.23Lg\n", d);

apollo:/home/dillon> ./x

It's similar to, say, the problem one might have trying to increment a
floating point value by 0.1.  Small errors will build up because '0.1'
cannot be exactly represented in 2s complement floating point.

        long double d;

        for (d = 0.0; d < 1000.0; d = d + 0.1)
                printf("%23.23Lg\n", d);


Also, side note: 80-bit floating point is not performance-optimized on
Intel CPUs.  The HW doesn't optimize it, the vector instructions don't
support it, etc.  So the compiler has to fall-back to older FP stack
instructions in order to retain the precision.  You generally want to just
use float or double.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dragonflybsd.org/pipermail/users/attachments/20160924/655bbf65/attachment-0003.htm>

More information about the Users mailing list