Как получить точность без округления?
Например:
import math
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
r = lambda f,p: f - f % p
size = 3245.249999999999853146309903
y = r(size,0.01)
print(truncate(y,2))
y = r(size,0.001)
print(truncate(y,3))
y = r(size,0.0001)
print(truncate(y,4))
y = r(size,0.00001)
print(truncate(y,5))
y = r(size,0.000001)
print(truncate(y,6))
y = r(size,0.0000001)
print(truncate(y,7))
y = r(size,0.00000001)
print(truncate(y,8))
# Output:
# 3245.24
# 3245.249
# 3245.2499
# 3245.24999
# 3245.25 # wrong
# 3245.25 # wrong
# 3245.24999999
Или
r = lambda f,p: f - f % p
size = 3245.249999999999853146309903
y = r(size,0.01)
print(y)
y = r(size,0.000001)
print(y)
y = r(size,0.0000001)
print(y)
# outputs:
# 3245.2400000000002 # wrong
# 3245.25 # wrong
# 3245.25 # wrong
Или
from decimal import Decimal
size = 3245.249999999999853146309903
print(Decimal(size))
print(Decimal(size)-(Decimal(size)%Decimal(0.000001)))
print(int(size) - (size % 0.01))
# outputs
# 3245.25 # wrong
# 3245.249999999999853146309903 # wrong
# 3244.9900000000002 # wrong
Я знаю есть аппаратное / электронное ограничение c. Однако есть ли способ справиться с этой ситуацией? Все ли компьютерные "математики" таковы?