Double dblValue = 0.0001;
Boolean a = (dblValue >= (1 / 1000));
Boolean b = (dblValue >= 0.001);
Console.WriteLine("dblValue >= (1 / 1000) is " + a);
Console.WriteLine("dblValue >= 0.001 is " + b);
Console.ReadLine();
The above C# code evaluates ‘a’ to true and ‘b’ to false. In VB.NET, the equivalent code evaluates ‘a’ to false and ‘b’ to false. Why would ‘a’ evaluate to true?
Is there an implicit conversion I’m missing here – and why doesn’t it affect VB.NET (Strict)?
The expression
1 / 1000is evaluated (at compile time in this case, although it’s irrelevant really) using integer arithmetic in C#, so evaluates to 0. Use1.0 / 10001 instead to forcedoublearithmetic to be used.I believe VB always uses floating point arithmetic for
/, and you have to use\if you want to perform division using integer arithmetic, which is why you’re seeing different behaviour there.1 Or, as per comments, use
1dor(double) 1or anything else that will force either of the operands to be considered to be of typedouble.