[Python-ideas] Way to check for floating point "closeness"?

Andrew Barnert abarnert at yahoo.com
Thu Jan 15 03:00:53 CET 2015

On Wednesday, January 14, 2015 4:18 PM, Steven D'Aprano <steve at pearwood.info> wrote:

>On Wed, Jan 14, 2015 at 02:44:27AM -0800, Andrew Barnert wrote:
>> I think your confusion here is entirely my fault. For simplicity, it's 
>> often helpful to look at tiny float representations--e.g., a 4-bit 
>> float with 1 sign bit, 1 mantissa bit, and 2 exponent bits (that, if 
>> both 1, mean inf/nan), 
>Since this is python-ideas, how about a minifloat type?

>I think a 4-bit float is a bit *too* mini, 8-bit sounds about right.

Anything up to 5 or 6 bits, you can fit a table of all the values on your monitor at decent font size. 8 bits is a bit too much for that.

Which reminds me, I found an old blog post where I used such a 6-bit (1+3+2) format to demonstrate all the details of IEEE binary floats, including exactly such a table. It's now 
back online (http://stupidpythonideas.blogspot.com/2015/01/ieee-floats-and-python.html?view=sidebar) if anyone's interested.

I had a Binary6 class to go with it—which didn't have the complete float interface, but did have a lot of handy optionals like as_tuple and next_plus, as Decimal does—but I can no longer find that, so I stripped out that section of the blog post. But it still shows how to fiddle with the bits manually using struct plus integer operations or a library like bitstring.

>(I know, I know, it should be a third-party library, not a built-in :-)

I don't think it would be very useful as a third-party lib either. But an arbitrary-precision IEEE 754-2008 Binary type, with all the optional features that aren't part of float, akin to decimal.Decimal—that might be useful. And while you're at it (you were volunteering, right?), want to go over 754-2008 vs. 854-1987 and see if there's anything worth adding to Decimal? :)

More information about the Python-ideas mailing list