Skip to content

Commit

Permalink
feat: add dsmath (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xh9n authored Oct 16, 2023
1 parent e3446d2 commit c085a8d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions dsmath/dsmath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dsmath

import (
"math/big"

"github.com/KyberNetwork/blockchain-toolkit/integer"
)

var (
WAD *big.Int = integer.TenPow(18)
)

// WMul
// ((x * y) + (WAD / 2)) / WAD;
func WMul(x *big.Int, y *big.Int) *big.Int {
return new(big.Int).Div(
new(big.Int).Add(
new(big.Int).Mul(x, y),
new(big.Int).Div(WAD, integer.Two()),
),
WAD,
)
}

// WDiv
// ((x * WAD) + (y / 2)) / y
func WDiv(x *big.Int, y *big.Int) *big.Int {
return new(big.Int).Div(
new(big.Int).Add(
new(big.Int).Mul(x, WAD),
new(big.Int).Div(y, integer.Two()),
),
y,
)
}

0 comments on commit c085a8d

Please sign in to comment.