Python Forum

Full Version: the smallest floating point value that when arithmetically added
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
inspired by my own post over here i coded this solution:
#!/usr/bin/env python3
from sys import argv
def mininc(a):
    """Return the minimum float value that can increment the given value, and its power of 2"""
    v=float(a)
    u=1.0
    p=0
    r=u,p
    if v+u>v:
        while v+u>v:
            r=u,p
            u=u/2.0
            if u==0:
                break
            p=p-1
    elif v+u<v:
        raise ValueError('adding {} to {} goes down to {}'.format(repr(u),repr(v),repr(v+u)))
    else:
        while v+u==u:
            r=u,p
            u=u+u
            p=p+1
    return r

a=float(eval(argv[1]))
b,p=mininc(a)
print('the smallest value that makes a change when added to {} is {} (which is 2.0**{}) and then is {}'.format(repr(a),repr(b),repr(p),repr(a+b)))