Professional Web Applications Themes

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

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> -- Simon Strandgaard...

  1. #1

    Default 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?


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

    --
    Simon Strandgaard
    Simon Guest

  2. #2

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

    Hi,

    In message "Math.sqrt(-1) doesn't raise ArgumentError"
    on 04/02/16, Simon Strandgaard <dk> writes:

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


    Yukihiro Guest

  3. #3

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

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

    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>


    --
    Simon Strandgaard
    Simon Guest

  4. #4

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

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

    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.

    --
    Simon Strandgaard
    Simon Guest

  5. #5

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

    Hi!

    * Simon Strandgaard: 

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

    Correct ogy 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


    Josef Guest

Similar Threads

  1. Replies: 0
    Last Post: September 8th, 10:15 PM
  2. Replies: 0
    Last Post: September 8th, 10:15 PM
  3. Replies: 0
    Last Post: September 8th, 08:59 PM
  4. Replies: 0
    Last Post: September 8th, 08:49 PM
  5. Math::Project3D doesn't work for me
    By Mike Flannigan in forum PERL Miscellaneous
    Replies: 0
    Last Post: August 16th, 07:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139