lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 12/10/2011 12:20 AM, Roberto Ierusalimschy wrote:
and the simple tests:

  print(2^3 - 8)
-1.7763568394003e-015
  print(3^4 - 81)
2.8421709430404e-014
=2^4-16
-1.7763568394003e-015

These all should be zero. It really seems that the 'pow' function
from the C library is broken.

The above results can be calculated as follows:

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

int main()
{
    // from cygwin's newlib.c
    // calculate x ** y as 2 ** (y log2(x))
    double x, y, z;
    x = 3; y = 4;
    z = pow(2, y*log2(x));
    printf("%g\n", z-81);
    return 0;
}

This gives (MinGW gcc Win32):
$ gcc -o test test.c ; ./test
-2.84217e-014

Looking at newlib's math sources, more robust implementations of pow() checks for integer exponents and handles that properly.

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia