Math functions
Note that many standard math functions such as sin
and sqrt
are defined as methods on the f32 and f64 types.
Functions
-
fn abs<T>(a: T) -> T
T: NumericAbsolute value of a number
-
fn widening_abs<T>(a: T) -> unsigned_of<T>
T: IntegerAbsolute value of an integer (widened to the unsigned type)
Example
use std::math::widening_abs; assert_eq!(widening_abs(-128i8), 128u8);
Run this example -
fn div_floor<T>(a: T, b: T) -> (T, T)
T: IntegerCalculates the quotient and remainder of integer division
Unlike the
/
operator which rounds towards zero, this function always rounds towards negative infinity. This is similar to how//
and%
work in Python.Example
use std::math::div_floor; let a = -3; let b = 2; assert_eq!((a / b, a % b), (-1, -1)); assert_eq!(a.div_floor(b), (-2, 1));
Run this example
Consts
-
const E: f64 = /* ... */
Base of the natural logarithm
-
const FRAC_1_PI: f64 = /* ... */
1/π
-
const FRAC_1_SQRT_2: f64 = /* ... */
1/sqrt(2)
-
const FRAC_2_PI: f64 = /* ... */
2/π
-
const FRAC_2_SQRT_PI: f64 = /* ... */
2/sqrt(π)
-
const FRAC_PI_2: f64 = /* ... */
π/2
-
const FRAC_PI_3: f64 = /* ... */
π/3
-
const FRAC_PI_4: f64 = /* ... */
π/4
-
const FRAC_PI_6: f64 = /* ... */
π/6
-
const FRAC_PI_8: f64 = /* ... */
π/8
-
const LN_10: f64 = /* ... */
Natural logarithm of 10
-
const LN_2: f64 = /* ... */
Natural logarithm of 2
-
const LOG10_2: f64 = /* ... */
Logarithm base 10 of 2
-
const LOG10_E: f64 = /* ... */
Logarithm base 10 of E
-
const LOG2_10: f64 = /* ... */
Logarithm base 2 of 10
-
const LOG2_E: f64 = /* ... */
Logarithm base 2 of E
-
const PI: f64 = /* ... */
π
-
const SQRT_2: f64 = /* ... */
Square root of 2
-
const TAU: f64 = /* ... */
2π (also known as τ)