------------------------------------------------------------------ */ decCompare -- compare two decNumbers by numerical value */ / This routine compares A ? B without altering them. */ / Arg1 is A, a decNumber which is not a NaN */ Arg2 is B, a decNumber which is not a NaN */ Arg3 is 1 for a sign-independent compare, 0 other
| 6165 | /* (the only possible failure is an allocation error) */ |
| 6166 | /* ------------------------------------------------------------------ */ |
| 6167 | static Int decCompare(const decNumber *lhs, const decNumber *rhs, |
| 6168 | Flag abs) { |
| 6169 | Int result; // result value |
| 6170 | Int sigr; // rhs signum |
| 6171 | Int compare; // work |
| 6172 | |
| 6173 | result=1; // assume signum(lhs) |
| 6174 | if (ISZERO(lhs)) result=0; |
| 6175 | if (abs) { |
| 6176 | if (ISZERO(rhs)) return result; // LHS wins or both 0 |
| 6177 | // RHS is non-zero |
| 6178 | if (result==0) return -1; // LHS is 0; RHS wins |
| 6179 | // [here, both non-zero, result=1] |
| 6180 | } |
| 6181 | else { // signs matter |
| 6182 | if (result && decNumberIsNegative(lhs)) result=-1; |
| 6183 | sigr=1; // compute signum(rhs) |
| 6184 | if (ISZERO(rhs)) sigr=0; |
| 6185 | else if (decNumberIsNegative(rhs)) sigr=-1; |
| 6186 | if (result > sigr) return +1; // L > R, return 1 |
| 6187 | if (result < sigr) return -1; // L < R, return -1 |
| 6188 | if (result==0) return 0; // both 0 |
| 6189 | } |
| 6190 | |
| 6191 | // signums are the same; both are non-zero |
| 6192 | if ((lhs->bits | rhs->bits) & DECINF) { // one or more infinities |
| 6193 | if (decNumberIsInfinite(rhs)) { |
| 6194 | if (decNumberIsInfinite(lhs)) result=0;// both infinite |
| 6195 | else result=-result; // only rhs infinite |
| 6196 | } |
| 6197 | return result; |
| 6198 | } |
| 6199 | // must compare the coefficients, allowing for exponents |
| 6200 | if (lhs->exponent>rhs->exponent) { // LHS exponent larger |
| 6201 | // swap sides, and sign |
| 6202 | const decNumber *temp=lhs; |
| 6203 | lhs=rhs; |
| 6204 | rhs=temp; |
| 6205 | result=-result; |
| 6206 | } |
| 6207 | compare=decUnitCompare(lhs->lsu, D2U(lhs->digits), |
| 6208 | rhs->lsu, D2U(rhs->digits), |
| 6209 | rhs->exponent-lhs->exponent); |
| 6210 | if (compare!=BADINT) compare*=result; // comparison succeeded |
| 6211 | return compare; |
| 6212 | } // decCompare |
| 6213 | |
| 6214 | /* ------------------------------------------------------------------ */ |
| 6215 | /* decUnitCompare -- compare two >=0 integers in Unit arrays */ |
no test coverage detected