xmath

package module
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2021 License: BSD-3-Clause Imports: 5 Imported by: 1

README

xmath

Go Reference

Package xmath provides some extended capabilities according to GoLang's math. Please see godoc.

Examples

To run any example, please use the command like the following:

cd examples/
go run example1.go

Tests

To run all tests, please use the following command:

go test -v

To run all examples, please use the following command:

go test -v -run=^Example

To run all benchmarks, please use the following command:

go test -v -run=^Benchmark -bench=.

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

Examples

Constants

View Source
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

View Source
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

func AlmostEqual(x ...float64) bool

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

func AlmostEqual32(x ...float32) bool

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

func AlmostEqual64(x ...float64) bool

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

func AlmostEqualD(d float64, x ...float64) bool

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

func AlmostEqualD32(d float32, x ...float32) bool

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

func AlmostEqualD64(d float64, x ...float64) bool

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

func AlmostEqualP(p uint64, x ...float64) bool

AlmostEqualP is synonym with AlmostEqualP64.

func AlmostEqualP32 added in v1.6.0

func AlmostEqualP32(p uint32, x ...float32) bool

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

func AlmostEqualP64(p uint64, x ...float64) bool

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

func Avg(x ...float64) (avg float64)

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

func AvgInt(x ...int64) (avg float64)

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

func AvgInt2(x ...int64) (avg int64, overflow bool)

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

func AvgUint(x ...uint64) (avg float64)

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

func AvgUint2(x ...uint64) (avg uint64, overflow bool)

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

func Between(x float64, a, b float64) bool

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

func BetweenIn(x float64, a, b float64) bool

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

func CeilBigFloat(x *big.Float) *big.Int

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

func CeilBigRat(x *big.Rat) *big.Int

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

func CeilP(x float64, prec int) float64

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

func CeilPB(x float64, prec int, base int) float64

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

func CryptoRandCode(n int) int64

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

func CryptoRandInt(max int64) int64

CryptoRandInt returns a random integer in [0, max). It returns -1 when error occurs.

func Equal added in v1.4.0

func Equal(x ...float64) bool

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

func Equal32(x ...float32) bool

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

func Equal64(x ...float64) bool

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

func FloorBigFloat(x *big.Float) *big.Int

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

func FloorBigRat(x *big.Rat) *big.Int

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

func FloorP(x float64, prec int) float64

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

func FloorPB(x float64, prec int, base int) float64

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

func Int64BigInt(x *big.Int) (int64, big.Accuracy)

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

func Int64BigRat(x *big.Rat) (int64, big.Accuracy)

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

func IntBigRat(x *big.Rat) (*big.Int, big.Accuracy)

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

func IsZero(x float64) bool

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

func Max(x ...float64) float64

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

func MaxInt(x ...int64) int64

MaxInt returns the larger integer of x...

Special cases are:

MaxInt(x) = x
MaxInt() = math.MaxInt64

func MaxMin added in v1.4.0

func MaxMin(x ...float64) (max float64, min float64)

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

func MaxMinInt(x ...int64) (max int64, min int64)

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

func MaxMinUint(x ...uint64) (max uint64, min uint64)

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

func MaxUint(x ...uint64) uint64

MaxUint returns the larger unsigned integer of x...

Special cases are:

MaxUint(x) = x
MaxUint() = math.MaxUint64

func Min added in v1.3.0

func Min(x ...float64) float64

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

func MinInt(x ...int64) int64

MinInt returns the smaller integer of x...

Special cases are:

MinInt(x) = x
MinInt() = math.MinInt64

func MinMax

func MinMax(x ...float64) (min float64, max float64)

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

func MinMaxInt(x ...int64) (min int64, max int64)

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

func MinMaxUint(x ...uint64) (min uint64, max uint64)

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

func MinUint(x ...uint64) uint64

MinUint returns the smaller unsigned integer of x...

Special cases are:

MinUint(x) = x
MinUint() = 0

func Round

func Round(x float64) float64

Round returns the nearest integer value, rounding half away from zero.

func RoundBigFloat added in v1.6.0

func RoundBigFloat(x *big.Float) *big.Int

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

func RoundBigRat(x *big.Rat) *big.Int

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

func RoundP(x float64, prec int) float64

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

func RoundPB(x float64, prec int, base int) float64

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

func SafeDiv(x, y float64, allowNaN bool) float64

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

func Sign(x float64) int

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

func SignInt(x int64) int

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

func Sum(x ...float64) (sum float64)

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

func SumInt(x ...int64) (sum float64)

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

func SumInt2(x ...int64) (sum int64, overflow bool)

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

func SumUint(x ...uint64) (sum float64)

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

func SumUint2(x ...uint64) (sum uint64, overflow bool)

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

func Uint64BigInt(x *big.Int) (uint64, big.Accuracy)

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

func Uint64BigRat(x *big.Rat) (uint64, big.Accuracy)

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

func Zero(sign int) float64

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 NewBinary added in v1.6.0

func NewBinary(prec int) *Real

NewBinary returns a new Real with base 2.

func NewDecimal added in v1.6.0

func NewDecimal(prec int) *Real

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

func NewHexadecimal(prec int) *Real

NewHexadecimal returns a new Real with base 16.

func NewHexdecimal added in v1.6.0

func NewHexdecimal(prec int) *Real

NewHexdecimal is synonym with NewHexadecimal.

func NewOctal added in v1.6.0

func NewOctal(prec int) *Real

NewOctal returns a new Real with base 8.

func NewReal added in v1.6.0

func NewReal(prec, base int) *Real

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) Abs added in v1.6.0

func (z *Real) Abs(x *Real) *Real

Abs is similar with Abs method of big.Float.

func (*Real) Acc added in v1.6.0

func (x *Real) Acc() big.Accuracy

Acc is similar with Acc method of big.Float.

func (*Real) Add added in v1.6.0

func (z *Real) Add(x, y *Real) *Real

Add is similar with Add method of big.Float.

func (*Real) Append added in v1.6.0

func (x *Real) Append(buf []byte, fmt byte, prec int) []byte

Append is similar with Append method of big.Float.

func (*Real) Base added in v1.6.0

func (x *Real) Base() int

Base returns base of the Real.

func (*Real) Cmp added in v1.6.0

func (x *Real) Cmp(y *Real) int

Cmp is similar with Cmp method of big.Float.

func (*Real) Copy added in v1.6.0

func (z *Real) Copy(x *Real) *Real

Copy is similar with Copy method of big.Float.

func (*Real) Float added in v1.6.0

func (x *Real) Float() *big.Float

Float returns a copy of big.Float value.

func (*Real) Float32 added in v1.6.0

func (x *Real) Float32() (float32, big.Accuracy)

Float32 is similar with Float32 method of big.Float.

func (*Real) Float64 added in v1.6.0

func (x *Real) Float64() (float64, big.Accuracy)

Float64 is similar with Float64 method of big.Float.

func (*Real) FloatMinPrec added in v1.6.0

func (x *Real) FloatMinPrec() uint

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) FloatPrec added in v1.6.0

func (x *Real) FloatPrec() uint

FloatPrec is similar with Prec method of big.Float.

func (*Real) Format added in v1.6.0

func (x *Real) Format(s fmt.State, format rune)

Format is similar with Format method of big.Float.

func (*Real) GobDecode added in v1.6.0

func (z *Real) GobDecode(buf []byte) error

GobDecode is similar with GobDecode method of big.Float.

func (*Real) GobEncode added in v1.6.0

func (x *Real) GobEncode() ([]byte, error)

GobEncode is similar with GobEncode method of big.Float.

func (*Real) Int added in v1.6.0

func (x *Real) Int(z *big.Int) (*big.Int, big.Accuracy)

Int is similar with Int method of big.Float.

func (*Real) Int64 added in v1.6.0

func (x *Real) Int64() (int64, big.Accuracy)

Int64 is similar with Int64 method of big.Float.

func (*Real) IsInf added in v1.6.0

func (x *Real) IsInf() bool

IsInf is similar with IsInf method of big.Float.

func (*Real) IsInt added in v1.6.0

func (x *Real) IsInt() bool

IsInt is similar with IsInt method of big.Float.

func (*Real) MantExp added in v1.6.0

func (x *Real) MantExp(mant *Real) (exp int)

MantExp is similar with MantExp method of big.Float.

func (*Real) MarshalText added in v1.6.0

func (x *Real) MarshalText() (text []byte, err error)

MarshalText is similar with MarshalText method of big.Float.

func (*Real) Mul added in v1.6.0

func (z *Real) Mul(x, y *Real) *Real

Mul is similar with Mul method of big.Float.

func (*Real) Neg added in v1.6.0

func (z *Real) Neg(x *Real) *Real

Neg is similar with Neg method of big.Float.

func (*Real) Parse added in v1.6.0

func (z *Real) Parse(s string, base int) (r *Real, b int, err error)

Parse is similar with Parse method of big.Float.

func (*Real) Prec added in v1.6.0

func (x *Real) Prec() int

Prec returns precision of the Real.

func (*Real) Quo added in v1.6.0

func (z *Real) Quo(x, y *Real) *Real

Quo is similar with Quo method of big.Float.

func (*Real) Rat added in v1.6.0

func (x *Real) Rat(z *big.Rat) (*big.Rat, big.Accuracy)

Rat is similar with Rat method of big.Float.

func (*Real) Scan added in v1.6.0

func (z *Real) Scan(s fmt.ScanState, ch rune) error

Scan is similar with Scan method of big.Float.

func (*Real) Set added in v1.6.0

func (z *Real) Set(x *Real) *Real

Set is similar with Set method of big.Float.

func (*Real) SetFloat added in v1.6.0

func (z *Real) SetFloat(x *big.Float) *Real

SetFloat is similar with Set except that x is big.Float.

func (*Real) SetFloat64 added in v1.6.0

func (z *Real) SetFloat64(x float64) *Real

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

func (z *Real) SetFloatPrec(prec uint) *Real

SetFloatPrec is similar with SetPrec method of big.Float.

func (*Real) SetInf added in v1.6.0

func (z *Real) SetInf(signbit bool) *Real

SetInf is similar with SetInf method of big.Float.

func (*Real) SetInt added in v1.6.0

func (z *Real) SetInt(x *big.Int) *Real

SetInt is similar with SetInt method of big.Float.

func (*Real) SetInt64 added in v1.6.0

func (z *Real) SetInt64(x int64) *Real

SetInt64 is similar with SetInt64 method of big.Float.

func (*Real) SetMantExp added in v1.6.0

func (z *Real) SetMantExp(mant *Real, exp int) *Real

SetMantExp is similar with SetMantExp method of big.Float.

func (*Real) SetRat added in v1.6.0

func (z *Real) SetRat(x *big.Rat) *Real

SetRat is similar with SetRat method of big.Float.

func (*Real) SetString added in v1.6.0

func (z *Real) SetString(s string) (r *Real, ok bool)

SetString is similar with SetString method of big.Float.

func (*Real) SetUint64 added in v1.6.0

func (z *Real) SetUint64(x uint64) *Real

SetUint64 is similar with SetUint64 method of big.Float.

func (*Real) Sign added in v1.6.0

func (x *Real) Sign() int

Sign is similar with Sign method of big.Float.

func (*Real) Signbit added in v1.6.0

func (x *Real) Signbit() bool

Signbit is similar with Signbit method of big.Float.

func (*Real) Sqrt added in v1.6.0

func (z *Real) Sqrt(x *Real) *Real

Sqrt is similar with Sqrt method of big.Float.

func (*Real) String added in v1.6.0

func (x *Real) String() string

String is similar with String method of big.Float.

func (*Real) Sub added in v1.6.0

func (z *Real) Sub(x, y *Real) *Real

Sub is similar with Sub method of big.Float.

func (*Real) Text added in v1.6.0

func (x *Real) Text(format byte, prec int) string

Text is similar with Text method of big.Float.

func (*Real) Uint64 added in v1.6.0

func (x *Real) Uint64() (uint64, big.Accuracy)

Uint64 is similar with Uint64 method of big.Float.

func (*Real) UnmarshalText added in v1.6.0

func (z *Real) UnmarshalText(text []byte) error

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

func NewStepper(prec, base int, step, max, min float64) (s *Stepper, err error)

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) Base added in v1.6.0

func (s *Stepper) Base() int

Base returns base of the Stepper.

func (*Stepper) Count added in v1.6.0

func (s *Stepper) Count() int

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

func (s *Stepper) Count64() int64

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

func (s *Stepper) Normalize(f float64) (float64, error)

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) Prec added in v1.6.0

func (s *Stepper) Prec() int

Prec returns precision of the Stepper.

func (*Stepper) Step added in v1.6.0

func (s *Stepper) Step(index int) (float64, error)

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>

func (*Stepper) Step64 added in v1.6.0

func (s *Stepper) Step64(index int64) (float64, error)

Step64 returns proper step value by given index. If the range of Stepper is infinity, step of index 0 is 0.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL