Skip to content

Commit 3ae1a53

Browse files
committed
Fixes for mod
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent f0cde8c commit 3ae1a53

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

pkg/lang/bigdecimal.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func (n *BigDecimal) Quotient(other *BigDecimal) *BigDecimal {
107107
}
108108

109109
func (n *BigDecimal) Remainder(other *BigDecimal) *BigDecimal {
110-
panic("not implemented")
110+
quotient := new(big.Float).Quo(n.val, other.val)
111+
intQuotient, _ := quotient.Int(nil)
112+
intQuotientFloat := new(big.Float).SetInt(intQuotient)
113+
product := new(big.Float).Mul(intQuotientFloat, other.val)
114+
remainder := new(big.Float).Sub(n.val, product)
115+
return &BigDecimal{val: remainder}
111116
}
112117

113118
func (n *BigDecimal) Cmp(other *BigDecimal) int {

pkg/lang/numberops.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,20 @@ func (o ratioOps) Quotient(x, y any) any {
427427
return AsRatio(x).Quotient(AsRatio(y))
428428
}
429429
func (o ratioOps) Remainder(x, y any) any {
430-
xRat := AsRatio(x)
431-
yRat := AsRatio(y)
430+
xRat := AsRatio(x).val
431+
yRat := AsRatio(y).val
432432

433-
q := new(big.Int)
434-
q.Mul(xRat.val.Num(), yRat.val.Denom())
433+
// BigInteger q = rx.numerator.multiply(ry.denominator).divide(
434+
// rx.denominator.multiply(ry.numerator));
435+
// Number ret = Numbers.minus(x, Numbers.multiply(q, y));
436+
// return ret
435437

436-
qd := new(big.Int)
437-
qd.Mul(xRat.val.Denom(), yRat.val.Num())
438+
// result should be a BigInt
439+
qn := new(big.Int).Mul(xRat.Num(), yRat.Denom())
440+
qd := new(big.Int).Mul(xRat.Denom(), yRat.Num())
441+
rem := new(big.Int)
442+
q, rem := qn.QuoRem(qn, qd, rem)
438443

439-
q.Div(q, qd)
440444
return Sub(x, Multiply(q, y))
441445
}
442446
func (o ratioOps) LT(x, y any) bool {

0 commit comments

Comments
 (0)