New submission from Raymond Hettinger <[EMAIL PROTECTED]>: 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 <[EMAIL PROTECTED]> <http://bugs.python.org/issue3008> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com