Go Home Page
The Go Programming Language

Package math

import "math"

The math package provides basic constants and mathematical functions.

Package files

acosh.go asin.go asin_decl.go asinh.go atan.go atan2.go atan2_decl.go atan_decl.go atanh.go bits.go cbrt.go const.go copysign.go erf.go exp.go exp2_decl.go exp_decl.go expm1.go fabs.go fabs_decl.go fdim.go floor.go floor_decl.go fmod.go fmod_decl.go frexp.go frexp_decl.go hypot.go hypot_decl.go ldexp.go ldexp_decl.go lgamma.go log.go log1p.go log1p_decl.go log_decl.go modf.go modf_decl.go nextafter.go pow.go pow10.go sin.go sin_decl.go sincos.go sincos_decl.go sinh.go sqrt.go sqrt_decl.go sqrt_port.go tan.go tan_decl.go tanh.go unsafe.go

Constants

Mathematical constants. Reference: http://www.research.att.com/~njas/sequences/Axxxxxx

const (
    E   = 2.71828182845904523536028747135266249775724709369995957496696763 // A001113
    Pi  = 3.14159265358979323846264338327950288419716939937510582097494459 // A000796
    Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // A001622

    Sqrt2   = 1.41421356237309504880168872420969807856967187537694807317667974 // A002193
    SqrtE   = 1.64872127070012814684865078781416357165377610071014801157507931 // A019774
    SqrtPi  = 1.77245385090551602729816748334114518279754945612238712821380779 // A002161
    SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // A139339

    Ln2    = 0.693147180559945309417232121458176568075500134360255254120680009 // A002162
    Log2E  = 1 / Ln2
    Ln10   = 2.30258509299404568401799145468436420760110148862877297603332790 // A002392
    Log10E = 1 / Ln10
)

Floating-point limit values. Max is the largest finite value representable by the type. Min is the smallest nonzero value representable by the type.

const (
    MaxFloat32 = 3.40282346638528859811704183484516925440e+38  /* 2^127 * (2^24 - 1) / 2^23 */
    MinFloat32 = 1.401298464324817070923729583289916131280e-45 /* 1 / 2^(127 - 1 + 23) */

    MaxFloat64 = 1.797693134862315708145274237317043567981e+308 /* 2^1023 * (2^53 - 1) / 2^52 */
    MinFloat64 = 4.940656458412465441765687928682213723651e-324 /* 1 / 2^(1023 - 1 + 52) */
)

Integer limit values.

const (
    MaxInt8   = 1<<7 - 1
    MinInt8   = -1 << 7
    MaxInt16  = 1<<15 - 1
    MinInt16  = -1 << 15
    MaxInt32  = 1<<31 - 1
    MinInt32  = -1 << 31
    MaxInt64  = 1<<63 - 1
    MinInt64  = -1 << 63
    MaxUint8  = 1<<8 - 1
    MaxUint16 = 1<<16 - 1
    MaxUint32 = 1<<32 - 1
    MaxUint64 = 1<<64 - 1
)

func Acos

func Acos(x float64) float64

func Acosh

func Acosh(x float64) float64

Acosh(x) calculates the inverse hyperbolic cosine of x.

Special cases are:

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

func Asin

func Asin(x float64) float64

func Asinh

func Asinh(x float64) float64

Asinh(x) calculates the inverse hyperbolic sine of x.

Special cases are:

Asinh(+Inf) = +Inf
Asinh(-Inf) = -Inf
Asinh(NaN) = NaN

func Atan

func Atan(x float64) float64

func Atan2

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

Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(0, x>=0) = 0
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

func Atanh(x float64) float64

Atanh(x) calculates the inverse hyperbolic tangent of x.

Special cases are:

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

func Cbrt

func Cbrt(x float64) float64

Cbrt returns the cube root of its argument.

Special cases are:

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

func Ceil

func Ceil(x float64) float64

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

Special cases are:

Ceil(+Inf) = +Inf
Ceil(-Inf) = -Inf
Ceil(NaN) = NaN

func Copysign

func Copysign(x, y float64) float64

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

func Cos

func Cos(x float64) float64

Cos returns the cosine of x.

func Cosh

func Cosh(x float64) float64

Cosh returns the hyperbolic cosine of x.

func Erf

func Erf(x float64) float64

Erf(x) returns the error function of x.

Special cases are:

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

func Erfc

func Erfc(x float64) float64

Erfc(x) returns the complementary error function of x.

Special cases are:

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

func Exp

func Exp(x float64) float64

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

func Exp2(x float64) float64

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

Special cases are the same as Exp.

func Expm1

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:

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

Very large values overflow to -1 or +Inf.

func Fabs

func Fabs(x float64) float64

func Fdim

func Fdim(x, y float64) float64

Fdim returns the maximum of x-y or 0.

func Float32bits

func Float32bits(f float32) uint32

Float32bits returns the IEEE 754 binary representation of f.

func Float32frombits

func Float32frombits(b uint32) float32

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

func Float64bits

func Float64bits(f float64) uint64

Float64bits returns the IEEE 754 binary representation of f.

func Float64frombits

func Float64frombits(b uint64) float64

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

func Floor

func Floor(x float64) float64

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

Special cases are:

Floor(+Inf) = +Inf
Floor(-Inf) = -Inf
Floor(NaN) = NaN

func Fmax

func Fmax(x, y float64) float64

Fmax returns the larger of x or y.

func Fmin

func Fmin(x, y float64) float64

Fmin returns the smaller of x or y.

func Fmod

func Fmod(x, y float64) float64

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

if x is not finite, Fmod returns NaN
if y is 0 or NaN, Fmod returns NaN

func Frexp

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<sup>exp</sup>, with the absolute value of frac in the interval [½, 1).

func Hypot

func Hypot(x, y float64) float64

func Inf

func Inf(sign int) float64

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

func IsInf

func IsInf(f float64, sign int) bool

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

func IsNaN

func IsNaN(f float64) (is bool)

IsNaN returns whether f is an IEEE 754 “not-a-number” value.

func Ldexp

func Ldexp(frac float64, exp int) float64

Ldexp is the inverse of Frexp. It returns frac × 2<sup>exp</sup>.

func Lgamma

func Lgamma(x float64) (lgamma float64, 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

func Log(x float64) float64

func Log10

func Log10(x float64) float64

func Log1p

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:

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

func Log2

func Log2(x float64) float64

func Modf

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

func NaN

func NaN() float64

NaN returns an IEEE 754 “not-a-number” value.

func Nextafter

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

Nextafter returns the next representable value after x towards y. If x == y, then x is returned.

Special cases are:

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

func Pow

func Pow(x, y float64) float64

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

func Pow10

func Pow10(e int) float64

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

func Sin

func Sin(x float64) float64

Sin returns the sine of x.

func Sincos

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

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

Special conditions are:

Sincos(+Inf) = NaN, NaN
Sincos(-Inf) = NaN, NaN
Sincos(NaN) = NaN, NaN

func Sinh

func Sinh(x float64) float64

Sinh returns the hyperbolic sine of x.

func Sqrt

func Sqrt(x float64) float64

func Tan

func Tan(x float64) float64

func Tanh

func Tanh(x float64) float64

Tanh computes the hyperbolic tangent of x.

func Trunc

func Trunc(x float64) float64

Trunc returns the integer value of x.

Special cases are:

Trunc(+Inf) = +Inf
Trunc(-Inf) = -Inf
Trunc(NaN) = NaN

Bugs

The manual should define the special cases for all of these functions.