Strange behavior of Double.IsPositiveInfinity [modified]
-
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
-
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
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] }
-
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] }
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