Strange behavior of comparison with Double.IsPositiveInfinity [modified]
-
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
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] }
-
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] }
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 ofDouble.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
-
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 ofDouble.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
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] }
-
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] }
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 changedrange == double.PositiveInfinity
todouble.IsPositiveInfinity(range)
and the behavior staid the same I jumped to the conclusion that it is not a matter of doing a comparison againstDouble.PositiveInfinity
. You're right that it is no good excuse (especially not for usingDouble.PositiveInfinity
in the first place), but the there is still the strange behavior ofDouble.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
-
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 changedrange == double.PositiveInfinity
todouble.IsPositiveInfinity(range)
and the behavior staid the same I jumped to the conclusion that it is not a matter of doing a comparison againstDouble.PositiveInfinity
. You're right that it is no good excuse (especially not for usingDouble.PositiveInfinity
in the first place), but the there is still the strange behavior ofDouble.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
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] }
-
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] }
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
-
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
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] }
-
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] }
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
-
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] }
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!
-
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!
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