diff options
| author | Robert Griesemer <gri@golang.org> | 2015-03-04 17:00:41 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2015-03-12 18:41:45 +0000 |
| commit | 363617c7d3afdc9df3caf49a240679d6b6c7cc4b (patch) | |
| tree | 80524a7551907793d0cf27e671a43a021fab2733 /src/math/big/floatconv.go | |
| parent | 0ff7c3ea458dc9162333efa5ffd1ff9dccfd4fe8 (diff) | |
| download | go-363617c7d3afdc9df3caf49a240679d6b6c7cc4b.tar.xz | |
math/big: added (internal) Float.form field for easier case distinctions
This is a fairly significant _internal_ representation change. Instead
of encoding 0, finite, infinite, and NaN values with special mantissa
and exponent values, a new (1 byte) 'form' field is used (without making
the Float struct bigger). The form field permits simpler and faster
case distinctions. As a side benefit, for zero and non-finite floats,
fewer fields need to be set. Also, the exponent range is not the full
int32 range (in the old format, infExp and nanExp were used to represent
Inf and NaN values and tests for those values sometimes didn't test
for the empty mantissa, so the range was reduced by 2 values).
The correspondence between the old and new fields is as follows.
Old representation:
x neg mant exp
---------------------------------------------------------------
+/-0 sign empty 0
0 < |x| < +Inf sign mantissa exponent
+/-Inf sign empty infExp
NaN false empty nanExp
New representation (- stands for ignored fields):
x neg mant exp form
---------------------------------------------------------------
+/-0 sign - - zero
0 < |x| < +Inf sign mantissa exponent finite
+/-Inf sign - - inf
NaN - - - nan
Client should not be affected by this change.
Change-Id: I7e355894d602ceb23f9ec01da755fe6e0386b101
Reviewed-on: https://go-review.googlesource.com/6870
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/floatconv.go')
| -rw-r--r-- | src/math/big/floatconv.go | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/math/big/floatconv.go b/src/math/big/floatconv.go index 23cf948a02..a3d0ff97a8 100644 --- a/src/math/big/floatconv.go +++ b/src/math/big/floatconv.go @@ -96,11 +96,13 @@ func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) { // special-case 0 if len(z.mant) == 0 { z.acc = Exact - z.exp = 0 + z.form = zero return } // len(z.mant) > 0 + z.form = finite + // The mantissa may have a decimal point (fcount <= 0) and there // may be a nonzero exponent exp. The decimal point amounts to a // division by b**(-fcount). An exponent means multiplication by @@ -275,9 +277,13 @@ func (x *Float) bstring(buf []byte) []byte { if x.neg { buf = append(buf, '-') } - if len(x.mant) == 0 { + if x.form == zero { return append(buf, '0') } + + if debugFloat && x.form != finite { + panic("non-finite float") + } // x != 0 // adjust mantissa to use exactly x.prec bits @@ -306,9 +312,13 @@ func (x *Float) pstring(buf []byte) []byte { if x.neg { buf = append(buf, '-') } - if len(x.mant) == 0 { + if x.form == zero { return append(buf, '0') } + + if debugFloat && x.form != finite { + panic("non-finite float") + } // x != 0 // remove trailing 0 words early |
