diff options
Diffstat (limited to 'src/pkg/math/big/rat.go')
-rw-r--r-- | src/pkg/math/big/rat.go | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/pkg/math/big/rat.go b/src/pkg/math/big/rat.go index 7faee61a4..f0973b390 100644 --- a/src/pkg/math/big/rat.go +++ b/src/pkg/math/big/rat.go @@ -47,7 +47,7 @@ func (z *Rat) SetFloat64(f float64) *Rat { shift := 52 - exp - // Optimisation (?): partially pre-normalise. + // Optimization (?): partially pre-normalise. for mantissa&1 == 0 && shift > 0 { mantissa >>= 1 shift-- @@ -477,7 +477,7 @@ func (z *Rat) SetString(s string) (*Rat, bool) { return z, true } -// String returns a string representation of z in the form "a/b" (even if b == 1). +// String returns a string representation of x in the form "a/b" (even if b == 1). func (x *Rat) String() string { s := "/1" if len(x.b.abs) != 0 { @@ -486,7 +486,7 @@ func (x *Rat) String() string { return x.a.String() + s } -// RatString returns a string representation of z in the form "a/b" if b != 1, +// RatString returns a string representation of x in the form "a/b" if b != 1, // and in the form "a" if b == 1. func (x *Rat) RatString() string { if x.IsInt() { @@ -495,7 +495,7 @@ func (x *Rat) RatString() string { return x.String() } -// FloatString returns a string representation of z in decimal form with prec +// FloatString returns a string representation of x in decimal form with prec // digits of precision after the decimal point and the last digit rounded. func (x *Rat) FloatString(prec int) string { if x.IsInt() { @@ -585,3 +585,16 @@ func (z *Rat) GobDecode(buf []byte) error { z.b.abs = z.b.abs.setBytes(buf[i:]) return nil } + +// MarshalText implements the encoding.TextMarshaler interface +func (r *Rat) MarshalText() (text []byte, err error) { + return []byte(r.RatString()), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface +func (r *Rat) UnmarshalText(text []byte) error { + if _, ok := r.SetString(string(text)); !ok { + return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", text) + } + return nil +} |