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 Double.IsPositiveInfinity [modified]

Strange behavior of Double.IsPositiveInfinity [modified]

Scheduled Pinned Locked Moved C#
helpquestiondotnetcomalgorithms
3 Posts 2 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 Offline
    S Offline
    Stefan Troschuetz
    wrote on last edited by
    #1

    Hello all, I've posted this before in another thread[^] but I'm anxious to get some more opinions so I'm posting this again. Consider the following small command line application:

    class Program
    {
    static void Main(string[] args)
    {
    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);	
    }
    

    }

    Looks very simple and should always print out "NextDouble: Infinity", but it does NOT. If running the program in debug mode everything is fine. If the program is run in release mode, it prints out "NextDouble: Not infinity"!! If the program is run in release mode with the last line in NextDouble uncommented everything is fine again. First I thought it may be some problem with code optimization that is by default activated for release build, but deactivating the code optimization has no influence on the result. I've furthermore taken a look at the IL code but could not see any obvious error that could explain the behavior. In my previous thread I got two responses: One responder obeserved the same weird behavior and one does not i.e. the program always prints "NextDouble: Infinity" as expected. Does anyone have an explaination or clue what is happening here? Also, maybe some of you have the time to run the program in order to get some more observations.? I'm looking forward to your input. -- modified at 5:02 Thursday 2nd August, 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 1 Reply Last reply
    0
    • S Stefan Troschuetz

      Hello all, I've posted this before in another thread[^] but I'm anxious to get some more opinions so I'm posting this again. Consider the following small command line application:

      class Program
      {
      static void Main(string[] args)
      {
      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);	
      }
      

      }

      Looks very simple and should always print out "NextDouble: Infinity", but it does NOT. If running the program in debug mode everything is fine. If the program is run in release mode, it prints out "NextDouble: Not infinity"!! If the program is run in release mode with the last line in NextDouble uncommented everything is fine again. First I thought it may be some problem with code optimization that is by default activated for release build, but deactivating the code optimization has no influence on the result. I've furthermore taken a look at the IL code but could not see any obvious error that could explain the behavior. In my previous thread I got two responses: One responder obeserved the same weird behavior and one does not i.e. the program always prints "NextDouble: Infinity" as expected. Does anyone have an explaination or clue what is happening here? Also, maybe some of you have the time to run the program in order to get some more observations.? I'm looking forward to your input. -- modified at 5:02 Thursday 2nd August, 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

      Hi again,

      Stefan Troschuetz wrote:

      If running the program in debug mode everything is fine. If the program is run in release mode, it prints out "NextDouble: Infinity"!! If the program is run in release mode with the last line in NextDouble uncommented everything is fine again.

      So what is the problem ?? And did you ever care to print out range (that is, if it ever goes wrong) ? :)

      Luc Pattyn


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


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi again,

        Stefan Troschuetz wrote:

        If running the program in debug mode everything is fine. If the program is run in release mode, it prints out "NextDouble: Infinity"!! If the program is run in release mode with the last line in NextDouble uncommented everything is fine again.

        So what is the problem ?? And did you ever care to print out range (that is, if it ever goes wrong) ? :)

        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

        Luc Pattyn wrote:

        So what is the problem ??

        Error in my post: It should be: "If the program is run in release mode, it prints out "NextDouble: Not infinity"!!". I modified this.

        Luc Pattyn wrote:

        And did you ever care to print out range (that is, if it ever goes wrong) ?

        Yes I did. The last line in NextDouble prints out range (if uncommented of course). It always gives that range is positive infinity and as I said in this case the program works as expected.


        "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