Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

math.gno

0.37 Kb · 20 lines
 1package lastcall
 2
 3import "math/bits"
 4
 5const maxInt64 = uint64(1<<63 - 1)
 6
 7func mulDiv(a, b, c int64) int64 {
 8	if a < 0 || b < 0 || c <= 0 {
 9		panic("desk: math domain")
10	}
11	hi, lo := bits.Mul64(uint64(a), uint64(b))
12	if hi >= uint64(c) {
13		panic("desk: math overflow")
14	}
15	q, _ := bits.Div64(hi, lo, uint64(c))
16	if q > maxInt64 {
17		panic("desk: math overflow")
18	}
19	return int64(q)
20}