Forum: Ruby-core [ruby-trunk - Bug #9170][Open] Math.sqrt returns different types when mathn is included; breaks vari

1ca3a0180e953e56d528a3b5ddb3d6b3?d=identicon&s=25 kranzky (Jason Hutchens) (Guest)
on 2013-11-28 03:55
(Received via mailing list)
Issue #9170 has been reported by kranzky (Jason Hutchens).

----------------------------------------
Bug #9170: Math.sqrt returns different types when mathn is included;
breaks various gems - this bug can be reproduced in Ruby 1.8 as well
https://bugs.ruby-lang.org/issues/9170

Author: kranzky (Jason Hutchens)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin12.2.1]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


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 `<main>': 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 `<main>': 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
7cca11c5257fda526eeb4b1ada28f904?d=identicon&s=25 mrkn (Kenta Murata) (Guest)
on 2013-12-19 16:09
(Received via mailing list)
Issue #9170 has been updated by mrkn (Kenta Murata).

Category set to core
Status changed from Open to Feedback
Target version set to next minor

It's not a bug.

Do you want to introduce Numeric#nan? method to resolve this problem?
If so, this issue should be in Feature tracker.

Please tell us how do you think.
----------------------------------------
Bug #9170: Math.sqrt returns different types when mathn is included;
breaks various gems - this bug can be reproduced in Ruby 1.8 as well
https://bugs.ruby-lang.org/issues/9170#change-43760

Author: kranzky (Jason Hutchens)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: next minor
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin12.2.1]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


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 `<main>': 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 `<main>': 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
7cca11c5257fda526eeb4b1ada28f904?d=identicon&s=25 mrkn (Kenta Murata) (Guest)
on 2013-12-19 16:10
(Received via mailing list)
Issue #9170 has been updated by mrkn (Kenta Murata).


I think introducing Numeric#nan? is good.
----------------------------------------
Bug #9170: Math.sqrt returns different types when mathn is included;
breaks various gems - this bug can be reproduced in Ruby 1.8 as well
https://bugs.ruby-lang.org/issues/9170#change-43761

Author: kranzky (Jason Hutchens)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: next minor
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin12.2.1]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


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 `<main>': 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 `<main>': 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
1ca3a0180e953e56d528a3b5ddb3d6b3?d=identicon&s=25 kranzky (Jason Hutchens) (Guest)
on 2014-01-06 06:44
(Received via mailing list)
Issue #9170 has been updated by kranzky (Jason Hutchens).


mrkn (Kenta Murata) wrote:
> It's not a bug.

I'm a middle-man here, but I have gem maintainers claiming that it is a
bug, because the documentation for Math.sqrt() indicates that it returns
a Float, and that it's therefore OK to assume that it can be treated as
a Float by the caller. Ref:
http://ruby-doc.org/core-2.1.0/Math.html#method-c-sqrt
----------------------------------------
Bug #9170: Math.sqrt returns different types when mathn is included;
breaks various gems - this bug can be reproduced in Ruby 1.8 as well
https://bugs.ruby-lang.org/issues/9170#change-44105

Author: kranzky (Jason Hutchens)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: next minor
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin12.2.1]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


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 `<main>': 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 `<main>': 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
054b5f6b8afdd5f6190bad08e46cd782?d=identicon&s=25 Zachary Scott (Guest)
on 2014-01-09 23:33
(Received via mailing list)
I will check the docs and see if I can fix it.
0e610136db92027148906c92d57fdb36?d=identicon&s=25 marcandre (Marc-Andre Lafortune) (Guest)
on 2014-01-09 23:49
(Received via mailing list)
Issue #9170 has been updated by marcandre (Marc-Andre Lafortune).


Isn't this more an issue with the documentation of `mathn` not
specifying all the methods it changes and how?

It might be wise to add a warning that it is suggested not to require it
from a library because it might induce unexpected changes in any code
using it?

It would be a prime candidate for refinements...


----------------------------------------
Bug #9170: Math.sqrt returns different types when mathn is included;
breaks various gems - this bug can be reproduced in Ruby 1.8 as well
https://bugs.ruby-lang.org/issues/9170#change-44203

Author: kranzky (Jason Hutchens)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: next minor
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin12.2.1]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


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 `<main>': 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 `<main>': 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
This topic is locked and can not be replied to.