Operations of Relation
关系运算
布尔值 FALSE 用整数零表示,而布尔值 TRUE 则用任何非零值表示。
包含关系运算或逻辑运算的表达式值为 FALSE(0)或 TRUE(1)。
True if a is equal to b a == b;
True if a is not equal to b a != b;
True if a is less than b a < b;
True if a is greater than b a > b;
True if a is less than or equal to b a <= b;
True if a is greater than or equal to b a >= b;两个实数的相等性无法进行比较。在大多数情况下,两个看似相同的数可能因为第15位小数不同而不相等。为了正确比较两个实数,需要将这些数的归一化差值与零进行比较。
示例:
bool CompareDoubles(double number1,double number2)
{
if(NormalizeDouble(number1-number2,8)==0) return(true);
else return(false);
}
void OnStart()
{
double first=0.3;
double second=3.0;
double third=second-2.7;
if(first!=third)
{
if(CompareDoubles(first,third))
printf("%.16f and %.16f are equal",first,third);
}
}
// Result: 0.3000000000000000 0.2999999999999998 are equal
另请参阅
最后更新于