3

Here's an illustration of what I'm asking about:

enter image description here

I'm just curious about how Ruby is interpreting these problems, which are clearly different as far as Ruby is concerned. I became curious when I was trying to write a simple math problem without using floats and noted that Ruby would read Floats differently than their Integer counterpart (perhaps 1/2 isn't 0.5's counterpart as far as Ruby is concerned, but that's part of what I'm asking here: why not?).

Can anyone explain what's going on here?

Proto
  • 764
  • 1
  • 7
  • 22

2 Answers2

6
  • 17424 ** 1 / 2 is interpreted as (17424 ** 1) / 2, which is just 17424/2

  • 17424 ** (1 / 2) is counter-intuitive because 1 / 2 is actually 0, not 0.5. This is because when you divide integers, the result's decimal is truncated. You can change one of the operands (or both) to a float to fix this: 17424 ** (1.0 / 2)

August
  • 12,410
  • 3
  • 35
  • 51
3
17424 ** 1 / 2
    17424  / 2
         8712


17424 ** (1/2)
17424 **   0
      1

/ of two integers is an integer. See also: Why is division in Ruby returning an integer instead of decimal value?

Community
  • 1
  • 1
Karol S
  • 9,028
  • 2
  • 32
  • 45