【问题标题】:Is the golden ratio defined in Python?黄金比例是否在 Python 中定义?
【发布时间】:2014-08-08 21:01:34
【问题描述】:

有没有办法在标准 python 模块中获得黄金比例,phi?我知道math 模块中的epi,但我可能错过了在某处定义的phi

【问题讨论】:

标签: python math


【解决方案1】:

scipy.constants 将黄金分割率定义为scipy.constants.golden。它在标准库中没有定义,大概是因为它很容易定义自己:

golden = (1 + 5 ** 0.5) / 2

【讨论】:

    【解决方案2】:

    没有。但是,由于您无论如何都在导入 ma​​th,因此 phi 的计算方式可能与 pi 的计算方式相同:

    >>> import math
    >>> pi = 4 * math.atan(1)
    >>> pi
    3.141592653589793
    >>> math.pi
    3.141592653589793
    >>> phi = ( 1 + math.sqrt(5) ) / 2
    >>> phi
    1.618033988749895
    

    ma​​th 定义了 pie 而没有定义 phi 的原因可能是因为没有人要求它.


    python ma​​th 文档说 math.pi 是“数学常数 π = 3.141592...,达到可用精度”。但是,您可以计算四次一的反正切并得到roughly the same result:pi=4*math.atan(1)

    对于 phi 也可以这样说,它不容易以 math.phi 的形式获得,但您可以使用 solution to the quadratic equation x² + x -1 = 0 找到最接近的可用精度:phi=(1+math.sqrt(5))/2

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多