MCPcopy Create free account
hub / github.com/cockroachdb/apd / Cmp

Method Cmp

decimal.go:523–596  ·  view source on GitHub ↗

Cmp compares d and x and returns: -1 if d < x 0 if d == x +1 if d > x undefined if d or x are NaN

(x *Decimal)

Source from the content-addressed store, hash-verified

521// +1 if d > x
522// undefined if d or x are NaN
523func (d *Decimal) Cmp(x *Decimal) int {
524 ds := d.Sign()
525 xs := x.Sign()
526
527 // First compare signs.
528 if ds < xs {
529 return -1
530 } else if ds > xs {
531 return 1
532 } else if ds == 0 && xs == 0 {
533 return 0
534 }
535
536 // Use gt and lt here with flipped signs if d is negative. gt and lt then
537 // allow for simpler comparisons since we can ignore the sign of the decimals
538 // and only worry about the form and value.
539 gt := 1
540 lt := -1
541 if ds == -1 {
542 gt = -1
543 lt = 1
544 }
545
546 if d.Form == Infinite {
547 if x.Form == Infinite {
548 return 0
549 }
550 return gt
551 } else if x.Form == Infinite {
552 return lt
553 }
554
555 if d.Exponent == x.Exponent {
556 cmp := d.Coeff.Cmp(&x.Coeff)
557 if ds < 0 {
558 cmp = -cmp
559 }
560 return cmp
561 }
562
563 // Next compare adjusted exponents.
564 dn := d.NumDigits() + int64(d.Exponent)
565 xn := x.NumDigits() + int64(x.Exponent)
566 if dn < xn {
567 return lt
568 } else if dn > xn {
569 return gt
570 }
571
572 // Now have to use aligned BigInts. This function previously used upscale to
573 // align in all cases, but that requires an error in the return value. upscale
574 // does that so that it can fail if it needs to take the Exp of too-large a
575 // number, which is very slow. The only way for that to happen here is for d
576 // and x's coefficients to be of hugely differing values. That is practically
577 // more difficult, so we are assuming the user is already comfortable with
578 // slowness in those operations.
579
580 var cmp int

Callers 8

TestDecomposerRoundTripFunction · 0.95
RoundMethod · 0.95
gdaTestFunction · 0.95
CbrtMethod · 0.95
ExpMethod · 0.95
Int64Method · 0.95
setExponentMethod · 0.95
CmpTotalMethod · 0.95

Calls 7

SignMethod · 0.95
NumDigitsMethod · 0.95
SetMethod · 0.95
MulMethod · 0.95
CmpMethod · 0.95
tableExp10Function · 0.85
CmpMethod · 0.45

Tested by 2

TestDecomposerRoundTripFunction · 0.76
gdaTestFunction · 0.76