[Python-ideas] [Python Ideas] Python Float Update

Andrew Barnert abarnert at yahoo.com
Thu Jun 4 01:20:15 CEST 2015

On Jun 3, 2015, at 15:46, u8y7541 The Awesome Person <surya.subbarao1 at gmail.com> wrote:
>> On Wed, Jun 3, 2015 at 3:35 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
>> On Jun 3, 2015, at 15:17, u8y7541 The Awesome Person <surya.subbarao1 at gmail.com> wrote:
>>>> I?m going to show a few examples of how Decimals violate the fundamental
>>>> laws of mathematics just as floats do.
>>> Decimal is also uses sign and mantissa, except it's Base 10. I think
>>> Decimal should use numerators and denominators, because they are more
>>> accurate.
>> So sqrt(2) should be represented as an exact fraction? Do you have infinite RAM?
> You can't represent sqrt(2) exactly with sign and mantissa either.

That's exactly the point: Decimal never _pretends_ to be exact, and therefore there's no problem when it can't be.

By the way, it's not just "sign and mantissa" (that just gives you an integer, or maybe a fixed-point number), it's sign, mantissa, _and exponent_.

> When Decimal detects a non-repeating decimal, it should round it, and
> assign it a numerator and denominator something like 14142135623730951
> / 10000000000000000 simplified.
> That's better than sign and mantissa
> errors.

No, that's exactly the same value as mantissa 1.4142135623730951 and exponent 0, and therefore it has exactly the same error. You haven't gained anything over using Decimal.

And meanwhile, you've lost some efficiency (it takes twice as much memory because you have to store all those zeroes, where in Decimal they're implied by the exponent), and you've lost the benefit of a well-designed standard to follow (how many digits should you keep? what rounding rule should you use? should there be some way to optionally signal the user that rounding has occurred? and so on...). 

And, again, you've made things more surprising, not less, because now you have a type that's always exact, except when it isn't.

Meanwhile, when you asked about the problems, I gave you a whole list of them. Have you thought about the others, or only the third one on the list? For example, do you really want adding up a long string of simple numbers to give you a value that takes 500x as much memory to store and 500x as long to calculate with if you don't need the exactness? Or is there going to be another rounding rule that when the fraction gets "too big" you truncate it to a smaller approximation?

And meanwhile, if you do need the exactness, why don't you need to be able to carry around exact rational multiplies of pi or an exact representation of 2 ** 0.5 (both of which SymPy can do for you, by representing numbers symbolically, the way humans do when they need to)?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150603/a2774199/attachment.html>

More information about the Python-ideas mailing list