Steven D'Aprano steve at pearwood.info
Thu Jul 3 11:36:47 CEST 2014

On Thu, 03 Jul 2014 09:51:35 +0300, Marko Rauhamaa wrote:

> Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
>> By the way, there's no need to use an invented example. Here is an
>> actual example:
>> py> import math
>> py> from fractions import Fraction
>> py> math.sqrt(Fraction(2))**2
>> 2.0000000000000004
> Sure, although you were invoking "arbitrary-precision" rational numbers,
> which Fraction() is not.

In what way is Fraction not an arbitrary precision rational number? It's 
a rational number, and it can store numbers to any arbitrary precision 
you like (up to the limit of RAM) in any base you like. 

How about the smallest non-zero number representable in base 17 to 13004 
significant figures? I can represent that as a Fraction with no 
difficulty at all:

py> x = 1/(Fraction(17)**13004)
py> str(x)[:20] + "..." + str(x)[-5:]

And it is calculated *exactly*.

Now, I admit that I have misgivings about using the term "precision" when 
it comes to discussing rational numbers, since the idea of significant 
figures doesn't really work very well with fraction notation. It's not 
clear to me how many "significant figures" x above should be described as 
having. The number of digits in its decimal expansion perhaps? But you 
started using the term, not me, so I'm just following your lead.

If you don't think Fraction counts as "arbitrary precision rational 
number", what do you think does?

>> Floating-point is *hard*, not "perfect".
> It can be both. 

"Perfect" requires that it be flawless. It certainly is not flawless. As 
I have repeatedly stated, there are mathematical properties which 
floating point numbers do not obey. Given that they are supposed to model 
real numbers, the fact that they do not obey the mathematical laws 
applicable to real numbers is a pretty big flaw.

> The point is, regular floating point numbers will likely
> the optimal choice for your numeric calculation needs. They are compact,
> fast and readily supported by hardware and numeric software. Switching
> to Decimal might give you a false sense of security.

Ah, now this is a much more reasonable thing to say. Why didn't you say 
so in the first place? :-)


More information about the Python-list mailing list