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. General Programming
  3. C#
  4. Strange behavior of comparison with Double.IsPositiveInfinity [modified]

Strange behavior of comparison with Double.IsPositiveInfinity [modified]

Scheduled Pinned Locked Moved C#
helpcsharpdotnetalgorithmstesting
11 Posts 3 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.
  • S Stefan Troschuetz

    Today, I stumbled on the strangest behavior I've seen so far and unfortunatley I'm not able to find an explaination. Here is the problem: I'm currently testing a library with random generators. The generators have a function NextDouble that generates a floating point random number within a specified range. The range is computed by subtracting the specified minValue from the specified maxValue. Afterwards the computed range is passed to double.IsPositiveInfinity, so the method can throw an exception if an overflow occured. Strangely the comparison only works in Debug mode (below code outputs "NextDouble: Infinity"). If I run the same code in Release mode the comparison does not work (below code outputs "NextDouble: Not infinity"). I first thought this might be an issue of code optimization, but even if I turn off the optimization in release mode it doesn't work. To make the whole thing even more strange, the comparison works if the final Console.Out is uncommented. Does the small console application show the same behavior on your computers? Does someone have an explaination for this strange behavior? By the way, I'm running this with .NET framework 2.0.50727.

    static class Program
    {
    [STAThread]
    static void Main()
    {
    NextDouble(Double.MinValue, Double.MaxValue);

    	Console.ReadLine();
    }
    
    public static void NextDouble(double minValue, double maxValue)
    {
    	double range = maxValue - minValue;
    
    	if (double.IsPositiveInfinity(range))
    		Console.Out.WriteLine("NextDouble: Infinity");
    	else
    		Console.Out.WriteLine("NextDouble: Not infinity");
    
    	//Console.Out.WriteLine("NextDouble: " + range);
    }
    

    }

    -- modified at 9:11 Tuesday 31st July, 2007


    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

    www.troschuetz.de

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    Did you read the documentation ? on Double.PositiveInfinity MSDN gives the remark: Use IsPositiveInfinity to determine whether a value evaluates to positive infinity. It is not possible to determine whether a value evaluates to positive infinity by comparing it to another value equal to PositiveInfinity I don't know about different handling of extreme double values under debug/release conditions; as long as you go against the manual, you can't trust the behavior anyway. :)

    Luc Pattyn


    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


    S 1 Reply Last reply
    0
    • L Luc Pattyn

      Did you read the documentation ? on Double.PositiveInfinity MSDN gives the remark: Use IsPositiveInfinity to determine whether a value evaluates to positive infinity. It is not possible to determine whether a value evaluates to positive infinity by comparing it to another value equal to PositiveInfinity I don't know about different handling of extreme double values under debug/release conditions; as long as you go against the manual, you can't trust the behavior anyway. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #3

      I forgot to mentioned that I suspected something like this too and also tested with Double.IsPositiveInfinity. The behavior is the same, so I didn't took a look at the documentation of Double.PositiveInfinity.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      L 1 Reply Last reply
      0
      • S Stefan Troschuetz

        I forgot to mentioned that I suspected something like this too and also tested with Double.IsPositiveInfinity. The behavior is the same, so I didn't took a look at the documentation of Double.PositiveInfinity.


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        Stefan Troschuetz wrote:

        so I didn't took a look at the documentation

        There is no excuse; you should read the documentation before using something. And you should consult it again as soon as you don't agree with the observed behavior of it. :mad: In the end, that is only after after careful reading and analysis, you can start publishing messages with "strange behavior" in their subject...

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        S 1 Reply Last reply
        0
        • L Luc Pattyn

          Stefan Troschuetz wrote:

          so I didn't took a look at the documentation

          There is no excuse; you should read the documentation before using something. And you should consult it again as soon as you don't agree with the observed behavior of it. :mad: In the end, that is only after after careful reading and analysis, you can start publishing messages with "strange behavior" in their subject...

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #5

          Ok, normally I read the docs very carefully. In this case I must admit that I only read the documentation of Double.IsPositiveInfinity, since after I changed range == double.PositiveInfinity to double.IsPositiveInfinity(range) and the behavior staid the same I jumped to the conclusion that it is not a matter of doing a comparison against Double.PositiveInfinity. You're right that it is no good excuse (especially not for using Double.PositiveInfinity in the first place), but the there is still the strange behavior of Double.IsPositiveInfinity.


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          L 1 Reply Last reply
          0
          • S Stefan Troschuetz

            Ok, normally I read the docs very carefully. In this case I must admit that I only read the documentation of Double.IsPositiveInfinity, since after I changed range == double.PositiveInfinity to double.IsPositiveInfinity(range) and the behavior staid the same I jumped to the conclusion that it is not a matter of doing a comparison against Double.PositiveInfinity. You're right that it is no good excuse (especially not for using Double.PositiveInfinity in the first place), but the there is still the strange behavior of Double.IsPositiveInfinity.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            Stefan Troschuetz wrote:

            I read the docs very carefully

            ;) Your modified code runs fine for me; it gives "Infinity" when running in debug under Visual 2005 when running in release under Visual 2005 and when running outside Visual 2005 :confused:

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            S P 2 Replies Last reply
            0
            • L Luc Pattyn

              Stefan Troschuetz wrote:

              I read the docs very carefully

              ;) Your modified code runs fine for me; it gives "Infinity" when running in debug under Visual 2005 when running in release under Visual 2005 and when running outside Visual 2005 :confused:

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #7

              I was afraid of something like this. It still does not work on my computer neither under VS2005 nor outside. The same result on my laptop. Maybe this is an issue of the german version only :confused: Tested the code under framework 1.0 and 1.1 and there it runs just fine. Also I took a look at the IL code and could not find an obvious error though I must admit that I have not much experience with IL. I'm somewhat out of ideas what to do with this :sigh:


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              L 1 Reply Last reply
              0
              • S Stefan Troschuetz

                I was afraid of something like this. It still does not work on my computer neither under VS2005 nor outside. The same result on my laptop. Maybe this is an issue of the german version only :confused: Tested the code under framework 1.0 and 1.1 and there it runs just fine. Also I took a look at the IL code and could not find an obvious error though I must admit that I have not much experience with IL. I'm somewhat out of ideas what to do with this :sigh:


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                If I were living in a country where infinity can not be trusted, I would emigrate the same day...

                Luc Pattyn


                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                S 1 Reply Last reply
                0
                • L Luc Pattyn

                  If I were living in a country where infinity can not be trusted, I would emigrate the same day...

                  Luc Pattyn


                  try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #9

                  THX for the tip :)


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Stefan Troschuetz wrote:

                    I read the docs very carefully

                    ;) Your modified code runs fine for me; it gives "Infinity" when running in debug under Visual 2005 when running in release under Visual 2005 and when running outside Visual 2005 :confused:

                    Luc Pattyn


                    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                    P Offline
                    P Offline
                    PhilDanger
                    wrote on last edited by
                    #10

                    The code runs fine for me (printing "Infinity") in the following situations: Commented, Uncommented Last Line in Debug Uncommented Last Line in Release It does NOT work (printing "Not Infinity"), when: Commented Last Line in Release. Strange indeed!

                    S 1 Reply Last reply
                    0
                    • P PhilDanger

                      The code runs fine for me (printing "Infinity") in the following situations: Commented, Uncommented Last Line in Debug Uncommented Last Line in Release It does NOT work (printing "Not Infinity"), when: Commented Last Line in Release. Strange indeed!

                      S Offline
                      S Offline
                      Stefan Troschuetz
                      wrote on last edited by
                      #11

                      What version of the framework is installed on your computer? Any language packs?


                      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                      www.troschuetz.de

                      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