Projects

Ticket #584 (closed defect: fixed)

Opened 2 years ago

Last modified 19 months ago

BigMath.sqrt always returns NaN

Reported by: hghoehne@… Owned by: lsansonetti@…
Priority: major Milestone: MacRuby 0.7
Component: MacRuby Keywords:
Cc:

Description (last modified by martinlagardette@…) (diff)

require "bigdecimal"
require "bigdecimal/math"
four = BigDecimal("4")
puts BigMath.sqrt(four, 10) #=> NaN

Change History

in reply to: ↑ description   Changed 2 years ago by maxime.curioni@…

The problem seems to be more of a general difference between ruby 1.8 and ruby 1.9, than a MacRuby-specific problem:

# ruby -v => ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

>> require 'bigdecimal/math'
=> true
>> BigMath.methods.include? :sqrt
=> false

# ruby -v => ruby 1.9.2dev (2010-01-26 trunk 26419) [i386-darwin10.2.0]

>> require 'bigdecimal/math'
=> true
>> BigMath.methods.include? :sqrt
=> false

#macruby -v => MacRuby version 0.6 (ruby 1.9.0) [universal-darwin10.0, i386]

>> require 'bigdecimal/math'
=> true
>> BigMath.methods.include? :sqrt
=> false

Since we are aiming to match Ruby 1.9's behavior, this is definitely something that should be fixed. In the mean time, users can go around the problem by:
- either adding 'include BigMath' after "require 'bigdecimal/math'"
- or using the instance method #sqrt on the object (i.e. "four.sqrt(10)")

follow-up: ↓ 3   Changed 2 years ago by maxime.curioni@…

Sorry about the misprint for ruby 1.9:

# ruby -v => ruby 1.9.2dev (2010-01-26 trunk 26419) [i386-darwin10.2.0]

>> require 'bigdecimal/math'
=> true
>> BigMath.methods.include? :sqrt
=> true

in reply to: ↑ 2   Changed 2 years ago by hghoehne@…

Replying to maxime.curioni@…:

The "correct" way to check for sqrt in 1.87, 1.92 and macruby is

BigDecimal.method_defined? :sqrt

which returns true in ruby 1.87, ruby 1.92 and macruby.

In 1.87 and macruby you can call

result = BigDecimal.sqrt(..,..)

That doesn't work in 1.92. Here you call

result = BigMath.sqrt(..,..)

which also works in 1.87 and macruby. But

BigMath.method_defined? :sqrt

will return false in ruby 1.92, but true in 1.87 and macruby.

include BigMath
result = sqrt(..,..)

is working in all versions. This small changes are definitely confusing and probably a bug in ruby 1.92.

But BigMath.sqrt is defined in macruby: calling sqrt doesn't raise a NoMethodError, but returns NaN for any BigDecimal object.

  Changed 21 months ago by martinlagardette@…

  • description modified (diff)
  • milestone MacRuby 0.5 deleted

I commited an updated version of bigdecimal/math with r4202, but for some reason, get NaN when I include both bigdecimal and bigdecimal/math, but not if I include any of them:

$> macruby -r bigdecimal/math -e 'p BigDecimal.new("2").sqrt(10)'
#<BigDecimal:2000f15a0,'0.1414213562 3730950488 016887185E1',32(32)>
$> macruby -r bigdecimal -e 'p BigDecimal.new("2").sqrt(10)'
#<BigDecimal:2000e0aa0,'0.1414213562 3730950488 016887185E1',32(32)>
$> macruby -r bigdecimal -r bigdecimal/math -e 'p BigDecimal.new("2").sqrt(10)'
#<BigDecimal:2000d0660,'NaN',4(32)>

  Changed 19 months ago by lsansonetti@…

  • status changed from new to closed
  • resolution set to fixed
  • milestone set to MacRuby 0.7

This seems to work for me (as of r4401):

$ /usr/local/bin/macruby -r bigdecimal/math -e 'p BigDecimal.new("2").sqrt(10)'
#<BigDecimal:4004cb500,'0.1414213562 3730950488 016887185E1',32(32)>
$ /usr/local/bin/macruby -r bigdecimal -e 'p BigDecimal.new("2").sqrt(10)'
#<BigDecimal:4004c7480,'0.1414213562 3730950488 016887185E1',32(32)>
$ /usr/local/bin/macruby -r bigdecimal -r bigdecimal/math -e 'p BigDecimal.new("2").sqrt(10)'
#<BigDecimal:4004cb820,'0.1414213562 3730950488 016887185E1',32(32)>

Please re-open if you still see the problem with trunk.

  Changed 19 months ago by martinlagardette@…

This looks fixed indeed :-)

Note: See TracTickets for help on using tickets.