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)
  print(3^4 - 81)

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

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

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