Project

General

Profile

Actions

Bug #9170

closed

Math.sqrt returns different types when mathn is included; breaks various gems - this bug can be reproduced in Ruby 1.8 as well

Added by kranzky (Jason Hutchens) over 10 years ago. Updated almost 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.2.1]
Backport:
[ruby-core:58653]

Description

Math.sqrt() usually returns a Float or throws an exception. Some gems rely on this behaviour (e.g. by calling nan? on the results of a calculation that includes a sqrt).

After requiring mathn, Math.sqrt() returns an Integer in some cases (when the results of the sqrt would be a whole number).

Here is a transcript that demonstrates the problem:

$ rvm use ree
Using /Users/jason/.rvm/gems/ree-1.8.7-2012.02
$ ruby -v
ruby 1.8.7 (2012-02-08 MBARI 8/0x6770 on patchlevel 358) [i686-darwin12.1.0], MBARI 0x6770, Ruby Enterprise Edition 2012.02
$ ruby -e "puts Math.sqrt(4).nan?"
false
$ ruby -e "require 'mathn'; puts Math.sqrt(4).nan?"
-e:1: undefined method nan?' for 2:Fixnum (NoMethodError) $ rvm use 1.9.3 Using /Users/jason/.rvm/gems/ruby-1.9.3-p448 $ ruby -v ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.2.0] $ ruby -e "puts Math.sqrt(4).nan?" false $ ruby -e "require 'mathn'; puts Math.sqrt(4).nan?" -e:1:in ': undefined method nan?' for 2:Fixnum (NoMethodError) $ rvm use 2.0 Using /Users/jason/.rvm/gems/ruby-2.0.0-p247 $ ruby -v ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.2.1] $ ruby -e "puts Math.sqrt(4).nan?" false $ ruby -e "require 'mathn'; puts Math.sqrt(4).nan?" -e:1:in ': undefined method nan?' for 2:Fixnum (NoMethodError) $ rvm use jruby Using /Users/jason/.rvm/gems/jruby-1.7.4 $ ruby -v jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.6.0_51-b11-457-11M4509 [darwin-x86_64] $ ruby -e "puts Math.sqrt(4).nan?" false $ ruby -e "require 'mathn'; puts Math.sqrt(4).nan?" NoMethodError: undefined method nan?' for 2:Fixnum
(root) at -e:1

Here is a discussion of how the bug manifests itself: https://github.com/olbrich/ruby-units/pull/48

Inclusion of mathn causes issues in RGeo, Alchemist, ActiveShipping and pry-rails

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0