Math.sqrt(-1) doesn't raise ArgumentError

irb
irb(main):001:0> Math.sqrt(-1)
=> NaN
irb(main):002:0>

NaN == Not a Number

Why has sqrt behavier changed, when it hasn’t for divisionbyzero?

irb(main):002:0> 3 / 0
ZeroDivisionError: divided by 0
from (irb):2:in `/’
from (irb):2
irb(main):003:0>

···


Simon Strandgaard

Hi,

···

In message “Math.sqrt(-1) doesn’t raise ArgumentError” on 04/02/16, Simon Strandgaard neoneye@adslhome.dk writes:

irb
irb(main):001:0> Math.sqrt(-1)
=> NaN
irb(main):002:0>

NaN == Not a Number

Why has sqrt behavier changed, when it hasn’t for divisionbyzero?

Because sqrt is a float operation.

p 3.0 / 0
=> Infinity

						matz.

Hi!

  • Simon Strandgaard:

irb
irb(main):001:0> Math.sqrt(-1)
=> NaN
irb(main):002:0>

NaN == Not a Number

Why has sqrt behavier changed, when it hasn’t for divisionbyzero?

Math.sqrt(-1) is Math.sqrt(-1.to_f) is Math.sqr(-1.0)

irb(main):002:0> 3 / 0
ZeroDivisionError: divided by 0
from (irb):2:in `/’
from (irb):2
irb(main):003:0>

Correct analogy were

ruby -e ‘puts 3.0 / 0.0’
Infinity

No exception either.

Josef ‘Jupp’ SCHUGT

···


http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Germany 2004: To boldly spy where no GESTAPO / STASI has spied before

I want to do an assert_equal in rubicon, like this

Version.less_than("1.8.1") do
	assert_raise(ArgumentError) { Math.sqrt(-1) }
end
Version.greater_or_equal("1.8.1") do
	assert_equal(NaN, Math.sqrt(-1))
end 

However How do I make an Float instance of NaN?

irb(main):001:0> Math.sqrt(-1)
=> NaN
irb(main):002:0> v = NaN
NameError: uninitialized constant NaN
from (irb):2
irb(main):003:0> v = Math::NaN
NameError: uninitialized constant Math::NaN
from (irb):3
irb(main):004:0> v = Math.NaN
NoMethodError: undefined method NaN' for Math:Module from (irb):4 irb(main):005:0> v = Math.NaN NoMethodError: undefined method NaN’ for Math:Module
from (irb):5
irb(main):006:0> v = Math.sqrt(-1)
=> NaN
irb(main):007:0> v.class
=> Float
irb(main):008:0> x = Float::NaN
NameError: uninitialized constant Float::NaN
from (irb):8
irb(main):009:0> x = Float.NaN
NoMethodError: undefined method `NaN’ for Float:Class
from (irb):9
irb(main):010:0> x = Float.new(NaN)
NameError: uninitialized constant NaN
from (irb):10
irb(main):011:0>

···

On Mon, 16 Feb 2004 19:10:37 +0900, Yukihiro Matsumoto wrote:

Hi,

In message “Math.sqrt(-1) doesn’t raise ArgumentError” > on 04/02/16, Simon Strandgaard neoneye@adslhome.dk writes:

irb
irb(main):001:0> Math.sqrt(-1)
=> NaN
irb(main):002:0>

NaN == Not a Number

Why has sqrt behavier changed, when it hasn’t for divisionbyzero?

Because sqrt is a float operation.

p 3.0 / 0
=> Infinity

  					matz.


Simon Strandgaard

Wrong thinking… don’t create an instance.

Instead do

Version.less_than("1.8.1") do
	assert_raise(ArgumentError) { Math.sqrt(-1) }
end
Version.greater_or_equal("1.8.1") do
	assert_equal(true, Math.sqrt(-1).nan?)
end  

Much better.

···

On Mon, 16 Feb 2004 11:56:33 +0100, Simon Strandgaard wrote:

However How do I make an Float instance of NaN?


Simon Strandgaard