You might want to 'require "mathn"' on the beginning of the script if it bugs you, the module causes Ruby math to work more... err... mathemathically. E.g.:
Thanks, Joe and David. Although I don't quite understand the science behind
that, add a bit more ".to_i" is enough for me.
···
On 1/14/06, David Vallner <david@vallner.net> wrote:
On Sat, 14 Jan 2006 16:52:41 +0100, Sky Yin <sky.yin@gmail.com> wrote:
>
> Basic math tells me 2/1 = 2, so what's the point of returning Rational
> instead of Fixnum here?
>
Because the code calculates in rationals?
You might want to 'require "mathn"' on the beginning of the script if it
bugs you, the module causes Ruby math to work more... err...
mathemathically. E.g.:
Though getting a Rational back is not incorrect, clearly getting a Fixnum
back would be one's initial expectation.
Interesting that 'mathn' caused a change in return type. I suppose that
means the return type is not part of the date subtraction contract. I
quickly eyeballed mathn.rb to try to see the specific reason for the change
in return type. My guess if that Date - Date uses the ** operator because
** is redefined in mathn.
Requiring mathn did not change the return type. Requiring mathn changed how rationals display themselves (ie it changed Rational#inspect). (It also changed Integer#/ to return rationals instead of Integers, among other things I am sure)
···
On Jan 15, 2006, at 1:12 AM, Brian Buckley wrote:
irb(main):002:0> require "mathn"
Though getting a Rational back is not incorrect, clearly getting a Fixnum
back would be one's initial expectation.
Interesting that 'mathn' caused a change in return type. I suppose that
means the return type is not part of the date subtraction contract. I
quickly eyeballed mathn.rb to try to see the specific reason for the change
in return type. My guess if that Date - Date uses the ** operator because
** is redefined in mathn.
Yes, the change of return type is intentional - from skimming the source code, aside from the newly defined features, it seems mathn tweaks some flags to make Rational and Complex always unify to Integers if possible, and aliases Integer#/ for division of Integers to return Rationals. The latter might NOT be what you might want if you're used to the (IMO illogical) C behaviour of using integral division instead. mathn doesn't redefine Rational#**, it defines it in the first place.
The date substraction contract is not violated, because Integers should have the same contract for every operation they share with Rationals of the same value. If that's not the case, it could be considered a bug in the Ruby core API.
David Vallner
···
On Sun, 15 Jan 2006 07:12:26 +0100, Brian Buckley <briankbuckley@gmail.com> wrote:
irb(main):002:0> require "mathn"
Though getting a Rational back is not incorrect, clearly getting a Fixnum
back would be one's initial expectation.
Interesting that 'mathn' caused a change in return type. I suppose that
means the return type is not part of the date subtraction contract. I
quickly eyeballed mathn.rb to try to see the specific reason for the change
in return type. My guess if that Date - Date uses the ** operator because
** is redefined in mathn.