3.2*2 is 9.6 ... or maybe it isn't?
pdpi
pdpinheiro at gmail.com
Fri Jun 26 16:09:25 CEST 2009
On Jun 26, 11:01 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 25 Jun 2009 12:41:13 -0600, Michael Torrie wrote:
> > If you want accurate math, check out other types like what is in the
> > decimal module:
>
> >>>> import decimal
> >>>> a=decimal.Decimal('3.2')
> >>>> print a * 3
> > 9.6
>
> Not so. Decimal suffers from the exact same problem, just with different
> numbers:
>
> >>> import decimal
> >>> x = decimal.Decimal('1')/decimal.Decimal('3')
> >>> 3*x == 1
>
> False
>
> Some numbers can't be represented exactly in base 2, and some numbers
> can't be represented exactly in base 10.
>
> --
> Steven
But since 10 = 2 * 5, all numbers that can be finitely represented in
binary can be represented finitely in decimal as well, with the exact
same number of places for the fractional part (and no more digits
than the binary representation in the integer part)
More information about the Python-list
mailing list