'Why does reversing the function not get me the original number?
I wanted to calculate the radius of a sphere from a given volume. As the volume was in ml, I converted it to mm^3. However, when I did a sanity check to put the radius back into the sphere formula, it gave me a number 1/4 of the original volume.
print minvol
mmminvol = minvol*1000
print mmminvol
rcubed = mmminvol/(4/3*ma.pi)
r0 = rcubed**1/3 #min radius
print r0
print 4/3*ma.pi*r0**3
Running the code gives me
0.003
3.0
0.238732414638
0.0569931657988
Not really sure what is going on...thank you in advance!
Solution 1:[1]
To avoid such issues in future, use pow function to calculate power.
import math as ma
minvol = 0.003
print(minvol)
mmminvol = minvol*1000
print(mmminvol)
rcubed = mmminvol/(4/3*ma.pi)
r0 = pow(rcubed,1/3) #min radius
print(r0)
print(4/3*ma.pi*r0**3)
The output is
0.003
3.0
0.8947002289396496
3.0000000000000004
Earlier you were doing.
print(rcubed)
#0.7161972439135291
print(rcubed**1/3)
#0.23873241463784303
Which means raise rcubed to power of 1 and divide by 3, hence your wierd behaviour.
Solution 2:[2]
Missing parentheses:
Use this:
print minvol
mmminvol = minvol*1000
print mmminvol
rcubed = mmminvol/(4/3*ma.pi)
r0 = rcubed**(1/3) # this was changed
print r0
print 4/3*ma.pi*r0**3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
