[New-bugs-announce] [issue3008] Let bin() show floats

Raymond Hettinger report at bugs.python.org
Fri May 30 09:47:45 CEST 2008


New submission from Raymond Hettinger <rhettinger at users.sourceforge.net>:

Let bin() show floating point values.  This would contribute quite a 
bit to people's understanding of floating point arithmetic.  It has a 
nice education value and it makes it easier to diagnose floating point 
mysteries.

def vis(f):
    """ Show binary representation of a floating point number:

        >>> vis(math.pi)
        '0b11.001001000011111101101010100010001000010110100011'
        >>> vis(-0.375)
        '-0b0.011'
    """
    f, sign = (f, '') if f >= 0 else (-f, '-')
    n, d = f.as_integer_ratio() if isinstance(f, float) else (f, 1)
    n, d = map(lambda x: bin(x)[2:], (n, d))
    n = n.rjust(len(d), '0')
    s = list(n)
    s.insert(len(n) - len(d) + 1, '.')
    return sign + '0b' + ''.join(s)

----------
components: Interpreter Core
messages: 67525
nosy: rhettinger
severity: normal
status: open
title: Let bin() show floats
type: feature request
versions: Python 2.6, Python 3.0

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3008>
_______________________________________


More information about the New-bugs-announce mailing list