Search code examples
pythonfloor

How to find floor on big floating point numbers?


How could I correctly find floor(a*n) in python where n is a very large floating point number?

I tried it using Decimal module but

Deciaml(1.1) * Decimal(123456789123456789123456789)

It is not giving right answer.


Solution

  • You can't use a float to construct a Decimal, the precision has already been lost. Initialize it with a string:

    >>> Decimal(1.1) * Decimal(123456789123456789123456789)
    Decimal('135802468035802479000968054.4')
    >>> Decimal('1.1') * Decimal(123456789123456789123456789)
    Decimal('135802468035802468035802467.9')