Message67525
| Author | rhettinger |
|---|---|
| Recipients | rhettinger |
| Date | 2008-05-30.07:47:43 |
| SpamBayes Score | 0.10133792 |
| Marked as misclassified | No |
| Message-id | <1212133665.79.0.610497521222.issue3008@psf.upfronthosting.co.za> |
| In-reply-to |
| Content | |
|---|---|
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) |
|
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2008-05-30 07:47:46 | rhettinger | set | spambayes_score: 0.101338 -> 0.10133792 recipients: + rhettinger |
| 2008-05-30 07:47:45 | rhettinger | set | spambayes_score: 0.101338 -> 0.101338 messageid: <1212133665.79.0.610497521222.issue3008@psf.upfronthosting.co.za> |
| 2008-05-30 07:47:44 | rhettinger | link | issue3008 messages |
| 2008-05-30 07:47:43 | rhettinger | create | |