diff options
| author | Robert Griesemer <gri@golang.org> | 2015-02-05 17:21:48 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2015-02-06 17:21:01 +0000 |
| commit | 15594df6b4e913d1ed9d7b38fa71868be28e9b63 (patch) | |
| tree | ba08ef6bf4aa3719f91bebed379790f0634a15a7 /src/math/big/floatconv.go | |
| parent | 9b6ccb13233f2977c74c73ae836212c55d342d28 (diff) | |
| download | go-15594df6b4e913d1ed9d7b38fa71868be28e9b63.tar.xz | |
math/big: handling of +/-Inf and zero precision, enable zero values
- clarified representation of +/-Inf
- only 0 and Inf values can have 0 precision
- a zero precision value used as result value takes the max precision
of the arguments (to be fine-tuned for setters)
- the zero precision approach makes Float zero values possible
(they represent +0)
- more tests
Missing: Filling in the blanks. More tests.
Change-Id: Ibb4f97e12e1f356c3085ce80f3464e97b82ac130
Reviewed-on: https://go-review.googlesource.com/4000
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/floatconv.go')
| -rw-r--r-- | src/math/big/floatconv.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/math/big/floatconv.go b/src/math/big/floatconv.go index 7628e77d9a..e3611b234b 100644 --- a/src/math/big/floatconv.go +++ b/src/math/big/floatconv.go @@ -191,13 +191,26 @@ func (x *Float) Format(format byte, prec int) string { // Append appends the string form of the floating-point number x, // as generated by x.Format, to buf and returns the extended buffer. func (x *Float) Append(buf []byte, format byte, prec int) []byte { - // pick off simple cases + // TODO(gri) factor out handling of sign? + + // Inf + if x.IsInf(0) { + var ch byte = '+' + if x.neg { + ch = '-' + } + buf = append(buf, ch) + return append(buf, "Inf"...) + } + + // easy formats switch format { case 'b': return x.bstring(buf) case 'p': return x.pstring(buf) } + return x.bigFtoa(buf, format, prec) } @@ -212,7 +225,6 @@ func (x *Float) String() string { // The mantissa is normalized such that is uses x.Precision() bits in binary // representation. func (x *Float) bstring(buf []byte) []byte { - // TODO(gri) handle Inf if x.neg { buf = append(buf, '-') } @@ -240,7 +252,6 @@ func (x *Float) bstring(buf []byte) []byte { // ad returns the extended buffer. // The mantissa is normalized such that 0.5 <= 0.mantissa < 1.0. func (x *Float) pstring(buf []byte) []byte { - // TODO(gri) handle Inf if x.neg { buf = append(buf, '-') } |
