diff options
| author | Emmanuel Odeke <emm.odeke@gmail.com> | 2016-10-09 22:45:17 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2016-10-19 03:25:30 +0000 |
| commit | f36e1adaa2c72d74dc669b596ea1c4df5e938def (patch) | |
| tree | 3404c0d617f987a68e2d7251aea48e974b9b7726 /src/math/big/floatconv.go | |
| parent | ead08e91f6468ab1c35c250ec487935103c580f6 (diff) | |
| download | go-f36e1adaa2c72d74dc669b596ea1c4df5e938def.tar.xz | |
math/big: implement Float.Scan, type assert fmt interfaces to enforce docs
Implements Float.Scan which satisfies fmt.Scanner interface.
Also enforces docs' interface implementation claims with compile time
type assertions, that is:
+ Float always implements fmt.Formatter and fmt.Scanner
+ Int always implements fmt.Formatter and fmt.Scanner
+ Rat always implements fmt.Formatter
which will ensure that the API claims are strictly matched.
Also note that Float.Scan doesn't handle ±Inf.
Fixes #17391
Change-Id: I3d3dfbe7f602066975c7a7794fe25b4c645440ce
Reviewed-on: https://go-review.googlesource.com/30723
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/math/big/floatconv.go')
| -rw-r--r-- | src/math/big/floatconv.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/math/big/floatconv.go b/src/math/big/floatconv.go index 4ba03bc105..186dfe4a6e 100644 --- a/src/math/big/floatconv.go +++ b/src/math/big/floatconv.go @@ -12,6 +12,8 @@ import ( "strings" ) +var floatZero Float + // SetString sets z to the value of s and returns z and a boolean indicating // success. s must be a floating-point number of the same format as accepted // by Parse, with base argument 0. The entire string (not just a prefix) must @@ -276,3 +278,16 @@ func (z *Float) Parse(s string, base int) (f *Float, b int, err error) { func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b int, err error) { return new(Float).SetPrec(prec).SetMode(mode).Parse(s, base) } + +var _ fmt.Scanner = &floatZero // *Float must implement fmt.Scanner + +// Scan is a support routine for fmt.Scanner; it sets z to the value of +// the scanned number. It accepts formats whose verbs are supported by +// fmt.Scan for floating point values, which are: +// 'b' (binary), 'e', 'E', 'f', 'F', 'g' and 'G'. +// Scan doesn't handle ±Inf. +func (z *Float) Scan(s fmt.ScanState, ch rune) error { + s.SkipSpace() + _, _, err := z.scan(byteReader{s}, 0) + return err +} |
