PL/Python
[Python] 정확하고 작은 수가 필요할 때 - decimal
SweetDev
2021. 11. 19. 20:51
decimal.getcontext().prec = 50
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
if value < decimal.Decimal(1e-9) and value > decimal.Decimal(-1e-9):
pi = Decimal('3.14159265358979323846264338327950288419716939937510')
def sin(x):
x = x % (2 * pi)
getcontext().prec += 2
i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1
while s != lasts:
lasts = s
i += 2
fact *= i * (i - 1)
num *= x * x
sign *= -1
s += num / fact * sign
getcontext().prec -= 2
return +s