[Python-ideas] Consider generalizing Decimal to support arbitrary radix

Steven D'Aprano steve at pearwood.info
Wed Feb 7 19:10:20 EST 2018


On Wed, Feb 07, 2018 at 11:49:27PM +0000, Neil Girdhar wrote:

> I see your list was about converting to and from base 10.  That wasn't
> really intended in my proposal.  I meant wholly working in another base.
> In that sense, 10 isn't particularly "fast, error-free, better at rounding,
> etc."

I never said it was. Base 2 floats is the one that is faster and better 
at rounding than any other base. No finite precision floating point 
arithmetic can be error free, but all else being equal, base 2 minimises 
the errors you get.

The advantage of base 10 is that it matches the standard base 10 
numbers we write. Within the boundaries of the available precision, if 
you can write it in decimal, you can represent it in a decimal float. 
That isn't necessarily true of decimal -> binary floats.


> > > Right, I was playing with this problem
> > > (
> > https://brilliant.org/weekly-problems/2017-10-02/advanced/?problem=no-computer-needed
> > )
> > > and wanted to work in base 2.  I realize it's niche, but it's not
> > exactly a
> > > significant change to the interface even if it's a big change to the
> > > implementation.
> >
> > You should be able to use the native float type for binary
> > floating-point. But the whole point of that challenge is that you
> > shouldn't need a computer.
> >
> 
> Yeah, I know, but I wanted to play with it.  Anyway, native floats don't
> help.

Why not?

If you can write the float in binary exactly, you can write it in hex, 
and use float.fromhex() to convert it exactly (provided it fits into the 
64-bit floats Python uses).



-- 
Steve


More information about the Python-ideas mailing list