Rounding to next lowest float

Hi, Can anyone think of a clever way to round an integer to the next lowest integer represented in a particular floating point format? For example: In [247]: a = 2**25+3 This is out of range of the continuous integers representable by float32, hence: In [248]: print a, int(np.float32(a)) 33554435 33554436 But I want to round down (floor) the integer in float32. That is, in this case I want:
floor_exact(a, np.float32) 33554432
I can break the float into its parts to do it: https://github.com/matthew-brett/nibabel/blob/f687bfc88d1676a09fc76c968a346b... but that's obviously rather ugly... Is there a simpler way? I'm sure there is and I haven't thought of it... Best, Matthew

If you are using integers, why not use Python's Long? Colin W. On 11/10/2011 2:00 PM, Matthew Brett wrote:
Hi,
Can anyone think of a clever way to round an integer to the next lowest integer represented in a particular floating point format?
For example:
In [247]: a = 2**25+3
This is out of range of the continuous integers representable by float32, hence:
In [248]: print a, int(np.float32(a)) 33554435 33554436
But I want to round down (floor) the integer in float32. That is, in this case I want:
floor_exact(a, np.float32) 33554432
I can break the float into its parts to do it:
https://github.com/matthew-brett/nibabel/blob/f687bfc88d1676a09fc76c968a346b...
but that's obviously rather ugly... Is there a simpler way? I'm sure there is and I haven't thought of it...
Best,
Matthew _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Hi, On Tue, Oct 11, 2011 at 3:20 PM, Colin J. Williams <cjwilliams43@gmail.com> wrote:
If you are using integers, why not use Python's Long?
You mean, why do I need to know the next lowest representable integer in a float type? It's because I have a floating point array that I'm converting to integers, and I'm trying to find the right threshold for clipping the floating point array, to stop this happening: In [84]: np.array([2**32], np.float).astype(np.int32) Out[84]: array([-2147483648]) I need then to clip my floating point input array at some maximum floating point value ``M``, such that in_type(M).astype(out_type) <= X, where X is the maximum of the out_type. Best, Matthew
participants (2)
-
Colin J. Williams
-
Matthew Brett