프로그래밍/Python
Python의 math.floor() 함수에 대한 문제점(?) 고찰
Scripter
2023. 1. 11. 01:38
10**100 을 7로 나누고 다시 7을 곱해 보았습니다.
math.floor() 함수는 유효숫자의 개수 때문에 오차가 좀(?) 많네요.
(float 인스턴스).as_integer_ratio() 메서드로도 확인해 보았습니다.
>>> import math
>>> help(math.floor)
Help on built-in function floor in module math:
floor(x, /)
Return the floor of x as an Integral.
This is the largest integer <= x.
>>> math.floor(1.0/7*1.0E100)*7
9999999999999999916195299569383046603428070211005418059480472676230398615693829843450434335205752832
>>> 10**100 // 7
1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428
>>> 10**100 // 7 * 7
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999996
>>> (0.7*1E100).as_integer_ratio()
(6999999999999999528519570100600705052013993947706024980124877574781630764975543820898143428537221120, 1)