xur_

> module

std::math

mathematical functions

use std::math; in the browser

std::math provides floating-point maths, a few integer helpers, and constants. Angles are in radians. Constants are functions: math::PI().

> functions

# E() -> f64

Euler's number, e.

# INFINITY() -> f64

Positive infinity and not-a-number.

# NAN() -> f64

Positive infinity and not-a-number.

# PI() -> f64

Pi and 2 pi (tau).

# TAU() -> f64

Pi and 2 pi (tau).

# abs(x: f64) -> f64

Absolute value.

# acos(x: f64) -> f64

Inverse sine, cosine and tangent. asin and acos return NaN outside [-1, 1].

# asin(x: f64) -> f64

Inverse sine, cosine and tangent. asin and acos return NaN outside [-1, 1].

# atan(x: f64) -> f64

Inverse sine, cosine and tangent. asin and acos return NaN outside [-1, 1].

# atan2(y: f64, x: f64) -> f64

The angle of the point (x, y) from the positive x axis, in [-pi, pi].

# ceil(x: f64) -> f64

Rounds down or up to a whole number.

# clamp(val: f64, min: f64, max: f64) -> f64

val limited to the range [min, max]. min greater than max, or either of them NaN, is a runtime error.

# cos(x: f64) -> f64

Sine, cosine and tangent.

# exp(x: f64) -> f64

e raised to the power x.

# floor(x: f64) -> f64

Rounds down or up to a whole number.

# fmax(a: f64, b: f64) -> f64

The smaller or larger of two floats. If one is NaN, the other is returned.

# fmin(a: f64, b: f64) -> f64

The smaller or larger of two floats. If one is NaN, the other is returned.

# lerp(a: f64, b: f64, t: f64) -> f64

Linear interpolation: a + (b - a) * t. t is not clamped.

# ln(x: f64) -> f64

Natural, base-2 and base-10 logarithm.

# log10(x: f64) -> f64

Natural, base-2 and base-10 logarithm.

# log2(x: f64) -> f64

Natural, base-2 and base-10 logarithm.

# max(a: i64, b: i64) -> i64

The smaller or larger of two integers.

# min(a: i64, b: i64) -> i64

The smaller or larger of two integers.

# pow(base: f64, exp: f64) -> f64

base raised to the power exp.

# round(x: f64) -> f64

Rounds to the nearest whole number, halves away from zero (2.5 gives 3.0).

# sin(x: f64) -> f64

Sine, cosine and tangent.

# sqrt(x: f64) -> f64

Square root; NaN for a negative x.

# tan(x: f64) -> f64

Sine, cosine and tangent.

> examples

use std::math;
use std::io;

fn main() {
    io::println("sqrt(9) = {}", math::sqrt(9.0));
    io::println("2^10 = {}", math::pow(2.0, 10.0));
    io::println("{}", math::clamp(1.5, 0.0, 1.0));  // 1
    io::println("{}", math::sin(math::PI() / 2.0)); // 1
}