Uint64 casting bug for MSVC builds
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
On Wed, Dec 3, 2014 at 8:44 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
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
I have tried this out on Python 3 and 2, both 32 and 64 bits, and cannot reproduce it: Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import numpy as np np.float64(2**63 + 2**62).astype(np.uint64) 13835058055282163712
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import numpy as np np.float64(2**63 + 2**62).astype(np.uint64) 13835058055282163712
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import numpy as np np.float64(2**63 + 2**62).astype(np.uint64) 13835058055282163712
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import numpy as np np.float64(2**63 + 2**62).astype(np.uint64) 13835058055282163712
These are all WinPython (http://winpython.sourceforge.net/) builds, which I believe use a similar toolchain to Christophe's, including MKL. Jaime -- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
On 12/3/2014 8:44 AM, Matthew Brett wrote:
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
This is a know issue with older (<= 2010) 32 bit msvc, which uses x87 instead of SSE instructions. See also <https://github.com/scipy/scipy/blob/master/scipy/ndimage/tests/test_datatypes.py#L51>. Christoph
participants (3)
-
Christoph Gohlke
-
Jaime Fernández del Río
-
Matthew Brett