[Tutor] Hoping for a brief explanation!
Oscar Benjamin
oscar.j.benjamin at gmail.com
Wed Aug 17 14:51:41 EDT 2022
On Wed, 17 Aug 2022 at 07:23, Peter Otten <__peter__ at web.de> wrote:
>
> On 16/08/2022 16:04, Mats Wichmann wrote:
>
> > There is a different type in Python that you can use when you need to
> > keep exactness (for example when representing monetary transactions)
> >
> > https://docs.python.org/3/library/decimal.html
>
> You may run into counterintuitive results with that, too.
>
> >>> from decimal import Decimal, getcontext
> >>> getcontext().prec = 3
> >>> x = Decimal("0.9999")
> >>> x < 1
> True
> >>> x * x < 1
> False
>
> There is no numerical type that "just works" like you learned at school.
There is Fraction which gives perfectly exact results for all of the
examples presented in this thread.
--
Oscar
More information about the Tutor
mailing list