Documentation ¶
Overview ¶
Package goarith implements general numeric arithmetic.
Index ¶
- Constants
- type BigInt
- func (a *BigInt) Add(b Number) Number
- func (a *BigInt) Cmp(b Number) int
- func (a *BigInt) Int() (int, bool)
- func (a *BigInt) Mul(b Number) Number
- func (a *BigInt) QuoRem(b Number) (Number, Number)
- func (a *BigInt) RQuo(b Number) Float64
- func (a *BigInt) String() string
- func (a *BigInt) Sub(b Number) Number
- type Float64
- func (a Float64) Add(b Number) Number
- func (a Float64) Cmp(b Number) int
- func (a Float64) Int() (int, bool)
- func (a Float64) Mul(b Number) Number
- func (a Float64) QuoRem(b Number) (Number, Number)
- func (a Float64) RQuo(b Number) Float64
- func (a Float64) String() string
- func (a Float64) Sub(b Number) Number
- type Int32
- type Int64
- type Number
Examples ¶
Constants ¶
View Source
const ( MaxInt = (1<<bits.UintSize)/2 - 1 MinInt = (1 << bits.UintSize) / -2 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BigInt ¶
*BigInt implements Number.
Example (Factorial) ¶
a := AsNumber(1) for i := Int64(2); i <= 40; i++ { a = a.Mul(i) if i%10 == 0 { fmt.Printf("%3d %T \t%s\n", i, a, a.String()) } }
Output: 10 goarith.Int32 3628800 20 goarith.Int64 2432902008176640000 30 *goarith.BigInt 265252859812191058636308480000000 40 *goarith.BigInt 815915283247897734345611269596115894272000000000
Example (Fibonacci) ¶
var a Number = Int32(0) var b Number = Int32(1) for i := 0; i <= 100; i++ { if i%10 == 0 { fmt.Printf("%3d %T \t%s\n", i, a, a.String()) } a = a.Add(b) a, b = b, a }
Output: 0 goarith.Int32 0 10 goarith.Int32 55 20 goarith.Int32 6765 30 goarith.Int32 832040 40 goarith.Int32 102334155 50 goarith.Int64 12586269025 60 goarith.Int64 1548008755920 70 goarith.Int64 190392490709135 80 goarith.Int64 23416728348467685 90 goarith.Int64 2880067194370816120 100 *goarith.BigInt 354224848179261915075
func (*BigInt) Int ¶ added in v0.2.0
Example ¶
x := big.NewInt(100) a := (*BigInt)(x) i, b := a.Int() fmt.Printf("%d %t\n", i, b) x.SetString("123456789012345678901234567890", 0) i, b = a.Int() fmt.Printf("%t %t\n", i == MaxInt, b)
Output: 100 true true false
func (*BigInt) QuoRem ¶
Example ¶
q, r := (*BigInt)(big.NewInt(13)).QuoRem(Int64(4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = (*BigInt)(big.NewInt(-13)).QuoRem(Int64(4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = (*BigInt)(big.NewInt(13)).QuoRem(Int64(-4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
Output: goarith.Int32 3, goarith.Int32 1 goarith.Int32 -3, goarith.Int32 -1 goarith.Int32 -3, goarith.Int32 1
type Float64 ¶
type Float64 float64
Float64 implements Number.
func (Float64) Int ¶ added in v0.2.0
Example ¶
a := Float64(1.234) i, b := a.Int() fmt.Printf("%d %t", i, b)
Output: 1 false
func (Float64) QuoRem ¶
Example ¶
q, r := Float64(13).QuoRem(Float64(4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = Float64(-13).QuoRem(Float64(4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = Float64(13).QuoRem(Float64(-4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = Float64(13.4).QuoRem(Float64(1)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = Float64(-13.4).QuoRem(Float64(1)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
Output: goarith.Int32 3, goarith.Float64 1.0 goarith.Int32 -3, goarith.Float64 -1.0 goarith.Int32 -3, goarith.Float64 1.0 goarith.Int32 13, goarith.Float64 0.40000000000000036 goarith.Int32 -13, goarith.Float64 -0.40000000000000036
type Int64 ¶
type Int64 int64
Int64 implements Number.
func (Int64) QuoRem ¶
Example ¶
q, r := Int64(13).QuoRem(Int64(4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = Int64(-13).QuoRem(Int64(4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String()) q, r = Int64(13).QuoRem(Int64(-4)) fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
Output: goarith.Int32 3, goarith.Int32 1 goarith.Int32 -3, goarith.Int32 -1 goarith.Int32 -3, goarith.Int32 1
type Number ¶
type Number interface { // String returns a string representation of the number. String() string // Int returns the int value for this and a bool indicating whether // the int value represents this exactly. Int() (i int, exact bool) // Add adds this and b (i.e. it return this + b). Add(b Number) Number // Sub subtracts b from this (i.e. it returns this - b). Sub(b Number) Number // Cmp compares this and b and returns: // // -1 if this < b // 0 if this == b // 1 if this > b // Cmp(b Number) int // Mul multiplies this by b (i.e. it returns this * b). Mul(b Number) Number // RQuo returns the rounded quotient of this and b. RQuo(b Number) Float64 // QuoRem returns the quotient and the remainder of this and b. // The quotient will be an Int32, Int64 or BigInt. QuoRem(b Number) (quotient Number, remainder Number) }
Number is a general numeric type.
func AsNumber ¶
func AsNumber(a interface{}) Number
AsNumber converts a numeric value into a Number. The numeric value may be int32, int64, int, float32, float64 or *big.Int. For Int32, Int64, Float64 and *BigInt, it behaves as an identity function. For the other types, it returns nil.
Example ¶
a := AsNumber(2147483647) fmt.Printf("%T %s\n", a, a.String()) a = AsNumber(2147483648) fmt.Printf("%T %s\n", a, a.String()) a = AsNumber(-2147483648) fmt.Printf("%T %s\n", a, a.String()) a = AsNumber(-2147483649) fmt.Printf("%T %s\n", a, a.String())
Output: goarith.Int32 2147483647 goarith.Int64 2147483648 goarith.Int32 -2147483648 goarith.Int64 -2147483649
Click to show internal directories.
Click to hide internal directories.