Next float?

Paul Rubin http
Thu Nov 22 01:42:16 EST 2007


Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> writes:
> Is there a simple, elegant way in Python to get the next float from a 
> given one? By "next float", I mean given a float x, I want the smallest 
> float larger than x.

I think you have to do it by bit twiddling.  But something like bisection
search could come pretty close, for well-behaved values of x:

def nextfloat(x):
    dx = (x, x/2.0)
    while x+dx[1] != x:
        dx = (dx[1], dx[1]/2.0)
    return dx[0]+x



More information about the Python-list mailing list