package math

import "math"

math包提供了基本的数学常数和数学函数。

Index

返回首页


  • Constants
  • func NaN() float64
  • func IsNaN(f float64) (is bool)
  • func Inf(sign int) float64
  • func IsInf(f float64, sign int) bool
  • func Float32bits(f float32) uint32
  • func Float32frombits(b uint32) float32
  • func Float64bits(f float64) uint64
  • func Float64frombits(b uint64) float64
  • func Signbit(x float64) bool
  • func Copysign(x, y float64) float64
  • func Ceil(x float64) float64
  • func Floor(x float64) float64
  • func Trunc(x float64) float64
  • func Modf(f float64) (int float64, frac float64)
  • func Nextafter(x, y float64) (r float64)
  • func Abs(x float64) float64
  • func Max(x, y float64) float64
  • func Min(x, y float64) float64
  • func Dim(x, y float64) float64
  • func Mod(x, y float64) float64
  • func Remainder(x, y float64) float64
  • func Sqrt(x float64) float64
  • func Cbrt(x float64) float64
  • func Hypot(p, q float64) float64
  • func Sin(x float64) float64
  • func Cos(x float64) float64
  • func Tan(x float64) float64
  • func Sincos(x float64) (sin, cos float64)
  • func Asin(x float64) float64
  • func Acos(x float64) float64
  • func Atan(x float64) float64
  • func Atan2(y, x float64) float64
  • func Sinh(x float64) float64
  • func Cosh(x float64) float64
  • func Tanh(x float64) float64
  • func Asinh(x float64) float64
  • func Acosh(x float64) float64
  • func Atanh(x float64) float64
  • func Log(x float64) float64
  • func Log1p(x float64) float64
  • func Log2(x float64) float64
  • func Log10(x float64) float64
  • func Logb(x float64) float64
  • func Ilogb(x float64) int
  • func Frexp(f float64) (frac float64, exp int)
  • func Ldexp(frac float64, exp int) float64
  • func Exp(x float64) float64
  • func Expm1(x float64) float64
  • func Exp2(x float64) float64
  • func Pow(x, y float64) float64
  • func Pow10(e int) float64
  • func Gamma(x float64) float64
  • func Lgamma(x float64) (lgamma float64, sign int)
  • func Erf(x float64) float64
  • func Erfc(x float64) float64
  • func J0(x float64) float64
  • func J1(x float64) float64
  • func Jn(n int, x float64) float64
  • func Y0(x float64) float64
  • func Y1(x float64) float64
  • func Yn(n int, x float64) float64
  • Constants

    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
    )

    数学常数,参见:http://oeis.org/Axxxxxx

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

    浮点数的取值极限。Max是该类型所能表示的最大有限值;SmallestNonzero是该类型所能表示的最小非零正数值。

    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 NaN

    func NaN() float64

    函数返回一个IEEE 754“这不是一个数字”值。

    func IsNaN

    func IsNaN(f float64) (is bool)

    报告f是否表示一个NaN(Not A Number)值。

    func Inf

    func Inf(sign int) float64

    如果sign>=0函数返回正无穷大,否则返回负无穷大。

    func IsInf

    func IsInf(f float64, sign int) bool

    如果sign > 0,f是正无穷大时返回真;如果sign<0,f是负无穷大时返回真;sign==0则f是两种无穷大时都返回真。

    func Float32bits

    func Float32bits(f float32) uint32

    函数返回浮点数f的IEEE 754格式二进制表示对应的4字节无符号整数。

    func Float32frombits

    func Float32frombits(b uint32) float32

    函数返回无符号整数b对应的IEEE 754格式二进制表示的4字节浮点数。

    func Float64bits

    func Float64bits(f float64) uint64

    函数返回浮点数f的IEEE 754格式二进制表示对应的8字节无符号整数。

    func Float64frombits

    func Float64frombits(b uint64) float64

    函数返回无符号整数b对应的IEEE 754格式二进制表示的8字节浮点数。

    func Signbit

    func Signbit(x float64) bool

    如果x是一个负数或者负零,返回真。

    func Copysign

    func Copysign(x, y float64) float64

    返回拥有x的量值(绝对值)和y的标志位(正负号)的浮点数。

    func Ceil

    func Ceil(x float64) float64

    返回不小于x的最小整数(的浮点值),特例如下:

    Ceil(±0) = ±0
    Ceil(±Inf) = ±Inf
    Ceil(NaN) = NaN

    func Floor

    func Floor(x float64) float64

    返回不大于x的最小整数(的浮点值),特例如下:

    Floor(±0) = ±0
    Floor(±Inf) = ±Inf
    Floor(NaN) = NaN

    func Trunc

    func Trunc(x float64) float64

    返回x的整数部分(的浮点值)。特例如下:

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

    func Modf

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

    返回f的整数部分和小数部分,结果的正负号和都x相同;特例如下:

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

    func Nextafter

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

    参数x到参数y的方向上,下一个可表示的数值;如果x==y将返回x。特例如下:

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

    func Abs

    func Abs(x float64) float64

    返回x的绝对值;特例如下:

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

    func Max

    func Max(x, y float64) float64

    返回x和y中最大值,特例如下:

    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

    func Min

    func Min(x, y float64) float64

    返回x和y中最小值,特例如下:

    Min(x, -Inf) = Min(-Inf, x) = -Inf
    Min(x, NaN) = Min(NaN, x) = NaN
    Min(-0, ±0) = Min(±0, -0) = -0

    func Dim

    func Dim(x, y float64) float64

    函数返回x-y和0中的最大值,特殊情况:

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

    func Mod

    func Mod(x, y float64) float64

    取余运算,可以理解为 x-Trunc(x/y)*y,结果的正负号和x相同;特例如下:

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

    func Remainder

    func Remainder(x, y float64) float64

    IEEE 754差数求值,即x减去最接近x/y的整数值(如果有两个整数与x/y距离相同,则取其中的偶数)与y的乘积。特例如下:

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

    func Sqrt

    func Sqrt(x float64) float64

    返回x的二次方根,特例如下:

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

    func Cbrt

    func Cbrt(x float64) float64

    返回x的三次方根,特例如下:

    Cbrt(±0) = ±0
    Cbrt(±Inf) = ±Inf
    Cbrt(NaN) = NaN

    func Hypot

    func Hypot(p, q float64) float64

    返回Sqrt(p*p + q*q),注意要避免不必要的溢出或下溢。特例如下:

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

    func Sin

    func Sin(x float64) float64

    求正弦。特例如下:

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

    func Cos

    func Cos(x float64) float64

    求余弦。特例如下:

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

    func Tan

    func Tan(x float64) float64

    求正切。特例如下:

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

    func Sincos

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

    函数返回Sin(x), Cos(x)。特例如下:

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

    func Asin

    func Asin(x float64) float64

    求反正弦(x是弧度)。特例如下:

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

    func Acos

    func Acos(x float64) float64

    求反余弦(x是弧度)。特例如下:

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

    func Atan

    func Atan(x float64) float64

    求反正切(x是弧度)。特例如下:

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

    func Atan2

    func Atan2(y, x float64) float64

    类似Atan(y/x),但会根据x,y的正负号确定象限。特例如下(前面的优先):

    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 Sinh

    func Sinh(x float64) float64

    求双曲正弦,特例如下:

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

    func Cosh

    func Cosh(x float64) float64

    求双曲余弦,特例如下:

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

    func Tanh

    func Tanh(x float64) float64

    求双曲正切,特例如下:

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

    func Asinh

    func Asinh(x float64) float64

    求反双曲正弦,特例如下:

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

    func Acosh

    func Acosh(x float64) float64

    求反双曲余弦,特例如下:

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

    func Atanh

    func Atanh(x float64) float64

    求反双曲正切,特例如下:

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

    func Log

    func Log(x float64) float64

    求自然对数,特例如下:

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

    func Log1p

    func Log1p(x float64) float64

    等价于Log(1+x)。但是在x接近0时,本函数更加精确;特例如下:

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

    func Log2

    func Log2(x float64) float64

    求2为底的对数;特例和Log相同。

    func Log10

    func Log10(x float64) float64

    求10为底的对数;特例和Log相同。

    func Logb

    func Logb(x float64) float64

    返回x的二进制指数值,可以理解为Trunc(Log2(x));特例如下:

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

    func Ilogb

    func Ilogb(x float64) int

    类似Logb,但返回值是整型;特例如下:

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

    func Frexp

    func Frexp(f float64) (frac float64, exp int)

    返回一个标准化小数frac和2的整型指数exp,满足f == frac * 2**exp,且0.5 <= Abs(frac) < 1;特例如下:

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

    func Ldexp

    func Ldexp(frac float64, exp int) float64

    Frexp的反函数,返回 frac * 2**exp。特例如下:

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

    func Exp

    func Exp(x float64) float64

    返回E**x;x绝对值很大时可能会溢出为0或者+Inf,x绝对值很小时可能会下溢为1。特例如下:

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

    func Expm1

    func Expm1(x float64) float64

    等价于Exp(x)-1,但是在x接近零时更精确;x绝对值很大时可能会溢出为-1或+Inf。特例如下:

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

    func Exp2

    func Exp2(x float64) float64

    返回2**x;特例和Exp相同。

    func Pow

    func Pow(x, y float64) float64

    返回x**y;特例如下(前面的优先):

    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

    func Pow10(e int) float64

    返回10**e;特例如下:

    Pow10(e) = +Inf for e > 309
    Pow10(e) = 0 for e < -324
    

    func Gamma

    func Gamma(x float64) float64

    伽玛函数(当x为正整数时,值为(x-1)!)。特例如下:

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

    func Lgamma

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

    返回Gamma(x)的自然对数和正负号。特例如下:

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

    func Erf

    func Erf(x float64) float64

    误差函数,特例如下:

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

    func Erfc

    func Erfc(x float64) float64

    余补误差函数,特例如下:

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

    func J0

    func J0(x float64) float64

    第一类贝塞尔函数,0阶。特例如下:

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

    func J1

    func J1(x float64) float64

    第一类贝塞尔函数,1阶。特例如下:

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

    func Jn

    func Jn(n int, x float64) float64

    第一类贝塞尔函数,n阶。特例如下:

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

    func Y0

    func Y0(x float64) float64

    第二类贝塞尔函数,0阶。特例如下:

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

    func Y1

    func Y1(x float64) float64

    第二类贝塞尔函数,1阶。特例如下:

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

    func Yn

    func Yn(n int, x float64) float64

    第二类贝塞尔函数,n阶。特例如下:

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