marko at pacujo.net
Thu Jul 3 08:51:35 CEST 2014
Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> On Wed, 02 Jul 2014 23:00:15 +0300, Marko Rauhamaa wrote:
>> Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
>>>> >>> Rational(2).sqrt() * Rational(2).sqrt() == Rational(2)
>>> Square root of 2 is not a rational number.
>> Nobody said it was.
> Your comment can be read as implying it. You stated:
> Even "arbitrary-precision" RATIONALS [emphasis added] would
> suffer from the same problem
> [end quote]
> and then showed an invented example where you squared a NON-RATIONAL.
While √2 is irrational, the hypothetical Rational(2).sqrt() probably
would be another "arbitrary-precision" Rational number and thus,
necessarily an approximation.
That's completely analogous to Decimal(1) / Decimal(3) being an
And the point: when dealing with real numbers on a computer, there's no
way to avoid approximations. If an aspiring programmer is dismayed at
the imprecision of 0.1, that probably wouldn't be the right moment to
talk about Decimal().
> 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
Sure, although you were invoking "arbitrary-precision" rational numbers,
which Fraction() is not.
> I'm sorry Marko, have you not being paying attention? Have you ever
> done any numeric programming?
Your style is consistent and impeccable.
> Floating-point is *hard*, not "perfect".
It can be both. 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.
More information about the Python-list