-1

Using Python 2.7. Here is the code and output, my purpose is simply to check if a number is a cube number.

Source code,

x = 1728 ** (1.0/3)
print x
y = int(x)
print y

Output,

12.0
11
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 2
    `x` is perhaps `11.9999...something`. – AlexD Jan 24 '17 at 02:12
  • 1
    I suggest that you learn about the general concerns of representing floating point numbers in computer hardware and software. You should also read about Python-specific issues as well. – Code-Apprentice Jan 24 '17 at 02:13
  • @AlexD, if so, how to resolve this issue? – Lin Ma Jan 24 '17 at 02:13
  • 1
    Resolving the issue is highly dependent on your real use-case. – Code-Apprentice Jan 24 '17 at 02:14
  • @Code-Apprentice, I just want to check if a number is a cube number. – Lin Ma Jan 24 '17 at 02:14
  • 1
    You should consider going the opposite direction and actually cube integers instead of taking the cube root. The trade off is that this will be less efficient while avoiding accuracy issues. Of course, if you are doing this repeatedly, you should create a list of cubes so that you don't have to recompute them every time. – Code-Apprentice Jan 24 '17 at 02:16
  • 2
    Note that `int(12)` as you describe in the title does not actually appear in your code. `int(12)` is indeed 12. – TigerhawkT3 Jan 24 '17 at 02:20
  • @TigerhawkT3, I mean this line `y = int(x)`, where `x` is `12.0`, any thoughts why `y` becomes `11` other than `12`? – Lin Ma Jan 24 '17 at 04:10
  • @Code-Apprentice, I like your approach. But just got stuck in my issue, `y = int(x)`, where `x` is `12.0`, any thoughts why `y` becomes `11` other than `12`? – Lin Ma Jan 24 '17 at 04:11
  • 1
    Again, `x` is neither 12 nor 12.0. It is another number entirely. The question's title is misleading. – TigerhawkT3 Jan 24 '17 at 04:14
  • @TigerhawkT3, what is the actual value do you mean? I said it is `12.0` from print output. – Lin Ma Jan 24 '17 at 04:20
  • `print` gives an approximation. You have to [specify when you want to display more precision](http://stackoverflow.com/questions/8568233/print-float-to-n-decimal-places-including-trailing-0s). – TigerhawkT3 Jan 24 '17 at 04:38
  • "any thoughts why y becomes 11 other than 12" The first two comments from AlexD and myself already address this. – Code-Apprentice Jan 24 '17 at 04:42

1 Answers1

4

Because you're using floating points, and the result is some very very small fraction less than 12, and when you cast x to an int, the entire decimal portion of the number is discarded.

If what you want to do is round the number, use round().

furkle
  • 5,019
  • 1
  • 15
  • 24
  • Thanks furkle, do you have any elegant ways to check if a number is a cube number? – Lin Ma Jan 24 '17 at 02:14
  • Thanks, but `round` will return the nearest number, how could it be used to check if a number is a cube number? – Lin Ma Jan 24 '17 at 02:15
  • 2
    @LinMa I would suggest opening a new question for that -- you're likely to get more info specific to that problem and not the vagaries of floating-point math in Python 2.x. – furkle Jan 24 '17 at 02:16
  • Sure, just to confirm, in my specific problem, `11` is returned since `int` gets the floor, other than round to nearest number? – Lin Ma Jan 24 '17 at 02:19