SafeMulDiv computes (a * b) / c safely using big.Int.
(a, b, c uint64)
| 986 | |
| 987 | // SafeMulDiv computes (a * b) / c safely using big.Int. |
| 988 | func SafeMulDiv(a, b, c uint64) uint64 { |
| 989 | if c == 0 { |
| 990 | return 0 |
| 991 | } |
| 992 | bigA := new(big.Int).SetUint64(a) |
| 993 | bigB := new(big.Int).SetUint64(b) |
| 994 | bigC := new(big.Int).SetUint64(c) |
| 995 | |
| 996 | num := new(big.Int).Mul(bigA, bigB) |
| 997 | res := new(big.Int).Div(num, bigC) |
| 998 | |
| 999 | return res.Uint64() |
| 1000 | } |
| 1001 | |
| 1002 | // IntSqrt returns the integer square root of n (truncated) |
| 1003 | func IntSqrt(n uint64) uint64 { |