version: 1.10

package math

import "math"

Overview

Package math provides basic constants and mathematical functions.

This package does not guarantee bit-identical results across architectures.

Index

Examples

Package files

abs.go acosh.go asin.go asinh.go atan.go atan2.go atanh.go bits.go cbrt.go const.go copysign.go dim.go erf.go erfinv.go exp.go exp_asm.go expm1.go floor.go floor_asm.go frexp.go gamma.go hypot.go j0.go j1.go jn.go ldexp.go lgamma.go log.go log10.go log1p.go logb.go mod.go modf.go nextafter.go pow.go pow10.go remainder.go signbit.go sin.go sincos.go sinh.go sqrt.go tan.go tanh.go unsafe.go

Constants

  1. const (
  2. E = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113
  3. Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
  4. Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622
  5.  
  6. Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193
  7. SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774
  8. SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161
  9. SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339
  10.  
  11. Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
  12. Log2E = 1 / Ln2
  13. Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
  14. Log10E = 1 / Ln10
  15. )

Mathematical constants.

  1. const (
  2. MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
  3. SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
  4.  
  5. MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
  6. SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
  7. )

Floating-point limit values. Max is the largest finite value representable by
the type. SmallestNonzero is the smallest positive, non-zero value representable
by the type.

  1. const (
  2. MaxInt8 = 1<<7 - 1
  3. MinInt8 = -1 << 7
  4. MaxInt16 = 1<<15 - 1
  5. MinInt16 = -1 << 15
  6. MaxInt32 = 1<<31 - 1
  7. MinInt32 = -1 << 31
  8. MaxInt64 = 1<<63 - 1
  9. MinInt64 = -1 << 63
  10. MaxUint8 = 1<<8 - 1
  11. MaxUint16 = 1<<16 - 1
  12. MaxUint32 = 1<<32 - 1
  13. MaxUint64 = 1<<64 - 1
  14. )

Integer limit values.

func Abs

  1. func Abs(x float64) float64

Abs returns the absolute value of x.

Special cases are:

  1. AbsInf) = +Inf
  2. Abs(NaN) = NaN

func Acos

  1. func Acos(x float64) float64

Acos returns the arccosine, in radians, of x.

Special case is:

  1. Acos(x) = NaN if x < -1 or x > 1


Example:

  1. fmt.Printf("%.2f", math.Acos(1))
  2. // Output: 0.00

func Acosh

  1. func Acosh(x float64) float64

Acosh returns the inverse hyperbolic cosine of x.

Special cases are:

  1. Acosh(+Inf) = +Inf
  2. Acosh(x) = NaN if x < 1
  3. Acosh(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Acosh(1))
  2. // Output: 0.00

func Asin

  1. func Asin(x float64) float64

Asin returns the arcsine, in radians, of x.

Special cases are:

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


Example:

  1. fmt.Printf("%.2f", math.Asin(0))
  2. // Output: 0.00

func Asinh

  1. func Asinh(x float64) float64

Asinh returns the inverse hyperbolic sine of x.

Special cases are:

  1. Asinh0) = ±0
  2. AsinhInf) = ±Inf
  3. Asinh(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Asinh(0))
  2. // Output: 0.00

func Atan

  1. func Atan(x float64) float64

Atan returns the arctangent, in radians, of x.

Special cases are:

  1. Atan0) = ±0
  2. AtanInf) = ±Pi/2


Example:

  1. fmt.Printf("%.2f", math.Atan(0))
  2. // Output: 0.00

func Atan2

  1. func Atan2(y, x float64) float64

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):

  1. Atan2(y, NaN) = NaN
  2. Atan2(NaN, x) = NaN
  3. Atan2(+0, x>=0) = +0
  4. Atan2(-0, x>=0) = -0
  5. Atan2(+0, x<=-0) = +Pi
  6. Atan2(-0, x<=-0) = -Pi
  7. Atan2(y>0, 0) = +Pi/2
  8. Atan2(y<0, 0) = -Pi/2
  9. Atan2(+Inf, +Inf) = +Pi/4
  10. Atan2(-Inf, +Inf) = -Pi/4
  11. Atan2(+Inf, -Inf) = 3Pi/4
  12. Atan2(-Inf, -Inf) = -3Pi/4
  13. Atan2(y, +Inf) = 0
  14. Atan2(y>0, -Inf) = +Pi
  15. Atan2(y<0, -Inf) = -Pi
  16. Atan2(+Inf, x) = +Pi/2
  17. Atan2(-Inf, x) = -Pi/2


Example:

  1. fmt.Printf("%.2f", math.Atan2(0, 0))
  2. // Output: 0.00

func Atanh

  1. func Atanh(x float64) float64

Atanh returns the inverse hyperbolic tangent of x.

Special cases are:

  1. Atanh(1) = +Inf
  2. Atanh0) = ±0
  3. Atanh(-1) = -Inf
  4. Atanh(x) = NaN if x < -1 or x > 1
  5. Atanh(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Atanh(0))
  2. // Output: 0.00

func Cbrt

  1. func Cbrt(x float64) float64

Cbrt returns the cube root of x.

Special cases are:

  1. Cbrt0) = ±0
  2. CbrtInf) = ±Inf
  3. Cbrt(NaN) = NaN

func Ceil

  1. func Ceil(x float64) float64

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

Special cases are:

  1. Ceil0) = ±0
  2. CeilInf) = ±Inf
  3. Ceil(NaN) = NaN

func Copysign

  1. func Copysign(x, y float64) float64

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

func Cos

  1. func Cos(x float64) float64

Cos returns the cosine of the radian argument x.

Special cases are:

  1. CosInf) = NaN
  2. Cos(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Cos(math.Pi/2))
  2. // Output: 0.00

func Cosh

  1. func Cosh(x float64) float64

Cosh returns the hyperbolic cosine of x.

Special cases are:

  1. Cosh0) = 1
  2. CoshInf) = +Inf
  3. Cosh(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Cosh(0))
  2. // Output: 1.00

func Dim

  1. func Dim(x, y float64) float64

Dim returns the maximum of x-y or 0.

Special cases are:

  1. Dim(+Inf, +Inf) = NaN
  2. Dim(-Inf, -Inf) = NaN
  3. Dim(x, NaN) = Dim(NaN, x) = NaN

func Erf

  1. func Erf(x float64) float64

Erf returns the error function of x.

Special cases are:

  1. Erf(+Inf) = 1
  2. Erf(-Inf) = -1
  3. Erf(NaN) = NaN

func Erfc

  1. func Erfc(x float64) float64

Erfc returns the complementary error function of x.

Special cases are:

  1. Erfc(+Inf) = 0
  2. Erfc(-Inf) = 2
  3. Erfc(NaN) = NaN

func Erfcinv

  1. func Erfcinv(x float64) float64

Erfcinv returns the inverse of Erfc(x).

Special cases are:

  1. Erfcinv(0) = +Inf
  2. Erfcinv(2) = -Inf
  3. Erfcinv(x) = NaN if x < 0 or x > 2
  4. Erfcinv(NaN) = NaN

func Erfinv

  1. func Erfinv(x float64) float64

Erfinv returns the inverse error function of x.

Special cases are:

  1. Erfinv(1) = +Inf
  2. Erfinv(-1) = -Inf
  3. Erfinv(x) = NaN if x < -1 or x > 1
  4. Erfinv(NaN) = NaN

func Exp

  1. func Exp(x float64) float64

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

Special cases are:

  1. Exp(+Inf) = +Inf
  2. Exp(NaN) = NaN

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

func Exp2

  1. func Exp2(x float64) float64

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

Special cases are the same as Exp.

func Expm1

  1. func Expm1(x float64) float64

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:

  1. Expm1(+Inf) = +Inf
  2. Expm1(-Inf) = -1
  3. Expm1(NaN) = NaN

Very large values overflow to -1 or +Inf.

func Float32bits

  1. func Float32bits(f float32) uint32

Float32bits returns the IEEE 754 binary representation of f.

func Float32frombits

  1. func Float32frombits(b uint32) float32

Float32frombits returns the floating point number corresponding to the IEEE 754
binary representation b.

func Float64bits

  1. func Float64bits(f float64) uint64

Float64bits returns the IEEE 754 binary representation of f.

func Float64frombits

  1. func Float64frombits(b uint64) float64

Float64frombits returns the floating point number corresponding the IEEE 754
binary representation b.

func Floor

  1. func Floor(x float64) float64

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

Special cases are:

  1. Floor0) = ±0
  2. FloorInf) = ±Inf
  3. Floor(NaN) = NaN

func Frexp

  1. func Frexp(f float64) (frac float64, 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:

  1. Frexp0) = ±0, 0
  2. FrexpInf) = ±Inf, 0
  3. Frexp(NaN) = NaN, 0

func Gamma

  1. func Gamma(x float64) float64

Gamma returns the Gamma function of x.

Special cases are:

  1. Gamma(+Inf) = +Inf
  2. Gamma(+0) = +Inf
  3. Gamma(-0) = -Inf
  4. Gamma(x) = NaN for integer x < 0
  5. Gamma(-Inf) = NaN
  6. Gamma(NaN) = NaN

func Hypot

  1. func Hypot(p, q float64) float64

Hypot returns Sqrt(pp + qq), taking care to avoid unnecessary overflow and
underflow.

Special cases are:

  1. HypotInf, q) = +Inf
  2. Hypot(p, ±Inf) = +Inf
  3. Hypot(NaN, q) = NaN
  4. Hypot(p, NaN) = NaN

func Ilogb

  1. func Ilogb(x float64) int

Ilogb returns the binary exponent of x as an integer.

Special cases are:

  1. IlogbInf) = MaxInt32
  2. Ilogb(0) = MinInt32
  3. Ilogb(NaN) = MaxInt32

func Inf

  1. func Inf(sign int) float64

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

func IsInf

  1. func IsInf(f float64, 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

  1. func IsNaN(f float64) (is bool)

IsNaN reports whether f is an IEEE 754 ``not-a-number’’ value.

func J0

  1. func J0(x float64) float64

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

Special cases are:

  1. J0Inf) = 0
  2. J0(0) = 1
  3. J0(NaN) = NaN

func J1

  1. func J1(x float64) float64

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

Special cases are:

  1. J1Inf) = 0
  2. J1(NaN) = NaN

func Jn

  1. func Jn(n int, x float64) float64

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

Special cases are:

  1. Jn(n, ±Inf) = 0
  2. Jn(n, NaN) = NaN

func Ldexp

  1. func Ldexp(frac float64, exp int) float64

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

Special cases are:

  1. Ldexp0, exp) = ±0
  2. LdexpInf, exp) = ±Inf
  3. Ldexp(NaN, exp) = NaN

func Lgamma

  1. func Lgamma(x float64) (lgamma float64, sign int)

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

Special cases are:

  1. Lgamma(+Inf) = +Inf
  2. Lgamma(0) = +Inf
  3. Lgamma(-integer) = +Inf
  4. Lgamma(-Inf) = -Inf
  5. Lgamma(NaN) = NaN

func Log

  1. func Log(x float64) float64

Log returns the natural logarithm of x.

Special cases are:

  1. Log(+Inf) = +Inf
  2. Log(0) = -Inf
  3. Log(x < 0) = NaN
  4. Log(NaN) = NaN

func Log10

  1. func Log10(x float64) float64

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

func Log1p

  1. func Log1p(x float64) float64

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:

  1. Log1p(+Inf) = +Inf
  2. Log1p0) = ±0
  3. Log1p(-1) = -Inf
  4. Log1p(x < -1) = NaN
  5. Log1p(NaN) = NaN

func Log2

  1. func Log2(x float64) float64

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

func Logb

  1. func Logb(x float64) float64

Logb returns the binary exponent of x.

Special cases are:

  1. LogbInf) = +Inf
  2. Logb(0) = -Inf
  3. Logb(NaN) = NaN

func Max

  1. func Max(x, y float64) float64

Max returns the larger of x or y.

Special cases are:

  1. Max(x, +Inf) = Max(+Inf, x) = +Inf
  2. Max(x, NaN) = Max(NaN, x) = NaN
  3. Max(+0, ±0) = Max0, +0) = +0
  4. Max(-0, -0) = -0

func Min

  1. func Min(x, y float64) float64

Min returns the smaller of x or y.

Special cases are:

  1. Min(x, -Inf) = Min(-Inf, x) = -Inf
  2. Min(x, NaN) = Min(NaN, x) = NaN
  3. Min(-0, ±0) = Min0, -0) = -0

func Mod

  1. func Mod(x, y float64) float64

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:

  1. ModInf, y) = NaN
  2. Mod(NaN, y) = NaN
  3. Mod(x, 0) = NaN
  4. Mod(x, ±Inf) = x
  5. Mod(x, NaN) = NaN

func Modf

  1. func Modf(f float64) (int float64, frac float64)

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

Special cases are:

  1. ModfInf) = ±Inf, NaN
  2. Modf(NaN) = NaN, NaN

func NaN

  1. func NaN() float64

NaN returns an IEEE 754 ``not-a-number’’ value.

func Nextafter

  1. func Nextafter(x, y float64) (r float64)

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

Special cases are:

  1. Nextafter(x, x) = x
  2. Nextafter(NaN, y) = NaN
  3. Nextafter(x, NaN) = NaN

func Nextafter32

  1. func Nextafter32(x, y float32) (r float32)

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

Special cases are:

  1. Nextafter32(x, x) = x
  2. Nextafter32(NaN, y) = NaN
  3. Nextafter32(x, NaN) = NaN

func Pow

  1. func Pow(x, y float64) float64

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

Special cases are (in order):

  1. Pow(x, ±0) = 1 for any x
  2. Pow(1, y) = 1 for any y
  3. Pow(x, 1) = x for any x
  4. Pow(NaN, y) = NaN
  5. Pow(x, NaN) = NaN
  6. Pow0, y) = ±Inf for y an odd integer < 0
  7. Pow0, -Inf) = +Inf
  8. Pow0, +Inf) = +0
  9. Pow0, y) = +Inf for finite y < 0 and not an odd integer
  10. Pow0, y) = ±0 for y an odd integer > 0
  11. Pow0, y) = +0 for finite y > 0 and not an odd integer
  12. Pow(-1, ±Inf) = 1
  13. Pow(x, +Inf) = +Inf for |x| > 1
  14. Pow(x, -Inf) = +0 for |x| > 1
  15. Pow(x, +Inf) = +0 for |x| < 1
  16. Pow(x, -Inf) = +Inf for |x| < 1
  17. Pow(+Inf, y) = +Inf for y > 0
  18. Pow(+Inf, y) = +0 for y < 0
  19. Pow(-Inf, y) = Pow(-0, -y)
  20. Pow(x, y) = NaN for finite x < 0 and finite non-integer y

func Pow10

  1. func Pow10(n int) float64

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

Special cases are:

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

func Remainder

  1. func Remainder(x, y float64) float64

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

Special cases are:

  1. RemainderInf, y) = NaN
  2. Remainder(NaN, y) = NaN
  3. Remainder(x, 0) = NaN
  4. Remainder(x, ±Inf) = x
  5. Remainder(x, NaN) = NaN

func Round

  1. func Round(x float64) float64

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

Special cases are:

  1. Round0) = ±0
  2. RoundInf) = ±Inf
  3. Round(NaN) = NaN

func RoundToEven

  1. func RoundToEven(x float64) float64

RoundToEven returns the nearest integer, rounding ties to even.

Special cases are:

  1. RoundToEven0) = ±0
  2. RoundToEvenInf) = ±Inf
  3. RoundToEven(NaN) = NaN

func Signbit

  1. func Signbit(x float64) bool

Signbit returns true if x is negative or negative zero.

func Sin

  1. func Sin(x float64) float64

Sin returns the sine of the radian argument x.

Special cases are:

  1. Sin0) = ±0
  2. SinInf) = NaN
  3. Sin(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Sin(math.Pi))
  2. // Output: 0.00

func Sincos

  1. func Sincos(x float64) (sin, cos float64)

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

Special cases are:

  1. Sincos0) = ±0, 1
  2. SincosInf) = NaN, NaN
  3. Sincos(NaN) = NaN, NaN


Example:

  1. sin, cos := math.Sincos(0)
  2. fmt.Printf("%.2f, %.2f", sin, cos)
  3. // Output: 0.00, 1.00

func Sinh

  1. func Sinh(x float64) float64

Sinh returns the hyperbolic sine of x.

Special cases are:

  1. Sinh0) = ±0
  2. SinhInf) = ±Inf
  3. Sinh(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Sinh(0))
  2. // Output: 0.00

func Sqrt

  1. func Sqrt(x float64) float64

Sqrt returns the square root of x.

Special cases are:

  1. Sqrt(+Inf) = +Inf
  2. Sqrt0) = ±0
  3. Sqrt(x < 0) = NaN
  4. Sqrt(NaN) = NaN


Example:

  1. const (
  2. a = 3
  3. b = 4
  4. )
  5. c := math.Sqrt(a*a + b*b)
  6. fmt.Printf("%.1f", c)
  7. // Output: 5.0

func Tan

  1. func Tan(x float64) float64

Tan returns the tangent of the radian argument x.

Special cases are:

  1. Tan0) = ±0
  2. TanInf) = NaN
  3. Tan(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Tan(0))
  2. // Output: 0.00

func Tanh

  1. func Tanh(x float64) float64

Tanh returns the hyperbolic tangent of x.

Special cases are:

  1. Tanh0) = ±0
  2. TanhInf) = ±1
  3. Tanh(NaN) = NaN


Example:

  1. fmt.Printf("%.2f", math.Tanh(0))
  2. // Output: 0.00

func Trunc

  1. func Trunc(x float64) float64

Trunc returns the integer value of x.

Special cases are:

  1. Trunc0) = ±0
  2. TruncInf) = ±Inf
  3. Trunc(NaN) = NaN

func Y0

  1. func Y0(x float64) float64

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

Special cases are:

  1. Y0(+Inf) = 0
  2. Y0(0) = -Inf
  3. Y0(x < 0) = NaN
  4. Y0(NaN) = NaN

func Y1

  1. func Y1(x float64) float64

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

Special cases are:

  1. Y1(+Inf) = 0
  2. Y1(0) = -Inf
  3. Y1(x < 0) = NaN
  4. Y1(NaN) = NaN

func Yn

  1. func Yn(n int, x float64) float64

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

Special cases are:

  1. Yn(n, +Inf) = 0
  2. Yn(n 0, 0) = -Inf
  3. Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
  4. Yn(n, x < 0) = NaN
  5. Yn(n, NaN) = NaN

Subdirectories