aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/float.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-02-13 13:44:39 -0800
committerRobert Griesemer <gri@golang.org>2015-02-14 00:42:40 +0000
commitdf218d33935e1d5d4e535080e01cd83e6a508efa (patch)
treeb12bbefade37985963074e5542ae9291a2c235c2 /src/math/big/float.go
parent31e852402fdf36ccc9fd84436d082e960b755cd3 (diff)
downloadgo-df218d33935e1d5d4e535080e01cd83e6a508efa.tar.xz
math/big: implement/rename accessors for precision and rounding mode
Also: remove NewFloat - not needed anymore. Work-around for places where has been used so far: NewFloat(x, prec, mode) === new(Float).SetMode(mode).SetPrec(prec).SetFloat64(x) However, if mode == ToNearestEven, SetMode is not needed. SetPrec is needed if the default precision (53 after SetFloat64) is not adequate. TBR adonovan Change-Id: Ifda12c479ba157f2dea306c32b47c7afbf31e759 Reviewed-on: https://go-review.googlesource.com/4842 Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/math/big/float.go')
-rw-r--r--src/math/big/float.go55
1 files changed, 29 insertions, 26 deletions
diff --git a/src/math/big/float.go b/src/math/big/float.go
index 32e320a140..971a9e5c56 100644
--- a/src/math/big/float.go
+++ b/src/math/big/float.go
@@ -29,7 +29,7 @@ const debugFloat = true // enable for debugging
//
// Each Float value also has a precision, rounding mode, and accuracy.
//
-// The precision is the (maximum) number of mantissa bits available to
+// The precision is the maximum number of mantissa bits available to
// represent the value. The rounding mode specifies how a result should
// be rounded to fit into the mantissa bits, and accuracy describes the
// rounding error with respect to the exact result.
@@ -39,8 +39,10 @@ const debugFloat = true // enable for debugging
// and according to its rounding mode, unless specified otherwise. If the
// result precision is 0 (see below), it is set to the precision of the
// argument with the largest precision value before any rounding takes
-// place.
-// TODO(gri) should the rounding mode also be copied in this case?
+// place. The rounding mode remains unchanged, thus uninitialized Floats
+// provided as result arguments will "inherit" a reasonble precision from
+// the incoming arguments and their mode is the zero value for RoundingMode
+// (ToNearestEven).
//
// By setting the desired precision to 24 or 53 and using ToNearestEven
// rounding, Float operations produce the same results as the corresponding
@@ -69,24 +71,6 @@ type Float struct {
// of the the Word size _W, x.mant[0] has trailing zero bits. Zero and Inf
// values have an empty mantissa and a 0 or infExp exponent, respectively.
-// NewFloat returns a new Float with value x rounded
-// to prec bits according to the given rounding mode.
-// If prec == 0, the result has value 0.0 independent
-// of the value of x.
-// BUG(gri) For prec == 0 and x == Inf, the result
-// should be Inf as well.
-// TODO(gri) rethink this signature.
-func NewFloat(x float64, prec uint, mode RoundingMode) *Float {
- var z Float
- if prec > 0 {
- // TODO(gri) should make this more efficient
- z.SetFloat64(x)
- return z.Round(&z, prec, mode)
- }
- z.mode = mode // TODO(gri) don't do this twice for prec > 0
- return &z
-}
-
const (
MaxExp = math.MaxInt32 // largest supported exponent magnitude
infExp = -MaxExp - 1 // exponent for Inf values
@@ -158,14 +142,33 @@ func (mode RoundingMode) String() string {
panic("unreachable")
}
-// Precision returns the mantissa precision of x in bits.
-// The precision may be 0 for |x| == 0 or |x| == Inf.
-func (x *Float) Precision() uint {
+// SetPrec sets z's precision to prec and returns the (possibly) rounded
+// value of z. Rounding occurs according to z's rounding mode if the mantissa
+// cannot be represented in prec bits without loss of precision.
+func (z *Float) SetPrec(prec uint) *Float {
+ old := z.prec
+ z.prec = prec
+ if prec < old {
+ z.round(0)
+ }
+ return z
+}
+
+// SetMode sets z's rounding mode to mode and returns z.
+// z remains unchanged otherwise.
+func (z *Float) SetMode(mode RoundingMode) *Float {
+ z.mode = mode
+ return z
+}
+
+// Prec returns the mantissa precision of x in bits.
+// The result may be 0 for |x| == 0 or |x| == Inf.
+func (x *Float) Prec() uint {
return uint(x.prec)
}
-// Accuracy returns the accuracy of x produced by the most recent operation.
-func (x *Float) Accuracy() Accuracy {
+// Acc returns the accuracy of x produced by the most recent operation.
+func (x *Float) Acc() Accuracy {
return x.acc
}