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.
In [6]: def ratioFunction(): ...: num1 = input('Enter the first number: ') ...: num1 = int(num1) # Now we are good ...: num2 = input('Enter the second number: ') ...: num2 = int(num2) # Good, good ...: ratio12 = int(num1/num2) ...: print('The ratio of', num1, 'and', num2,'is', str(ratio12) + '.
The Golden Ratio is a number that's (kind of) equal to 1.618, just like pi is approximately equal to 3.14, but not exactly. You take a line and divide it into two parts – a long part (a) and a short part (b).
The number phi, often known as the golden ratio, is a mathematical concept that people have known about since the time of the ancient Greeks. It is an irrational number like pi and e, meaning that its terms go on forever after the decimal point without repeating.
The Golden Ratio is equal to: 1.61803398874989484820... (etc.) The digits just keep on going, with no pattern.
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
There isn'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 solution to the quadratic equation x² + x -1 = 0: phi = ( 1 + math.sqrt(5) ) / 2
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With