0

So, I've started a program to calculate the nth root of a number:

*x = float(input('Enter a number: '))
n = float(input('Enter the root you would like to calculate: '))
result=x**(1/float(n))
print(f'The {n}th root of = {x} is:',result)*

The program works with every number and root, but when I enter as x=64 and n=3 it shows this: The 3.0 root of = 64.0 is: 3.9999999999999996

2 Answers2

0

As noted in comments, this is the nature of floating point math.

In your case, you can control what you print with a specifier in an f-string:

>>> n = 3.999996
>>> print(f"{n : .0f}")
 4
>>> 
Chris
  • 26,361
  • 5
  • 21
  • 42
-2

Its because of the way floating point is stored. Often you'll see stuff like this, a solution is to always round result to the nearest like, thousandth.