Fix multiplication instead of division

This commit is contained in:
Jan Bader 2022-02-06 22:18:50 +00:00
parent 487aa89f18
commit f09a2a4ca7
2 changed files with 19 additions and 18 deletions

View File

@ -192,7 +192,6 @@ func (h *Handler) calculateBalances(c *gin.Context, budget postgres.Budget, firs
categoryWithBalance.Activity = bal.Transactions categoryWithBalance.Activity = bal.Transactions
categoryWithBalance.Assigned = bal.Assignments categoryWithBalance.Assigned = bal.Assignments
} }
} }
// do not show hidden categories // do not show hidden categories

View File

@ -44,7 +44,7 @@ func (n Numeric) IsZero() bool {
} }
func (n Numeric) MatchExp(exp int32) Numeric { func (n Numeric) MatchExp(exp int32) Numeric {
diffExp := exp - n.Exp diffExp := n.Exp - exp
factor := big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(diffExp)), nil) factor := big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(diffExp)), nil)
return Numeric{pgtype.Numeric{ return Numeric{pgtype.Numeric{
Exp: exp, Exp: exp,
@ -55,34 +55,36 @@ func (n Numeric) MatchExp(exp int32) Numeric {
} }
func (n Numeric) Sub(o Numeric) Numeric { func (n Numeric) Sub(o Numeric) Numeric {
if n.Exp > o.Exp { left := n
o = o.MatchExp(n.Exp) right := o
} else if n.Exp < o.Exp { if n.Exp < o.Exp {
n = n.MatchExp(o.Exp) right = o.MatchExp(n.Exp)
} else if n.Exp > o.Exp {
left = n.MatchExp(o.Exp)
} }
if o.Exp == n.Exp { if left.Exp == right.Exp {
return Numeric{pgtype.Numeric{ return Numeric{pgtype.Numeric{
Exp: n.Exp, Exp: left.Exp,
Int: big.NewInt(0).Sub(o.Int, n.Int), Int: big.NewInt(0).Sub(left.Int, right.Int),
}} }}
} }
panic("Cannot subtract with different exponents") panic("Cannot subtract with different exponents")
} }
func (n Numeric) Add(o Numeric) Numeric { func (n Numeric) Add(o Numeric) Numeric {
fmt.Println("N", n, "O", o) left := n
if n.Exp > o.Exp { right := o
o = o.MatchExp(n.Exp) if n.Exp < o.Exp {
} else if n.Exp < o.Exp { right = o.MatchExp(n.Exp)
n = n.MatchExp(o.Exp) } else if n.Exp > o.Exp {
left = n.MatchExp(o.Exp)
} }
fmt.Println("NM", n, "OM", o) if left.Exp == right.Exp {
if o.Exp == n.Exp {
return Numeric{pgtype.Numeric{ return Numeric{pgtype.Numeric{
Exp: n.Exp, Exp: left.Exp,
Int: big.NewInt(0).Add(o.Int, n.Int), Int: big.NewInt(0).Add(left.Int, right.Int),
}} }}
} }