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) -> Int2 つの値のうち大きい方。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.max(3, 7) == 7)
}min
yaoxiang
min: (a: Int, b: Int) -> Int2 つの値のうち小さい方。
yaoxiang
use std.assert
use std.math
main: () -> Void = {
assert(math.min(3, 7) == 3)
}clamp
yaoxiang
clamp: (value: Int, min: Int, max: Int) -> Intvalue を [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にパースする
