[Numpy-discussion] Clipping, casting, and maximum values

Todd Miller jmiller at stsci.edu
Tue Jan 3 04:18:12 EST 2006


On my machine running FC4 x86_64, I get:

Float64 1e+300
L13       9223372036854775808.0000
Int64     9223372036854775807

What are you getting?  What did you expect to get?

At 19 digits,  the numbers we're talking about are outside the precision 
of Float64 which is ~16 digits.

I did see one (for me) surprise:

 >>> farr = numarray.clip(farr, 0.0, float(2**63))
 >>> farr
array([  9.22337204e+18])
 >>> numarray.Error.pushMode(invalid='ignore')
 >>> arr = farr.astype(Int64)
 >>> numarray.Error.popMode()
 >>> arr
array([-9223372036854775808])

Removing the Error suppression,  I also got:

Warning: Encountered invalid numeric result(s)  during type conversion

That makes it (for me) much less surprising:  since you can't stuff 
2**63 into Int64, there is no right answer for the astype().   
Everything else looked OK to me on Fedora Core 4.

Regards,
Todd

Edward C. Jones wrote:

> #! /usr/bin/env python
>
> import numarray
> from numarray.numerictypes import *
>
> # Is this program too complicated?
> # On my machine (AMD64 chip, Debian unstable i386, numarray 1.5.0):
> #    float(2**63-1) == float(2**63)
> def clipcast(farr):
>    if farr.type() != Float64:
>        raise TypeError('input must be a Float64 array')
>    farr = numarray.clip(farr, 0.0, float(2**63))
>    print 'L13 %30.4f' % farr[0]
>    top = (farr == float(2**63))
>    numarray.Error.pushMode(invalid='ignore')
>    arr = farr.astype(Int64)
>    numarray.Error.popMode()
>    return numarray.where(top, 2**63-1, arr)
>
> farr = numarray.array([1.0e300], Float64)
> print farr.type(), farr[0]
> arr = clipcast(farr)
> print arr.type(), arr[0]
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log 
> files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion






More information about the NumPy-Discussion mailing list