xmath

package
v1.113.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MPL-2.0 Imports: 5 Imported by: 18

Documentation

Overview

Package xmath provides math-related utilities.

Index

Constants

View Source
const (
	E   = math.E
	Pi  = math.Pi
	Phi = math.Phi

	Sqrt2   = math.Sqrt2
	SqrtE   = math.SqrtE
	SqrtPi  = math.SqrtPi
	SqrtPhi = math.SqrtPhi

	Ln2    = math.Ln2
	Log2E  = 1 / Ln2
	Ln10   = math.Ln10
	Log10E = 1 / Ln10
)

Mathematical constants. Mostly just re-exported from the math package for convenience.

View Source
const (
	MaxFloat32             = math.MaxFloat32
	SmallestNonzeroFloat32 = math.SmallestNonzeroFloat32
	MaxFloat64             = math.MaxFloat64
	SmallestNonzeroFloat64 = math.SmallestNonzeroFloat64
)

Floating-point limit values. Mostly just re-exported from the math package for convenience. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

View Source
const (
	MaxInt    = math.MaxInt
	MinInt    = math.MinInt
	MaxInt8   = math.MaxInt8
	MinInt8   = math.MinInt8
	MaxInt16  = math.MaxInt16
	MinInt16  = math.MinInt16
	MaxInt32  = math.MaxInt32
	MinInt32  = math.MinInt32
	MaxInt64  = math.MaxInt64
	MinInt64  = math.MinInt64
	MaxUint   = math.MaxUint
	MaxUint8  = math.MaxUint8
	MaxUint16 = math.MaxUint16
	MaxUint32 = math.MaxUint32
	MaxUint64 = math.MaxUint64
)

Integer limit values. Mostly just re-exported from the math package for convenience.

View Source
const (
	// DegreesToRadians converts a value in degrees to radians when multiplied with the value.
	DegreesToRadians = math.Pi / 180
	// RadiansToDegrees converts a value in radians to degrees when multiplied with the value.
	RadiansToDegrees = 180 / math.Pi
)

Variables

This section is empty.

Functions

func Abs added in v1.66.0

func Abs[T Numeric](x T) T

Abs returns the absolute value of x.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN

func Acos added in v1.66.0

func Acos[T constraints.Float](x T) T

Acos returns the arccosine of x.

func Acosh added in v1.66.0

func Acosh[T constraints.Float](x T) T

Acosh returns the inverse hyperbolic cosine of x.

Special cases are:

Acosh(+Inf) = +Inf
Acosh(x) = NaN if x < 1
Acosh(NaN) = NaN

func Asin added in v1.66.0

func Asin[T constraints.Float](x T) T

Asin returns the arcsine, in radians, of x.

Special cases are:

Asin(±0) = ±0
Asin(x) = NaN if x < -1 or x > 1

func Asinh added in v1.66.0

func Asinh[T constraints.Float](x T) T

Asinh returns the inverse hyperbolic sine of x.

Special cases are:

Asinh(±0) = ±0
Asinh(±Inf) = ±Inf
Asinh(NaN) = NaN

func Atan added in v1.66.0

func Atan[T constraints.Float](x T) T

Atan returns the arctangent, in radians, of x.

Special cases are:

Atan(±0) = ±0
Atan(±Inf) = ±Pi/2

func Atan2 added in v1.66.0

func Atan2[T constraints.Float](y, x T) T

Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.

Special cases are (in order):

Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +Pi
Atan2(-0, x<=-0) = -Pi
Atan2(y>0, 0) = +Pi/2
Atan2(y<0, 0) = -Pi/2
Atan2(+Inf, +Inf) = +Pi/4
Atan2(-Inf, +Inf) = -Pi/4
Atan2(+Inf, -Inf) = 3Pi/4
Atan2(-Inf, -Inf) = -3Pi/4
Atan2(y, +Inf) = 0
Atan2(y>0, -Inf) = +Pi
Atan2(y<0, -Inf) = -Pi
Atan2(+Inf, x) = +Pi/2
Atan2(-Inf, x) = -Pi/2

func Atanh added in v1.66.0

func Atanh[T constraints.Float](x T) T

Atanh returns the inverse hyperbolic tangent of x.

Special cases are:

Atanh(1) = +Inf
Atanh(±0) = ±0
Atanh(-1) = -Inf
Atanh(x) = NaN if x < -1 or x > 1
Atanh(NaN) = NaN

func Cbrt added in v1.66.0

func Cbrt[T constraints.Float](x T) T

Cbrt returns the cube root of x.

func Ceil added in v1.66.0

func Ceil[T Numeric](x T) T

Ceil returns the smallest integer value greater than or equal to x.

func Copysign added in v1.66.0

func Copysign[T constraints.Float](x, y T) T

Copysign returns a value with the magnitude of x and the sign of y.

func Cos added in v1.66.0

func Cos[T constraints.Float](x T) T

Cos returns the cosine of the radian argument x.

Special cases are:

Cos(±Inf) = NaN
Cos(NaN) = NaN

func Cosh added in v1.66.0

func Cosh[T constraints.Float](x T) T

Cosh returns the hyperbolic cosine of x.

Special cases are:

Cosh(±0) = 1
Cosh(±Inf) = +Inf
Cosh(NaN) = NaN

func Dim added in v1.66.0

func Dim[T constraints.Float](x, y T) T

Dim returns the maximum of x-y or 0.

Special cases are:

Dim(+Inf, +Inf) = NaN
Dim(-Inf, -Inf) = NaN
Dim(x, NaN) = Dim(NaN, x) = NaN

func EqualWithin added in v1.89.0

func EqualWithin[T Numeric](a, b, tolerance T) bool

EqualWithin returns true if a and b are within the given tolerance of each other.

func Erf added in v1.66.0

func Erf[T constraints.Float](x T) T

Erf returns the error function of x.

Special cases are:

Erf(+Inf) = 1
Erf(-Inf) = -1
Erf(NaN) = NaN

func Erfc added in v1.66.0

func Erfc[T constraints.Float](x T) T

Erfc returns the complementary error function of x.

Special cases are:

Erfc(+Inf) = 0
Erfc(-Inf) = 2
Erfc(NaN) = NaN

func Erfcinv added in v1.66.0

func Erfcinv[T constraints.Float](x T) T

Erfcinv returns the inverse of Erfc(x).

Special cases are:

Erfcinv(0) = +Inf
Erfcinv(2) = -Inf
Erfcinv(x) = NaN if x < 0 or x > 2
Erfcinv(NaN) = NaN

func Erfinv added in v1.66.0

func Erfinv[T constraints.Float](x T) T

Erfinv returns the inverse error function of x.

Special cases are:

Erfinv(1) = +Inf
Erfinv(-1) = -Inf
Erfinv(x) = NaN if x < -1 or x > 1
Erfinv(NaN) = NaN

func Exp added in v1.66.0

func Exp[T constraints.Float](x T) T

Exp returns e**x, the base-e exponential of x.

Special cases are:

Exp(+Inf) = +Inf
Exp(NaN) = NaN

Very large values overflow to 0 or +Inf. Very small values underflow to 1.

func Exp2 added in v1.66.0

func Exp2[T constraints.Float](x T) T

Exp2 returns 2**x, the base-2 exponential of x.

Special cases are the same as Exp.

func Expm1 added in v1.66.0

func Expm1[T constraints.Float](x T) T

Expm1 returns e**x - 1, the base-e exponential of x minus 1. It is more accurate than Exp(x) - 1 when x is near zero.

Special cases are:

Expm1(+Inf) = +Inf
Expm1(-Inf) = -1
Expm1(NaN) = NaN

Very large values overflow to -1 or +Inf.

func FMA added in v1.66.0

func FMA[T constraints.Float](x, y, z T) T

FMA returns x * y + z, computed with only one rounding. (That is, FMA returns the fused multiply-add of x, y, and z.)

func Floor added in v1.66.0

func Floor[T Numeric](x T) T

Floor returns the greatest integer value less than or equal to x.

func Frexp added in v1.66.0

func Frexp[T constraints.Float](f T) (frac T, exp int)

Frexp breaks f into a normalized fraction and an integral power of two. It returns frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).

Special cases are:

Frexp(±0) = ±0, 0
Frexp(±Inf) = ±Inf, 0
Frexp(NaN) = NaN, 0

func Gamma added in v1.66.0

func Gamma[T constraints.Float](x T) T

Gamma returns the Gamma function of x.

Special cases are:

Gamma(+Inf) = +Inf
Gamma(+0) = +Inf
Gamma(-0) = -Inf
Gamma(x) = NaN for integer x < 0
Gamma(-Inf) = NaN
Gamma(NaN) = NaN

func Hypot added in v1.66.0

func Hypot[T constraints.Float](p, q T) T

Hypot returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.

Special cases are:

Hypot(±Inf, q) = +Inf
Hypot(p, ±Inf) = +Inf
Hypot(NaN, q) = NaN
Hypot(p, NaN) = NaN

func Ilogb added in v1.66.0

func Ilogb[T constraints.Float](x T) int

Ilogb returns the binary exponent of x as an integer.

Special cases are:

Ilogb(±Inf) = MaxInt32
Ilogb(0) = MinInt32
Ilogb(NaN) = MaxInt32

func Inf added in v1.66.0

func Inf[T constraints.Float](sign int) T

Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

func IsInf added in v1.66.0

func IsInf[T constraints.Float](f T, sign int) bool

IsInf reports whether f is an infinity, according to sign. If sign > 0, IsInf reports whether f is positive infinity. If sign < 0, IsInf reports whether f is negative infinity. If sign == 0, IsInf reports whether f is either infinity.

func IsNaN added in v1.66.0

func IsNaN[T constraints.Float](f T) bool

IsNaN reports whether f is a "not-a-number" value.

func J0 added in v1.66.0

func J0[T constraints.Float](x T) T

J0 returns the order-zero Bessel function of the first kind.

Special cases are:

J0(±Inf) = 0
J0(0) = 1
J0(NaN) = NaN

func J1 added in v1.66.0

func J1[T constraints.Float](x T) T

J1 returns the order-one Bessel function of the first kind.

Special cases are:

J1(±Inf) = 0
J1(NaN) = NaN

func Jn added in v1.66.0

func Jn[T constraints.Float](n int, x T) T

Jn returns the order-n Bessel function of the first kind.

Special cases are:

Jn(n, ±Inf) = 0
Jn(n, NaN) = NaN

func Ldexp added in v1.66.0

func Ldexp[T constraints.Float](frac T, exp int) T

Ldexp is the inverse of Frexp. It returns frac × 2**exp.

Special cases are:

Ldexp(±0, exp) = ±0
Ldexp(±Inf, exp) = ±Inf
Ldexp(NaN, exp) = NaN

func Lgamma added in v1.66.0

func Lgamma[T constraints.Float](x T) (lgamma T, sign int)

Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x).

Special cases are:

Lgamma(+Inf) = +Inf
Lgamma(0) = +Inf
Lgamma(-integer) = +Inf
Lgamma(-Inf) = -Inf
Lgamma(NaN) = NaN

func Log added in v1.66.0

func Log[T constraints.Float](x T) T

Log returns the natural logarithm of x.

Special cases are:

Log(+Inf) = +Inf
Log(0) = -Inf
Log(x < 0) = NaN
Log(NaN) = NaN

func Log10 added in v1.66.0

func Log10[T constraints.Float](x T) T

Log10 returns the decimal logarithm of x. The special cases are the same as for Log.

func Log1p added in v1.66.0

func Log1p[T constraints.Float](x T) T

Log1p returns the natural logarithm of 1 plus its argument x. It is more accurate than Log(1 + x) when x is near zero.

Special cases are:

Log1p(+Inf) = +Inf
Log1p(±0) = ±0
Log1p(-1) = -Inf
Log1p(x < -1) = NaN
Log1p(NaN) = NaN

func Log2 added in v1.66.0

func Log2[T constraints.Float](x T) T

Log2 returns the binary logarithm of x. The special cases are the same as for Log.

func Logb added in v1.66.0

func Logb[T constraints.Float](x T) T

Logb returns the binary exponent of x.

Special cases are:

Logb(±Inf) = +Inf
Logb(0) = -Inf
Logb(NaN) = NaN

func MaxValue added in v1.66.0

func MaxValue[T Numeric]() T

MaxValue returns the maximum value for the type.

func MinValue added in v1.66.0

func MinValue[T Numeric]() T

MinValue returns the minimum value for the type.

func Mod added in v1.66.0

func Mod[T constraints.Float](x, y T) T

Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x.

Special cases are:

Mod(±Inf, y) = NaN
Mod(NaN, y) = NaN
Mod(x, 0) = NaN
Mod(x, ±Inf) = x
Mod(x, NaN) = NaN

func Modf added in v1.66.0

func Modf[T constraints.Float](f T) (i, frac T)

Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.

Special cases are:

Modf(±Inf) = ±Inf, NaN
Modf(NaN) = NaN, NaN

func NaN added in v1.66.0

func NaN[T constraints.Float]() T

NaN returns the "not-a-number" value.

func Nextafter added in v1.66.0

func Nextafter[T constraints.Float](x, y T) (r T)

Nextafter returns the next representable float32 value after x towards y.

Special cases are:

Nextafter(x, x)   = x
Nextafter(NaN, y) = NaN
Nextafter(x, NaN) = NaN

func Pow added in v1.66.0

func Pow[T constraints.Float](x, y T) T

Pow returns x**y, the base-x exponential of y.

Special cases are (in order):

Pow(x, ±0) = 1 for any x
Pow(1, y) = 1 for any y
Pow(x, 1) = x for any x
Pow(NaN, y) = NaN
Pow(x, NaN) = NaN
Pow(±0, y) = ±Inf for y an odd integer < 0
Pow(±0, -Inf) = +Inf
Pow(±0, +Inf) = +0
Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
Pow(±0, y) = ±0 for y an odd integer > 0
Pow(±0, y) = +0 for finite y > 0 and not an odd integer
Pow(-1, ±Inf) = 1
Pow(x, +Inf) = +Inf for |x| > 1
Pow(x, -Inf) = +0 for |x| > 1
Pow(x, +Inf) = +0 for |x| < 1
Pow(x, -Inf) = +Inf for |x| < 1
Pow(+Inf, y) = +Inf for y > 0
Pow(+Inf, y) = +0 for y < 0
Pow(-Inf, y) = Pow(-0, -y)
Pow(x, y) = NaN for finite x < 0 and finite non-integer y

func Pow10 added in v1.66.0

func Pow10[T constraints.Float](n int) T

Pow10 returns 10**n, the base-10 exponential of n.

Special cases are:

Pow10(n) =    0 for n < -323
Pow10(n) = +Inf for n > 308

func Remainder added in v1.66.0

func Remainder[T constraints.Float](x, y T) T

Remainder returns the IEEE 754 floating-point remainder of x/y.

Special cases are:

Remainder(±Inf, y) = NaN
Remainder(NaN, y) = NaN
Remainder(x, 0) = NaN
Remainder(x, ±Inf) = x
Remainder(x, NaN) = NaN

func Round

func Round[T constraints.Float](x T) T

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

Special cases are:

Round(±0) = ±0
Round(±Inf) = ±Inf
Round(NaN) = NaN

func RoundToEven added in v1.66.0

func RoundToEven[T constraints.Float](x T) T

RoundToEven returns the nearest integer, rounding ties to even.

Special cases are:

RoundToEven(±0) = ±0
RoundToEven(±Inf) = ±Inf
RoundToEven(NaN) = NaN

func Signbit added in v1.66.0

func Signbit[T constraints.Float](x T) bool

Signbit reports whether x is negative or negative zero.

func Sin added in v1.66.0

func Sin[T constraints.Float](x T) T

Sin returns the sine of the radian argument x.

Special cases are:

Sin(±0) = ±0
Sin(±Inf) = NaN
Sin(NaN) = NaN

func Sincos added in v1.66.0

func Sincos[T constraints.Float](x T) (sin, cos T)

Sincos returns Sin(x), Cos(x).

Special cases are:

Sincos(±0) = ±0, 1
Sincos(±Inf) = NaN, NaN
Sincos(NaN) = NaN, NaN

func Sinh added in v1.66.0

func Sinh[T constraints.Float](x T) T

Sinh returns the hyperbolic sine of x.

Special cases are:

Sinh(±0) = ±0
Sinh(±Inf) = ±Inf
Sinh(NaN) = NaN

func SmallestPositiveNonZeroValue added in v1.89.0

func SmallestPositiveNonZeroValue[T Numeric]() T

SmallestPositiveNonZeroValue returns the smallest, positive, non-zero value for the type.

func Sqrt added in v1.66.0

func Sqrt[T constraints.Float](x T) T

Sqrt returns the square root of x.

Special cases are:

Sqrt(+Inf) = +Inf
Sqrt(±0) = ±0
Sqrt(x < 0) = NaN
Sqrt(NaN) = NaN

func Tan added in v1.66.0

func Tan[T constraints.Float](x T) T

Tan returns the tangent of the radian argument x.

Special cases are:

Tan(±0) = ±0
Tan(±Inf) = NaN
Tan(NaN) = NaN

func Tanh added in v1.66.0

func Tanh[T constraints.Float](x T) T

Tanh returns the hyperbolic tangent of x.

Special cases are:

Tanh(±0) = ±0
Tanh(±Inf) = ±1
Tanh(NaN) = NaN

func Trunc added in v1.66.0

func Trunc[T constraints.Float](x T) T

Trunc returns the integer value of x.

Special cases are:

Trunc(±0) = ±0
Trunc(±Inf) = ±Inf
Trunc(NaN) = NaN

func Y0 added in v1.66.0

func Y0[T constraints.Float](x T) T

Y0 returns the order-zero Bessel function of the second kind.

Special cases are:

Y0(+Inf) = 0
Y0(0) = -Inf
Y0(x < 0) = NaN
Y0(NaN) = NaN

func Y1 added in v1.66.0

func Y1[T constraints.Float](x T) T

Y1 returns the order-one Bessel function of the second kind.

Special cases are:

Y1(+Inf) = 0
Y1(0) = -Inf
Y1(x < 0) = NaN
Y1(NaN) = NaN

func Yn added in v1.66.0

func Yn[T constraints.Float](n int, x T) T

Yn returns the order-n Bessel function of the second kind.

Special cases are:

Yn(n, +Inf) = 0
Yn(n ≥ 0, 0) = -Inf
Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
Yn(n, x < 0) = NaN
Yn(n, NaN) = NaN

Types

type BitSet

type BitSet struct {
	// contains filtered or unexported fields
}

BitSet contains a set of bits.

func (*BitSet) Clear

func (b *BitSet) Clear(index int)

Clear the bit at 'index'.

func (*BitSet) ClearRange

func (b *BitSet) ClearRange(start, end int)

ClearRange clears the bits from 'start' to 'end', inclusive.

func (*BitSet) Clone

func (b *BitSet) Clone() *BitSet

Clone this BitSet.

func (*BitSet) Copy

func (b *BitSet) Copy(other *BitSet)

Copy the content of 'other' into this BitSet, making them equal.

func (*BitSet) Count

func (b *BitSet) Count() int

Count returns the number of set bits.

func (*BitSet) Data

func (b *BitSet) Data() []uint64

Data returns a copy of the underlying storage.

func (*BitSet) EnsureCapacity

func (b *BitSet) EnsureCapacity(words int)

EnsureCapacity ensures that the BitSet has enough underlying storage to accommodate setting a bit as high as index position 'words' x 64 - 1 without needing to allocate more storage.

func (*BitSet) Equal

func (b *BitSet) Equal(other *BitSet) bool

Equal returns true if this BitSet is equal to 'other'.

func (*BitSet) FirstSet

func (b *BitSet) FirstSet() int

FirstSet returns the first set bit. If no bits are set, then -1 is returned.

func (*BitSet) Flip

func (b *BitSet) Flip(index int)

Flip the bit at 'index'.

func (*BitSet) FlipRange

func (b *BitSet) FlipRange(start, end int)

FlipRange flips the bits from 'start' to 'end', inclusive.

func (*BitSet) LastSet

func (b *BitSet) LastSet() int

LastSet returns the last set bit. If no bits are set, then -1 is returned.

func (*BitSet) Load

func (b *BitSet) Load(data []uint64)

Load replaces the current data with the bits set in 'data'.

func (*BitSet) NextClear

func (b *BitSet) NextClear(start int) int

NextClear returns the next clear bit starting from 'start'.

func (*BitSet) NextSet

func (b *BitSet) NextSet(start int) int

NextSet returns the next set bit starting from 'start'. If no bits are set at or beyond 'start', then -1 is returned.

func (*BitSet) PreviousClear

func (b *BitSet) PreviousClear(start int) int

PreviousClear returns the previous clear bit starting from 'start'. If no bits are clear at or before 'start', then -1 is returned.

func (*BitSet) PreviousSet

func (b *BitSet) PreviousSet(start int) int

PreviousSet returns the previous set bit starting from 'start'. If no bits are set at or before 'start', then -1 is returned.

func (*BitSet) Reset

func (b *BitSet) Reset()

Reset the BitSet back to an empty state.

func (*BitSet) Set

func (b *BitSet) Set(index int)

Set the bit at 'index'.

func (*BitSet) SetRange

func (b *BitSet) SetRange(start, end int)

SetRange sets the bits from 'start' to 'end', inclusive.

func (*BitSet) State

func (b *BitSet) State(index int) bool

State returns the state of the bit at 'index'.

func (*BitSet) Trim

func (b *BitSet) Trim()

Trim the BitSet down to the minimum required to store the set bits.

type Numeric added in v1.66.0

type Numeric interface {
	constraints.Float | constraints.Integer
}

Numeric is a constraint that permits any integer or float type.

Directories

Path Synopsis
f64
Package geom provides geometry primitives.
Package geom provides geometry primitives.
Package rand provides a Randomizer based upon the crypto/rand package.
Package rand provides a Randomizer based upon the crypto/rand package.

Jump to

Keyboard shortcuts

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