Should I use scipy.pi, numpy.pi, or math.pi?

2019-01-13 11:45发布

In a project using SciPy and NumPy, should I use scipy.pi, numpy.pi, or math.pi?

2条回答
男人必须洒脱
2楼-- · 2019-01-13 12:20
>>> import math
>>> import numpy as np
>>> import scipy
>>> math.pi == np.pi == scipy.pi
True

So it doesn't matter, they are all the same value.

The only reason all three modules provide a pi value is so if you are using just one of the three modules, you can conveniently have access to pi without having to import another module. They're not providing different values for pi.

查看更多
仙女界的扛把子
3楼-- · 2019-01-13 12:32

One thing to note is that not all libraries will use the same meaning for pi, of course, so it never hurts to know what you're using. For example, the symbolic math library Sympy's representation of pi is not the same as math and numpy:

import math
import numpy
import scipy
import sympy

print(math.pi == numpy.pi)
> True
print(math.pi == scipy.pi)
> True
print(math.pi == sympy.pi)
> False
查看更多
登录 后发表回答