开发中使用中使用到了math这个包,总结一下math包的主要方法与功能。
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 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(pp + qq),注意要避免不必要的溢出或下溢。特例如下:
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
参考:GO标准库中文文档