Python Forum
the smallest floating point value that when arithmetically added
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
the smallest floating point value that when arithmetically added
#1
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)))
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020