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

S

Simon Strandgaard

server> 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>
 
Y

Yukihiro Matsumoto

Hi,

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

|server> 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.
 
S

Simon Strandgaard

Hi,

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

|server> 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.

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>
 
S

Simon Strandgaard

However How do I make an Float instance of NaN?

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.
 
J

Josef 'Jupp' SCHUGT

Hi!

* Simon Strandgaard:
server> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top