[Numpy-discussion] Uint64 casting bug for MSVC builds

Matthew Brett matthew.brett at gmail.com
Wed Dec 3 11:44:07 EST 2014


Hi,

I just noticed this using Christophe Gohlke's MKL builds of numpy:

>>> import numpy as np
>>> val = 2**63 + 2**62
>>> np.float64(val)
1.3835058055282164e+19
>>> np.float64(val).astype(np.uint64)
9223372036854775808

In general it seems that floats get clipped at 2**63 when casting to
uint64.   This appears to be a bug in MSVS express 2010 (the only version I
tested):

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

int main(int argc, char* argv[]) {
    double fval = pow(2, 63) + pow(2, 11);
    double fval2;
    unsigned long long int ival = fval;
    fval2 = ival;
    printf("Float %f\n", fval);
    printf("Integer %f\n", fval2);
    printf("sizeof ulong %u\n", sizeof(unsigned long long int));
}
</test_cast.c>

Z:\>test_cast.exe
Float 9223372036854777900.000000
Integer 9223372036854775800.000000
sizeof ulong 8

I realize there's nothing much numpy can do about this, just thought I'd
let y'all know.

Cheers,

Matthew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141203/3551a3aa/attachment.html>


More information about the NumPy-Discussion mailing list