lixo.org

How Much Is 3/2?

Sorry for being so happy-happy-joy-joy about Ruby lately, I’m still so excited with the new toy I can’t write about much else. I just read this on Guaracy Monteiro’s (mostly brazilian portuguese link, I’m loosely translating):


Well, I’m living in 2005. New languages are born daily, and most of them insist in some of the same mistakes. 3/2 is not equal to 1, but 3/2. I think I learned that in first grade.


Ok, there’s a long and heated argument behind all that, but hey, I started programming exactly because I wanted an easy way out of dull math homework like what we get on the first grade, and computers could do calculations so much better and faster than me when I was 8 (and they still can, last time I checked). But back in the QBasic days, I couldn’t teach the damn computer to figure out the fractions for me, and there was no answer labelled “0.444…” on those tests. So, as you can see, this topic brings back the memories of the first few times I became really frustrated at a computer, something that happens every couple of minutes nowadays. And Ruby just relieved some of that pain:

irb(main):001:0> 3/2
=> 1
irb(main):002:0> 3/2.0
=> 1.5


Well, so far, it’s still frustrating. Behold, the great mathn:

irb(main):003:0> require 'mathn'
=> true
irb(main):004:0> 3/2
=> 3/2
irb(main):005:0> 3/2.0
=> 1.5
irb(main):006:0> 3/2 - 2/2
=> 1/2


Rather odd to see the way basic math works differently after requiring a so innocent-looking module, especially for those used to static languages, but there’s a lot of power and flexibility behind that. I do like power and flexibility, if simplicity comes in the package, and in this case, it does.