Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. trying to figure out if number is negative

trying to figure out if number is negative

Scheduled Pinned Locked Moved The Weird and The Wonderful
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Omar Gameel Salem
    wrote on last edited by
    #1

    Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

    J J J P K 6 Replies Last reply
    0
    • O Omar Gameel Salem

      Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Shenanigans! Nobody is that dumb. Please... no.

      1 Reply Last reply
      0
      • O Omar Gameel Salem

        Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

        J Offline
        J Offline
        Jarek Kruza
        wrote on last edited by
        #3

        It's almost like this one I saw a long time ago:

        if (abs(num)!=num) ...

        (this is a real one...)

        O A 2 Replies Last reply
        0
        • J Jarek Kruza

          It's almost like this one I saw a long time ago:

          if (abs(num)!=num) ...

          (this is a real one...)

          O Offline
          O Offline
          Omar Gameel Salem
          wrote on last edited by
          #4

          jkruza wrote:

          (this is a real one...)

          lol, i didnt make this up :)

          J 1 Reply Last reply
          0
          • O Omar Gameel Salem

            jkruza wrote:

            (this is a real one...)

            lol, i didnt make this up :)

            J Offline
            J Offline
            Jarek Kruza
            wrote on last edited by
            #5

            Yeah, I believe you. But it's so stupid, it's really hard to believe it's real. ;)

            O 1 Reply Last reply
            0
            • J Jarek Kruza

              Yeah, I believe you. But it's so stupid, it's really hard to believe it's real. ;)

              O Offline
              O Offline
              Omar Gameel Salem
              wrote on last edited by
              #6

              true that :-D

              1 Reply Last reply
              0
              • O Omar Gameel Salem

                Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

                J Offline
                J Offline
                Jeroen De Dauw
                wrote on last edited by
                #7

                Ah! I've been looking on how to do this for months!!!

                Jeroen De Dauw --- Forums ; Blog ; Wiki --- 70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!

                1 Reply Last reply
                0
                • O Omar Gameel Salem

                  Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  In the original BASIC, the absolute value would be used, so no error.

                  1 Reply Last reply
                  0
                  • O Omar Gameel Salem

                    Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

                    K Offline
                    K Offline
                    Keith Barrow
                    wrote on last edited by
                    #9

                    I will never complain about our code base again. OK - that was a lie, but I'm tempted.

                    CCC solved so far: 2 (including a Hard One!)

                    1 Reply Last reply
                    0
                    • O Omar Gameel Salem

                      Try Dim d As Double = Math.Sqrt(number) Catch ex As Exception 'do stuff End Try

                      D Offline
                      D Offline
                      David Skelly
                      wrote on last edited by
                      #10

                      I don't think this code is doing what you think: it is not testing whether number is negative. No exception will be thrown because Math.Sqrt(n) returns NaN if n is negative. This is a valid Double value, and the code will drop out of the Try-Catch block without executing the line you have labelled 'do stuff. The only way this will get an exception is if the variable number is a String such as "XYZ". Math.Sqrt will cast a string such as "16" to a Double then take the square root of that, so as long as the String represents a numeric value it will work. Even "-16" will work because this will return NaN. But something such as "XYZ" will fail with an exception because this cannot be converted to a number and you will get an error. Having said that, there are better ways to check whether a String is numeric.

                      1 Reply Last reply
                      0
                      • J Jarek Kruza

                        It's almost like this one I saw a long time ago:

                        if (abs(num)!=num) ...

                        (this is a real one...)

                        A Offline
                        A Offline
                        AspDotNetDev
                        wrote on last edited by
                        #11

                        jkruza wrote:

                        (this is a real one

                        As opposed to an imaginary 1?

                        Visual Studio is an excellent GUIIDE.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups