46

Is there a way to get the golden ratio, phi, in the standard python module? I know of e and pi in the math module, but I might have missed phi defined somewhere.

dwitvliet
  • 7,242
  • 7
  • 36
  • 62

2 Answers2

83

scipy.constants defines the golden ratio as scipy.constants.golden. It is nowhere defined in the standard library, presumably because it is easy to define yourself:

golden = (1 + 5 ** 0.5) / 2
Lynn
  • 10,425
  • 43
  • 75
4

The standard library doesn't. However, since you are importing math anyway, phi may be calculated the same way pi would be calculated:

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

The reason math has pi and e defined but not phi may be because no one asked for it.


The python math docs says math.pi is "The mathematical constant π = 3.141592..., to available precision". However, you may calculate four times the arc tangent of one and get roughly the same result: pi = 4 * math.atan(1), or pi = math.acos(-1):

>>> math.pi == math.acos(-1) == 4 * math.atan(1)
True

The same could be said about phi, which is not readily available as math.phi but you may find the nearest available precision with the regular formula: phi = ( 1 + math.sqrt(5) ) / 2.


Libraries that define or provide a "shortcut" to the golden ratio are these:

Scipy

Scipy calculates a static value for the algebraic formula using standard math package at import time and it's the same thing as defining it yourself (specifically to cpython both will be computed at compile time):

import math as _math
golden = golden_ratio = (1 + _math.sqrt(5)) / 2
print(golden)

MpMath

Mpmath calculates the algebraic formula at call time to supplied precision:

import mpmath
print(mpmath.mp.phi)

Sympy

The closest thing to a definition of golden ratio is the sympy's singleton GoldenRatio which uses the mpmath float (mpf) calculated at call time:

import sympy
print(float(sympy.S.GoldenRatio))

Sage

Sage Math goes further allowing you to calculate phi in many different ways.

Iuri Guilherme
  • 389
  • 1
  • 10
  • 2
    The wording of this answer makes it sound like the limited precision of math.pi is a bug. It's not a bug. [Python uses doubles internally.](https://docs.python.org/3/tutorial/floatingpoint.html#representation-error) That's why the docs say "to available precision". `4*math.atan(1)` will not give you a "better approximation", because you're still using doubles underneath. Yes, even "if you have a supercomputer". :) You don't need a supercomputer to generate more than 53 bits of pi. – ppm Dec 09 '19 at 06:56
  • 1
    I figured the [truncated constant I had originally referenced](https://github.com/python/cpython/blob/cb9879b948a19c9434316f8ab6aba9c4601a8173/Modules/mathmodule.c#L75) is not the result of calling `math.pi`, but rather [used in the reflection formula for the gamma function](https://github.com/python/cpython/blob/cb9879b948a19c9434316f8ab6aba9c4601a8173/Modules/mathmodule.c#L70). Will update the answer to acknowledge my mistake. – Iuri Guilherme Jul 17 '20 at 16:39