[Numpy-discussion] Rounding to next lowest float

Matthew Brett matthew.brett at gmail.com
Tue Oct 11 14:00:31 EDT 2011


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/f687bfc88d1676a09fc76c968a346bc81e4d0d04/nibabel/floating.py

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



More information about the NumPy-Discussion mailing list