0

Version: Python 2.7

I am trying to check if the number x, in this case 64, is a cube. I check if the root-cube of the number is an integer:

root3 = math.pow(64, (1/3.0))
print(root3)
print(root3.is_integer())

Which prints:

4.0
False

Which is wrong, since 4.0 is an integer. In comparison, using 4.0 directly:

print((4.0).is_integer())

Which prints True.

How can I make sure the root cube of 64 is considered as a whole float?

Momo
  • 3,542
  • 4
  • 21
  • 34
  • You didn't suspect an calculation round up issues? `>>> root3` show 3.9999999999999996 – mootmoot Jun 10 '16 at 14:32
  • IDLE tells me that `64 ** (1/3.0)` is `3.9999999999999996` (Python 3.4) and that's not an integer. Of course we know that the result should be `4.0` but floating point math can't be exact. – Matthias Jun 10 '16 at 14:33
  • @Morno Check the dupe [post](http://stackoverflow.com/questions/21586340/how-do-i-get-a-whole-number-as-a-result-for-a-cube-root). `root3` here will be `3.9999999999999996`. Another dupe can be [Is cube root integer?](http://stackoverflow.com/q/23621833). A highly related read [Is floating point math broken?](http://stackoverflow.com/q/588004) – Bhargav Rao Jun 10 '16 at 14:33

0 Answers0