"638952175999932299156089414639761565182862536979208272237582" +
"511852109168640000000000000000000000", // -99!
},
+
+ // overflow situations
+ {math.MaxInt64 - 0, math.MaxInt64, "9223372036854775807"},
+ {math.MaxInt64 - 1, math.MaxInt64, "85070591730234615838173535747377725442"},
+ {math.MaxInt64 - 2, math.MaxInt64, "784637716923335094969050127519550606919189611815754530810"},
+ {math.MaxInt64 - 3, math.MaxInt64, "7237005577332262206126809393809643289012107973151163787181513908099760521240"},
}
func TestMulRangeZ(t *testing.T) {
var tmp Int
// test entirely positive ranges
for i, r := range mulRangesN {
+ // skip mulRangesN entries that overflow int64
+ if int64(r.a) < 0 || int64(r.b) < 0 {
+ continue
+ }
prod := tmp.MulRange(int64(r.a), int64(r.b)).String()
if prod != r.prod {
t.Errorf("#%da: got %s; want %s", i, prod, r.prod)
case a+1 == b:
return z.mul(nat(nil).setUint64(a), nat(nil).setUint64(b))
}
- m := (a + b) / 2
+ m := a + (b-a)/2 // avoid overflow
return z.mul(nat(nil).mulRange(a, m), nat(nil).mulRange(m+1, b))
}
import (
"fmt"
+ "math"
"runtime"
"strings"
"testing"
"638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000", // 100!
},
+ {math.MaxUint64 - 0, math.MaxUint64, "18446744073709551615"},
+ {math.MaxUint64 - 1, math.MaxUint64, "340282366920938463408034375210639556610"},
+ {math.MaxUint64 - 2, math.MaxUint64, "6277101735386680761794095221682035635525021984684230311930"},
+ {math.MaxUint64 - 3, math.MaxUint64, "115792089237316195360799967654821100226821973275796746098729803619699194331160"},
}
func TestMulRangeN(t *testing.T) {