# Math 数学库 ## 功能 Math 是全局无状态静态数学对象,提供数学常量、基础数学、取整、指数、对数、三角函数、双曲函数和随机数能力。 ## 语法 ```bt Math.sqrt(9) Math.pow(2, 3) Math.PI ``` ## API 列表 | API | 说明 | | ------ | ------ | | [Math 常量](/zh-hans/docs/math/constants) | 提供 E、PI、TAU 等数学常量。 | | [Math.abs](/zh-hans/docs/math/abs) | 返回 value 的绝对值。 | | [Math.pow](/zh-hans/docs/math/pow) | 返回 value 的 power 次幂。 | | [Math.sqrt](/zh-hans/docs/math/sqrt) | 返回 value 的平方根。 | | [Math.cbrt](/zh-hans/docs/math/cbrt) | 返回 value 的立方根。 | | [Math.min](/zh-hans/docs/math/min) | 返回所有参数中的最小值。 | | [Math.max](/zh-hans/docs/math/max) | 返回所有参数中的最大值。 | | [Math.clamp](/zh-hans/docs/math/clamp) | value 小于 min 时返回 min,大于 max 时返回 max,否则返回 value。 | | [Math.sign](/zh-hans/docs/math/sign) | 负数返回 -1,零返回 0,正数返回 1。 | | [Math.hypot](/zh-hans/docs/math/hypot) | 返回 sqrt(x * x + y * y) 的斜边长度。 | | [Math.round](/zh-hans/docs/math/round) | 返回四舍五入后的数字。 | | [Math.ceil](/zh-hans/docs/math/ceil) | 返回大于等于 value 的最小整数。 | | [Math.floor](/zh-hans/docs/math/floor) | 返回小于等于 value 的最大整数。 | | [Math.trunc](/zh-hans/docs/math/trunc) | 返回去掉小数部分后的数字。 | | [Math.exp](/zh-hans/docs/math/exp) | 返回 e 的 value 次方。 | | [Math.exp2](/zh-hans/docs/math/exp2) | 返回 2 的 value 次方。 | | [Math.expm1](/zh-hans/docs/math/expm1) | 返回 exp(value) - 1。 | | [Math.ln](/zh-hans/docs/math/ln) | 返回 value 的自然对数。 | | [Math.log](/zh-hans/docs/math/log) | 返回以 base 为底的 value 对数。 | | [Math.log10](/zh-hans/docs/math/log10) | 返回以 10 为底的 value 对数。 | | [Math.log2](/zh-hans/docs/math/log2) | 返回以 2 为底的 value 对数。 | | [Math.log1p](/zh-hans/docs/math/log1p) | 返回 ln(1 + value)。 | | [Math.sin](/zh-hans/docs/math/sin) | 返回 value 的正弦值。 | | [Math.cos](/zh-hans/docs/math/cos) | 返回 value 的余弦值。 | | [Math.tan](/zh-hans/docs/math/tan) | 返回 value 的正切值。 | | [Math.asin](/zh-hans/docs/math/asin) | 返回 value 的反正弦弧度。 | | [Math.acos](/zh-hans/docs/math/acos) | 返回 value 的反余弦弧度。 | | [Math.atan](/zh-hans/docs/math/atan) | 返回 value 的反正切弧度。 | | [Math.atan2](/zh-hans/docs/math/atan2) | 根据 y 和 x 返回方向角弧度。 | | [Math.rad](/zh-hans/docs/math/rad) | 返回对应弧度值。 | | [Math.deg](/zh-hans/docs/math/deg) | 返回对应角度值。 | | [Math.sinh](/zh-hans/docs/math/sinh) | 返回 value 的双曲正弦值。 | | [Math.cosh](/zh-hans/docs/math/cosh) | 返回 value 的双曲余弦值。 | | [Math.tanh](/zh-hans/docs/math/tanh) | 返回 value 的双曲正切值。 | | [Math.asinh](/zh-hans/docs/math/asinh) | 返回 value 的反双曲正弦值。 | | [Math.acosh](/zh-hans/docs/math/acosh) | 返回 value 的反双曲余弦值。 | | [Math.atanh](/zh-hans/docs/math/atanh) | 返回 value 的反双曲正切值。 | | [Math.random](/zh-hans/docs/math/random) | 返回 0 <= n < 1 的随机浮点数。 | ## 常量 | 常量 | 说明 | | ------ | ------ | | Math.E | 自然常数 e。 | | Math.LN2 | 2 的自然对数。 | | Math.LN10 | 10 的自然对数。 | | Math.LOG2E | 以 2 为底的 e 的对数。 | | Math.LOG10E | 以 10 为底的 e 的对数。 | | Math.PI | 圆周率。 | | Math.SQRT1_2 | 2 的平方根的倒数。 | | Math.SQRT2 | 2 的平方根。 | | Math.TAU | 圆周率的 2 倍。 | ## 示例 ```bt // 输出:8 print Math.pow(2, 3) // 输出:3 print Math.sqrt(9) // 输出:3.141592653589793 print Math.PI ``` ## 注意事项 - Math 是全局静态对象,不需要也不再支持使用 math() 创建对象。 - Math.min() 和 Math.max() 至少需要 1 个数字参数。 - 数字结果会尽量返回 Int;非整数、非有限值或超出 i64 范围的结果返回 Float。