package math
import "math"
math 包提供基本的常量和数学函数。
本包不保证跨架构的位完全一致结果。
Index
- Constants
- func Abs(x float64) float64
- func Acos(x float64) float64
- func Acosh(x float64) float64
- func Asin(x float64) float64
- func Asinh(x float64) float64
- func Atan(x float64) float64
- func Atan2(y, x float64) float64
- func Atanh(x float64) float64
- func Cbrt(x float64) float64
- func Ceil(x float64) float64
- func Copysign(f, sign float64) float64
- func Cos(x float64) float64
- func Cosh(x float64) float64
- func Dim(x, y float64) float64
- func Erf(x float64) float64
- func Erfc(x float64) float64
- func Erfcinv(x float64) float64
- func Erfinv(x float64) float64
- func Exp(x float64) float64
- func Exp2(x float64) float64
- func Expm1(x float64) float64
- func FMA(x, y, z float64) float64
- func Float32bits(f float32) uint32
- func Float32frombits(b uint32) float32
- func Float64bits(f float64) uint64
- func Float64frombits(b uint64) float64
- func Floor(x float64) float64
- func Frexp(f float64) (frac float64, exp int)
- func Gamma(x float64) float64
- func Hypot(p, q float64) float64
- func Ilogb(x float64) int
- func Inf(sign int) float64
- func IsInf(f float64, sign int) bool
- func IsNaN(f float64) (is bool)
- func J0(x float64) float64
- func J1(x float64) float64
- func Jn(n int, x float64) float64
- func Ldexp(frac float64, exp int) float64
- func Lgamma(x float64) (lgamma float64, sign int)
- func Log(x float64) float64
- func Log10(x float64) float64
- func Log1p(x float64) float64
- func Log2(x float64) float64
- func Logb(x float64) float64
- func Max(x, y float64) float64
- func Min(x, y float64) float64
- func Mod(x, y float64) float64
- func Modf(f float64) (integer float64, fractional float64)
- func NaN() float64
- func Nextafter(x, y float64) (r float64)
- func Nextafter32(x, y float32) (r float32)
- func Pow(x, y float64) float64
- func Pow10(n int) float64
- func Remainder(x, y float64) float64
- func Round(x float64) float64
- func RoundToEven(x float64) float64
- func Signbit(x float64) bool
- func Sin(x float64) float64
- func Sincos(x float64) (sin, cos float64)
- func Sinh(x float64) float64
- func Sqrt(x float64) float64
- func Tan(x float64) float64
- func Tanh(x float64) float64
- func Trunc(x float64) float64
- func Y0(x float64) float64
- func Y1(x float64) float64
- func Yn(n int, x float64) float64
Examples
- Abs
- Acos
- Acosh
- Asin
- Asinh
- Atan
- Atan2
- Atanh
- Cbrt
- Ceil
- Copysign
- Cos
- Cosh
- Dim
- Exp
- Exp2
- Expm1
- Floor
- Log
- Log10
- Log2
- Mod
- Modf
- Pow
- Pow10
- Remainder
- Round
- RoundToEven
- Sin
- Sincos
- Sinh
- Sqrt
- Tan
- Tanh
- Trunc
Constants
const ( E = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113 Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796 Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622 Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193 SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774 SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161 SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339 Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162 Log2E = 1 / Ln2 Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392 Log10E = 1 / Ln10 )
数学常量。
const ( MaxFloat32 = 0x1p127 * (1 + (1 - 0x1p-23)) // 3.40282346638528859811704183484516925440e+38 SmallestNonzeroFloat32 = 0x1p-126 * 0x1p-23 // 1.401298464324817070923729583289916131280e-45 MaxFloat64 = 0x1p1023 * (1 + (1 - 0x1p-52)) // 1.79769313486231570814527423731704356798070e+308 SmallestNonzeroFloat64 = 0x1p-1022 * 0x1p-52 // 4.9406564584124654417656879286822137236505980e-324 )
浮点极限值。 Max 是该类型可表示的最大有限值。 SmallestNonzero 是该类型可表示的最小正非零值。
const ( MaxInt = 1<<(intSize-1) - 1 // MaxInt32 或 MaxInt64,取决于 intSize。 MinInt = -1 << (intSize - 1) // MinInt32 或 MinInt64,取决于 intSize。 MaxInt8 = 1<<7 - 1 // 127 MinInt8 = -1 << 7 // -128 MaxInt16 = 1<<15 - 1 // 32767 MinInt16 = -1 << 15 // -32768 MaxInt32 = 1<<31 - 1 // 2147483647 MinInt32 = -1 << 31 // -2147483648 MaxInt64 = 1<<63 - 1 // 9223372036854775807 MinInt64 = -1 << 63 // -9223372036854775808 MaxUint = 1<<intSize - 1 // MaxUint32 或 MaxUint64,取决于 intSize。 MaxUint8 = 1<<8 - 1 // 255 MaxUint16 = 1<<16 - 1 // 65535 MaxUint32 = 1<<32 - 1 // 4294967295 MaxUint64 = 1<<64 - 1 // 18446744073709551615 )
整数极限值。
Functions
func Abs
func Abs(x float64) float64
Abs 返回 x 的绝对值。
特殊情况为:
Abs(±Inf) = +Inf Abs(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { x := math.Abs(-2) fmt.Printf("%.1f\n", x) y := math.Abs(2) fmt.Printf("%.1f\n", y) }
Output:
2.0 2.0
func Acos
func Acos(x float64) float64
Acos 返回 x 的反余弦(以弧度为单位)。
特殊情况为:
Acos(x) = NaN 若 x < -1 或 x > 1
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Acos(1)) }
Output:
0.00
func Acosh
func Acosh(x float64) float64
Acosh 返回 x 的反双曲余弦。
特殊情况为:
Acosh(+Inf) = +Inf Acosh(x) = NaN 若 x < 1 Acosh(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Acosh(1)) }
Output:
0.00
func Asin
func Asin(x float64) float64
Asin 返回 x 的反正弦(以弧度为单位)。
特殊情况为:
Asin(±0) = ±0 Asin(x) = NaN 若 x < -1 或 x > 1
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Asin(0)) }
Output:
0.00
func Asinh
func Asinh(x float64) float64
Asinh 返回 x 的反双曲正弦。
特殊情况为:
Asinh(±0) = ±0 Asinh(±Inf) = ±Inf Asinh(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Asinh(0)) }
Output:
0.00
func Atan
func Atan(x float64) float64
Atan 返回 x 的反正切(以弧度为单位)。
特殊情况为:
Atan(±0) = ±0 Atan(±Inf) = ±Pi/2
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Atan(0)) }
Output:
0.00
func Atan2
func Atan2(y, x float64) float64
Atan2 返回 y/x 的反正切,使用 两者的符号来确定返回值的象限。
特殊情况为(按顺序):
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
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Atan2(0, 0)) }
Output:
0.00
func Atanh
func Atanh(x float64) float64
Atanh 返回 x 的反双曲正切。
特殊情况为:
Atanh(1) = +Inf Atanh(±0) = ±0 Atanh(-1) = -Inf Atanh(x) = NaN 若 x < -1 或 x > 1 Atanh(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Atanh(0)) }
Output:
0.00
func Cbrt
func Cbrt(x float64) float64
Cbrt 返回 x 的立方根。
特殊情况为:
Cbrt(±0) = ±0 Cbrt(±Inf) = ±Inf Cbrt(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f\n", math.Cbrt(8)) fmt.Printf("%.2f\n", math.Cbrt(27)) }
Output:
2.00 3.00
func Ceil
func Ceil(x float64) float64
Ceil 返回大于或等于 x 的最小整数值。
特殊情况为:
Ceil(±0) = ±0 Ceil(±Inf) = ±Inf Ceil(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { c := math.Ceil(1.49) fmt.Printf("%.1f", c) }
Output:
2.0
func Copysign
func Copysign(f, sign float64) float64
Copysign 返回一个值,其大小为 f 的大小,
符号为 sign 的符号。
Output:Example
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.2f", math.Copysign(3.2, -1))
}
-3.20
func Cos
func Cos(x float64) float64
Cos 返回弧度参数 x 的余弦。
特殊情况为:
Cos(±Inf) = NaN Cos(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Cos(math.Pi/2)) }
Output:
0.00
func Cosh
func Cosh(x float64) float64
Cosh 返回 x 的双曲余弦。
特殊情况为:
Cosh(±0) = 1 Cosh(±Inf) = +Inf Cosh(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Cosh(0)) }
Output:
1.00
func Dim
func Dim(x, y float64) float64
Dim 返回 x-y 与 0 中的较大值。
特殊情况为:
Dim(+Inf, +Inf) = NaN Dim(-Inf, -Inf) = NaN Dim(x, NaN) = Dim(NaN, x) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f\n", math.Dim(4, -2)) fmt.Printf("%.2f\n", math.Dim(-4, 2)) }
Output:
6.00 0.00
func Erf
func Erf(x float64) float64
Erf 返回 x 的误差函数。
特殊情况为:
Erf(+Inf) = 1 Erf(-Inf) = -1 Erf(NaN) = NaN
func Erfc
func Erfc(x float64) float64
Erfc 返回 x 的互补误差函数。
特殊情况为:
Erfc(+Inf) = 0 Erfc(-Inf) = 2 Erfc(NaN) = NaN
func Erfcinv
func Erfcinv(x float64) float64
Erfcinv 返回 Erfc(x) 的反函数。
特殊情况为:
Erfcinv(0) = +Inf Erfcinv(2) = -Inf Erfcinv(x) = NaN 若 x < 0 或 x > 2 Erfcinv(NaN) = NaN
func Erfinv
func Erfinv(x float64) float64
Erfinv 返回 x 的反误差函数。
特殊情况为:
Erfinv(1) = +Inf Erfinv(-1) = -Inf Erfinv(x) = NaN 若 x < -1 或 x > 1 Erfinv(NaN) = NaN
func Exp
func Exp(x float64) float64
Exp 返回 e**x,即 x 的以 e 为底的指数。
特殊情况为:
Exp(+Inf) = +Inf Exp(NaN) = NaN
非常大的值会溢出为 0 或 +Inf。
非常小的值会下溢为 1。
Output:Example
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.2f\n", math.Exp(1))
fmt.Printf("%.2f\n", math.Exp(2))
fmt.Printf("%.2f\n", math.Exp(-1))
}
2.72
7.39
0.37
func Exp2
func Exp2(x float64) float64
Exp2 返回 2**x,即 x 的以 2 为底的指数。
特殊情况与 Exp 相同。
Output:Example
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.2f\n", math.Exp2(1))
fmt.Printf("%.2f\n", math.Exp2(-3))
}
2.00
0.12
func Expm1
func Expm1(x float64) float64
Expm1 返回 e**x - 1,即 x 的以 e 为底的指数减 1。 当 x 接近零时,它比 Exp(x) - 1 更精确。
特殊情况为:
Expm1(+Inf) = +Inf Expm1(-Inf) = -1 Expm1(NaN) = NaN
非常大的值会溢出为 -1 或 +Inf。
Output:Example
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.6f\n", math.Expm1(0.01))
fmt.Printf("%.6f\n", math.Expm1(-1))
}
0.010050
-0.632121
func FMA
func FMA(x, y, z float64) float64
FMA 返回 x * y + z,且仅进行一次舍入。 (也就是说,FMA 返回 x、y、z 的融合乘加结果。)
func Float32bits
func Float32bits(f float32) uint32
Float32bits 返回 f 的 IEEE 754 二进制表示, 其中 f 的符号位与结果位于相同的位位置。 Float32bits(Float32frombits(x)) == x.
func Float32frombits
func Float32frombits(b uint32) float32
Float32frombits 返回对应于 IEEE 754 二进制表示 b 的浮点数, 其中 b 的符号位与结果位于相同的位位置。 Float32frombits(Float32bits(x)) == x.
func Float64bits
func Float64bits(f float64) uint64
Float64bits 返回 f 的 IEEE 754 二进制表示, 其中 f 的符号位与结果位于相同的位位置, 且 Float64bits(Float64frombits(x)) == x。
func Float64frombits
func Float64frombits(b uint64) float64
Float64frombits 返回对应于 IEEE 754 二进制表示 b 的浮点数, 其中 b 的符号位与结果位于相同的位位置。 Float64frombits(Float64bits(x)) == x.
func Floor
func Floor(x float64) float64
Floor 返回小于或等于 x 的最大整数值。
特殊情况为:
Floor(±0) = ±0 Floor(±Inf) = ±Inf Floor(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { c := math.Floor(1.51) fmt.Printf("%.1f", c) }
Output:
1.0
func Frexp
func Frexp(f float64) (frac float64, exp int)
Frexp 将 f 拆分为一个归一化小数 和一个 2 的整数次幂。 它返回满足 f == frac × 2**exp 的 frac 和 exp, 其中 frac 的绝对值位于区间 [½, 1)。
特殊情况为:
Frexp(±0) = ±0, 0 Frexp(±Inf) = ±Inf, 0 Frexp(NaN) = NaN, 0
func Gamma
func Gamma(x float64) float64
Gamma 返回 x 的 Gamma 函数。
特殊情况为:
Gamma(+Inf) = +Inf Gamma(+0) = +Inf Gamma(-0) = -Inf Gamma(x) = NaN for integer x < 0 Gamma(-Inf) = NaN Gamma(NaN) = NaN
func Hypot
func Hypot(p, q float64) float64
Hypot 返回 Sqrt(p*p + q*q),并注意避免 不必要的溢出和下溢。
特殊情况为:
Hypot(±Inf, q) = +Inf Hypot(p, ±Inf) = +Inf Hypot(NaN, q) = NaN Hypot(p, NaN) = NaN
func Ilogb
func Ilogb(x float64) int
Ilogb 以整数形式返回 x 的二进制指数。
特殊情况为:
Ilogb(±Inf) = MaxInt32 Ilogb(0) = MinInt32 Ilogb(NaN) = MaxInt32
func Inf
func Inf(sign int) float64
Inf 返回正无穷(若 sign >= 0)、负无穷(若 sign < 0)。
func IsInf
func IsInf(f float64, sign int) bool
IsInf 根据 sign 报告 f 是否为无穷。 若 sign > 0,IsInf 报告 f 是否为正无穷。 若 sign < 0,IsInf 报告 f 是否为负无穷。 若 sign == 0,IsInf 报告 f 是否为任一无穷。
func IsNaN
func IsNaN(f float64) (is bool)
IsNaN 报告 f 是否为 IEEE 754 “非数”值。
func J0
func J0(x float64) float64
J0 返回零阶第一类贝塞尔函数。
特殊情况为:
J0(±Inf) = 0 J0(0) = 1 J0(NaN) = NaN
func J1
func J1(x float64) float64
J1 返回一阶第一类贝塞尔函数。
特殊情况为:
J1(±Inf) = 0 J1(NaN) = NaN
func Jn
func Jn(n int, x float64) float64
Jn 返回 n 阶第一类贝塞尔函数。
特殊情况为:
Jn(n, ±Inf) = 0 Jn(n, NaN) = NaN
func Ldexp
func Ldexp(frac float64, exp int) float64
Ldexp 是 Frexp 的逆运算。 它返回 frac × 2**exp。
特殊情况为:
Ldexp(±0, exp) = ±0 Ldexp(±Inf, exp) = ±Inf Ldexp(NaN, exp) = NaN
func Lgamma
func Lgamma(x float64) (lgamma float64, sign int)
Lgamma 返回 Gamma(x) 的自然对数和符号(-1 或 +1)。
特殊情况为:
Lgamma(+Inf) = +Inf Lgamma(0) = +Inf Lgamma(-integer) = +Inf Lgamma(-Inf) = -Inf Lgamma(NaN) = NaN
func Log
func Log(x float64) float64
Log 返回 x 的自然对数。
特殊情况为:
Log(+Inf) = +Inf Log(0) = -Inf Log(x < 0) = NaN Log(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { x := math.Log(1) fmt.Printf("%.1f\n", x) y := math.Log(2.7183) fmt.Printf("%.1f\n", y) }
Output:
0.0 1.0
func Log10
func Log10(x float64) float64
Log10 返回 x 的十进制对数。
特殊情况与 Log 的相同。
Output:Example
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.1f", math.Log10(100))
}
2.0
func Log1p
func Log1p(x float64) float64
Log1p 返回 1 加上其参数 x 的自然对数。 当 x 接近零时,它比 Log(1 + x) 更精确。
特殊情况为:
Log1p(+Inf) = +Inf Log1p(±0) = ±0 Log1p(-1) = -Inf Log1p(x < -1) = NaN Log1p(NaN) = NaN
func Log2
func Log2(x float64) float64
Log2 返回 x 的二进制对数。
特殊情况与 Log 的相同。
Output:Example
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.1f", math.Log2(256))
}
8.0
func Logb
func Logb(x float64) float64
Logb 返回 x 的二进制指数。
特殊情况为:
Logb(±Inf) = +Inf Logb(0) = -Inf Logb(NaN) = NaN
func Max
func Max(x, y float64) float64
Max 返回 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
注意,当以 NaN 和 +Inf 调用时,这与内置函数 max 不同。
func Min
func Min(x, y float64) float64
Min 返回 x 或 y 中的较小值。
特殊情况为:
Min(x, -Inf) = Min(-Inf, x) = -Inf Min(x, NaN) = Min(NaN, x) = NaN Min(-0, ±0) = Min(±0, -0) = -0
注意,当以 NaN 和 -Inf 调用时,这与内置函数 min 不同。
func Mod
func Mod(x, y float64) float64
Mod 返回 x/y 的浮点余数。 结果的绝对值小于 y,且其 符号与 x 的相同。
特殊情况为:
Mod(±Inf, y) = NaN Mod(NaN, y) = NaN Mod(x, 0) = NaN Mod(x, ±Inf) = x Mod(x, NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { c := math.Mod(7, 4) fmt.Printf("%.1f", c) }
Output:
3.0
func Modf
func Modf(f float64) (integer float64, fractional float64)
Modf 返回相加等于 f 的整数部分和小数部分浮点数。 两个值的符号都与 f 相同。
特殊情况为:
Modf(±Inf) = ±Inf, NaN Modf(NaN) = NaN, NaN
Example
package main import ( "fmt" "math" ) func main() { int, frac := math.Modf(3.14) fmt.Printf("%.2f, %.2f\n", int, frac) int, frac = math.Modf(-2.71) fmt.Printf("%.2f, %.2f\n", int, frac) }
Output:
3.00, 0.14 -2.00, -0.71
func NaN
func NaN() float64
NaN 返回一个 IEEE 754 “非数”值。
func Nextafter
func Nextafter(x, y float64) (r float64)
Nextafter 返回朝 y 方向 x 之后的下一个可表示的 float64 值。
特殊情况为:
Nextafter(x, x) = x Nextafter(NaN, y) = NaN Nextafter(x, NaN) = NaN
func Nextafter32
func Nextafter32(x, y float32) (r float32)
Nextafter32 返回朝 y 方向 x 之后的下一个可表示的 float32 值。
特殊情况为:
Nextafter32(x, x) = x Nextafter32(NaN, y) = NaN Nextafter32(x, NaN) = NaN
func Pow
func Pow(x, y float64) float64
Pow 返回 x**y,即 y 的以 x 为底的指数。
特殊情况(按顺序)为:
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
Example
package main import ( "fmt" "math" ) func main() { c := math.Pow(2, 3) fmt.Printf("%.1f", c) }
Output:
8.0
func Pow10
func Pow10(n int) float64
Pow10 返回 10**n,即 n 的以 10 为底的指数。
特殊情况为:
Pow10(n) = 0 for n < -323 Pow10(n) = +Inf for n > 308
Example
package main import ( "fmt" "math" ) func main() { c := math.Pow10(2) fmt.Printf("%.1f", c) }
Output:
100.0
func Remainder
func Remainder(x, y float64) float64
Remainder 返回 x/y 的 IEEE 754 浮点余数。
特殊情况为:
Remainder(±Inf, y) = NaN Remainder(NaN, y) = NaN Remainder(x, 0) = NaN Remainder(x, ±Inf) = x Remainder(x, NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.1f", math.Remainder(100, 30)) }
Output:
10.0
func Round
func Round(x float64) float64
Round 返回最接近的整数,半数舍离零。
特殊情况为:
Round(±0) = ±0 Round(±Inf) = ±Inf Round(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { p := math.Round(10.5) fmt.Printf("%.1f\n", p) n := math.Round(-10.5) fmt.Printf("%.1f\n", n) }
Output:
11.0 -11.0
func RoundToEven
func RoundToEven(x float64) float64
RoundToEven 返回最接近的整数,平局舍向偶数。
特殊情况为:
RoundToEven(±0) = ±0 RoundToEven(±Inf) = ±Inf RoundToEven(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { u := math.RoundToEven(11.5) fmt.Printf("%.1f\n", u) d := math.RoundToEven(12.5) fmt.Printf("%.1f\n", d) }
Output:
12.0 12.0
func Signbit
func Signbit(x float64) bool
Signbit 报告 x 是否为负数或负零。
func Sin
func Sin(x float64) float64
Sin 返回弧度参数 x 的正弦。
特殊情况为:
Sin(±0) = ±0 Sin(±Inf) = NaN Sin(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Sin(math.Pi)) }
Output:
0.00
func Sincos
func Sincos(x float64) (sin, cos float64)
Sincos 返回 Sin(x), Cos(x)。
特殊情况为:
Sincos(±0) = ±0, 1 Sincos(±Inf) = NaN, NaN Sincos(NaN) = NaN, NaN
Example
package main import ( "fmt" "math" ) func main() { sin, cos := math.Sincos(0) fmt.Printf("%.2f, %.2f", sin, cos) }
Output:
0.00, 1.00
func Sinh
func Sinh(x float64) float64
Sinh 返回 x 的双曲正弦。
特殊情况为:
Sinh(±0) = ±0 Sinh(±Inf) = ±Inf Sinh(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Sinh(0)) }
Output:
0.00
func Sqrt
func Sqrt(x float64) float64
Sqrt 返回 x 的平方根。
特殊情况为:
Sqrt(+Inf) = +Inf Sqrt(±0) = ±0 Sqrt(x < 0) = NaN Sqrt(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { const ( a = 3 b = 4 ) c := math.Sqrt(a*a + b*b) fmt.Printf("%.1f", c) }
Output:
5.0
func Tan
func Tan(x float64) float64
Tan 返回弧度参数 x 的正切。
特殊情况为:
Tan(±0) = ±0 Tan(±Inf) = NaN Tan(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Tan(0)) }
Output:
0.00
func Tanh
func Tanh(x float64) float64
Tanh 返回 x 的双曲正切。
特殊情况为:
Tanh(±0) = ±0 Tanh(±Inf) = ±1 Tanh(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f", math.Tanh(0)) }
Output:
0.00
func Trunc
func Trunc(x float64) float64
Trunc 返回 x 的整数值。
特殊情况为:
Trunc(±0) = ±0 Trunc(±Inf) = ±Inf Trunc(NaN) = NaN
Example
package main import ( "fmt" "math" ) func main() { fmt.Printf("%.2f\n", math.Trunc(math.Pi)) fmt.Printf("%.2f\n", math.Trunc(-1.2345)) }
Output:
3.00 -1.00
func Y0
func Y0(x float64) float64
Y0 返回零阶第二类贝塞尔函数。
特殊情况为:
Y0(+Inf) = 0 Y0(0) = -Inf Y0(x < 0) = NaN Y0(NaN) = NaN
func Y1
func Y1(x float64) float64
Y1 返回一阶第二类贝塞尔函数。
特殊情况为:
Y1(+Inf) = 0 Y1(0) = -Inf Y1(x < 0) = NaN Y1(NaN) = NaN
func Yn
func Yn(n int, x float64) float64
Yn 返回 n 阶第二类贝塞尔函数。
特殊情况为:
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
Directories
| big | Package big 实现任意精度算术(大数)。 |
| bits | Package bits 实现了针对预声明无符号整数类型的位计数和位操作函数。 |
| cmplx | Package cmplx 为复数提供基本常量和数学函数。 |
| rand | Package rand 实现了适用于模拟等任务的伪随机数生成器, 但不应用于安全敏感的工作。 |