Commit 3b9e8bb7 authored by Robert Griesemer's avatar Robert Griesemer

math/big: more documentation

Good enough for now.

Fixes #11241.

Change-Id: Ieb50809f104d20bcbe14daecac503f72486bec92
Reviewed-on: https://go-review.googlesource.com/15111Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 18563f8a
...@@ -10,25 +10,42 @@ The following numeric types are supported: ...@@ -10,25 +10,42 @@ The following numeric types are supported:
Rat rational numbers Rat rational numbers
Float floating-point numbers Float floating-point numbers
Declaration: The zero value for an Int, Rat, or Float (not the pointers The zero value for an Int, Rat, or Float correspond to 0. Thus, new
*Int, *Rat, *Float!) correspond to 0. Thus, new values can be declared values can be declared in the usual ways and denote 0 without further
in the usual ways and denote 0 without further initialization: initialization:
var x Int // &x is an *Int of value 0 var x Int // &x is an *Int of value 0
var r = &Rat{} // r is a *Rat of value 0 var r = &Rat{} // r is a *Rat of value 0
y := new(Float) // y is a *Float of value 0 y := new(Float) // y is a *Float of value 0
Arithmetic: Setters, numeric operations and predicates are represented Alternatively, new values can be allocated and initialized with factory
as methods of the form: functions of the form:
func NewT(v V) *T
For instance, NewInt(x) returns an *Int set to the value of the int64
argument x, NewRat(a, b) returns a *Rat set to the fraction a/b where
a and b are int64 values, and NewFloat(f) returns a *Float initialized
to the float64 argument f. More flexibility is provided with explicit
setters, for instance:
var z1 Int
z1.SetUint64(123) // z1 := 123
z2 := new(Rat).SetFloat64(1.2) // z2 := 6/5
z3 := new(Float).SetInt(z1) // z3 := 123.0
Setters, numeric operations and predicates are represented as methods of
the form:
func (z *T) SetV(v V) *T // z = v func (z *T) SetV(v V) *T // z = v
func (z *T) Unary(x *T) *T // z = unary x func (z *T) Unary(x *T) *T // z = unary x
func (z *T) Binary(x, y *T) *T // z = x binary y func (z *T) Binary(x, y *T) *T // z = x binary y
func (x *T) Pred() T1 // v = pred(x) func (x *T) Pred() P // p = pred(x)
with T one of Int, Rat, or Float. For unary and binary operations, the with T one of Int, Rat, or Float. For unary and binary operations, the
result is the receiver (usually named z in that case); if it is one of result is the receiver (usually named z in that case; see below); if it
the operands x or y it may be safely overwritten (and its memory reused). is one of the operands x or y it may be safely overwritten (and its memory
reused).
Arithmetic expressions are typically written as a sequence of individual Arithmetic expressions are typically written as a sequence of individual
method calls, with each call corresponding to an operation. The receiver method calls, with each call corresponding to an operation. The receiver
...@@ -45,10 +62,10 @@ aliasing of parameters, so it is perfectly ok to write ...@@ -45,10 +62,10 @@ aliasing of parameters, so it is perfectly ok to write
to accumulate values x in a sum. to accumulate values x in a sum.
Rationale: By always passing in a result value via the receiver, memory (By always passing in a result value via the receiver, memory use can be
use can be much better controlled. Instead of having to allocate new memory much better controlled. Instead of having to allocate new memory for each
for each result, an operation can reuse the space allocated for the result result, an operation can reuse the space allocated for the result value,
value, and overwrite that value with the new result in the process. and overwrite that value with the new result in the process.)
Notational convention: Incoming method parameters (including the receiver) Notational convention: Incoming method parameters (including the receiver)
are named consistently in the API to clarify their use. Incoming operands are named consistently in the API to clarify their use. Incoming operands
...@@ -68,5 +85,15 @@ Int.Sign), simply return the result. In this case, the receiver is typically ...@@ -68,5 +85,15 @@ Int.Sign), simply return the result. In this case, the receiver is typically
the first operand, named x: the first operand, named x:
func (x *Int) Sign() int func (x *Int) Sign() int
Various methods support conversions between strings and corresponding
numeric values, and vice versa: *Int, *Rat, and *Float values implement
the Stringer interface for a (default) string representation of the value,
but also provide SetString methods to initialize a value from a string in
a variety of supported formats (see the respective SetString documentation).
Finally, *Int, *Rat, and *Float satisfy the fmt package's Scanner interface
for scanning and (except for *Rat) the Formatter interface for formatted
printing.
*/ */
package big package big
...@@ -567,7 +567,6 @@ func TestFloatFormat(t *testing.T) { ...@@ -567,7 +567,6 @@ func TestFloatFormat(t *testing.T) {
{"%v", -1e-9, "-1e-09"}, {"%v", -1e-9, "-1e-09"},
{"%v", float32(-1e-9), "-1e-09"}, {"%v", float32(-1e-9), "-1e-09"},
{"%010v", 0.0, "0000000000"}, {"%010v", 0.0, "0000000000"},
{"%010v", 0.0, "0000000000"},
// *Float cases // *Float cases
{"%.20f", "1e-20", "0.00000000000000000001"}, {"%.20f", "1e-20", "0.00000000000000000001"},
......
...@@ -46,6 +46,7 @@ func (x *Float) Text(format byte, prec int) string { ...@@ -46,6 +46,7 @@ func (x *Float) Text(format byte, prec int) string {
} }
// String formats x like x.Text('g', 10). // String formats x like x.Text('g', 10).
// (String must be called explicitly, Float.Format does not support %s verb.)
func (x *Float) String() string { func (x *Float) String() string {
return x.Text('g', 10) return x.Text('g', 10)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment