clbustos / distribution

Statistical Distributions multi library wrapper. Uses Ruby by default and C (statistics2/GSL) or Java extensions where available.
Other
141 stars 52 forks source link

The gem changes the behavior of integer division #4

Open freefrancisco opened 12 years ago

freefrancisco commented 12 years ago

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 commented 12 years ago

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.