+ 1
Roots In Python
Hi Guys,I'm Trying To Make A Function That Takes Two Parameters a And b,Then Returns The bth Root Of a,I Made It Like This: def root(a,b): .......return a**(1/b) But When I Input Numbers Like 125 And 3,That Should Return 5,It Returns 4.999999 Instead,Can Anybody Tell Me Why Does This Happen And How Can I Fix This? Note: I Tried To To Use gmpy2 Module,But When Installing It,IT Gives Me An Error,And I Didn't Find The Wheel Version Of It On Python 3.8
9 Respuestas
+ 3
Float numbers sometimes come out slightly off.
Can you try to use the round function on your result?
+ 3
This is problem of floating operations accuracy.
More even try to do 0.1+0.2 )))))))
So it is a small old trick)) use it.
https://code.sololearn.com/cW24N6z1tJVf/?ref=app
+ 1
HonFu
This Will Round The Number Even If The Answer Is True
+ 1
You mean, if the result is already as expected?
Well, there's no problem.
round(5) will still be 5.
So just apply it on all the numbers equally.
+ 1
Have you tried using round?
Because I fail to see the problem.
When I run this...
print(round(5))
print(round(5.0))
... I get 5 both times.
+ 1
HonFu
For Example:
root(6.25,2)
answer should be float
but round will return an integer
+ 1
I searched but only found the same formula that you are using (some were coded slightly different)
I even found the same formula with the example of 125 and 3 and the one who answered said it should be 5. However, I tried it and it gave a fraction.
0
HonFu
No If The Result Is Float
0
george
Thanks❤️❤️