private EComparison CompareNm
(
SqlDecimal snumOp
)
{
AssertValid();
snumOp.AssertValid();
//Signs of the two numeric operands
int Sign1;
int Sign2;
int iFinalResult; //Final result of comparision: positive = greater
//than, 0 = equal, negative = less than
//Initialize the sign values to be 1(positive) or -1(negative)
Sign1 = IsPositive ? 1 : -1;
Sign2 = snumOp.IsPositive ? 1 : -1;
if (Sign1 != Sign2) //If different sign, the positive one is greater
return Sign1 == 1 ? EComparison.GT : EComparison.LT;
else
{ //same sign, have to compare absolute values
//Temporary memory to hold the operand since it is const
//but its scale may get adjusted during comparison
int ScaleDiff;
SqlDecimal snumArg1 = this;
SqlDecimal snumArg2 = snumOp;
//First make the two operands the same scale if necessary
ScaleDiff = _bScale - snumOp._bScale;
if (ScaleDiff < 0)
{
//If increasing the scale of operand1 caused an overflow,
//then its absolute value is greater than that of operand2.
try
{
snumArg1.AdjustScale(-ScaleDiff, true);
}
catch (OverflowException)
{
return (Sign1 > 0) ? EComparison.GT : EComparison.LT;
}
}
else if (ScaleDiff > 0)
{
//If increasing the scale of operand2 caused an overflow, then
//operand1's absolute value is less than that of operand2.
try
{
snumArg2.AdjustScale(ScaleDiff, true);
}
catch (OverflowException)
{
return (Sign1 > 0) ? EComparison.LT : EComparison.GT;
}
}
//Compare the absolute value of the two numerics
//Note: We are sure that scale of arguments is the same,
// so LAbsCmp() will not modify its argument.
int lResult = snumArg1.LAbsCmp(snumArg2);
if (0 == lResult)
return EComparison.EQ;
//if both positive, result same as result from LAbsCmp;
//if both negative, result reverse of result from LAbsCmp
iFinalResult = Sign1 * lResult;
if (iFinalResult < 0)
return EComparison.LT;
else
return EComparison.GT;
}
}