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. The Lounge
  3. What the NaN?

What the NaN?

Scheduled Pinned Locked Moved The Lounge
csharpcomquestion
66 Posts 18 Posters 12 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.
  • F Florian Rappl

    This has nothing to do with C#. I refer you to IEEE 754.

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #11

    I'm glad that a lot of people have thought about how NaN should have contradictory results in different usages :doh: <Edit> So it seems the contradictory result are in IEEE 754, but the weird behavior in Min and Max is Microsoft[^] :laugh: </Edit>

    Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

    Regards, Sander

    F 1 Reply Last reply
    0
    • L Lost User

      "Not a number" means exactly that. So you cannot equate it to any numeric value.

      Sander RosselS Offline
      Sander RosselS Offline
      Sander Rossel
      wrote on last edited by
      #12

      Richard MacCutchan wrote:

      So you cannot equate it to any numeric value

      Yes you can, and that's the point. I expected either an exception (compile or run time) or at least a predictable weird behavior (well, it's predictable once you know all the edge cases I guess). Now whether you should is a different discussion... :) I found this because I had some weird JavaScript bug by the way, casting some object to a number results in NaN and I was wondering how C# handled the case the followed. NaN is not smaller than 1 (when comparing and when using the Min function), but when both are thrown into the Max function NaN is smaller than 1. Got it :~ Luckily, I've never had to work with NaN in C# because why would there even be a NaN anyway...

      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

      Regards, Sander

      M G L 3 Replies Last reply
      0
      • B BillWoodruff

        Interesting; imho that implies the treatment of double.NaN in the 'Min function is different than in the 'Max function. I am relieved to know that in .NET C# double.PositiveInfinity == double.NegativeInfinity => 'false, since, if that returned 'true, I would assume my system is now a conscious entity ... in which case it would certainly be planning to kill me in league with the various cpu's in my home appliances. Be careful, Sander, it was the contemplation of the mathematics of the ordinality of infinities (the aleph) that drove both Cantor, and Godel, insane. cheers, Bill

        «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #13

        BillWoodruff wrote:

        that implies the treatment of double.NaN in the 'Min function is different than in the 'Max function

        Actually, it doesn't. Both Min and Max treat NaN as the smallest value. However, when comparing NaN to any other value it's both bigger and smaller.

        BillWoodruff wrote:

        it was the contemplation of the mathematics of the ordinality of infinities (the aleph) that drove both Cantor, and Godel, insane

        It was mathematics that drove me insane in high school . And then again at University. And then again at another University... :sigh:

        Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

        Regards, Sander

        1 Reply Last reply
        0
        • L Lost User

          In WPF you can set the width of certain visual components to Double.NaN. This is interpreted by .NET to mean "width is set to auto". At least that is how I remember the use for Double.NaN

          Get me coffee and no one gets hurt!

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #14

          I guess "Auto" would be one example of NaN :laugh:

          Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

          Regards, Sander

          1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            Richard MacCutchan wrote:

            So you cannot equate it to any numeric value

            Yes you can, and that's the point. I expected either an exception (compile or run time) or at least a predictable weird behavior (well, it's predictable once you know all the edge cases I guess). Now whether you should is a different discussion... :) I found this because I had some weird JavaScript bug by the way, casting some object to a number results in NaN and I was wondering how C# handled the case the followed. NaN is not smaller than 1 (when comparing and when using the Min function), but when both are thrown into the Max function NaN is smaller than 1. Got it :~ Luckily, I've never had to work with NaN in C# because why would there even be a NaN anyway...

            Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

            Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

            Regards, Sander

            M Offline
            M Offline
            Mladen Jankovic
            wrote on last edited by
            #15

            Sander Rossel wrote:

            why would there even be a NaN anyway...

            1. 0/0
            2. ∞/∞
            3. ∞+(-∞)
            4. √-1
            5. and so on...

            Just because you are not familiar with the subject, does not make others wrong.

            GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

            Sander RosselS 1 Reply Last reply
            0
            • B BillWoodruff

              Interesting; imho that implies the treatment of double.NaN in the 'Min function is different than in the 'Max function. I am relieved to know that in .NET C# double.PositiveInfinity == double.NegativeInfinity => 'false, since, if that returned 'true, I would assume my system is now a conscious entity ... in which case it would certainly be planning to kill me in league with the various cpu's in my home appliances. Be careful, Sander, it was the contemplation of the mathematics of the ordinality of infinities (the aleph) that drove both Cantor, and Godel, insane. cheers, Bill

              «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

              M Offline
              M Offline
              Mladen Jankovic
              wrote on last edited by
              #16

              BillWoodruff wrote:

              that implies the treatment of double.NaN in the 'Min function is different than in the 'Max function.

              I doubt it there is different treatment, it's just different implementation of if condition. For instance, Min method could have this condition:

              if (currentMin > values[i])
              currentMin = values[i];

              and Max this one:

              if (!(currentMax > values[i]))
              currentMax = values[i];

              If Max was implemented in this way:

              if (currentMax < values[i])
              currentMax = values[i];

              it would return NaN also. edit: I'll be damned. NaN indeed has different treatment in Min method. Comment from the source code[^]:

              // Normally NaN < anything is false, as is anything < NaN
              // However, this leads to some irksome outcomes in Min and Max.
              // If we use those semantics then Min(NaN, 5.0) is NaN, but
              // Min(5.0, NaN) is 5.0! To fix this, we impose a total
              // ordering where NaN is smaller than every value, including
              // negative infinity.
              if (x < value || System.Single.IsNaN(x)) value = x;

              GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

              B 1 Reply Last reply
              0
              • M Mladen Jankovic

                Sander Rossel wrote:

                why would there even be a NaN anyway...

                1. 0/0
                2. ∞/∞
                3. ∞+(-∞)
                4. √-1
                5. and so on...

                Just because you are not familiar with the subject, does not make others wrong.

                GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                Sander RosselS Offline
                Sander RosselS Offline
                Sander Rossel
                wrote on last edited by
                #17

                0/0 should throw a DivideByZeroException (which it does for integers). And apparently 1/0 equals infinity. Now what is it? NaN, infinity or just plain not possible? Doesn't it sound weird (and, indeed, very wrong) that a NUMERIC type has a value "NOT A NUMBER"!? Anyway, when I said "why would there even be a NaN anyway" I was referring to NaN in actual real life business cases that make sense and have practical use :)

                Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                Regards, Sander

                M L M 3 Replies Last reply
                0
                • Sander RosselS Sander Rossel

                  var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Max(); // Infinity
                  var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Min(); // NaN
                  var isNaNSmaller = double.NaN < 1; // false

                  So NaN is not the biggest value, it's still bigger than one, but it's also the smallest value. I hate to sound infinitely negative, but that's messed up :wtf:

                  Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                  Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                  Regards, Sander

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #18

                  Comparing a floating point value with NaN always returns false (an "unordered result"; even when both operands are NaN). This can be seen at your 3rd test. With the Min() code, all values are checked if they are smaller than the others. Because comparing with NaN is always false, no other element is detected as smaller and the check retunrs NaN. With the Max() code the values are compared to be greater which always fails for NaN. See also NaN - Wikipedia, the free encyclopedia[^].

                  Sander RosselS 1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    0/0 should throw a DivideByZeroException (which it does for integers). And apparently 1/0 equals infinity. Now what is it? NaN, infinity or just plain not possible? Doesn't it sound weird (and, indeed, very wrong) that a NUMERIC type has a value "NOT A NUMBER"!? Anyway, when I said "why would there even be a NaN anyway" I was referring to NaN in actual real life business cases that make sense and have practical use :)

                    Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                    Regards, Sander

                    M Offline
                    M Offline
                    Mladen Jankovic
                    wrote on last edited by
                    #19

                    I would recommend you to this article before you continue your rant about IEEE754.

                    GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                    Sander RosselS 1 Reply Last reply
                    0
                    • J Jochen Arndt

                      Comparing a floating point value with NaN always returns false (an "unordered result"; even when both operands are NaN). This can be seen at your 3rd test. With the Min() code, all values are checked if they are smaller than the others. Because comparing with NaN is always false, no other element is detected as smaller and the check retunrs NaN. With the Max() code the values are compared to be greater which always fails for NaN. See also NaN - Wikipedia, the free encyclopedia[^].

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #20

                      So it's completely dependent on the internals of Min and Max. Had Max checked if any values were smaller than the current then NaN would always be max and had Min checked if any values were greater than the current then NaN would never be min. So change your implementation and NaN will behave differently, feels very random.

                      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                      Regards, Sander

                      B J 2 Replies Last reply
                      0
                      • Sander RosselS Sander Rossel

                        var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Max(); // Infinity
                        var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Min(); // NaN
                        var isNaNSmaller = double.NaN < 1; // false

                        So NaN is not the biggest value, it's still bigger than one, but it's also the smallest value. I hate to sound infinitely negative, but that's messed up :wtf:

                        Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                        Regards, Sander

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

                        I can sort of see how that might happen, but I would prefer Min and Max ignored the NaN unless they had no choice (if it's the only thing in the list, there's no better choice).

                        1 Reply Last reply
                        0
                        • Sander RosselS Sander Rossel

                          var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Max(); // Infinity
                          var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Min(); // NaN
                          var isNaNSmaller = double.NaN < 1; // false

                          So NaN is not the biggest value, it's still bigger than one, but it's also the smallest value. I hate to sound infinitely negative, but that's messed up :wtf:

                          Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                          Regards, Sander

                          M Offline
                          M Offline
                          Mladen Jankovic
                          wrote on last edited by
                          #22

                          Here's the reasoning from the source code[^]:

                                          // Normally NaN < anything is false, as is anything < NaN
                                          // However, this leads to some irksome outcomes in Min and Max.
                                          // If we use those semantics then Min(NaN, 5.0) is NaN, but
                                          // Min(5.0, NaN) is 5.0!  To fix this, we impose a total
                                          // ordering where NaN is smaller than every value, including
                                          // negative infinity.
                          

                          GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                          Sander RosselS 2 Replies Last reply
                          0
                          • M Mladen Jankovic

                            I would recommend you to this article before you continue your rant about IEEE754.

                            GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                            Sander RosselS Offline
                            Sander RosselS Offline
                            Sander Rossel
                            wrote on last edited by
                            #23

                            Tried to read it once, but to me it makes as much sense as the whole NaN implementation: none whatsoever (and I admit my limited math skills are to blame). However, I tried reading the NaN part and what they basically say is that in some edge cases you don't want computations to stop (throw exceptions) when some bogus values are inserted (e.g. divide by 0). The workaround without NaN would be to catch exceptions and simply try again. Unfortunately, every language handles exceptions differently so they standardized on NaN. Awesome, they destroyed our numeric system to support some edge cases (correct me if I'm wrong) :D Luckily .NET offers some sensible numeric types with int, long and decimal :D Unfortunately, I'm currently working in JavaScript, with floating point arithmetic, where NaN is quite common, and 0.1 + 0.2 equals 0.30000000000000004 (yes, I know that's IEEE754, but that doesn't make it right) :sigh:

                            Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                            Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                            Regards, Sander

                            M 1 Reply Last reply
                            0
                            • M Mladen Jankovic

                              Here's the reasoning from the source code[^]:

                                              // Normally NaN < anything is false, as is anything < NaN
                                              // However, this leads to some irksome outcomes in Min and Max.
                                              // If we use those semantics then Min(NaN, 5.0) is NaN, but
                                              // Min(5.0, NaN) is 5.0!  To fix this, we impose a total
                                              // ordering where NaN is smaller than every value, including
                                              // negative infinity.
                              

                              GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                              Sander RosselS Offline
                              Sander RosselS Offline
                              Sander Rossel
                              wrote on last edited by
                              #24

                              Thanks, good find! At least that clears that up! :D

                              Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                              Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                              Regards, Sander

                              1 Reply Last reply
                              0
                              • M Mladen Jankovic

                                Here's the reasoning from the source code[^]:

                                                // Normally NaN < anything is false, as is anything < NaN
                                                // However, this leads to some irksome outcomes in Min and Max.
                                                // If we use those semantics then Min(NaN, 5.0) is NaN, but
                                                // Min(5.0, NaN) is 5.0!  To fix this, we impose a total
                                                // ordering where NaN is smaller than every value, including
                                                // negative infinity.
                                

                                GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                                Sander RosselS Offline
                                Sander RosselS Offline
                                Sander Rossel
                                wrote on last edited by
                                #25

                                By the way, the email for this message had a YouTube link attached to it. I just found it in your sig :laugh: And I would once again argue that the existence of NaN is just plain wrong. Well, at least you're right about the Lounge ;p

                                Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                Regards, Sander

                                1 Reply Last reply
                                0
                                • M Mladen Jankovic

                                  BillWoodruff wrote:

                                  that implies the treatment of double.NaN in the 'Min function is different than in the 'Max function.

                                  I doubt it there is different treatment, it's just different implementation of if condition. For instance, Min method could have this condition:

                                  if (currentMin > values[i])
                                  currentMin = values[i];

                                  and Max this one:

                                  if (!(currentMax > values[i]))
                                  currentMax = values[i];

                                  If Max was implemented in this way:

                                  if (currentMax < values[i])
                                  currentMax = values[i];

                                  it would return NaN also. edit: I'll be damned. NaN indeed has different treatment in Min method. Comment from the source code[^]:

                                  // Normally NaN < anything is false, as is anything < NaN
                                  // However, this leads to some irksome outcomes in Min and Max.
                                  // If we use those semantics then Min(NaN, 5.0) is NaN, but
                                  // Min(5.0, NaN) is 5.0! To fix this, we impose a total
                                  // ordering where NaN is smaller than every value, including
                                  // negative infinity.
                                  if (x < value || System.Single.IsNaN(x)) value = x;

                                  GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                                  B Offline
                                  B Offline
                                  BillWoodruff
                                  wrote on last edited by
                                  #26

                                  Mladen Janković wrote:

                                  I'll be damned. NaN indeed has different treatment

                                  Oh my, are you saying that my post actually meant something ? If that's the case, I hereby offer you enough plenary indulgence to be un-damned ... once you have voted.

                                  «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                                  1 Reply Last reply
                                  0
                                  • Sander RosselS Sander Rossel

                                    Richard MacCutchan wrote:

                                    So you cannot equate it to any numeric value

                                    Yes you can, and that's the point. I expected either an exception (compile or run time) or at least a predictable weird behavior (well, it's predictable once you know all the edge cases I guess). Now whether you should is a different discussion... :) I found this because I had some weird JavaScript bug by the way, casting some object to a number results in NaN and I was wondering how C# handled the case the followed. NaN is not smaller than 1 (when comparing and when using the Min function), but when both are thrown into the Max function NaN is smaller than 1. Got it :~ Luckily, I've never had to work with NaN in C# because why would there even be a NaN anyway...

                                    Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                    Regards, Sander

                                    G Offline
                                    G Offline
                                    Gary R Wheeler
                                    wrote on last edited by
                                    #27

                                    To my mind, the only reasonable operations with NaN are assigning an lvalue to NaN and comparing a value to NaN for equality and inequality. Any of the arithmetic operators ought to yield NaN (or throw an exception). Come to think of it, the IEEE must have a formal document that describes all of this behavior. Pardon me while I JFGI... Ahh; typical. The formal document is only available to IEEE members who know the official circle jerk handshake. NaN - Wikipedia, the free encyclopedia[^] is a quick article that describes background and informally NaN's usage. This article: IEEE Standard 754 Floating-Point[^] also has some discussion on NaN behavior.

                                    Software Zen: delete this;

                                    1 Reply Last reply
                                    0
                                    • Sander RosselS Sander Rossel

                                      var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Max(); // Infinity
                                      var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Min(); // NaN
                                      var isNaNSmaller = double.NaN < 1; // false

                                      So NaN is not the biggest value, it's still bigger than one, but it's also the smallest value. I hate to sound infinitely negative, but that's messed up :wtf:

                                      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                      Regards, Sander

                                      S Offline
                                      S Offline
                                      Super Lloyd
                                      wrote on last edited by
                                      #28

                                      Did you know that both 1 < double.NaN and 1 > double.NaN are false?! ;P

                                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                      B Sander RosselS 2 Replies Last reply
                                      0
                                      • Sander RosselS Sander Rossel

                                        So it's completely dependent on the internals of Min and Max. Had Max checked if any values were smaller than the current then NaN would always be max and had Min checked if any values were greater than the current then NaN would never be min. So change your implementation and NaN will behave differently, feels very random.

                                        Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                        Regards, Sander

                                        B Offline
                                        B Offline
                                        BillWoodruff
                                        wrote on last edited by
                                        #29

                                        Sander Rossel wrote:

                                        NaN will behave differently, feels very random.

                                        Sander, this is a brilliant idea you've come up with; I'll offer a quick sketch, and look forward to your comprehensive implementation:

                                        namespace CatState
                                        {
                                        public static class CatStateExtensions
                                        {
                                        public static double CatDead(this double[] args)
                                        {
                                        if (args.Contains(double.NaN))
                                        {
                                        Random rnd = new Random((int) DateTime.Now.Ticks);

                                                    return (rnd.Next(0,2) == 0)
                                                        ? double.PositiveInfinity
                                                        : double.NegativeInfinity;
                                                }
                                        
                                                return args.Min();
                                            }
                                        
                                            public static double CatAlive(this double\[\] args)
                                            {
                                                if (args.Contains(double.NaN))
                                                {
                                                    Random rnd = new Random((int) DateTime.Now.Ticks);
                                        
                                                    return (rnd.Next(0,2) == 1)
                                                        ? double.PositiveInfinity
                                                        : double.NegativeInfinity;
                                                }
                                        
                                                return args.Max();
                                            }
                                        }
                                        

                                        }

                                        The half-dead cats fighting over half-dead fish-heads have been keeping me up nights, lately.

                                        «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                                        M 1 Reply Last reply
                                        0
                                        • Sander RosselS Sander Rossel

                                          Tried to read it once, but to me it makes as much sense as the whole NaN implementation: none whatsoever (and I admit my limited math skills are to blame). However, I tried reading the NaN part and what they basically say is that in some edge cases you don't want computations to stop (throw exceptions) when some bogus values are inserted (e.g. divide by 0). The workaround without NaN would be to catch exceptions and simply try again. Unfortunately, every language handles exceptions differently so they standardized on NaN. Awesome, they destroyed our numeric system to support some edge cases (correct me if I'm wrong) :D Luckily .NET offers some sensible numeric types with int, long and decimal :D Unfortunately, I'm currently working in JavaScript, with floating point arithmetic, where NaN is quite common, and 0.1 + 0.2 equals 0.30000000000000004 (yes, I know that's IEEE754, but that doesn't make it right) :sigh:

                                          Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                          Regards, Sander

                                          M Offline
                                          M Offline
                                          Mladen Jankovic
                                          wrote on last edited by
                                          #30

                                          Sander Rossel wrote:

                                          they destroyed our numeric system to support some edge cases

                                          Any representation of real numbers in computer memory will destroy 'numeric system' since you're trying to represent infinite set with finite amount of memory.

                                          Sander Rossel wrote:

                                          Luckily .NET offers some sensible numeric types with int, long and decimal :-D

                                          decimal comes with performance costs as it has no support in hardware so it might not be suitable as a replacement in fields that traditionally use floating point arithmetic. It is meant to be used for financial stuff since IEEE754 is not suitable for that purpose, so not everyone have to invent their own way of doing math.

                                          Sander Rossel wrote:

                                          0.1 + 0.2 equals 0.30000000000000004

                                          That's the problem with converting real numbers between different bases.

                                          GeoGame for Windows Phone | The Lounge Explained In 5 Minutes

                                          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