[Python-ideas] Consider generalizing Decimal to support arbitrary radix
Terry Reedy
tjreedy at udel.edu
Wed Feb 7 19:13:27 EST 2018
On 2/7/2018 6:08 PM, Neil Girdhar wrote:
>
> On Wed, Feb 7, 2018 at 5:52 PM Steven D'Aprano
> <steve at pearwood.info
> <mailto:steve at pearwood.info>> wrote:
>
> On Wed, Feb 07, 2018 at 10:08:50PM +0000, Neil Girdhar wrote:
>
> > Oh, and to answer your specific question, I want to change the way
> > arithmetic is done. I want it to be done in a different radix.
>
> Why?
>
> There are clear advantages to floating point arithmetic done in base 2
> (speed, minimum possible rounding error, least amount of wobble), and a
> different advantage to floating point done in base 10 (matches exactly
> the standard decimal notation used by humans with no conversion error),
This is the specialness of base 10
> Outside of those two bases, arithmetic done in any other base is going
> to combine the worst of both:
>
> - slower;
> - larger errors when converting from decimal numbers (in general);
> - larger rounding errors;
> - larger wobble;
> I don't see why it would have any of those problems.
Any base other than 2 has decreased speed (on a binary computer) and
increased computational rounding errors and wobble.
> Base 10 isn't special in any way.
Except as noted above and the fact that computation with binary coded
decimal goes back to the early days of electronic computation.
> with no corresponding advantages unless your data is coming to you in
> arbitrary bases.
> 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.
In cpython, decimal uses _cdecimal for speed. I suspect that 10 is not
only explicitly hard-coded as the base but implicitly hard-coded by
using algorithm tricks that depend on and only work when the base is 10.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list