Use functools.lru_cache for fp.bernoulli

This commit is contained in:
Sergey B Kirpichev
2023-10-07 08:46:48 +03:00
parent 9a2863abe6
commit 005da6a704
+3 -6
View File
@@ -1,4 +1,5 @@
import cmath
import functools
import math
import sys
@@ -18,7 +19,6 @@ class FPContext(StandardBaseContext):
# Override SpecialFunctions implementation
ctx.loggamma = libfp.loggamma
ctx._bernoulli_cache = {}
ctx.pretty = False
ctx._init_aliases()
@@ -56,12 +56,9 @@ class FPContext(StandardBaseContext):
f_wrapped.__doc__ = function_docs.__dict__.get(name, f.__doc__)
setattr(cls, name, f_wrapped)
@functools.lru_cache
def bernoulli(ctx, n):
cache = ctx._bernoulli_cache
if n in cache:
return cache[n]
cache[n] = to_float(mpf_bernoulli(n, 53, 'n'), strict=True)
return cache[n]
return to_float(mpf_bernoulli(n, 53, 'n'), strict=True)
pi = libfp.pi
e = math.e