Skip to content Skip to sidebar Skip to footer

Remainder On Float In Python

I just want to show you the results of the operations in python. I cannot explain. >>> 1.0%1.0 0.0 (OK) >>> 1.0%0.1 0.09999.... >>> 1.0%0.001 0.00999..

Solution 1:

As this (long) response says, use decimal module:

>>>from decimal import Decimal>>>Decimal('3.5') % Decimal('0.1')
Decimal('0.0')
>>>print(Decimal('3.5') % Decimal('0.1'))
0.0
>>>(Decimal(7)/2) % (Decimal(1)/10)
Decimal('0.0')

The problem is essentially due to the representation of floats in the system, you can read stuff about that everywhere on the Internet, and in the response linked.

Post a Comment for "Remainder On Float In Python"