Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "not >" equivalent to "<=" for a double

Tags:

c#

conditional

double a, b = ...;

Are the following C#-statements

!(a > b)

and

a <= b

equivalent or are there any numerical caveats?

like image 421
participant Avatar asked Nov 29 '18 10:11

participant


People also ask

Does == mean not equal?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

Is == and === the same?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What does <= mean in C#?

C# | Less than or equal to: <= | Easy language reference. C# | Visual C# | .NET. Types and variables. Basic data types.


1 Answers

They are equivalent if they are standard vanilla double numeric values.

With nullable, NaN, etc, this isn't as clear.

Consider

double? a = null;
double b = 1;

if (!(a > b))
{
   //yes
}
if ((a <= b))
{
   //no
}

Or as Marc Gravell♦ pointed out, the below demonstrates the exact same behaviour, while sticking with pure double:

double a = 42;
double b = double.NaN;
like image 130
TheGeneral Avatar answered Oct 15 '22 03:10

TheGeneral