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. So you think you know what double.Parse does?

So you think you know what double.Parse does?

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
12 Posts 7 Posters 3 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.
  • L Lost User

    I thought I knew, but I was wrong.

    double x0 = 1 / 0.0;
    double x1 = 1 / -0.0;
    double x2 = 1 / double.Parse("0.0");
    double x3 = 1 / double.Parse("-0.0");

    Obviously these are all going to be some sort of infinity (yes you can divide by zero[^], and the result is infinity). One might reasonably expect both x1 and x3 to be Negative Infinity .. however, only x1 is Negative Infinity. x3 is Positive Infinity, which is about as wrong as any answer could possibly be. This implies that double.Parse blatantly ignores the sign of zero.

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

    harold aptroot wrote:

    the sign of zero

    Sounds like something Sherlock Holmes (or his smarter brother) would investigate.

    1 Reply Last reply
    0
    • L Lost User

      I thought I knew, but I was wrong.

      double x0 = 1 / 0.0;
      double x1 = 1 / -0.0;
      double x2 = 1 / double.Parse("0.0");
      double x3 = 1 / double.Parse("-0.0");

      Obviously these are all going to be some sort of infinity (yes you can divide by zero[^], and the result is infinity). One might reasonably expect both x1 and x3 to be Negative Infinity .. however, only x1 is Negative Infinity. x3 is Positive Infinity, which is about as wrong as any answer could possibly be. This implies that double.Parse blatantly ignores the sign of zero.

      D Offline
      D Offline
      Dennis_E
      wrote on last edited by
      #3

      That's your opinion. Others (including me) would find it logical that zero cannot be negative. Some languages allow 2 different zeroes. I find it particularly annoying when x is negative when I expect it to be zero.

      L R 2 Replies Last reply
      0
      • D Dennis_E

        That's your opinion. Others (including me) would find it logical that zero cannot be negative. Some languages allow 2 different zeroes. I find it particularly annoying when x is negative when I expect it to be zero.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Well the problem is not so much that zero can't be negative, because zero can be negative in C#, if it has a floating point type. The problem I have with this is that it's inconsistent and weird that writing a certain number in the source and parsing it from a string give different results. Not even just some least-significant bit, but the sign. That's a pretty big deal.

        D 1 Reply Last reply
        0
        • L Lost User

          I thought I knew, but I was wrong.

          double x0 = 1 / 0.0;
          double x1 = 1 / -0.0;
          double x2 = 1 / double.Parse("0.0");
          double x3 = 1 / double.Parse("-0.0");

          Obviously these are all going to be some sort of infinity (yes you can divide by zero[^], and the result is infinity). One might reasonably expect both x1 and x3 to be Negative Infinity .. however, only x1 is Negative Infinity. x3 is Positive Infinity, which is about as wrong as any answer could possibly be. This implies that double.Parse blatantly ignores the sign of zero.

          A Offline
          A Offline
          AlphaDeltaTheta
          wrote on last edited by
          #5

          Ah, C#, negative zeores in floating points but I can't say why the parser ignores it. Negative zeores exist in computer only? Ever heard of the term negative zeroes?

          Beauty cannot be defined by abscissas and ordinates; neither are circles and ellipses created by their geometrical formulas. Carl von Clausewitz Source

          1 Reply Last reply
          0
          • L Lost User

            I thought I knew, but I was wrong.

            double x0 = 1 / 0.0;
            double x1 = 1 / -0.0;
            double x2 = 1 / double.Parse("0.0");
            double x3 = 1 / double.Parse("-0.0");

            Obviously these are all going to be some sort of infinity (yes you can divide by zero[^], and the result is infinity). One might reasonably expect both x1 and x3 to be Negative Infinity .. however, only x1 is Negative Infinity. x3 is Positive Infinity, which is about as wrong as any answer could possibly be. This implies that double.Parse blatantly ignores the sign of zero.

            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #6

            Did you try this:

            double y0 = -0.0;
            double y1 = 1 / y0;

            Does it give +nfty or -nfty? I ask because your x1 looks like this to me:

            double x1 = -1 * 1 / 0.0;

            Greetings - Jacek

            L 1 Reply Last reply
            0
            • L Lutoslaw

              Did you try this:

              double y0 = -0.0;
              double y1 = 1 / y0;

              Does it give +nfty or -nfty? I ask because your x1 looks like this to me:

              double x1 = -1 * 1 / 0.0;

              Greetings - Jacek

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              That should give Negative Infinity, and it does.

              L 1 Reply Last reply
              0
              • L Lost User

                That should give Negative Infinity, and it does.

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #8

                Horror accepted.

                Greetings - Jacek

                1 Reply Last reply
                0
                • L Lost User

                  Well the problem is not so much that zero can't be negative, because zero can be negative in C#, if it has a floating point type. The problem I have with this is that it's inconsistent and weird that writing a certain number in the source and parsing it from a string give different results. Not even just some least-significant bit, but the sign. That's a pretty big deal.

                  D Offline
                  D Offline
                  Dennis_E
                  wrote on last edited by
                  #9

                  I understand what you mean now, but i tried it on ideone.com and x3 is -Infinity :confused:

                  L 1 Reply Last reply
                  0
                  • D Dennis_E

                    I understand what you mean now, but i tried it on ideone.com and x3 is -Infinity :confused:

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Hm yes, me too.. Well, they do use Mono instead of the MS version, that could be it

                    1 Reply Last reply
                    0
                    • D Dennis_E

                      That's your opinion. Others (including me) would find it logical that zero cannot be negative. Some languages allow 2 different zeroes. I find it particularly annoying when x is negative when I expect it to be zero.

                      R Offline
                      R Offline
                      Rob Grainger
                      wrote on last edited by
                      #11

                      Others may find it logical that zero cannot be positive. Seriously though, floating-point numbers are defined by an international standard (IEEE 754) that allows for +0 and -0. They have well defined meanings. Wiki[^] The problem here is that while the language respects these, double.Parse fails to.

                      "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                      1 Reply Last reply
                      0
                      • L Lost User

                        I thought I knew, but I was wrong.

                        double x0 = 1 / 0.0;
                        double x1 = 1 / -0.0;
                        double x2 = 1 / double.Parse("0.0");
                        double x3 = 1 / double.Parse("-0.0");

                        Obviously these are all going to be some sort of infinity (yes you can divide by zero[^], and the result is infinity). One might reasonably expect both x1 and x3 to be Negative Infinity .. however, only x1 is Negative Infinity. x3 is Positive Infinity, which is about as wrong as any answer could possibly be. This implies that double.Parse blatantly ignores the sign of zero.

                        E Offline
                        E Offline
                        ExcellentOrg
                        wrote on last edited by
                        #12

                        Feds are gonna investigate and file a report on whoever has done this heinous crime ...

                        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