Commit dec68592 authored by Robert Griesemer's avatar Robert Griesemer

cmd/internal/gc/big: updated vendored version of math/big (fix build)

Change-Id: I04c2bd18a47cc775c78d074fe521cef2b0d6e7f0
Reviewed-on: https://go-review.googlesource.com/8426
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: 's avatarDavid Crawshaw <crawshaw@golang.org>
parent 398bf9d5
...@@ -4,13 +4,14 @@ package big ...@@ -4,13 +4,14 @@ package big
import "fmt" import "fmt"
const _Accuracy_name = "ExactBelowAboveUndef" const _Accuracy_name = "BelowExactAbove"
var _Accuracy_index = [...]uint8{0, 5, 10, 15, 20} var _Accuracy_index = [...]uint8{0, 5, 10, 15}
func (i Accuracy) String() string { func (i Accuracy) String() string {
i -= -1
if i < 0 || i+1 >= Accuracy(len(_Accuracy_index)) { if i < 0 || i+1 >= Accuracy(len(_Accuracy_index)) {
return fmt.Sprintf("Accuracy(%d)", i) return fmt.Sprintf("Accuracy(%d)", i+-1)
} }
return _Accuracy_name[_Accuracy_index[i]:_Accuracy_index[i+1]] return _Accuracy_name[_Accuracy_index[i]:_Accuracy_index[i+1]]
} }
This diff is collapsed.
This diff is collapsed.
...@@ -73,10 +73,8 @@ func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) { ...@@ -73,10 +73,8 @@ func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) {
prec = 64 prec = 64
} }
// NaNs ignore sign, mantissa, and exponent so we can set // A reasonable value in case of an error.
// them below while having a valid value for z in case of z.form = zero
// errors.
z.SetNaN()
// sign // sign
z.neg, err = scanSign(r) z.neg, err = scanSign(r)
...@@ -260,11 +258,6 @@ func (x *Float) Append(buf []byte, format byte, prec int) []byte { ...@@ -260,11 +258,6 @@ func (x *Float) Append(buf []byte, format byte, prec int) []byte {
return append(buf, "Inf"...) return append(buf, "Inf"...)
} }
// NaN
if x.IsNaN() {
return append(buf, "NaN"...)
}
// easy formats // easy formats
switch format { switch format {
case 'b': case 'b':
......
...@@ -102,7 +102,7 @@ func TestFloatSetFloat64String(t *testing.T) { ...@@ -102,7 +102,7 @@ func TestFloatSetFloat64String(t *testing.T) {
} }
f, _ := x.Float64() f, _ := x.Float64()
want := new(Float).SetFloat64(test.x) want := new(Float).SetFloat64(test.x)
if x.Cmp(want).Neq() { if x.Cmp(want) != 0 {
t.Errorf("%s: got %s (%v); want %v", test.s, &x, f, test.x) t.Errorf("%s: got %s (%v); want %v", test.s, &x, f, test.x)
} }
} }
......
...@@ -50,88 +50,62 @@ func Example_Shift() { ...@@ -50,88 +50,62 @@ func Example_Shift() {
func ExampleFloat_Cmp() { func ExampleFloat_Cmp() {
inf := math.Inf(1) inf := math.Inf(1)
zero := 0.0 zero := 0.0
nan := math.NaN()
operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf, nan} operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf}
fmt.Println(" x y cmp eql neq lss leq gtr geq") fmt.Println(" x y cmp")
fmt.Println("-----------------------------------------------") fmt.Println("---------------")
for _, x64 := range operands { for _, x64 := range operands {
x := big.NewFloat(x64) x := big.NewFloat(x64)
for _, y64 := range operands { for _, y64 := range operands {
y := big.NewFloat(y64) y := big.NewFloat(y64)
t := x.Cmp(y) fmt.Printf("%4s %4s %3d\n", x, y, x.Cmp(y))
fmt.Printf(
"%4s %4s %5s %c %c %c %c %c %c\n",
x, y, t.Acc(),
mark(t.Eql()), mark(t.Neq()), mark(t.Lss()), mark(t.Leq()), mark(t.Gtr()), mark(t.Geq()))
} }
fmt.Println() fmt.Println()
} }
// Output: // Output:
// x y cmp eql neq lss leq gtr geq // x y cmp
// ----------------------------------------------- // ---------------
// -Inf -Inf Exact ● ○ ○ ● ○ ● // -Inf -Inf 0
// -Inf -1.2 Below ○ ● ● ● ○ ○ // -Inf -1.2 -1
// -Inf -0 Below ○ ● ● ● ○ ○ // -Inf -0 -1
// -Inf 0 Below ○ ● ● ● ○ ○ // -Inf 0 -1
// -Inf 1.2 Below ○ ● ● ● ○ ○ // -Inf 1.2 -1
// -Inf +Inf Below ○ ● ● ● ○ ○ // -Inf +Inf -1
// -Inf NaN Undef ○ ● ○ ○ ○ ○
// //
// -1.2 -Inf Above ○ ● ○ ○ ● ● // -1.2 -Inf 1
// -1.2 -1.2 Exact ● ○ ○ ● ○ ● // -1.2 -1.2 0
// -1.2 -0 Below ○ ● ● ● ○ ○ // -1.2 -0 -1
// -1.2 0 Below ○ ● ● ● ○ ○ // -1.2 0 -1
// -1.2 1.2 Below ○ ● ● ● ○ ○ // -1.2 1.2 -1
// -1.2 +Inf Below ○ ● ● ● ○ ○ // -1.2 +Inf -1
// -1.2 NaN Undef ○ ● ○ ○ ○ ○
// //
// -0 -Inf Above ○ ● ○ ○ ● ● // -0 -Inf 1
// -0 -1.2 Above ○ ● ○ ○ ● ● // -0 -1.2 1
// -0 -0 Exact ● ○ ○ ● ○ ● // -0 -0 0
// -0 0 Exact ● ○ ○ ● ○ ● // -0 0 0
// -0 1.2 Below ○ ● ● ● ○ ○ // -0 1.2 -1
// -0 +Inf Below ○ ● ● ● ○ ○ // -0 +Inf -1
// -0 NaN Undef ○ ● ○ ○ ○ ○
// //
// 0 -Inf Above ○ ● ○ ○ ● ● // 0 -Inf 1
// 0 -1.2 Above ○ ● ○ ○ ● ● // 0 -1.2 1
// 0 -0 Exact ● ○ ○ ● ○ ● // 0 -0 0
// 0 0 Exact ● ○ ○ ● ○ ● // 0 0 0
// 0 1.2 Below ○ ● ● ● ○ ○ // 0 1.2 -1
// 0 +Inf Below ○ ● ● ● ○ ○ // 0 +Inf -1
// 0 NaN Undef ○ ● ○ ○ ○ ○
// //
// 1.2 -Inf Above ○ ● ○ ○ ● ● // 1.2 -Inf 1
// 1.2 -1.2 Above ○ ● ○ ○ ● ● // 1.2 -1.2 1
// 1.2 -0 Above ○ ● ○ ○ ● ● // 1.2 -0 1
// 1.2 0 Above ○ ● ○ ○ ● ● // 1.2 0 1
// 1.2 1.2 Exact ● ○ ○ ● ○ ● // 1.2 1.2 0
// 1.2 +Inf Below ○ ● ● ● ○ ○ // 1.2 +Inf -1
// 1.2 NaN Undef ○ ● ○ ○ ○ ○
// //
// +Inf -Inf Above ○ ● ○ ○ ● ● // +Inf -Inf 1
// +Inf -1.2 Above ○ ● ○ ○ ● ● // +Inf -1.2 1
// +Inf -0 Above ○ ● ○ ○ ● ● // +Inf -0 1
// +Inf 0 Above ○ ● ○ ○ ● ● // +Inf 0 1
// +Inf 1.2 Above ○ ● ○ ○ ● ● // +Inf 1.2 1
// +Inf +Inf Exact ● ○ ○ ● ○ ● // +Inf +Inf 0
// +Inf NaN Undef ○ ● ○ ○ ○ ○
//
// NaN -Inf Undef ○ ● ○ ○ ○ ○
// NaN -1.2 Undef ○ ● ○ ○ ○ ○
// NaN -0 Undef ○ ● ○ ○ ○ ○
// NaN 0 Undef ○ ● ○ ○ ○ ○
// NaN 1.2 Undef ○ ● ○ ○ ○ ○
// NaN +Inf Undef ○ ● ○ ○ ○ ○
// NaN NaN Undef ○ ● ○ ○ ○ ○
}
func mark(p bool) rune {
if p {
return '●'
}
return '○'
} }
...@@ -19,7 +19,7 @@ import "strconv" ...@@ -19,7 +19,7 @@ import "strconv"
// bigFtoa formats a float for the %e, %E, %f, %g, and %G formats. // bigFtoa formats a float for the %e, %E, %f, %g, and %G formats.
func (f *Float) bigFtoa(buf []byte, fmt byte, prec int) []byte { func (f *Float) bigFtoa(buf []byte, fmt byte, prec int) []byte {
if debugFloat && !f.IsFinite() { if debugFloat && f.IsInf() {
panic("non-finite float") panic("non-finite float")
} }
......
...@@ -184,6 +184,10 @@ func (z *Int) MulRange(a, b int64) *Int { ...@@ -184,6 +184,10 @@ func (z *Int) MulRange(a, b int64) *Int {
// Binomial sets z to the binomial coefficient of (n, k) and returns z. // Binomial sets z to the binomial coefficient of (n, k) and returns z.
func (z *Int) Binomial(n, k int64) *Int { func (z *Int) Binomial(n, k int64) *Int {
// reduce the number of multiplications by reducing k
if n/2 < k && k <= n {
k = n - k // Binomial(n, k) == Binomial(n, n-k)
}
var a, b Int var a, b Int
a.MulRange(n-k+1, n) a.MulRange(n-k+1, n)
b.MulRange(1, k) b.MulRange(1, k)
......
...@@ -219,6 +219,45 @@ func TestMulRangeZ(t *testing.T) { ...@@ -219,6 +219,45 @@ func TestMulRangeZ(t *testing.T) {
} }
} }
func TestBinomial(t *testing.T) {
var z Int
for _, test := range []struct {
n, k int64
want string
}{
{0, 0, "1"},
{0, 1, "0"},
{1, 0, "1"},
{1, 1, "1"},
{1, 10, "0"},
{4, 0, "1"},
{4, 1, "4"},
{4, 2, "6"},
{4, 3, "4"},
{4, 4, "1"},
{10, 1, "10"},
{10, 9, "10"},
{10, 5, "252"},
{11, 5, "462"},
{11, 6, "462"},
{100, 10, "17310309456440"},
{100, 90, "17310309456440"},
{1000, 10, "263409560461970212832400"},
{1000, 990, "263409560461970212832400"},
} {
if got := z.Binomial(test.n, test.k).String(); got != test.want {
t.Errorf("Binomial(%d, %d) = %s; want %s", test.n, test.k, got, test.want)
}
}
}
func BenchmarkBinomial(b *testing.B) {
var z Int
for i := b.N - 1; i >= 0; i-- {
z.Binomial(1000, 990)
}
}
// Examples from the Go Language Spec, section "Arithmetic operators" // Examples from the Go Language Spec, section "Arithmetic operators"
var divisionSignsTests = []struct { var divisionSignsTests = []struct {
x, y int64 x, y int64
...@@ -353,7 +392,7 @@ func checkBytes(b []byte) bool { ...@@ -353,7 +392,7 @@ func checkBytes(b []byte) bool {
} }
func TestBytes(t *testing.T) { func TestBytes(t *testing.T) {
if err := quick.Check(checkSetBytes, nil); err != nil { if err := quick.Check(checkBytes, nil); err != nil {
t.Error(err) t.Error(err)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment