std.math
数学模块。全部为纯值函数:参数按值(Copy 语义)传入,无借用、无移动、无副作用。
yaoxiang
use std.math常量
按名导入即可使用:
yaoxiang
use std.math.{PI, E, TAU}| 常量 | 类型 | 值 |
|---|---|---|
PI | Float | π ≈ 3.141592653589793 |
E | Float | e ≈ 2.718281828459045 |
TAU | Float | 2π ≈ 6.283185307179586 |
yaoxiang
use std.assert
use std.math.{E, PI, TAU}
main: () -> Void = {
assert(PI > 3.14 and PI < 3.15)
assert(E > 2.71 and E < 2.72)
assert(TAU > 6.28 and TAU < 6.29)
}函数一览
| 函数 | 签名 |
|---|---|
abs | (n: Int) -> Int |
max | (a: Int, b: Int) -> Int |
min | (a: Int, b: Int) -> Int |
clamp | (value: Int, min: Int, max: Int) -> Int |
fabs | (n: Float) -> Float |
fmax | (a: Float, b: Float) -> Float |
fmin | (a: Float, b: Float) -> Float |
pow | (base: Float, exp: Float) -> Float |
sqrt | (n: Float) -> Float |
floor | (n: Float) -> Float |
ceil | (n: Float) -> Float |
round | (n: Float) -> Float |
sin | (n: Float) -> Float |
cos | (n: Float) -> Float |
tan | (n: Float) -> Float |
PI | Float |
E | Float |
TAU | Float |
(
to_int/to_float转换失败时回退为0),不会报错——建议依赖类型检查器在编译期拦截。
整数函数
abs
yaoxiang
abs: (n: Int) -> Int绝对值。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.abs(-5) == 5)
assert(math.abs(5) == 5)
}max
yaoxiang
max: (a: Int, b: Int) -> Int两者中的较大值。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.max(3, 7) == 7)
}min
yaoxiang
min: (a: Int, b: Int) -> Int两者中的较小值。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.min(3, 7) == 3)
}clamp
yaoxiang
clamp: (value: Int, min: Int, max: Int) -> Int把 value 钳制到 [min, max] 区间。
value—— 待钳制的值min—— 下界(含)max—— 上界(含)
返回:区间内的值。低于下界返回 min,高于上界返回 max。
min > max会使解释器 panic(#339)(底层i64::clamp的前置条件),不会返回错误值。请保证min <= max。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.clamp(15, 1, 10) == 10)
assert(math.clamp(-5, 1, 10) == 1)
assert(math.clamp(5, 1, 10) == 5)
}浮点函数
fabs
yaoxiang
fabs: (n: Float) -> Float浮点绝对值。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.fabs(-2.5) == 2.5)
}fmax
yaoxiang
fmax: (a: Float, b: Float) -> Float浮点较大值。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.fmax(1.5, 2.5) == 2.5)
}fmin
yaoxiang
fmin: (a: Float, b: Float) -> Float浮点较小值。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.fmin(1.5, 2.5) == 1.5)
}pow
yaoxiang
pow: (base: Float, exp: Float) -> Floatbase 的 exp 次幂。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.pow(2.0, 10.0) == 1024.0)
}sqrt
yaoxiang
sqrt: (n: Float) -> Float平方根。负数返回 NaN(不报错)。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.sqrt(4.0) == 2.0)
assert(math.sqrt(2.0) > 1.41 and math.sqrt(2.0) < 1.42)
}floor
yaoxiang
floor: (n: Float) -> Float向下取整,返回仍为 Float。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.floor(3.7) == 3.0)
}ceil
yaoxiang
ceil: (n: Float) -> Float向上取整,返回仍为 Float。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.ceil(3.2) == 4.0)
}round
yaoxiang
round: (n: Float) -> Float四舍五入(远离零方向取半),返回仍为 Float。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.round(3.5) == 4.0)
assert(math.round(3.4) == 3.0)
}sin
yaoxiang
sin: (n: Float) -> Float正弦,参数为弧度。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.sin(0.0) == 0.0)
}cos
yaoxiang
cos: (n: Float) -> Float余弦,参数为弧度。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.cos(0.0) == 1.0)
}tan
yaoxiang
tan: (n: Float) -> Float正切,参数为弧度。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.tan(0.0) == 0.0)
}相关
std.string.parse_float—— 字符串解析为Float
