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
9.9999999999999994515327e-21
1.0000000000000000000342e-20

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);

0.10000000000000000555112
0.20000000000000001110223
...
999.59999999999992015276
...
999.99999999999992006394

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.

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


More information about the Users mailing list