oinkoink
11/16/2006 11:28:00 PM
> >> 5 / 2.0
> => 2.5
> >> 5 % 2
> => 1
> >> 5 / 2
> => 2
> >>
> In ruby when you divide integers, you get integer division. If you
> want float, use floats (just add
> point zero).
It's not quite so simple.
>> 5/2
=> 2
>> require 'mathn'
=> true
>> 5/2
=> 5/2
....where the last "5/2" is a rational number. If you want to do
integer division "portably" (i.e., in a way that is compatible with the
mathn standard library having been required earlier in the program),
you should use "div"
>> 5.div(2)
=> 2
>> 5.modulo(2)
=> 1
>> 5.divmod(2)
=> [2,1]
If you wish to use the Rational, Complex, or Matrix classes in the
standard library, you will need to require mathn to avoid strange and
even downright buggy behavior. But this redefines the meaning of
expressions like 5/2.