Project

General

Profile

Actions

Bug #1708

closed

require 'complex' Causes Unexpected Behaviour

Added by runpaint (Run Paint Run Run) over 15 years ago. Updated over 13 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 1.9.2dev (2009-06-27 trunk 23871) [i686-linux]
Backport:
[ruby-core:24099]

Description

=begin
1.9 has Complex in core, yet on 1.8 you had to explicitly require it with "require 'complex'". However, 1.9 also has a 'complex.rb' in its lib directory which in turn requires 'cmath.rb'. 'cmath.rb' redefines quite a few Math methods. This is particularly confusing because you would expect "require 'complex'" to be a no-op on 1.9, whereas it actually causes Math methods to fail in mysterious ways.

For example, where Math used to raise TypeErrors it now raises NoMethodErrors. This happens for all of the overridden methods.

 >> Math.atan(nil)
 TypeError: can't convert nil into Float
from (irb):1:in `atan'
from (irb):1
from /usr/local/bin/irb:12:in `<main>'
 >> require 'complex'
 => true
 >> Math.atan(nil)
 NoMethodError: undefined method `real?' for nil:NilClass
from /usr/local/lib/ruby/1.9.1/cmath.rb:145:in `atan'
from (irb):3
from /usr/local/bin/irb:12:in `<main>'

As Complex is in core Math methods should work with complex numbers by default. This half-in-, half-out-, approach is confusing. If the current behavior is intentional, it needs to be clarified in the Math and Complex documentation.
=end


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #2756: Issues with Math and Complex behavior on 1.9Rejectedmrkn (Kenta Murata)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0