Commit 2a1728d0 authored by Robert Griesemer's avatar Robert Griesemer

math/big: use stringer for enum String() methods

Change-Id: Ide0615542d67b7d81bf6c56aab550e142a8789f7
Reviewed-on: https://go-review.googlesource.com/6682Reviewed-by: 's avatarAlan Donovan <adonovan@google.com>
parent 0a8a6258
// generated by stringer -type=Accuracy; DO NOT EDIT
package big
import "fmt"
const _Accuracy_name = "ExactBelowAboveUndef"
var _Accuracy_index = [...]uint8{0, 5, 10, 15, 20}
func (i Accuracy) String() string {
if i < 0 || i+1 >= Accuracy(len(_Accuracy_index)) {
return fmt.Sprintf("Accuracy(%d)", i)
}
return _Accuracy_name[_Accuracy_index[i]:_Accuracy_index[i+1]]
}
This diff is collapsed.
......@@ -78,7 +78,7 @@ func TestFloatZeroValue(t *testing.T) {
z := make(test.z)
test.op(z, make(test.x), make(test.y))
got := 0
if !z.IsInf(0) && !z.IsNaN() {
if z.IsFinite() {
got = int(z.int64())
}
if got != test.want {
......@@ -288,6 +288,38 @@ func TestFloatSetMantExp(t *testing.T) {
}
}
func TestFloatPredicates(t *testing.T) {
for _, test := range []struct {
x string
neg, zero, finite, inf, nan bool
}{
{x: "-Inf", neg: true, inf: true},
{x: "-1", neg: true, finite: true},
{x: "-0", neg: true, zero: true, finite: true},
{x: "0", zero: true, finite: true},
{x: "1", finite: true},
{x: "+Inf", inf: true},
{x: "NaN", nan: true},
} {
x := makeFloat(test.x)
if got := x.IsNeg(); got != test.neg {
t.Errorf("(%s).IsNeg() = %v; want %v", test.x, got, test.neg)
}
if got := x.IsZero(); got != test.zero {
t.Errorf("(%s).IsZero() = %v; want %v", test.x, got, test.zero)
}
if got := x.IsFinite(); got != test.finite {
t.Errorf("(%s).IsFinite() = %v; want %v", test.x, got, test.finite)
}
if got := x.IsInf(); got != test.inf {
t.Errorf("(%s).IsInf() = %v; want %v", test.x, got, test.inf)
}
if got := x.IsNaN(); got != test.nan {
t.Errorf("(%s).IsNaN() = %v; want %v", test.x, got, test.nan)
}
}
}
func TestFloatIsInt(t *testing.T) {
for _, test := range []string{
"0 int",
......@@ -314,14 +346,6 @@ func TestFloatIsInt(t *testing.T) {
}
}
func TestFloatIsInf(t *testing.T) {
// TODO(gri) implement this
}
func TestFloatIsNaN(t *testing.T) {
// TODO(gri) implement this
}
func fromBinary(s string) int64 {
x, err := strconv.ParseInt(s, 2, 64)
if err != nil {
......@@ -740,10 +764,6 @@ func TestFloatSetInf(t *testing.T) {
}
}
func TestFloatSetNaN(t *testing.T) {
// TODO(gri) implement
}
func TestFloatUint64(t *testing.T) {
for _, test := range []struct {
x string
......
......@@ -63,7 +63,7 @@ func (z *Float) SetString(s string) (*Float, bool) {
// be binary, if present (an "e" or "E" exponent indicator cannot be
// distinguished from a mantissa digit).
//
// BUG(gri) This signature conflicts with Scan(s fmt.ScanState, ch rune) error.
// BUG(gri) The Float.Scan signature conflicts with Scan(s fmt.ScanState, ch rune) error.
func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) {
if z.prec == 0 {
z.prec = 64
......@@ -224,7 +224,7 @@ func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b i
// number of digits necessary such that ParseFloat will return f exactly.
// The prec value is ignored for the 'b' or 'p' format.
//
// BUG(gri) Currently, Format does not accept negative precisions.
// BUG(gri) Float.Format does not accept negative precisions.
func (x *Float) Format(format byte, prec int) string {
const extra = 10 // TODO(gri) determine a good/better value here
return string(x.Append(make([]byte, 0, prec+extra), format, prec))
......@@ -236,7 +236,7 @@ func (x *Float) Append(buf []byte, format byte, prec int) []byte {
// TODO(gri) factor out handling of sign?
// Inf
if x.IsInf(0) {
if x.IsInf() {
var ch byte = '+'
if x.neg {
ch = '-'
......@@ -261,7 +261,7 @@ func (x *Float) Append(buf []byte, format byte, prec int) []byte {
return x.bigFtoa(buf, format, prec)
}
// BUG(gri): Currently, String uses x.Format('g', 10) rather than x.Format('g', -1).
// BUG(gri): Float.String uses x.Format('g', 10) rather than x.Format('g', -1).
func (x *Float) String() string {
return x.Format('g', 10)
}
......
......@@ -12,7 +12,7 @@ import (
// TODO(gri) add more examples
func ExampleFloat_Add() {
// Operating on numbers of different precision is easy.
// Operating on numbers of different precision.
var x, y, z big.Float
x.SetInt64(1000) // x is automatically set to 64bit precision
y.SetFloat64(2.718281828) // y is automatically set to 53bit precision
......@@ -22,9 +22,9 @@ func ExampleFloat_Add() {
fmt.Printf("y = %s (%s, prec = %d, acc = %s)\n", &y, y.Format('p', 0), y.Prec(), y.Acc())
fmt.Printf("z = %s (%s, prec = %d, acc = %s)\n", &z, z.Format('p', 0), z.Prec(), z.Acc())
// Output:
// x = 1000 (0x.fap10, prec = 64, acc = exact)
// y = 2.718281828 (0x.adf85458248cd8p2, prec = 53, acc = exact)
// z = 1002.718282 (0x.faadf854p10, prec = 32, acc = below)
// x = 1000 (0x.fap10, prec = 64, acc = Exact)
// y = 2.718281828 (0x.adf85458248cd8p2, prec = 53, acc = Exact)
// z = 1002.718282 (0x.faadf854p10, prec = 32, acc = Below)
}
func Example_Shift() {
......
// generated by stringer -type=RoundingMode; DO NOT EDIT
package big
import "fmt"
const _RoundingMode_name = "ToNearestEvenToNearestAwayToZeroAwayFromZeroToNegativeInfToPositiveInf"
var _RoundingMode_index = [...]uint8{0, 13, 26, 32, 44, 57, 70}
func (i RoundingMode) String() string {
if i+1 >= RoundingMode(len(_RoundingMode_index)) {
return fmt.Sprintf("RoundingMode(%d)", i)
}
return _RoundingMode_name[_RoundingMode_index[i]:_RoundingMode_index[i+1]]
}
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