distribution icon indicating copy to clipboard operation
distribution copied to clipboard

The gem changes the behavior of integer division

Open freefrancisco opened this issue 12 years ago • 1 comments

When including the gem, division of two integers returns a rational, whereas normally it would return another integer. Is there a way to turn this off? I would like to use the gem for statistics, but it's clashing with other parts of the code.

ruby-1.8.7-p352 :001 > require 'rubygems' => true ruby-1.8.7-p352 :002 > 3/2 => 1 ruby-1.8.7-p352 :003 > require 'distribution' => true ruby-1.8.7-p352 :004 > 3/2 => 3/2 ruby-1.8.7-p352 :005 > (3/2).class => Rational

freefrancisco avatar Mar 15 '12 23:03 freefrancisco

Found the cause, the change in Fixnum behavior happens because of requiring mathn.

The gem only requires mathn once, in math_extension.rb, and only when the ruby version is less than 1.9. This is so it can use Prime, defined in mathn. For later versions of ruby it just requires prime, defined separately, so in later versions we will not see the issue. My solution is, instead of requiring mathn, just inline the code for the Prime class as defined in mathn in older rubies. That keeps the behavior of Fixnum division consistent across versions.

freefrancisco avatar Mar 16 '12 01:03 freefrancisco