Documentation
¶
Overview ¶
Package xmath provides some extended capabilities according to GoLang's math.
Example (Example1) ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { fmt.Printf("Pi is %v\n", math.Pi) fmt.Printf("Square root of 2 (Sqrt2) is %v\n", math.Sqrt2) fmt.Printf("floor of Pi with precision 4: %v\n", xmath.FloorP(math.Pi, 4)) fmt.Printf("ceil of Pi with precision 4: %v\n", xmath.CeilP(math.Pi, 4)) fmt.Printf("round of Pi with precision 4: %v\n", xmath.RoundP(math.Pi, 4)) fmt.Printf("round of Pi with precision 2: %v\n", xmath.RoundP(math.Pi, 2)) fmt.Printf("round of Pi: %v\n", xmath.Round(math.Pi)) fmt.Printf("max of Pi and Sqrt2 is %v\n", xmath.Max(math.Pi, math.Sqrt2)) fmt.Printf("min of Pi and Sqrt2 is %v\n", xmath.Min(math.Pi, math.Sqrt2)) max, min := xmath.MaxMin(math.Pi, math.Sqrt2) fmt.Printf("Pi and Sqrt2: max=%v min=%v\n", max, min) fmt.Printf("is 2.4 between 3 and 2.4: %t\n", xmath.Between(2.4, 3, 2.4)) fmt.Printf("is 2.6 between 3 and 2.4: %t\n", xmath.Between(2.6, 3, 2.4)) fmt.Printf("is 3 between 3 and 2.4: %t\n", xmath.Between(3, 3, 2.4)) fmt.Printf("is 2.4 between in 3 and 2.4: %t\n", xmath.BetweenIn(2.4, 3, 2.4)) fmt.Printf("is 2.6 between in 3 and 2.4: %t\n", xmath.BetweenIn(2.6, 3, 2.4)) fmt.Printf("is 3 between in 3 and 2.4: %t\n", xmath.BetweenIn(3, 3, 2.4)) }
Output: Pi is 3.141592653589793 Square root of 2 (Sqrt2) is 1.4142135623730951 floor of Pi with precision 4: 3.1415 ceil of Pi with precision 4: 3.1416 round of Pi with precision 4: 3.1416 round of Pi with precision 2: 3.14 round of Pi: 3 max of Pi and Sqrt2 is 3.141592653589793 min of Pi and Sqrt2 is 1.4142135623730951 Pi and Sqrt2: max=3.141592653589793 min=1.4142135623730951 is 2.4 between 3 and 2.4: false is 2.6 between 3 and 2.4: true is 3 between 3 and 2.4: false is 2.4 between in 3 and 2.4: true is 2.6 between in 3 and 2.4: true is 3 between in 3 and 2.4: true
Index ¶
- Constants
- Variables
- func AlmostEqual(x ...float64) bool
- func AlmostEqual32(x ...float32) bool
- func AlmostEqual64(x ...float64) bool
- func AlmostEqualD(d float64, x ...float64) bool
- func AlmostEqualD32(d float32, x ...float32) bool
- func AlmostEqualD64(d float64, x ...float64) bool
- func AlmostEqualP(p uint64, x ...float64) bool
- func AlmostEqualP32(p uint32, x ...float32) bool
- func AlmostEqualP64(p uint64, x ...float64) bool
- func Avg(x ...float64) (avg float64)
- func AvgInt(x ...int64) (avg float64)
- func AvgInt2(x ...int64) (avg int64, overflow bool)
- func AvgUint(x ...uint64) (avg float64)
- func AvgUint2(x ...uint64) (avg uint64, overflow bool)
- func Between(x float64, a, b float64) bool
- func BetweenIn(x float64, a, b float64) bool
- func CeilBigFloat(x *big.Float) *big.Int
- func CeilBigRat(x *big.Rat) *big.Int
- func CeilP(x float64, prec int) float64
- func CeilPB(x float64, prec int, base int) float64
- func CryptoRand() float64
- func CryptoRandCode(n int) int64
- func CryptoRandFloat() float64
- func CryptoRandInt(max int64) int64
- func Equal(x ...float64) bool
- func Equal32(x ...float32) bool
- func Equal64(x ...float64) bool
- func FloorBigFloat(x *big.Float) *big.Int
- func FloorBigRat(x *big.Rat) *big.Int
- func FloorP(x float64, prec int) float64
- func FloorPB(x float64, prec int, base int) float64
- func Int64BigInt(x *big.Int) (int64, big.Accuracy)
- func Int64BigRat(x *big.Rat) (int64, big.Accuracy)
- func IntBigRat(x *big.Rat) (*big.Int, big.Accuracy)
- func IsZero(x float64) bool
- func Max(x ...float64) float64
- func MaxInt(x ...int64) int64
- func MaxMin(x ...float64) (max float64, min float64)
- func MaxMinInt(x ...int64) (max int64, min int64)
- func MaxMinUint(x ...uint64) (max uint64, min uint64)
- func MaxUint(x ...uint64) uint64
- func Min(x ...float64) float64
- func MinInt(x ...int64) int64
- func MinMax(x ...float64) (min float64, max float64)
- func MinMaxInt(x ...int64) (min int64, max int64)
- func MinMaxUint(x ...uint64) (min uint64, max uint64)
- func MinUint(x ...uint64) uint64
- func Round(x float64) float64
- func RoundBigFloat(x *big.Float) *big.Int
- func RoundBigRat(x *big.Rat) *big.Int
- func RoundP(x float64, prec int) float64
- func RoundPB(x float64, prec int, base int) float64
- func SafeDiv(x, y float64, allowNaN bool) float64
- func Sign(x float64) int
- func SignInt(x int64) int
- func Sum(x ...float64) (sum float64)
- func SumInt(x ...int64) (sum float64)
- func SumInt2(x ...int64) (sum int64, overflow bool)
- func SumUint(x ...uint64) (sum float64)
- func SumUint2(x ...uint64) (sum uint64, overflow bool)
- func Uint64BigInt(x *big.Int) (uint64, big.Accuracy)
- func Uint64BigRat(x *big.Rat) (uint64, big.Accuracy)
- func Zero(sign int) float64
- type Real
- func (z *Real) Abs(x *Real) *Real
- func (x *Real) Acc() big.Accuracy
- func (z *Real) Add(x, y *Real) *Real
- func (x *Real) Append(buf []byte, fmt byte, prec int) []byte
- func (x *Real) Base() int
- func (x *Real) Cmp(y *Real) int
- func (z *Real) Copy(x *Real) *Real
- func (x *Real) Float() *big.Float
- func (x *Real) Float32() (float32, big.Accuracy)
- func (x *Real) Float64() (float64, big.Accuracy)
- func (x *Real) FloatMinPrec() uint
- func (x *Real) FloatMode() big.RoundingMode
- func (x *Real) FloatPrec() uint
- func (x *Real) Format(s fmt.State, format rune)
- func (z *Real) GobDecode(buf []byte) error
- func (x *Real) GobEncode() ([]byte, error)
- func (x *Real) Int(z *big.Int) (*big.Int, big.Accuracy)
- func (x *Real) Int64() (int64, big.Accuracy)
- func (x *Real) IsInf() bool
- func (x *Real) IsInt() bool
- func (x *Real) MantExp(mant *Real) (exp int)
- func (x *Real) MarshalText() (text []byte, err error)
- func (z *Real) Mul(x, y *Real) *Real
- func (z *Real) Neg(x *Real) *Real
- func (z *Real) Parse(s string, base int) (r *Real, b int, err error)
- func (x *Real) Prec() int
- func (z *Real) Quo(x, y *Real) *Real
- func (x *Real) Rat(z *big.Rat) (*big.Rat, big.Accuracy)
- func (z *Real) Scan(s fmt.ScanState, ch rune) error
- func (z *Real) Set(x *Real) *Real
- func (z *Real) SetFloat(x *big.Float) *Real
- func (z *Real) SetFloat64(x float64) *Real
- func (z *Real) SetFloatMode(mode big.RoundingMode) *Real
- func (z *Real) SetFloatPrec(prec uint) *Real
- func (z *Real) SetInf(signbit bool) *Real
- func (z *Real) SetInt(x *big.Int) *Real
- func (z *Real) SetInt64(x int64) *Real
- func (z *Real) SetMantExp(mant *Real, exp int) *Real
- func (z *Real) SetRat(x *big.Rat) *Real
- func (z *Real) SetString(s string) (r *Real, ok bool)
- func (z *Real) SetUint64(x uint64) *Real
- func (x *Real) Sign() int
- func (x *Real) Signbit() bool
- func (z *Real) Sqrt(x *Real) *Real
- func (x *Real) String() string
- func (z *Real) Sub(x, y *Real) *Real
- func (x *Real) Text(format byte, prec int) string
- func (x *Real) Uint64() (uint64, big.Accuracy)
- func (z *Real) UnmarshalText(text []byte) error
- type Stepper
Examples ¶
- Package (Example1)
- AlmostEqual
- AlmostEqual32
- AlmostEqual64
- AlmostEqualD
- AlmostEqualD32
- AlmostEqualD64
- Avg
- AvgInt
- AvgInt2
- AvgUint
- AvgUint2
- Between
- BetweenIn
- CeilBigFloat
- CeilBigRat
- CeilP
- Equal
- Equal32
- Equal64
- FloorBigFloat
- FloorBigRat
- FloorP
- Int64BigInt
- Int64BigRat
- IntBigRat
- IsZero
- MaxMin
- MaxMinInt
- MaxMinUint
- NewDecimal
- NewStepper
- Real
- RoundBigFloat
- RoundBigRat
- RoundP
- SafeDiv
- Sign
- SignInt
- Stepper.Normalize
- Stepper.Step
- Stepper.Step (Inf)
- Stepper.Step (Inf_max)
- Stepper.Step (Inf_min)
- Sum
- SumInt
- SumInt2
- SumUint
- SumUint2
- Uint64BigInt
- Uint64BigRat
- Zero
Constants ¶
const ( MinBase = 2 MaxBase = 36 MaxInt8Value = 1<<7 - 1 MinInt8Value = -1 << 7 MaxInt16Value = 1<<15 - 1 MinInt16Value = -1 << 15 MaxInt32Value = 1<<31 - 1 MinInt32Value = -1 << 31 MaxInt64Value = 1<<63 - 1 MinInt64Value = -1 << 63 MaxUint8Value = 1<<8 - 1 MaxUint16Value = 1<<16 - 1 MaxUint32Value = 1<<32 - 1 MaxUint64Value = 1<<64 - 1 MaxIntValue = 1<<(UintSize-1) - 1 MinIntValue = -1 << (UintSize - 1) MaxUintValue = 1<<UintSize - 1 UintSize = 32 << (^uint(0) >> 32 & 1) )
Variables ¶
var ( ErrStepperStepOverflow = errors.New("step overflow") ErrStepperMaxOverflow = errors.New("max overflow") ErrStepperMinOverflow = errors.New("min overflow") ErrStepperRangeOverflow = errors.New("range overflow") ErrStepperUnorderedMaxMin = errors.New("unordered max min") ErrStepperMaxExceeded = errors.New("max exceeded") ErrStepperMinExceeded = errors.New("min exceeded") )
Functions ¶
func AlmostEqual ¶ added in v1.6.0
AlmostEqual is synonym with AlmostEqualP(1, x...).
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float64 a, b = 1000.0, 2.00000004 x, y := a+b+b, b+b+a fmt.Printf("is AlmostEqual synonym with AlmostEqual64: %t\n", xmath.AlmostEqual(x, y) == xmath.AlmostEqual64(x, y)) }
Output: is AlmostEqual synonym with AlmostEqual64: true
func AlmostEqual32 ¶ added in v1.5.0
AlmostEqual32 is synonym with AlmostEqualP32(1, x...).
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float32 a, b = 1000.0, 2.4 x, y := a+b+b, b+b+a fmt.Printf("a=%v b=%v\na+b+b=%v\nb+b+a=%v\n", a, b, x, y) fmt.Printf("are %v and %v equal: %t\n", x, y, x == y) fmt.Printf("are %v and %v almost equal: %t\n", x, y, xmath.AlmostEqual32(x, y)) }
Output: a=1000 b=2.4 a+b+b=1004.80005 b+b+a=1004.8 are 1004.80005 and 1004.8 equal: false are 1004.80005 and 1004.8 almost equal: true
func AlmostEqual64 ¶ added in v1.5.0
AlmostEqual64 is synonym with AlmostEqualP64(1, x...).
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float64 a, b = 1000.0, 2.00000004 x, y := a+b+b, b+b+a fmt.Printf("a=%v b=%v\na+b+b=%v\nb+b+a=%v\n", a, b, x, y) fmt.Printf("are %v and %v equal: %t\n", x, y, x == y) fmt.Printf("are %v and %v almost equal: %t\n", x, y, xmath.AlmostEqual64(x, y)) }
Output: a=1000 b=2.00000004 a+b+b=1004.0000000800001 b+b+a=1004.00000008 are 1004.0000000800001 and 1004.00000008 equal: false are 1004.0000000800001 and 1004.00000008 almost equal: true
func AlmostEqualD ¶ added in v1.6.0
AlmostEqualD is synonym with AlmostEqualD64.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float64 a, b = 1000.0, 2.00000004 x, y := a+b+b, b+b+a fmt.Printf("is AlmostEqualD synonym with AlmostEqualD64: %t\n", xmath.AlmostEqualD(x, y) == xmath.AlmostEqualD64(x, y)) }
Output: is AlmostEqualD synonym with AlmostEqualD64: true
func AlmostEqualD32 ¶ added in v1.6.0
AlmostEqualD32 checks almost equality of all given 32-bit floating points values. Argument d is the least difference value of inequality. If d is 0, it checks exact equality. It returns true if all values are almost equal.
Special cases are:
AlmostEqualD32(d) = false AlmostEqualD32(d, x) = true AlmostEqualD32(d, NaN) = false AlmostEqualD32(d, NaN, x) = false AlmostEqualD32(d, x, NaN) = false AlmostEqualD32(d, +Inf, +Inf) = true AlmostEqualD32(d, -Inf, -Inf) = true
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float32 a, b = 1000.0, 2.4 x, y := a+b+b, b+b+a fmt.Printf("a=%v b=%v\na+b+b=%v\nb+b+a=%v\n", a, b, x, y) fmt.Printf("are %v and %v equal: %t\n", x, y, x == y) fmt.Printf("are %v and %v almost equal: %t\n", x, y, xmath.AlmostEqualD32(0.1, x, y)) }
Output: a=1000 b=2.4 a+b+b=1004.80005 b+b+a=1004.8 are 1004.80005 and 1004.8 equal: false are 1004.80005 and 1004.8 almost equal: true
func AlmostEqualD64 ¶ added in v1.6.0
AlmostEqualD64 checks almost equality of all given 64-bit floating points values. Argument d is the least difference value of inequality. If d is 0, it checks exact equality. It returns true if all values are almost equal.
Special cases are:
AlmostEqualD64(d) = false AlmostEqualD64(d, x) = true AlmostEqualD64(d, NaN) = false AlmostEqualD64(d, NaN, x) = false AlmostEqualD64(d, x, NaN) = false AlmostEqualD64(d, +Inf, +Inf) = true AlmostEqualD64(d, -Inf, -Inf) = true
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float64 a, b = 1000.0, 2.00000004 x, y := a+b+b, b+b+a fmt.Printf("a=%v b=%v\na+b+b=%v\nb+b+a=%v\n", a, b, x, y) fmt.Printf("are %v and %v equal: %t\n", x, y, x == y) fmt.Printf("are %v and %v almost equal: %t\n", x, y, xmath.AlmostEqualD64(0.00000001, x, y)) }
Output: a=1000 b=2.00000004 a+b+b=1004.0000000800001 b+b+a=1004.00000008 are 1004.0000000800001 and 1004.00000008 equal: false are 1004.0000000800001 and 1004.00000008 almost equal: true
func AlmostEqualP ¶ added in v1.6.0
AlmostEqualP is synonym with AlmostEqualP64.
func AlmostEqualP32 ¶ added in v1.6.0
AlmostEqualP32 checks almost equality of all given 32-bit floating points values. Argument p is measure of precision. If p is 0, it checks exact equality. It returns true if all values are almost equal.
Special cases are:
AlmostEqualP32(p) = false AlmostEqualP32(p, x) = true AlmostEqualP32(p, NaN) = false AlmostEqualP32(p, NaN, x) = false AlmostEqualP32(p, x, NaN) = false AlmostEqualP32(p, +Inf, +Inf) = true AlmostEqualP32(p, -Inf, -Inf) = true
func AlmostEqualP64 ¶ added in v1.6.0
AlmostEqualP64 checks almost equality of all given 64-bit floating points values. Argument p is measure of precision. If p is 0, it checks exact equality. It returns true if all values are almost equal.
Special cases are:
AlmostEqualP64(p) = false AlmostEqualP64(p, x) = true AlmostEqualP64(p, NaN) = false AlmostEqualP64(p, NaN, x) = false AlmostEqualP64(p, x, NaN) = false AlmostEqualP64(p, +Inf, +Inf) = true AlmostEqualP64(p, -Inf, -Inf) = true
func Avg ¶ added in v1.7.0
Avg returns the arithmetic mean of x...
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { x := []float64{-5.6, 2.1, 4.5, -10.9, -3.4} fmt.Printf("avg of %v: %v\n", x, xmath.Avg(x...)) }
Output: avg of [-5.6 2.1 4.5 -10.9 -3.4]: -2.66
func AvgInt ¶ added in v1.7.0
AvgInt returns the floating point of arithmetic mean of x...
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { x := []int64{-5, 2, 4, -10, -3} fmt.Printf("avg of integers %v: %v\n", x, xmath.AvgInt(x...)) }
Output: avg of integers [-5 2 4 -10 -3]: -2.4
func AvgInt2 ¶ added in v1.7.0
AvgInt2 returns the arithmetic mean of x... If the result overflows, it returns overflow is true.
Example ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { x := []int64{-5, 2, 4, -10, -3} r, o := xmath.AvgInt2(x...) fmt.Printf("avg of integers %v: %v overflow %v\n", x, r, o) x = []int64{5, math.MaxInt64, -2} r, o = xmath.AvgInt2(x...) fmt.Printf("avg of integers %v: %v overflow %v\n", x, r, o) x = []int64{-7, math.MinInt64, 1} r, o = xmath.AvgInt2(x...) fmt.Printf("avg of integers %v: %v overflow %v\n", x, r, o) }
Output: avg of integers [-5 2 4 -10 -3]: -2 overflow false avg of integers [5 9223372036854775807 -2]: -3074457345618258602 overflow true avg of integers [-7 -9223372036854775808 1]: 3074457345618258600 overflow true
func AvgUint ¶ added in v1.7.0
AvgUint returns the floating point of arithmetic mean of x...
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { x := []uint64{5, 2, 4, 10, 3} fmt.Printf("avg of unsigned integers %v: %v\n", x, xmath.AvgUint(x...)) }
Output: avg of unsigned integers [5 2 4 10 3]: 4.8
func AvgUint2 ¶ added in v1.7.0
AvgUint2 returns the arithmetic mean of x... If the result overflows, it returns overflow is true.
Example ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { x := []uint64{5, 2, 4, 10, 3} r, o := xmath.AvgUint2(x...) fmt.Printf("avg of unsigned integers %v: %v overflow %v\n", x, r, o) x = []uint64{5, math.MaxUint64} r, o = xmath.AvgUint2(x...) fmt.Printf("avg of unsigned integers %v: %v overflow %v\n", x, r, o) }
Output: avg of unsigned integers [5 2 4 10 3]: 4 overflow false avg of unsigned integers [5 18446744073709551615]: 2 overflow true
func Between ¶
Between checks x is between a and b
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var x float64 interval := []float64{+3.1416, +1.4142} x = +1.4141 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.Between(x, interval[0], interval[1])) x = +1.4142 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.Between(x, interval[0], interval[1])) x = +1.4143 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.Between(x, interval[0], interval[1])) x = +3.1415 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.Between(x, interval[0], interval[1])) x = +3.1416 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.Between(x, interval[0], interval[1])) x = +3.1417 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.Between(x, interval[0], interval[1])) }
Output: is +1.4141 between [+3.1416 +1.4142]: false is +1.4142 between [+3.1416 +1.4142]: false is +1.4143 between [+3.1416 +1.4142]: true is +3.1415 between [+3.1416 +1.4142]: true is +3.1416 between [+3.1416 +1.4142]: false is +3.1417 between [+3.1416 +1.4142]: false
func BetweenIn ¶
BetweenIn checks x is in a and b
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var x float64 interval := []float64{+3.1416, +1.4142} x = +1.4141 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.BetweenIn(x, interval[0], interval[1])) x = +1.4142 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.BetweenIn(x, interval[0], interval[1])) x = +1.4143 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.BetweenIn(x, interval[0], interval[1])) x = +3.1415 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.BetweenIn(x, interval[0], interval[1])) x = +3.1416 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.BetweenIn(x, interval[0], interval[1])) x = +3.1417 fmt.Printf("is %+6.4f between %+6.4f: %t\n", x, interval, xmath.BetweenIn(x, interval[0], interval[1])) }
Output: is +1.4141 between [+3.1416 +1.4142]: false is +1.4142 between [+3.1416 +1.4142]: true is +1.4143 between [+3.1416 +1.4142]: true is +3.1415 between [+3.1416 +1.4142]: true is +3.1416 between [+3.1416 +1.4142]: true is +3.1417 between [+3.1416 +1.4142]: false
func CeilBigFloat ¶ added in v1.6.0
CeilBigFloat returns the least integer value greater than or equal to x. It returns nil if x is an infinity.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { fmt.Printf("ceil of %v: %v\n", 2.4, xmath.CeilBigFloat(big.NewFloat(2.4))) fmt.Printf("ceil of %v: %v\n", 2.5, xmath.CeilBigFloat(big.NewFloat(2.5))) fmt.Printf("ceil of %v: %v\n", -6.5, xmath.CeilBigFloat(big.NewFloat(-6.5))) fmt.Printf("ceil of %v: %v\n", -6.6, xmath.CeilBigFloat(big.NewFloat(-6.6))) fmt.Printf("ceil of %v: %v\n", 5, xmath.CeilBigFloat(big.NewFloat(5))) fmt.Printf("ceil of %v: %v\n", -9, xmath.CeilBigFloat(big.NewFloat(-9))) }
Output: ceil of 2.4: 3 ceil of 2.5: 3 ceil of -6.5: -6 ceil of -6.6: -6 ceil of 5: 5 ceil of -9: -9
func CeilBigRat ¶ added in v1.6.0
CeilBigRat returns the least integer value greater than or equal to x.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { fmt.Printf("ceil of %v/%v: %v\n", 24, 10, xmath.CeilBigRat(big.NewRat(24, 10))) fmt.Printf("ceil of %v/%v: %v\n", 25, 10, xmath.CeilBigRat(big.NewRat(25, 10))) fmt.Printf("ceil of %v/%v: %v\n", -65, 10, xmath.CeilBigRat(big.NewRat(-65, 10))) fmt.Printf("ceil of %v/%v: %v\n", -66, 10, xmath.CeilBigRat(big.NewRat(-66, 10))) fmt.Printf("ceil of %v/%v: %v\n", 50, 10, xmath.CeilBigRat(big.NewRat(50, 10))) fmt.Printf("ceil of %v/%v: %v\n", -90, 10, xmath.CeilBigRat(big.NewRat(-90, 10))) }
Output: ceil of 24/10: 3 ceil of 25/10: 3 ceil of -65/10: -6 ceil of -66/10: -6 ceil of 50/10: 5 ceil of -90/10: -9
func CeilP ¶
CeilP returns the least value greater than or equal to x with specified decimal precision.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { fmt.Printf("ceil of +3.1416 with precision 3: %+6.4f\n", xmath.CeilP(+3.1416, 3)) fmt.Printf("ceil of -3.1416 with precision 2: %+6.4f\n", xmath.CeilP(-3.1416, 2)) }
Output: ceil of +3.1416 with precision 3: +3.1420 ceil of -3.1416 with precision 2: -3.1400
func CeilPB ¶ added in v1.6.0
CeilPB returns the least value greater than or equal to x with specified precision of base. It panics unless base is in valid range.
func CryptoRand ¶ added in v1.3.0
func CryptoRand() float64
CryptoRand is synonym with CryptoRandFloat.
func CryptoRandCode ¶ added in v1.1.0
CryptoRandCode generates random code in [10^(n-1), 10^n). It returns -1 when error occurs.
func CryptoRandFloat ¶ added in v1.1.0
func CryptoRandFloat() float64
CryptoRandFloat returns a random decimal number in [0, 1). It returns -1 when error occurs.
func CryptoRandInt ¶ added in v1.1.0
CryptoRandInt returns a random integer in [0, max). It returns -1 when error occurs.
func Equal ¶ added in v1.4.0
Equal is synonym with Equal64.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float64 a, b = 1000.0, 2.00000004 x, y := a+b+b, b+b+a fmt.Printf("is Equal synonym with Equal64: %t\n", xmath.Equal(x, y) == xmath.Equal64(x, y)) }
Output: is Equal synonym with Equal64: true
func Equal32 ¶ added in v1.6.0
Equal32 checks exact equality of all given 32-bit floating points values by comparing. It returns true if all values are equal.
Special cases are:
Equal32() = false Equal32(x) = true Equal32(NaN) = false Equal32(NaN, x) = false Equal32(x, NaN) = false Equal32(+Inf, +Inf) = true Equal32(-Inf, -Inf) = true
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float32 a, b = 1000.0, 2.4 x, y := a+b+b, b+b+a fmt.Printf("a=%v b=%v\na+b+b=%v\nb+b+a=%v\n", a, b, x, y) fmt.Printf("are %v and %v equal: %t\n", x, y, x == y) fmt.Printf("are %v and %v equal by function: %t\n", x, y, xmath.Equal32(x, y)) }
Output: a=1000 b=2.4 a+b+b=1004.80005 b+b+a=1004.8 are 1004.80005 and 1004.8 equal: false are 1004.80005 and 1004.8 equal by function: false
func Equal64 ¶ added in v1.6.0
Equal64 checks exact equality of all given 64-bit floating points values by comparing. It returns true if all values are equal.
Special cases are:
Equal64() = false Equal64(x) = true Equal64(NaN) = false Equal64(NaN, x) = false Equal64(x, NaN) = false Equal64(+Inf, +Inf) = true Equal64(-Inf, -Inf) = true
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var a, b float64 a, b = 1000.0, 2.00000004 x, y := a+b+b, b+b+a fmt.Printf("a=%v b=%v\na+b+b=%v\nb+b+a=%v\n", a, b, x, y) fmt.Printf("are %v and %v equal: %t\n", x, y, x == y) fmt.Printf("are %v and %v equal by function: %t\n", x, y, xmath.Equal64(x, y)) }
Output: a=1000 b=2.00000004 a+b+b=1004.0000000800001 b+b+a=1004.00000008 are 1004.0000000800001 and 1004.00000008 equal: false are 1004.0000000800001 and 1004.00000008 equal by function: false
func FloorBigFloat ¶ added in v1.6.0
FloorBigFloat returns the greatest integer value less than or equal to x. It returns nil if x is an infinity.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { fmt.Printf("floor of %v: %v\n", 2.4, xmath.FloorBigFloat(big.NewFloat(2.4))) fmt.Printf("floor of %v: %v\n", 2.5, xmath.FloorBigFloat(big.NewFloat(2.5))) fmt.Printf("floor of %v: %v\n", -6.5, xmath.FloorBigFloat(big.NewFloat(-6.5))) fmt.Printf("floor of %v: %v\n", -6.6, xmath.FloorBigFloat(big.NewFloat(-6.6))) fmt.Printf("floor of %v: %v\n", 5, xmath.FloorBigFloat(big.NewFloat(5))) fmt.Printf("floor of %v: %v\n", -9, xmath.FloorBigFloat(big.NewFloat(-9))) }
Output: floor of 2.4: 2 floor of 2.5: 2 floor of -6.5: -7 floor of -6.6: -7 floor of 5: 5 floor of -9: -9
func FloorBigRat ¶ added in v1.6.0
FloorBigRat returns the greatest integer value less than or equal to x.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { fmt.Printf("floor of %v/%v: %v\n", 24, 10, xmath.FloorBigRat(big.NewRat(24, 10))) fmt.Printf("floor of %v/%v: %v\n", 25, 10, xmath.FloorBigRat(big.NewRat(25, 10))) fmt.Printf("floor of %v/%v: %v\n", -65, 10, xmath.FloorBigRat(big.NewRat(-65, 10))) fmt.Printf("floor of %v/%v: %v\n", -66, 10, xmath.FloorBigRat(big.NewRat(-66, 10))) fmt.Printf("floor of %v/%v: %v\n", 50, 10, xmath.FloorBigRat(big.NewRat(50, 10))) fmt.Printf("floor of %v/%v: %v\n", -90, 10, xmath.FloorBigRat(big.NewRat(-90, 10))) }
Output: floor of 24/10: 2 floor of 25/10: 2 floor of -65/10: -7 floor of -66/10: -7 floor of 50/10: 5 floor of -90/10: -9
func FloorP ¶
FloorP returns the greatest value less than or equal to x with specified decimal precision.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { fmt.Printf("floor of +3.1416 with precision 3: %+6.4f\n", xmath.FloorP(+3.1416, 3)) fmt.Printf("floor of -3.1416 with precision 2: %+6.4f\n", xmath.FloorP(-3.1416, 2)) }
Output: floor of +3.1416 with precision 3: +3.1410 floor of -3.1416 with precision 2: -3.1500
func FloorPB ¶ added in v1.6.0
FloorPB returns the greatest value less than or equal to x with specified precision of base. It panics unless base is in valid range.
func Int64BigInt ¶ added in v1.7.0
Int64BigInt returns the integer resulting from truncating x towards zero. If math.MinInt64 <= x <= math.MaxInt64, the accuracy is big.Exact. The result is (math.MinInt64, big.Above) for x < math.MinInt64, and (math.MaxInt64, big.Below) for x > math.MaxInt64.
Example ¶
package main import ( "fmt" "math" "math/big" "github.com/goinsane/xmath" ) func main() { var x *big.Int var k int64 var acc big.Accuracy x = big.NewInt(49) k, acc = xmath.Int64BigInt(x) fmt.Printf("int64 of %v: %v acc=%v\n", x, k, acc) x = big.NewInt(-27) k, acc = xmath.Int64BigInt(x) fmt.Printf("int64 of %v: %v acc=%v\n", x, k, acc) x = big.NewInt(math.MaxInt64) k, acc = xmath.Int64BigInt(x) fmt.Printf("int64 of MaxInt64=%v: %v acc=%v\n", x, k, acc) x = new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(5)) k, acc = xmath.Int64BigInt(x) fmt.Printf("int64 of MaxInt64+5=%v: %v acc=%v\n", x, k, acc) x = big.NewInt(math.MinInt64) k, acc = xmath.Int64BigInt(x) fmt.Printf("int64 of MinInt64=%v: %v acc=%v\n", x, k, acc) x = new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(7)) k, acc = xmath.Int64BigInt(x) fmt.Printf("int64 of MinInt64-7=%v: %v acc=%v\n", x, k, acc) }
Output: int64 of 49: 49 acc=Exact int64 of -27: -27 acc=Exact int64 of MaxInt64=9223372036854775807: 9223372036854775807 acc=Exact int64 of MaxInt64+5=9223372036854775812: 9223372036854775807 acc=Below int64 of MinInt64=-9223372036854775808: -9223372036854775808 acc=Exact int64 of MinInt64-7=-9223372036854775815: -9223372036854775808 acc=Above
func Int64BigRat ¶ added in v1.6.0
Int64BigRat returns the integer resulting from truncating x towards zero. If math.MinInt64 <= x <= math.MaxInt64, the accuracy is like IntBigRat. The result is (math.MinInt64, big.Above) for x < math.MinInt64, and (math.MaxInt64, big.Below) for x > math.MaxInt64.
Example ¶
package main import ( "fmt" "math" "math/big" "github.com/goinsane/xmath" ) func main() { var k int64 var acc big.Accuracy k, acc = xmath.Int64BigRat(new(big.Rat).SetFrac(big.NewInt(50), big.NewInt(10))) fmt.Printf("int64 of 50/10: %v acc=%v\n", k, acc) k, acc = xmath.Int64BigRat(new(big.Rat).SetFrac(big.NewInt(-90), big.NewInt(10))) fmt.Printf("int64 of -90/10: %v acc=%v\n", k, acc) k, acc = xmath.Int64BigRat(new(big.Rat).SetFrac(big.NewInt(math.MaxInt64), big.NewInt(5))) fmt.Printf("int64 of MaxInt64/5: %v acc=%v\n", k, acc) k, acc = xmath.Int64BigRat(new(big.Rat).SetFrac(big.NewInt(math.MinInt64), big.NewInt(5))) fmt.Printf("int64 of MinInt64/5: %v acc=%v\n", k, acc) k, acc = xmath.Int64BigRat(new(big.Rat).SetFrac(new(big.Int).Mul(big.NewInt(7), big.NewInt(math.MaxInt64)), big.NewInt(5))) fmt.Printf("int64 of 7*MaxInt64/5: %v acc=%v\n", k, acc) k, acc = xmath.Int64BigRat(new(big.Rat).SetFrac(new(big.Int).Mul(big.NewInt(7), big.NewInt(math.MinInt64)), big.NewInt(5))) fmt.Printf("int64 of 7*MinInt64/5: %v acc=%v\n", k, acc) }
Output: int64 of 50/10: 5 acc=Exact int64 of -90/10: -9 acc=Exact int64 of MaxInt64/5: 1844674407370955161 acc=Below int64 of MinInt64/5: -1844674407370955161 acc=Above int64 of 7*MaxInt64/5: 9223372036854775807 acc=Below int64 of 7*MinInt64/5: -9223372036854775808 acc=Above
func IntBigRat ¶ added in v1.6.0
IntBigRat returns the result of truncating x towards zero. The accuracy is big.Exact if x.IsInt(); otherwise it is big.Below or big.Above.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { var n *big.Int var acc big.Accuracy n, acc = xmath.IntBigRat(big.NewRat(24, 10)) fmt.Printf("integer of %v/%v: %v acc=%v\n", 24, 10, n, acc) n, acc = xmath.IntBigRat(big.NewRat(25, 10)) fmt.Printf("integer of %v/%v: %v acc=%v\n", 25, 10, n, acc) n, acc = xmath.IntBigRat(big.NewRat(-65, 10)) fmt.Printf("integer of %v/%v: %v acc=%v\n", -65, 10, n, acc) n, acc = xmath.IntBigRat(big.NewRat(-66, 10)) fmt.Printf("integer of %v/%v: %v acc=%v\n", -66, 10, n, acc) n, acc = xmath.IntBigRat(big.NewRat(50, 10)) fmt.Printf("integer of %v/%v: %v acc=%v\n", 50, 10, n, acc) n, acc = xmath.IntBigRat(big.NewRat(-90, 10)) fmt.Printf("integer of %v/%v: %v acc=%v\n", -90, 10, n, acc) }
Output: integer of 24/10: 2 acc=Below integer of 25/10: 2 acc=Below integer of -65/10: -6 acc=Above integer of -66/10: -6 acc=Above integer of 50/10: 5 acc=Exact integer of -90/10: -9 acc=Exact
func IsZero ¶ added in v1.6.0
IsZero checks whether fraction of the given value is zero. It may return true even exponential isn't zero.
Example ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { fmt.Printf("is remainder of -2.4/0.4 zero: %t\n", xmath.IsZero(math.Remainder(-2.4, 0.4))) fmt.Printf("is remainder of -2.5/0.4 zero: %t\n", xmath.IsZero(math.Remainder(-2.5, 0.4))) }
Output: is remainder of -2.4/0.4 zero: true is remainder of -2.5/0.4 zero: false
func Max ¶ added in v1.3.0
Max returns the larger of x...
Special cases are:
Max(x, +Inf) = Max(+Inf, x) = +Inf Max(x, NaN) = Max(NaN, x) = NaN Max(+0, ±0) = Max(±0, +0) = +0 Max(-0, -0) = -0 Max(x) = x Max() = +Inf
func MaxInt ¶ added in v1.3.0
MaxInt returns the larger integer of x...
Special cases are:
MaxInt(x) = x MaxInt() = math.MaxInt64
func MaxMin ¶ added in v1.4.0
MaxMin returns the max, min values in this order, similar with Max and Min functions.
Special cases are:
MaxMin(x) = x, x MaxMin() = +Inf, -Inf
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { list := []float64{-1.215, -1.4142, +3.1416} max, min := xmath.MaxMin(list...) fmt.Printf("max=%+6.4f and min=%+6.4f of %+6.4f\n", max, min, list) }
Output: max=+3.1416 and min=-1.4142 of [-1.2150 -1.4142 +3.1416]
func MaxMinInt ¶ added in v1.4.0
MaxMinInt returns the max, min integers in this order, similar with MaxInt and MinInt functions.
Special cases are:
MaxMinInt(x) = x, x MaxMinInt() = math.MaxInt64, math.MinInt64
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { list := []int64{-1, -8, +7, -3, +5} max, min := xmath.MaxMinInt(list...) fmt.Printf("max=%+d and min=%+d of %+d\n", max, min, list) }
Output: max=+7 and min=-8 of [-1 -8 +7 -3 +5]
func MaxMinUint ¶ added in v1.6.0
MaxMinUint returns the max, min unsigned integers in this order, similar with MaxUint and MinUint functions.
Special cases are:
MaxMinUint(x) = x, x MaxMinUint() = math.MaxUint64, 0
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { list := []uint64{+1, +8, +7, +3, +5} max, min := xmath.MaxMinUint(list...) fmt.Printf("max=%+d and min=%+d of %+d\n", max, min, list) }
Output: max=+8 and min=+1 of [+1 +8 +7 +3 +5]
func MaxUint ¶ added in v1.6.0
MaxUint returns the larger unsigned integer of x...
Special cases are:
MaxUint(x) = x MaxUint() = math.MaxUint64
func Min ¶ added in v1.3.0
Min returns the smaller of x...
Special cases are:
Min(x, -Inf) = Min(-Inf, x) = -Inf Min(x, NaN) = Min(NaN, x) = NaN Min(-0, ±0) = Min(±0, -0) = -0 Min(x) = x Min() = -Inf
func MinInt ¶ added in v1.3.0
MinInt returns the smaller integer of x...
Special cases are:
MinInt(x) = x MinInt() = math.MinInt64
func MinMax ¶
MinMax returns the min, max values in this order, similar with Min and Max functions.
Special cases are:
MinMax(x) = x, x MinMax() = -Inf, +Inf
func MinMaxInt ¶ added in v1.3.0
MinMaxInt returns the min, max integers in this order, similar with MinInt and MaxInt functions.
Special cases are:
MinMaxInt(x) = x, x MinMaxInt() = math.MinInt64, math.MaxInt64
func MinMaxUint ¶ added in v1.6.0
MinMaxUint returns the min, max unsigned integers in this order, similar with MinUint and MaxUint functions.
Special cases are:
MinMaxUint(x) = x, x MinMaxUint() = 0, math.MaxUint64
func MinUint ¶ added in v1.6.0
MinUint returns the smaller unsigned integer of x...
Special cases are:
MinUint(x) = x MinUint() = 0
func RoundBigFloat ¶ added in v1.6.0
RoundBigFloat returns the nearest integer, rounding half away from zero. It returns nil if x is an infinity.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { fmt.Printf("round of %v: %v\n", 2.4, xmath.RoundBigFloat(big.NewFloat(2.4))) fmt.Printf("round of %v: %v\n", 2.5, xmath.RoundBigFloat(big.NewFloat(2.5))) fmt.Printf("round of %v: %v\n", -6.5, xmath.RoundBigFloat(big.NewFloat(-6.5))) fmt.Printf("round of %v: %v\n", -6.6, xmath.RoundBigFloat(big.NewFloat(-6.6))) fmt.Printf("round of %v: %v\n", 5, xmath.RoundBigFloat(big.NewFloat(5))) fmt.Printf("round of %v: %v\n", -9, xmath.RoundBigFloat(big.NewFloat(-9))) }
Output: round of 2.4: 2 round of 2.5: 3 round of -6.5: -6 round of -6.6: -7 round of 5: 5 round of -9: -9
func RoundBigRat ¶ added in v1.6.0
RoundBigRat returns the nearest integer, rounding half away from zero.
Example ¶
package main import ( "fmt" "math/big" "github.com/goinsane/xmath" ) func main() { fmt.Printf("round of %v/%v: %v\n", 24, 10, xmath.RoundBigRat(big.NewRat(24, 10))) fmt.Printf("round of %v/%v: %v\n", 25, 10, xmath.RoundBigRat(big.NewRat(25, 10))) fmt.Printf("round of %v/%v: %v\n", -65, 10, xmath.RoundBigRat(big.NewRat(-65, 10))) fmt.Printf("round of %v/%v: %v\n", -66, 10, xmath.RoundBigRat(big.NewRat(-66, 10))) fmt.Printf("round of %v/%v: %v\n", 50, 10, xmath.RoundBigRat(big.NewRat(50, 10))) fmt.Printf("round of %v/%v: %v\n", -90, 10, xmath.RoundBigRat(big.NewRat(-90, 10))) }
Output: round of 24/10: 2 round of 25/10: 3 round of -65/10: -6 round of -66/10: -7 round of 50/10: 5 round of -90/10: -9
func RoundP ¶
RoundP returns the nearest integer value, rounding half away from zero with specified decimal precision.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { fmt.Printf("round of +3.1416 with precision 2: %+6.4f\n", xmath.RoundP(+3.1416, 2)) fmt.Printf("round of -3.1416 with precision 3: %+6.4f\n", xmath.RoundP(-3.1416, 3)) }
Output: round of +3.1416 with precision 2: +3.1400 round of -3.1416 with precision 3: -3.1420
func RoundPB ¶ added in v1.6.0
RoundPB returns the nearest integer value, rounding half away from zero with specified precision of base. It panics unless base is in valid range.
func SafeDiv ¶ added in v1.1.0
SafeDiv divides x to y. For 'division by zero', it returns 0 if allowNaN is false. The GoLang's default behaviour is same with SafeDiv(x, y, true). Special cases are:
SafeDiv(0, ±n, true) = ±0 SafeDiv(0, ±n, false) = ±0 SafeDiv(±n, 0, true) = ±Inf SafeDiv(±n, 0, false) = ±Inf SafeDiv(0, 0, true) = NaN SafeDiv(0, 0, false) = 0
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var x, y float64 x, y = 0, 0 fmt.Printf("safe divide of %+6.4f/%+6.4f with allowNaN is %+6.4f\n", x, y, xmath.SafeDiv(x, y, false)) x, y = 0, 0 fmt.Printf("safe divide of %+6.4f/%+6.4f without allowNaN is %+6.4f\n", x, y, xmath.SafeDiv(x, y, true)) x, y = -5, 0 fmt.Printf("safe divide of %+6.4f/%+6.4f is %+6.4f\n", x, y, xmath.SafeDiv(x, y, false)) x, y = +7, 0 fmt.Printf("safe divide of %+6.4f/%+6.4f is %+6.4f\n", x, y, xmath.SafeDiv(x, y, false)) x, y = +11, 2 fmt.Printf("safe divide of %+6.4f/%+6.4f is %+6.4f\n", x, y, xmath.SafeDiv(x, y, false)) }
Output: safe divide of +0.0000/+0.0000 with allowNaN is +0.0000 safe divide of +0.0000/+0.0000 without allowNaN is +NaN safe divide of -5.0000/+0.0000 is -Inf safe divide of +7.0000/+0.0000 is +Inf safe divide of +11.0000/+2.0000 is +5.5000
func Sign ¶ added in v1.7.0
Sign returns:
-1 if x < 0 0 if x is ±0 +1 if x > 0
Sign panics if x is NaN.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var x float64 x = -1.3 fmt.Printf("sign of %+.2f: %+d\n", x, xmath.Sign(x)) x = xmath.Zero(-1) fmt.Printf("sign of %+.2f: %+d\n", x, xmath.Sign(x)) x = xmath.Zero(+1) fmt.Printf("sign of %+.2f: %+d\n", x, xmath.Sign(x)) x = 2.7 fmt.Printf("sign of %+.2f: %+d\n", x, xmath.Sign(x)) }
Output: sign of -1.30: -1 sign of -0.00: +0 sign of +0.00: +0 sign of +2.70: +1
func SignInt ¶ added in v1.7.0
SignInt returns:
-1 if x < 0 0 if x is 0 +1 if x > 0
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var x int64 x = -3 fmt.Printf("sign of integer %+d: %+d\n", x, xmath.SignInt(x)) x = 0 fmt.Printf("sign of integer %+d: %+d\n", x, xmath.SignInt(x)) x = 7 fmt.Printf("sign of integer %+d: %+d\n", x, xmath.SignInt(x)) }
Output: sign of integer -3: -1 sign of integer +0: +0 sign of integer +7: +1
func Sum ¶ added in v1.7.0
Sum returns the sum of x...
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { x := []float64{-5.6, 2.1, 4.5, -10.9, -3.4} fmt.Printf("sum of %v: %v\n", x, xmath.Sum(x...)) }
Output: sum of [-5.6 2.1 4.5 -10.9 -3.4]: -13.3
func SumInt ¶ added in v1.7.0
SumInt returns the floating point of sum of x...
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { x := []int64{-5, 2, 4, -10, -3} fmt.Printf("sum of integers %v: %v\n", x, xmath.SumInt(x...)) }
Output: sum of integers [-5 2 4 -10 -3]: -12
func SumInt2 ¶ added in v1.7.0
SumInt2 returns the sum of x... If the result overflows, it returns overflow is true.
Example ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { x := []int64{-5, 2, 4, -10, -3} r, o := xmath.SumInt2(x...) fmt.Printf("sum of integers %v: %v overflow %v\n", x, r, o) x = []int64{5, math.MaxInt64, -2} r, o = xmath.SumInt2(x...) fmt.Printf("sum of integers %v: %v overflow %v\n", x, r, o) x = []int64{-7, math.MinInt64, 1} r, o = xmath.SumInt2(x...) fmt.Printf("sum of integers %v: %v overflow %v\n", x, r, o) }
Output: sum of integers [-5 2 4 -10 -3]: -12 overflow false sum of integers [5 9223372036854775807 -2]: -9223372036854775806 overflow true sum of integers [-7 -9223372036854775808 1]: 9223372036854775802 overflow true
func SumUint ¶ added in v1.7.0
SumUint returns the floating point of sum of x...
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { x := []uint64{5, 2, 4, 10, 3} fmt.Printf("sum of unsigned integers %v: %v\n", x, xmath.SumUint(x...)) }
Output: sum of unsigned integers [5 2 4 10 3]: 24
func SumUint2 ¶ added in v1.7.0
SumUint2 returns the sum of x... If the result overflows, it returns overflow is true.
Example ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { x := []uint64{5, 2, 4, 10, 3} r, o := xmath.SumUint2(x...) fmt.Printf("sum of unsigned integers %v: %v overflow %v\n", x, r, o) x = []uint64{5, math.MaxUint64} r, o = xmath.SumUint2(x...) fmt.Printf("sum of unsigned integers %v: %v overflow %v\n", x, r, o) }
Output: sum of unsigned integers [5 2 4 10 3]: 24 overflow false sum of unsigned integers [5 18446744073709551615]: 4 overflow true
func Uint64BigInt ¶ added in v1.7.0
Uint64BigInt returns the integer resulting from truncating x towards zero. If 0 <= x <= math.MaxUint64, the accuracy is big.Exact. The result is (0, big.Above) for x < 0, and (math.MaxUint64, big.Below) for x > math.MaxUint64.
Example ¶
package main import ( "fmt" "math" "math/big" "github.com/goinsane/xmath" ) func main() { var x *big.Int var k uint64 var acc big.Accuracy x = big.NewInt(49) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of %v: %v acc=%v\n", x, k, acc) x = big.NewInt(-27) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of %v: %v acc=%v\n", x, k, acc) x = big.NewInt(math.MaxInt64) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of MaxInt64=%v: %v acc=%v\n", x, k, acc) x = new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(5)) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of MaxInt64+5=%v: %v acc=%v\n", x, k, acc) x = big.NewInt(math.MinInt64) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of MinInt64=%v: %v acc=%v\n", x, k, acc) x = new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(7)) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of MinInt64-7=%v: %v acc=%v\n", x, k, acc) x = new(big.Int).SetUint64(math.MaxUint64) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of MaxUint64=%v: %v acc=%v\n", x, k, acc) x = new(big.Int).Add(new(big.Int).SetUint64(math.MaxUint64), big.NewInt(3)) k, acc = xmath.Uint64BigInt(x) fmt.Printf("uint64 of MaxUint64+3=%v: %v acc=%v\n", x, k, acc) }
Output: uint64 of 49: 49 acc=Exact uint64 of -27: 0 acc=Above uint64 of MaxInt64=9223372036854775807: 9223372036854775807 acc=Exact uint64 of MaxInt64+5=9223372036854775812: 9223372036854775812 acc=Exact uint64 of MinInt64=-9223372036854775808: 0 acc=Above uint64 of MinInt64-7=-9223372036854775815: 0 acc=Above uint64 of MaxUint64=18446744073709551615: 18446744073709551615 acc=Exact uint64 of MaxUint64+3=18446744073709551618: 18446744073709551615 acc=Below
func Uint64BigRat ¶ added in v1.6.0
Uint64BigRat returns the integer resulting from truncating x towards zero. If 0 <= x <= math.MaxUint64, the accuracy is like IntBigRat. The result is (0, big.Above) for x < 0, and (math.MaxUint64, big.Below) for x > math.MaxUint64.
Example ¶
package main import ( "fmt" "math" "math/big" "github.com/goinsane/xmath" ) func main() { var k uint64 var acc big.Accuracy k, acc = xmath.Uint64BigRat(new(big.Rat).SetFrac(big.NewInt(50), big.NewInt(10))) fmt.Printf("uint64 of 50/10: %v acc=%v\n", k, acc) k, acc = xmath.Uint64BigRat(new(big.Rat).SetFrac(big.NewInt(0), big.NewInt(5))) fmt.Printf("uint64 of 0/5: %v acc=%v\n", k, acc) k, acc = xmath.Uint64BigRat(new(big.Rat).SetFrac(big.NewInt(math.MaxInt64), big.NewInt(7))) fmt.Printf("uint64 of MaxInt64/7: %v acc=%v\n", k, acc) k, acc = xmath.Uint64BigRat(new(big.Rat).SetFrac(new(big.Int).Mul(big.NewInt(13), big.NewInt(math.MaxInt64)), big.NewInt(5))) fmt.Printf("uint64 of 13*MaxInt64/5: %v acc=%v\n", k, acc) }
Output: uint64 of 50/10: 5 acc=Exact uint64 of 0/5: 0 acc=Exact uint64 of MaxInt64/7: 1317624576693539401 acc=Exact uint64 of 13*MaxInt64/5: 18446744073709551615 acc=Below
func Zero ¶ added in v1.7.0
Zero returns zero floating point value by given sign.
-0.0 if sign < 0 +0.0 if sign is 0 +0.0 if sign > 0
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { var sign int sign = -1 fmt.Printf("zero with sign %+d: %+.2f\n", sign, xmath.Zero(sign)) sign = 0 fmt.Printf("zero with sign %+d: %+.2f\n", sign, xmath.Zero(sign)) sign = 1 fmt.Printf("zero with sign %+d: %+.2f\n", sign, xmath.Zero(sign)) }
Output: zero with sign -1: -0.00 zero with sign +0: +0.00 zero with sign +1: +0.00
Types ¶
type Real ¶ added in v1.6.0
type Real struct {
// contains filtered or unexported fields
}
Real is a big.Float like implementation for real numbers. It rounds the big.Float to half away from zero by given precision and base after all of writing operations. A Real can be created with new(Real) or NewReal and etc. A Real which is created by new(Real) has precision 0 and base 10. Both of precision and base, can't change after the Real created.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { n := new(xmath.Real) for i := 0; i < 20; i++ { k := 0.25 * float64(i) n.SetFloat64(k) fmt.Println(n, k) } }
Output: 0 0 0 0.25 1 0.5 1 0.75 1 1 1 1.25 2 1.5 2 1.75 2 2 2 2.25 3 2.5 3 2.75 3 3 3 3.25 4 3.5 4 3.75 4 4 4 4.25 5 4.5 5 4.75
func NewDecimal ¶ added in v1.6.0
NewDecimal returns a new Real with base 10.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { n := xmath.NewDecimal(1) for i := 0; i < 10; i++ { k := 0.33 * float64(i) n.SetFloat64(k) fmt.Println(n) } }
Output: 0 0.3 0.7 1 1.3 1.7 2 2.3 2.6 3
func NewHexadecimal ¶ added in v1.6.1
NewHexadecimal returns a new Real with base 16.
func NewHexdecimal ¶ added in v1.6.0
NewHexdecimal is synonym with NewHexadecimal.
func NewReal ¶ added in v1.6.0
NewReal returns a new Real with given precision and base. Calling the NewReal, isn't necessary to create new Real. It panics unless base is in valid range.
func (*Real) FloatMinPrec ¶ added in v1.6.0
FloatMinPrec is similar with MinPrec method of big.Float.
func (*Real) FloatMode ¶ added in v1.6.0
func (x *Real) FloatMode() big.RoundingMode
FloatMode is similar with Mode method of big.Float.
func (*Real) MarshalText ¶ added in v1.6.0
MarshalText is similar with MarshalText method of big.Float.
func (*Real) SetFloat64 ¶ added in v1.6.0
SetFloat64 is similar with SetFloat64 method of big.Float.
func (*Real) SetFloatMode ¶ added in v1.6.0
func (z *Real) SetFloatMode(mode big.RoundingMode) *Real
SetFloatMode is similar with SetMode method of big.Float.
func (*Real) SetFloatPrec ¶ added in v1.6.0
SetFloatPrec is similar with SetPrec method of big.Float.
func (*Real) SetMantExp ¶ added in v1.6.0
SetMantExp is similar with SetMantExp method of big.Float.
func (*Real) UnmarshalText ¶ added in v1.6.0
UnmarshalText is similar with UnmarshalText method of big.Float.
type Stepper ¶ added in v1.6.0
type Stepper struct {
// contains filtered or unexported fields
}
Stepper is a utility to step and normalize floating point values by given precision and base.
func NewStepper ¶ added in v1.6.0
NewStepper returns a new Stepper with given precision, base and given step, max, min. Both of max and min can be infinity. In this case, the range of Stepper is infinity. It panics unless base is in valid range.
Example ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { var err error _, err = xmath.NewStepper(2, 10, 0.1, 3.01, 2.31) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.105, 3.01, 2.31) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.1, 3.015, 2.31) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.1, 3.01, 2.315) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.15, 3.01, 2.31) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.1, 2.31, 3.01) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.1, math.Inf(+1), math.Inf(-1)) fmt.Println(err) _, err = xmath.NewStepper(2, 10, 0.1, math.Inf(-1), math.Inf(+1)) fmt.Println(err) }
Output: <nil> step overflow max overflow min overflow range overflow unordered max min <nil> unordered max min
func (*Stepper) Count ¶ added in v1.6.0
Count is same with Count64 if count is less than or equal to MaxIntValue. If count is greater than MaxIntValue, it returns MaxIntValue.
func (*Stepper) Count64 ¶ added in v1.6.0
Count64 returns number of step for given range. If the range of Stepper is infinity, it returns 0.
func (*Stepper) Normalize ¶ added in v1.6.0
Normalize returns normalized float value by proper index. If the range of Stepper is infinity, alignment of steps is made to be as to provide step of index 0 is 0.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { s, err := xmath.NewStepper(2, 10, 0.25, -5.00, -7.00) if err != nil { panic(err) } fmt.Println(s.Normalize(0.50)) fmt.Println(s.Normalize(-7.75)) fmt.Println(s.Normalize(-6.376)) fmt.Println(s.Normalize(-6.375)) fmt.Println(s.Normalize(-6.374)) }
Output: -5 max exceeded -7 min exceeded -6.5 <nil> -6.25 <nil> -6.25 <nil>
func (*Stepper) Step ¶ added in v1.6.0
Step is same with Step64 except that Step indexes up to MaxIntValue.
Example ¶
package main import ( "fmt" "github.com/goinsane/xmath" ) func main() { s, err := xmath.NewStepper(2, 10, 0.1, 3.01, 2.31) if err != nil { panic(err) } for i := 0; i < s.Count(); i++ { fmt.Println(s.Step(i)) } }
Output: 2.31 <nil> 2.41 <nil> 2.51 <nil> 2.61 <nil> 2.71 <nil> 2.81 <nil> 2.91 <nil> 3.01 <nil>
Example (Inf) ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { s, err := xmath.NewStepper(2, 8, 0.125, math.Inf(+1), math.Inf(-1)) if err != nil { panic(err) } for i := -5; i <= +5; i++ { fmt.Println(s.Step(i)) } }
Output: -0.625 <nil> -0.5 <nil> -0.375 <nil> -0.25 <nil> -0.125 <nil> 0 <nil> 0.125 <nil> 0.25 <nil> 0.375 <nil> 0.5 <nil> 0.625 <nil>
Example (Inf_max) ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { s, err := xmath.NewStepper(2, 8, 0.125, math.Inf(+1), -5.0) if err != nil { panic(err) } for i := -5; i <= +5; i++ { fmt.Println(s.Step(i)) } }
Output: -0.625 <nil> -0.5 <nil> -0.375 <nil> -0.25 <nil> -0.125 <nil> 0 <nil> 0.125 <nil> 0.25 <nil> 0.375 <nil> 0.5 <nil> 0.625 <nil>
Example (Inf_min) ¶
package main import ( "fmt" "math" "github.com/goinsane/xmath" ) func main() { s, err := xmath.NewStepper(2, 8, 0.125, -5.0, math.Inf(-1)) if err != nil { panic(err) } for i := -5; i <= +5; i++ { fmt.Println(s.Step(i)) } }
Output: -0.625 <nil> -0.5 <nil> -0.375 <nil> -0.25 <nil> -0.125 <nil> 0 <nil> 0.125 <nil> 0.25 <nil> 0.375 <nil> 0.5 <nil> 0.625 <nil>