[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rational#to_i inconsistent with Float#to_i

Mark J. Reed

10/13/2003 5:34:00 PM

Float#to_i rounds toward zero; Rational#to_i rounds down (is equivalent
to Rational#floor). This is inconsistent and complicates the
replacing of Floats with Rationals for added precision in existing
code. Was this an intentional design decision?

irb(main):001:0> require 'rational'
=> true
irb(main):002:0> r = Rational(-7,2)
=> Rational(-7, 2)
irb(main):003:0> puts r.to_i, r.to_f.to_i
-4
-3
=> nil
irb(main):004:0>

-Mark