1

I'm testing a simple program that converts Fahrenheit to Celsius, using Ruby.

This code returns 36.999999999 in terminal.

def ftoc(far)
    return (far-32)/1.8
end

ftoc(98.6)

This code returns 37.

def ftoc(far)
    return (far-32)*(5.0/9)
end

ftoc(98.6)

Can anyone tell me why? Thanks!!

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • `(far-32)/1.8` should equal to `(far-32) * 5.0 / 9`. Try. On 2nd code, you force Ruby to calculate 5.0/9 first via brackets, which is 0.55555 causing precision loss. – Raptor Jan 15 '14 at 04:29
  • 4
    Both "work" just fine. The difference of the results is *within acceptable relative-precision tolerances*, with slightly different results based on the order of operations; when you *display* the value, round to the appropriate number of digits. – user2864740 Jan 15 '14 at 04:46
  • The reasons for this "inaccuracy" are discussed in http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken?rq=1 and related – user2864740 Jan 15 '14 at 04:47
  • Interesting, thanks a lot everyone. – user3196635 Jan 15 '14 at 04:54
  • 1
    @ShivanRaptor: `(far-32)/1.8` is not generally expected to be equal to `(far-32) * 5.0 / 9`. The former rounds 1.8 to a representable value, then divides, which rounds again. The latter multiplies by five, which includes rounding, then divides by nine and rounds. The different roundings and different intermediate values may produce different results. – Eric Postpischil Jan 15 '14 at 12:41
  • The number that you write as `1.8` actually represents 1.8000000000000000444089209850062616169452667236328125. – dan04 Jan 17 '14 at 13:56

1 Answers1

4

The type of numbers you are using are called floating point numbers.

In general, any floating point arithmetic operation will introduce rounding errors. You cannot expect two floating-point results to be exactly equal if they were computed in different ways; you must have some tolerance for small errors.

David Grayson
  • 84,103
  • 24
  • 152
  • 189