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

Method Modf

decimal.go:627–684  ·  view source on GitHub ↗

Modf sets integ to the integral part of d and frac to the fractional part such that d = integ+frac. If d is negative, both integ or frac will be either 0 or negative. integ.Exponent will be >= 0; frac.Exponent will be <= 0. Either argument can be nil, preventing it from being set.

(integ, frac *Decimal)

Source from the content-addressed store, hash-verified

625// 0 or negative. integ.Exponent will be >= 0; frac.Exponent will be <= 0.
626// Either argument can be nil, preventing it from being set.
627func (d *Decimal) Modf(integ, frac *Decimal) {
628 if integ == nil && frac == nil {
629 return
630 }
631
632 neg := d.Negative
633
634 // No fractional part.
635 if d.Exponent > 0 {
636 if frac != nil {
637 frac.Negative = neg
638 frac.Exponent = 0
639 frac.Coeff.SetInt64(0)
640 }
641 if integ != nil {
642 integ.Set(d)
643 }
644 return
645 }
646 nd := d.NumDigits()
647 exp := -int64(d.Exponent)
648 // d < 0 because exponent is larger than number of digits.
649 if exp > nd {
650 if integ != nil {
651 integ.Negative = neg
652 integ.Exponent = 0
653 integ.Coeff.SetInt64(0)
654 }
655 if frac != nil {
656 frac.Set(d)
657 }
658 return
659 }
660
661 var tmpE BigInt
662 e := tableExp10(exp, &tmpE)
663
664 var icoeff *BigInt
665 if integ != nil {
666 icoeff = &integ.Coeff
667 integ.Exponent = 0
668 integ.Negative = neg
669 } else {
670 // This is the integ == nil branch, and we already checked if both integ and
671 // frac were nil above, so frac can never be nil in this branch.
672 icoeff = new(BigInt)
673 }
674
675 if frac != nil {
676 icoeff.QuoRem(&d.Coeff, e, &frac.Coeff)
677 frac.Exponent = d.Exponent
678 frac.Negative = neg
679 } else {
680 // This is the frac == nil, which means integ must not be nil since they both
681 // can't be due to the check above.
682 icoeff.Quo(&d.Coeff, e)
683 }
684}

Callers 6

Int64Method · 0.95
setExponentMethod · 0.95
PowMethod · 0.80
CeilMethod · 0.80
FloorMethod · 0.80
TestModfFunction · 0.80

Calls 6

NumDigitsMethod · 0.95
QuoRemMethod · 0.95
QuoMethod · 0.95
tableExp10Function · 0.85
SetInt64Method · 0.45
SetMethod · 0.45

Tested by 1

TestModfFunction · 0.64