QuoRem does division with remainder d.QuoRem(d2,precision) returns quotient q and remainder r such that d = d2 * q + r, q an integer multiple of 10^(-precision) 0 <= r < abs(d2) * 10 ^(-precision) if d>=0 0 >= r > -abs(d2) * 10 ^(-precision) if d<0 Note that precision<0 is allowed as input.
(d2 Decimal, precision int32)
| 600 | // |
| 601 | // Note that precision<0 is allowed as input. |
| 602 | func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal) { |
| 603 | d.ensureInitialized() |
| 604 | d2.ensureInitialized() |
| 605 | if d2.value.Sign() == 0 { |
| 606 | panic("decimal division by 0") |
| 607 | } |
| 608 | scale := -precision |
| 609 | e := int64(d.exp) - int64(d2.exp) - int64(scale) |
| 610 | if e > math.MaxInt32 || e < math.MinInt32 { |
| 611 | panic("overflow in decimal QuoRem") |
| 612 | } |
| 613 | var aa, bb, expo big.Int |
| 614 | var scalerest int32 |
| 615 | // d = a 10^ea |
| 616 | // d2 = b 10^eb |
| 617 | if e < 0 { |
| 618 | aa = *d.value |
| 619 | expo.SetInt64(-e) |
| 620 | bb.Exp(tenInt, &expo, nil) |
| 621 | bb.Mul(d2.value, &bb) |
| 622 | scalerest = d.exp |
| 623 | // now aa = a |
| 624 | // bb = b 10^(scale + eb - ea) |
| 625 | } else { |
| 626 | expo.SetInt64(e) |
| 627 | aa.Exp(tenInt, &expo, nil) |
| 628 | aa.Mul(d.value, &aa) |
| 629 | bb = *d2.value |
| 630 | scalerest = scale + d2.exp |
| 631 | // now aa = a ^ (ea - eb - scale) |
| 632 | // bb = b |
| 633 | } |
| 634 | var q, r big.Int |
| 635 | q.QuoRem(&aa, &bb, &r) |
| 636 | dq := Decimal{value: &q, exp: scale} |
| 637 | dr := Decimal{value: &r, exp: scalerest} |
| 638 | return dq, dr |
| 639 | } |
| 640 | |
| 641 | // DivRound divides and rounds to a given precision |
| 642 | // i.e. to an integer multiple of 10^(-precision) |