Uncategorized

Why does Python’s Decimal class generate additional digits that weren’t there before?



Take a look at the following outputs

>>> from decimal import Decimal

>>> print(2.4)
2.4

>>> print(Decimal(2.4))
2.399999999999999911182158029987476766109466552734375

Why exactly is this happening? If the explanation is that 2.4 couldn’t be represented precisely, and therefore already had to be represented in approximate form, then how come the first print statement produced an exact result?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *