8

I am trying to calculate a^(1/n), where ^ denotes exponentiation.

However, the following:

Math.pow(8, 1/3)

returns 1.0 instead of returning 2.0.

Why is that?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
GltknBtn
  • 512
  • 6
  • 13
  • 9
    Aww. Adorable. :) –  Apr 11 '13 at 18:01
  • 10
    While this question could use a better description, I don't really understand all of the downvotes. This might be trivial to the majority of people here, but this sort of thing (integer division when float division is desired) is a very common issue, especially among new developers; this question will definitely help others. – Saggio Apr 11 '13 at 18:02
  • 1
    @Saggio - Yep, it's a major stumbling block for beginners, and even some of us "experts" run afoul of it from time to time (and spend more time on the problem than we care to admit before we figure it out). – Hot Licks Apr 11 '13 at 18:08
  • @Saggio Yea, we've all asked these sorts of questions at times. I don't understand the mass of down votes either. – cost Apr 11 '13 at 18:09
  • 2
    It is a dupe, of course, but not a bad question. Would be hard for a beginner to find the dupe. – Hot Licks Apr 11 '13 at 18:09
  • You are right Saggio. Thank you so much. New developers will need like this supports. – GltknBtn Apr 11 '13 at 18:11
  • I don't know why, but now days downvoting is becoming a fashion here... That downvoter could have earned about 120 reputation with 2-3 line answer now... Useless downvoting on good questions should be restricted somehow... This kind of downvoting is frustrating for beginners.. – Sam Apr 11 '13 at 18:16
  • @SAM: I agree 100%. Part of the reason is that it's so much easier to downvote than to write a good answer (however brief)... – NPE Apr 11 '13 at 18:21
  • @SAM: There is nothing _wrong_ with downvoting. Upvoting means nothing without downvoting. The light means nothing without the dark. Life means nothing without serial killers... It's important not to take it personally. – Lightness Races in Orbit Apr 11 '13 at 18:23
  • @NPE Ignoring would be even easier ;) – Sam Apr 11 '13 at 18:25
  • @LightnessRacesinOrbit Life means nothing without serial killers ?!?!?!?!?! – Sam Apr 11 '13 at 18:26
  • 1
    If 1/3 is not just an example but is something you use specifically, then you should consider `Math.cbrt` instead of `Math.pow`. – Eric Postpischil Apr 11 '13 at 21:52

3 Answers3

17

The problem is that 1/3 uses integer (truncating) division, the result of which is zero. Change your code to

Math.pow(8, 1./3);

(The . turns the 1. into a floating-point literal.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thank you so much. It seems trivial but important for me. – GltknBtn Apr 11 '13 at 18:07
  • @GltknBtn: You're welcome. Everyone runs into this at some point. It's even less obvious when the division is part of a more complex expression, as is the case here. – NPE Apr 11 '13 at 18:08
5

1/3 becomes 0(Because 1 and 3 are taken as int literals).

So you should make those literals float/double...

Do:

Math.pow(8, 1f/3) or

Math.pow(8, 1./3) or

Math.pow(8, 1.0/3)

Sam
  • 1,842
  • 3
  • 19
  • 33
4

Try Math.pow(8, (1.0f / 3.0f)) instead.

1 / 3 will do an integer division, which will give you 8 ^ 0 = 1

ssantos
  • 16,001
  • 7
  • 50
  • 70