Struct Math

Source
pub struct Math;
Expand description

A “static” structure used to compute math functions. Since f32 gets a lot of it’s functions stripped away when using no_std, you can use this structure to regain those functions. It will also work the same even if you don’t use it for no_std.

Implementations§

Source§

impl Math

Source

pub const PI: f32 = 3.14159274f32

Source

pub const PI_OVER_2: f32 = 1.57079637f32

Source

pub const PI_OVER_4: f32 = 0.785398185f32

Source

pub const TWO_PI: f32 = 6.28318548f32

Source

pub const E: f32 = 2.71828175f32

Source

pub const DEG_TO_RAD: f32 = 0.0174532924f32

Source

pub const RAD_TO_DEG: f32 = 57.2957802f32

Source

pub const LN2: f32 = 0.693147182f32

Source

pub const LN10: f32 = 2.30258512f32

Source§

impl Math

Source

pub fn abs(value: f32) -> f32

Gets the absolute value of the number

  • value: The number to get the absolute value from

Returns: Returns the absolute value of the number

§Examples
let value = Math::abs(10.0);
assert_eq!(10.0, value);
let value = Math::abs(-10.0);
assert_eq!(10.0, value);
let value = Math::abs(-0.0);
assert_eq!(0.0, value);
Source

pub fn abs_i32(value: i32) -> i32

Gets the absolute value of the number

  • value: The number to get the absolute value from

Returns: Returns the absolute value of the number

§Examples
let value = Math::abs_i32(10);
assert_eq!(10, value);
let value = Math::abs_i32(-10);
assert_eq!(10, value);
let value = Math::abs_i32(-0);
assert_eq!(0, value);
Source

pub fn approx(a: f32, b: f32) -> bool

Finds if the two floating point numbers are approximately close to each other. Checks with epsilon = 0.000001

  • a: The first number to check with
  • b: The second number to check with

Returns: Returns true if the two values are approximately close to each other

§Examples
assert!(Math::approx(1.20000001, 1.2));
Source

pub fn approx_epsilon(a: f32, b: f32, epsilon: f32) -> bool

Finds if the two floating point numbers are approximately close to each other, provided the epsilon

  • a: The first number to check with
  • b: The second number to check with
  • epsilon: The epsilon (smallest possible difference between numbers) to check with

Returns: Returns true if the two values are approximately close to each other

§Examples
assert!(Math::approx_epsilon(1.2001, 1.2, 0.001));
Source

pub fn acos(value: f32) -> f32

Computes the arc cosine (a.k.a. inverse cosine) with the provided value

  • value: The value to compute the arc cosine with, must be within -1 and 1

Returns: Returns the angle at which the value exists in radians, returns NaN if the value provided is less than -1 or greater than 1

§Examples
let value = Math::acos(0.0);
assert_range!(Math::PI_OVER_2, value);
let value = Math::acos(1.0);
assert_range!(0.0, value);
let value = Math::acos(-1.0);
assert_range!(Math::PI, value);
let value = Math::acos(0.707106781);
assert_range!(Math::PI_OVER_4, value);
let value = Math::acos(0.540302306);
assert_range!(1.0, value);
let value = Math::acos(2.0);
assert!(value.is_nan());
let value = Math::acos(-1.001);
assert!(value.is_nan());
Source

pub fn acos_deg(value: f32) -> f32

Computes the arc cosine (a.k.a. inverse cosine) with the provided value

  • value: The value to compute the arc cosine with, must be within -1 and 1

Returns: Returns the angle at which the value exists in degrees, returns NaN if the value provided is less than -1 or greater than 1

§Examples
let value = Math::acos_deg(0.0);
assert_range!(90.0, value);
let value = Math::acos_deg(1.0);
assert_range!(0.0, value);
let value = Math::acos_deg(-1.0);
assert_range!(180.0, value);
let value = Math::acos_deg(0.707106781);
assert_range!(45.0, value, 0.005);
let value = Math::acos_deg(0.540302306);
assert_range!(57.29578, value, 0.001);
let value = Math::acos_deg(2.0);
assert!(value.is_nan());
let value = Math::acos_deg(-1.001);
assert!(value.is_nan());
Source

pub fn acosh(value: f32) -> f32

Computes the arc hyperbolic cosine (a.k.a. inverse hyperbolic cosine)

  • value: The value to compute with

Returns: Returns the computed inverse hyperbolic cosine

§Examples
let value = Math::acosh(0.0);
assert!(value.is_nan());
let value = Math::acosh(1.0);
assert_range!(0.0, value);
let value = Math::acosh(1.54308063482);
assert_range!(1.0, value);
let value = Math::acosh(11.591954);
assert_range!(Math::PI, value);
let value = Math::acosh(7.6101246);
assert_range!(Math::E, value);
Source

pub fn asin(value: f32) -> f32

Computes the arc sine (a.k.a. inverse sine) with the provided value

  • value: The value to compute the arc sine with, must be within -1 and 1

Returns: Returns the angle at which the value exists in radians, returns NaN if the value provided is less than -1 or greater than 1

§Examples
let value = Math::asin(0.0);
assert_range!(0.0, value);
let value = Math::asin(1.0);
assert_range!(Math::PI_OVER_2, value);
let value = Math::asin(-1.0);
assert_range!(-Math::PI_OVER_2, value);
let value = Math::asin(0.707106781);
assert_range!(Math::PI_OVER_4, value);
let value = Math::asin(-1.1);
assert!(value.is_nan());
let value = Math::asin(2.0);
assert!(value.is_nan());
let value = Math::asin(0.9999);
assert_range!(1.5566529, value);
let value = Math::asin(-0.25);
assert_range!(-0.25268024, value);
Source

pub fn asin_deg(value: f32) -> f32

Computes the arc sine (a.k.a. inverse sine) with the provided value

  • value: The value to compute the arc sine with, must be within -1 and 1

Returns: Returns the angle at which the value exists in degrees, returns NaN if the value provided is less than -1 or greater than 1

§Examples
let value = Math::asin_deg(0.0);
assert_range!(0.0, value);
let value = Math::asin_deg(1.0);
assert_range!(90.0, value);
let value = Math::asin_deg(-1.0);
assert_range!(-90.0, value);
let value = Math::asin_deg(0.707106781);
assert_range!(45.0, value, 0.005);
let value = Math::asin_deg(-1.1);
assert!(value.is_nan());
let value = Math::asin_deg(2.0);
assert!(value.is_nan());
let value = Math::asin_deg(0.9999);
assert_range!(89.189644, value);
let value = Math::asin_deg(-0.25);
assert_range!(-14.477511, value, 0.005);
Source

pub fn asinh(value: f32) -> f32

Computes the arc hyperbolic sine (a.k.a. inverse hyperbolic sine)

  • value: The value to compute with

Returns: Returns the computed inverse hyperbolic sine

§Examples
let value = Math::asinh(0.0);
assert_range!(0.0, value);
let value = Math::asinh(1.0);
assert_range!(0.8813736, value);
let value = Math::asinh(1.1752012);
assert_range!(1.0, value);
let value = Math::asinh(-1.1752012);
assert_range!(-1.0, value);
let value = Math::asinh(11.54874);
assert_range!(Math::PI, value);
let value = Math::asinh(7.5441365);
assert_range!(Math::E, value);
Source

pub fn atan(value: f32) -> f32

Computes the arc tangent (a.k.a. inverse tangent) with the provided value

  • value: The value to compute the arc tangent with

Returns: Returns the angle at which the value exists in radians

§Examples
let value = Math::atan(0.0);
assert_range!(0.0, value);
let value = Math::atan(1.0);
assert_range!(Math::PI_OVER_4, value);
let value = Math::atan(-1.0);
assert_range!(-Math::PI_OVER_4, value);
let value = Math::atan(0.707106781);
assert_range!(0.615479708546, value);
let value = Math::atan(1.557407725);
assert_range!(1.0, value);
Source

pub fn atan_deg(value: f32) -> f32

Computes the arc tangent (a.k.a. inverse tangent) with the provided value

  • value: The value to compute the arc tangent with

Returns: Returns the angle at which the value exists in degrees

§Examples
let value = Math::atan_deg(0.0);
assert_range!(0.0, value);
let value = Math::atan_deg(1.0);
assert_range!(45.0, value, 0.001);
let value = Math::atan_deg(-1.0);
assert_range!(-45.0, value, 0.001);
let value = Math::atan_deg(0.707106781);
assert_range!(35.26439, value, 0.001);
let value = Math::atan_deg(1.557407725);
assert_range!(57.29578, value);
Source

pub fn atanh(value: f32) -> f32

Computes the arc hyperbolic tangent (a.k.a. inverse hyperbolic tangent)

  • value: The value to compute with

Returns: Returns the computed inverse hyperbolic tangent

§Examples
let value = Math::atanh(0.0);
assert_range!(0.0, value);
let value = Math::atanh(1.0);
assert!(value.is_infinite());
let value = Math::atanh(0.7615942);
assert_range!(1.0, value, 0.001);
let value = Math::atanh(-0.7615942);
assert_range!(-1.0, value, 0.001);
let value = Math::atanh(0.9962721);
assert_range!(Math::PI, value);
let value = Math::atanh(0.9913289);
assert_range!(Math::E, value);
Source

pub fn atan2(y: f32, x: f32) -> f32

Computes the arc tangent (a.k.a. inverse tangent) with the provided x and y values

  • y: The y value to compute the arc tangent with
  • x: The x value to compute the arc tangent with

Returns: Returns the angle at with the two values divided exists in radians

§Examples
let value = Math::atan2(0.0, 1.0);
assert_range!(0.0, value);
let value = Math::atan2(1.0, 1.0);
assert_range!(Math::PI_OVER_4, value);
let value = Math::atan2(-1.0, 1.0);
assert_range!(-Math::PI_OVER_4, value);
let value = Math::atan2(5.0, 1.0);
assert_range!(1.3734008, value);
let value = Math::atan2(1.0, 5.0);
assert_range!(0.19739556, value);
let value = Math::atan2(-5.0, 1.0);
assert_range!(-1.3734008, value);
let value = Math::atan2(-1.0, 5.0);
assert_range!(-0.19739556, value);
Source

pub fn atan2_deg(y: f32, x: f32) -> f32

Computes the arc tangent (a.k.a. inverse tangent) with the provided x and y values

  • y: The y value to compute the arc tangent with
  • x: The x value to compute the arc tangent with

Returns: Returns the angle at with the two values divided exists in degrees

§Examples
let value = Math::atan2_deg(0.0, 1.0);
assert_range!(0.0, value);
let value = Math::atan2_deg(1.0, 1.0);
assert_range!(45.0, value, 0.005);
let value = Math::atan2_deg(-1.0, 1.0);
assert_range!(-45.0, value, 0.005);
let value = Math::atan2_deg(5.0, 1.0);
assert_range!(78.69007, value);
let value = Math::atan2_deg(1.0, 5.0);
assert_range!(11.309933, value);
let value = Math::atan2_deg(-5.0, 1.0);
assert_range!(-78.69007, value);
let value = Math::atan2_deg(-1.0, 5.0);
assert_range!(-11.309933, value);
Source

pub fn ceil(value: f32) -> f32

Gets the smallest integer number that is greater than or equal to the given number

  • value: The value to get the ceiling with

Returns: Returns the ceiling number

§Examples
let value = Math::ceil(-3.0);
assert_eq!(-3.0, value);
let value = Math::ceil(1.4);
assert_eq!(2.0, value);
let value = Math::ceil(2.9);
assert_eq!(3.0, value);
let value = Math::ceil(-4.9);
assert_eq!(-4.0, value);
let value = Math::ceil(-5.3);
assert_eq!(-5.0, value);
Source

pub fn clamp(value: f32, min: f32, max: f32) -> f32

Clamps the value between the min and max values

  • value: The value to clamp with
  • min: The lower-bound minimum value to clamp to
  • max: The upper-bound maximum value to clamp to

Returns: Returns the clamped value

§Examples
let value = Math::clamp(20.0, 0.0, 10.0);
assert_eq!(10.0, value);
let value = Math::clamp(20.0, 0.0, 100.0);
assert_eq!(20.0, value);
let value = Math::clamp(-0.001, 0.0, 10.0);
assert_eq!(0.0, value);
let value = Math::clamp(0.18, -0.1, 0.1);
assert_eq!(0.1, value);
Source

pub fn cos(angle: f32) -> f32

Computes the cosine of the given angle in radians

  • angle: The angle to compute cosine with in radians

Returns: Returns a value from the computed cosine

§Remarks

If you need to compute both cos and sin of the same angle, use sin_cos instead as it’s more performant to produce both values than calling cos and sin separately

§Examples
let value = Math::cos(0.0);
assert_range!(1.0, value);
let value = Math::cos(Math::PI_OVER_2);
assert_range!(0.0, value);
let value = Math::cos(Math::PI);
assert_range!(-1.0, value);
let value = Math::cos(Math::PI + Math::PI_OVER_2);
assert_range!(0.0, value);
let value = Math::cos(Math::TWO_PI);
assert_range!(1.0, value);
let value = Math::cos(Math::PI_OVER_4);
assert_range!(0.707106781, value);
let value = Math::cos(1.0);
assert_range!(0.540302306, value);
let value = Math::cos(-100.0);
assert_range!(0.862318872, value);
Source

pub fn cos_deg(angle: f32) -> f32

Computes the cosine of the given angle in degrees

  • angle: The angle to compute cosine with in degrees

Returns: Returns a value from the computed cosine

§Remarks

If you need to compute both cos_deg and sin_deg of the same angle, use sin_cos_deg instead as it’s more performant to produce both values than calling cos_deg and sin_deg separately

§Examples
let value = Math::cos_deg(0.0);
assert_range!(1.0, value);
let value = Math::cos_deg(90.0);
assert_range!(0.0, value);
let value = Math::cos_deg(180.0);
assert_range!(-1.0, value);
let value = Math::cos_deg(270.0);
assert_range!(0.0, value);
let value = Math::cos_deg(360.0);
assert_range!(1.0, value);
let value = Math::cos_deg(45.0);
assert_range!(0.707106781, value);
let value = Math::cos_deg(57.29577951);
assert_range!(0.540302306, value);
let value = Math::cos_deg(-5729.577951);
assert_range!(0.862318872, value);
Source

pub fn cosh(value: f32) -> f32

Computes the hyperbolic cosine function

  • value: The value to compute the hyperbolic cosine function

Returns: Returns the computed hyperbolic cosine function

§Examples
let value = Math::cosh(0.0);
assert_range!(1.0, value);
let value = Math::cosh(1.0);
assert_range!(1.54308063482, value);
let value = Math::cosh(-1.0);
assert_range!(1.54308063482, value);
let value = Math::cosh(Math::PI);
assert_range!(11.591954, value);
let value = Math::cosh(Math::E);
assert_range!(7.6101246, value);
Source

pub fn cot(angle: f32) -> f32

Computes the cotangent of the given angle in radians

  • angle: The angle to compute the cotangent with in radians

Returns: Returns the computed cotangent value

§Examples
let value = Math::cot(Math::PI_OVER_2);
assert_range!(0.0, value);
let value = Math::cot(Math::PI + Math::PI_OVER_2);
assert_range!(0.0, value);
let value = Math::cot(Math::PI_OVER_4);
assert_range!(1.0, value);
let value = Math::cot(1.0);
assert_range!(0.642092616, value);
let value = Math::cot(-100.0);
assert_range!(1.702956919, value);
Source

pub fn cot_deg(angle: f32) -> f32

Computes the cotangent of the given angle in degrees

  • angle: The angle to compute the cotangent with in degrees

Returns: Returns the computed cotangent value

§Examples
let value = Math::cot_deg(90.0);
assert_range!(0.0, value);
let value = Math::cot_deg(270.0);
assert_range!(0.0, value);
let value = Math::cot_deg(45.0);
assert_range!(1.0, value);
let value = Math::cot_deg(57.29577951);
assert_range!(0.642092616, value);
let value = Math::cot_deg(-5729.577951);
assert_range!(1.702956919, value);
Source

pub fn csc(angle: f32) -> f32

Computes the cosecant of the given angle in radians

  • angle: The angle to compute the cosecant with in radians

Returns: Returns the computed cosecant value

§Examples
let value = Math::csc(Math::PI_OVER_2);
assert_range!(1.0, value);
let value = Math::csc(Math::PI + Math::PI_OVER_2);
assert_range!(-1.0, value);
let value = Math::csc(Math::PI_OVER_4);
assert_range!(1.414213562, value);
let value = Math::csc(1.0);
assert_range!(1.188395106, value);
let value = Math::csc(-100.0);
assert_range!(1.974857531, value);
Source

pub fn csc_deg(angle: f32) -> f32

Computes the cosecant of the given angle in degrees

  • angle: The angle to compute the cosecant with in degrees

Returns: Returns the computed cosecant value

§Examples
let value = Math::csc_deg(90.0);
assert_range!(1.0, value);
let value = Math::csc_deg(270.0);
assert_range!(-1.0, value);
let value = Math::csc_deg(45.0);
assert_range!(1.414213562, value);
let value = Math::csc_deg(57.29577951);
assert_range!(1.188395106, value);
let value = Math::csc_deg(-5729.577951);
assert_range!(1.974857531, value);
Source

pub fn deg2rad(degrees: f32) -> f32

Converts the value from degrees to radians

  • degrees: The value in degrees to convert

Returns: Returns the value in radians

§Examples
let value = Math::deg2rad(35.0);
assert_eq!(0.610865238198, value);
let value = Math::deg2rad(300.0);
assert_eq!(5.23598775598, value);
Source

pub fn exp(value: f32) -> f32

Computes e^x

  • value: The value to compute with

Returns: Returns the computed e^x

§Examples
let value = Math::exp(0.0);
assert_range!(1.0, value);
let value = Math::exp(-10.0);
assert_range!(0.000004539993, value);
let value = Math::exp(10.0);
assert_range!(22026.465, value);
let value = Math::exp(12.34);
assert_range!(228661.98, value, 0.05);
let value = Math::exp(2.9);
assert_range!(18.174147, value);
Source

pub fn exp2(value: f32) -> f32

Computes 2^x

  • value: The value to compute with

Returns: Returns the computed 2^x

§Examples
let value = Math::exp2(0.0);
assert_range!(1.0, value);
let value = Math::exp2(-10.0);
assert_range!(0.0009765625, value);
let value = Math::exp2(10.0);
assert_range!(1024.0, value, 0.0002);
let value = Math::exp2(12.34);
assert_range!(5184.5396, value, 0.05);
let value = Math::exp2(2.9);
assert_range!(7.464265, value);
Source

pub fn floor(value: f32) -> f32

Gets the largest integer number that is less than or equal to the given number

  • value: The value to get the floor with

Returns: Returns the floored number

§Examples
let value = Math::floor(-3.0);
assert_eq!(-3.0, value);
let value = Math::floor(1.4);
assert_eq!(1.0, value);
let value = Math::floor(2.9);
assert_eq!(2.0, value);
let value = Math::floor(-4.9);
assert_eq!(-5.0, value);
let value = Math::floor(-5.3);
assert_eq!(-6.0, value);
Source

pub fn fract(value: f32) -> f32

Gets the fractional part of the value, getting only a value between 0 and 1

  • value: The value to get the fraction from

Returns: Returns the fraction of the given number

§Examples
let value = Math::fract(3.0);
assert_range!(0.0, value);
let value = Math::fract(-3.0);
assert_range!(0.0, value);
let value = Math::fract(4.9);
assert_range!(0.9, value);
let value = Math::fract(-4.9);
assert_range!(0.1, value);
let value = Math::fract(12.34);
assert_range!(0.34, value);
Source

pub fn lerp(a: f32, b: f32, t: f32) -> f32

Linearly interpolates between the first and second values

  • a: The first value to start from
  • b: The second value to end from
  • t: The ratio value to interpolate between both values. Clamped between 0.0 and 1.0

Returns: Returns the interpolated value

§Examples
let value = Math::lerp(0.0, 1.0, 0.5);
assert_eq!(0.5, value);
let value = Math::lerp(0.0, 0.1, 0.9);
assert_eq!(0.089999996, value);
let value = Math::lerp(-10.0, 10.0, 0.6);
assert_eq!(2.0, value);
let value = Math::lerp(-10.0, -4.0, 0.7);
assert_eq!(-5.8, value);
Source

pub fn lerp_unclamped(a: f32, b: f32, t: f32) -> f32

Linearly interpolates between the first and second values (not clamped)

  • a: The first value to start from
  • b: The second value to end from
  • t: The ratio value to interpolate between both values

Returns: Returns the interpolated value

§Examples
let value = Math::lerp_unclamped(0.0, 1.0, 0.5);
assert_eq!(0.5, value);
let value = Math::lerp_unclamped(0.0, 0.1, 0.9);
assert_eq!(0.089999996, value);
let value = Math::lerp_unclamped(-10.0, 10.0, 0.6);
assert_eq!(2.0, value);
let value = Math::lerp_unclamped(-10.0, -4.0, 0.7);
assert_eq!(-5.8, value);
Source

pub fn ln(value: f32) -> f32

Computes the natural log of the given number

  • value: The value to compute the natural log of

Returns: Returns the natural log of the given value. Returns infinity if the value infinity and -infinity if the value is 0.0. Returns NaN if the value is NaN or less than 0.0

§Examples
let value = Math::ln(1.0);
assert_range!(0.0, value);
let value = Math::ln(100.0);
assert_range!(4.60517018599, value);
let value = Math::ln(0.01);
assert_range!(-4.60517018599, value);
let value = Math::ln(Math::E);
assert_range!(1.0, value);
let value = Math::ln(2.0);
assert_range!(0.69314718056, value);
let value = Math::ln(10.0);
assert_range!(2.30258509299, value);
let value = Math::ln(-10.0);
assert!(value.is_nan());
let value = Math::ln(0.0);
assert!(value.is_infinite());
Source

pub fn ln_1p(value: f32) -> f32

Computes the natural log of the given number plus one

  • value: The value to compute the natural log of

Returns: Returns the natural log of the given value. Returns infinity if the value infinity and -infinity if the value is -1.0. Returns NaN if the value is NaN or less than -1.0

§Examples
let value = Math::ln_1p(1.0);
assert_range!(0.6931472, value);
let value = Math::ln_1p(100.0);
assert_range!(4.6151204, value);
let value = Math::ln_1p(0.01);
assert_range!(0.0099503305, value);
let value = Math::ln_1p(2.0);
assert_range!(1.0986123, value);
let value = Math::ln_1p(10.0);
assert_range!(2.3978953, value);
let value = Math::ln_1p(-10.0);
assert!(value.is_nan());
let value = Math::ln_1p(0.0);
assert_range!(0.0, value);
Source

pub fn log(value: f32, base: f32) -> f32

Computes the log of the given number with a given base

  • value: The value to compute the logarithm with
  • base: The base of the logarithm

Returns: Returns the computed logarithm

§Examples
let value = Math::log(2.0, 2.0);
assert_range!(1.0, value);
let value = Math::log(1.0, 2.0);
assert_range!(0.0, value);
let value = Math::log(10.0, 2.0);
assert_range!(3.32192809489, value);
let value = Math::log(16.0, 4.0);
assert_range!(2.0, value);
let value = Math::log(2.0, 1.0);
assert!(value.is_infinite());
Source

pub fn log10(value: f32) -> f32

Computes the log of the given number with base 10

  • value: The value to compute the log with

Returns: Returns the computed log in base 10

§Examples
let value = Math::log10(1.0);
assert_range!(0.0, value);
let value = Math::log10(2.0);
assert_range!(0.301029995664, value);
let value = Math::log10(10.0);
assert_range!(1.0, value);
let value = Math::log10(50.0);
assert_range!(1.69897000434, value);
let value = Math::log10(100.0);
assert_range!(2.0, value);
Source

pub fn log2(value: f32) -> f32

Computes the log of the given number with base 2

  • value: The value to compute the log with

Returns: Returns the computed log in base 2

§Examples
let value = Math::log2(1.0);
assert_range!(0.0, value);
let value = Math::log2(2.0);
assert_range!(1.0, value);
let value = Math::log2(10.0);
assert_range!(3.32192809489, value);
let value = Math::log2(16.0);
assert_range!(4.0, value);
Source

pub fn map(value: f32, in_range: Range<f32>, out_range: Range<f32>) -> f32

Maps the value from one range into another range

  • value: The value to map
  • in_range: The starting input range to map from
  • out_range: The ending output range to map to

Returns: Returns the mapped value

§Examples
let value = Math::map(1.5, 1.0..2.0, 1.0..2.0);
assert_eq!(1.5, value);
let value = Math::map(1.0, 0.0..10.0, 0.0..1.0);
assert_eq!(0.1, value);
let value = Math::map(11.0, 0.0..10.0, 0.0..1.0);
assert_eq!(1.1, value);
let value = Math::map(1.0, -10.0..10.0, 0.0..1.0);
assert_eq!(0.55, value);
let value = Math::map(-10.0, -100.0..-10.0, 10.0..100.0);
assert_eq!(100.0, value);
Source

pub fn max(a: f32, b: f32) -> f32

Gets the maximum value between the two values

  • a: The first value to get the maximum value from
  • b: The second value to get the maximum value from

Returns: Returns the maximum number between the two values

§Examples
let value = Math::max(-1.0, 1.0);
assert_eq!(1.0, value);
let value = Math::max(-19.0, -19.1);
assert_eq!(-19.0, value);
Source

pub fn min(a: f32, b: f32) -> f32

Gets the minimum value between the two values

  • a: The first value to get the minimum value from
  • b: The second value to get the minimum value from

Returns: Returns the minimum number between the two values

§Examples
let value = Math::min(-1.0, 1.0);
assert_eq!(-1.0, value);
let value = Math::min(-19.0, -19.1);
assert_eq!(-19.1, value);
Source

pub fn min_max(a: f32, b: f32) -> (f32, f32)

Gets the minimum and maximum value returned as a tuple correctly sorted

  • a: The first value to get the minimum and maximum value from
  • b: The second value to get the minimum and maximum value from

Returns: Returns a tuple that holds the minimum and maximum values respectively

§Examples
let value = Math::min_max(-1.0, 1.0);
assert_eq!((-1.0, 1.0), value);
let value = Math::min_max(-19.0, -19.1);
assert_eq!((-19.1, -19.0), value);
Source

pub fn pow(value: f32, power: f32) -> f32

Raised the value by the power (as a floating point number)

  • value: The value to raise with
  • power: The power to raise by

Returns: Returns the value raised by the power

§Examples
let value = Math::pow(1.0, 0.0);
assert_range!(1.0, value);
let value = Math::pow(1.0, 10.0);
assert_range!(1.0, value);
let value = Math::pow(2.0, 10.0);
assert_range!(1024.0, value, 0.0002);
let value = Math::pow(40.0, 1.2);
assert_range!(83.65118, value);
let value = Math::pow(3.0, -2.3);
assert_range!(0.07991368, value);
Source

pub fn pow_i32(a: f32, b: i32) -> f32

Gets the power of the given number by the other given number, with the power being an i32

  • a: The base number to power
  • b: The number to power with

Returns: Returns the powered number

§Examples
let value = Math::pow_i32(3.0, 5);
assert_range!(243.0, value);
let value = Math::pow_i32(10.45, 3);
assert_range!(1141.166, value, 0.001);
let value = Math::pow_i32(0.0, 0);
assert_range!(1.0, value);
let value = Math::pow_i32(10.0, 0);
assert_range!(1.0, value);
let value = Math::pow_i32(0.0, 2);
assert_range!(0.0, value);
let value = Math::pow_i32(2.0, -3);
assert_range!(0.125, value);
Source

pub fn rad2deg(radians: f32) -> f32

Converts the value from radians to degrees

  • radians: The value in radians to convert

Returns: Returns the value in degrees

§Examples
let value = Math::rad2deg(1.0);
assert_eq!(57.2957795131, value);
let value = Math::rad2deg(4.0);
assert_eq!(229.183118052, value);
Source

pub fn repeat(value: f32, range: Range<f32>) -> f32

Repeats the value around the range, making sure it stays within the range

  • value: The value to repeat
  • range: The range to repeat around

Returns: Returns the wrapped value

§Examples
let value = Math::repeat(1.0, 0.0..2.0);
assert_range!(1.0, value);
let value = Math::repeat(1.0, 2.0..3.0);
assert_range!(3.0, value);
let value = Math::repeat(5.3, 0.0..3.0);
assert_range!(2.3, value);
let value = Math::repeat(-4.0, 0.0..1.23);
assert_range!(0.31, value);
let value = Math::repeat(-4.0, 10.0..12.23);
assert_range!(10.620003, value);
Source

pub fn round(value: f32) -> f32

Rounds the given value to the nearest zero

  • value: The value to round with

Returns: Returns the rounded value

§Examples
let value = Math::round(0.0);
assert_eq!(0.0, value);
let value = Math::round(1.1);
assert_eq!(1.0, value);
let value = Math::round(2.9);
assert_eq!(3.0, value);
let value = Math::round(3.5);
assert_eq!(4.0, value);
let value = Math::round(-4.5);
assert_eq!(-5.0, value);
let value = Math::round(-5.45);
assert_eq!(-5.0, value);
Source

pub fn round_to_digit(value: f32, digits: i32) -> f32

Rounds the value up to the given amount of digits past the decimal

  • value: The value to round with
  • digits: The digit past the decimal to round to, must be between -15 and 15
§Examples
let value = Math::round_to_digit(1.0, 0);
assert_eq!(1.0, value);
let value = Math::round_to_digit(-1.0, 0);
assert_eq!(-1.0, value);
let value = Math::round_to_digit(1.525, 0);
assert_eq!(2.0, value);
let value = Math::round_to_digit(1.525, 1);
assert_eq!(1.5, value);
let value = Math::round_to_digit(1.525, 2);
assert_eq!(1.53, value);
let value = Math::round_to_digit(-1.525, 0);
assert_eq!(-2.0, value);
let value = Math::round_to_digit(-1.525, 2);
assert_eq!(-1.53, value);
let value = Math::round_to_digit(-2.4, 0);
assert_eq!(-2.0, value);
let value = Math::round_to_digit(-2.6, 0);
assert_eq!(-3.0, value);
Source

pub fn sec(angle: f32) -> f32

Computes the secant of the given angle in radians

  • angle: The given angle to compute the secant with in radians

Returns: Returns the computed secant value

§Examples
let value = Math::sec(0.0);
assert_range!(1.0, value);
let value = Math::sec(Math::PI);
assert_range!(-1.0, value);
let value = Math::sec(Math::TWO_PI);
assert_range!(1.0, value);
let value = Math::sec(Math::PI_OVER_4);
assert_range!(1.414213562, value);
let value = Math::sec(1.0);
assert_range!(1.850815718, value);
let value = Math::sec(-100.0);
assert_range!(1.159663823, value);
Source

pub fn sec_deg(angle: f32) -> f32

Computes the secant of the given angle in degrees

  • angle: The given angle to compute the secant with in degrees

Returns: Returns the computed secant value

§Examples
let value = Math::sec_deg(0.0);
assert_range!(1.0, value);
let value = Math::sec_deg(180.0);
assert_range!(-1.0, value);
let value = Math::sec_deg(360.0);
assert_range!(1.0, value);
let value = Math::sec_deg(45.0);
assert_range!(1.414213562, value);
let value = Math::sec_deg(57.29577951);
assert_range!(1.850815718, value);
let value = Math::sec_deg(-5729.577951);
assert_range!(1.159663823, value);
Source

pub fn sign(value: f32) -> f32

Gets the sign (positive or negative) of the given value

  • value: The value to check the sign with

Returns: Returns 1.0 if the value is positive, and -1.0 if the value is negative

§Examples
let value = Math::sign(10.0);
assert_eq!(1.0, value);
let value = Math::sign(-10.0);
assert_eq!(-1.0, value);
let value = Math::sign(-0.0);
assert_eq!(-1.0, value);
Source

pub fn sin(angle: f32) -> f32

Computes the sine of the given angle in radians

  • angle: The angle to compute sine with in radians

Returns: Returns a value from the computed sine

§Remarks

If you need to compute both cos and sin of the same angle, use sin_cos instead as it’s more performant to produce both values than calling cos and sin separately

§Examples
let value = Math::sin(0.0);
assert_range!(0.0, value);
let value = Math::sin(Math::PI_OVER_2);
assert_range!(1.0, value);
let value = Math::sin(Math::PI);
assert_range!(0.0, value);
let value = Math::sin(Math::PI + Math::PI_OVER_2);
assert_range!(-1.0, value);
let value = Math::sin(Math::TWO_PI);
assert_range!(0.0, value);
let value = Math::sin(Math::PI_OVER_4);
assert_range!(0.707106781, value);
let value = Math::sin(1.0);
assert_range!(0.841470985, value);
let value = Math::sin(-100.0);
assert_range!(0.506365641, value);
Source

pub fn sin_deg(angle: f32) -> f32

Computes the sine of the given angle in degrees

  • angle: The angle to compute sine with in degrees

Returns: Returns a value from the computed sine

§Remarks

If you need to compute both cos_deg and sin_deg of the same angle, use sin_cos_deg instead as it’s more performant to produce both values than calling cos_deg and sin_deg separately

§Examples
let value = Math::sin_deg(0.0);
assert_range!(0.0, value);
let value = Math::sin_deg(90.0);
assert_range!(1.0, value);
let value = Math::sin_deg(180.0);
assert_range!(0.0, value);
let value = Math::sin_deg(270.0);
assert_range!(-1.0, value);
let value = Math::sin_deg(360.0);
assert_range!(0.0, value);
let value = Math::sin_deg(45.0);
assert_range!(0.707106781, value);
let value = Math::sin_deg(57.29577951);
assert_range!(0.841470985, value);
let value = Math::sin_deg(-5729.577951);
assert_range!(0.506365641, value);
Source

pub fn sin_cos(angle: f32) -> (f32, f32)

Computes the sine and cosine of the angle in radians

  • angle: The angle to compute the sine and cosine with in radians

Returns: Returns the sine and cosine (respectively) as a tuple

§Remarks

If you need to compute both cos and sin of the same angle, this function is more performant to produce both values than calling cos and sin separately

§Examples
let value = Math::sin_cos(0.0);
assert_range_tuple2!((0.0, 1.0), value);
let value = Math::sin_cos(Math::PI_OVER_2);
assert_range_tuple2!((1.0, 0.0), value);
let value = Math::sin_cos(Math::PI);
assert_range_tuple2!((0.0, -1.0), value);
let value = Math::sin_cos(Math::PI + Math::PI_OVER_2);
assert_range_tuple2!((-1.0, 0.0), value);
let value = Math::sin_cos(Math::TWO_PI);
assert_range_tuple2!((0.0, 1.0), value);
let value = Math::sin_cos(Math::PI_OVER_4);
assert_range_tuple2!((0.707106781, 0.707106781), value);
let value = Math::sin_cos(1.0);
assert_range_tuple2!((0.841470985, 0.540302306), value);
let value = Math::sin_cos(-100.0);
assert_range_tuple2!((0.506365641, 0.862318872), value);
Source

pub fn sin_cos_deg(angle: f32) -> (f32, f32)

Computes the sine and cosine of the angle in degrees

  • angle: The angle to compute the sine and cosine with in degrees

Returns: Returns the sine and cosine (respectively) as a tuple

§Remarks

If you need to compute both cos_deg and sin_deg of the same angle, this function is more performant to produce both values than calling cos_deg and sin_deg separately

§Examples
let value = Math::sin_cos_deg(0.0);
assert_range_tuple2!((0.0, 1.0), value);
let value = Math::sin_cos_deg(90.0);
assert_range_tuple2!((1.0, 0.0), value);
let value = Math::sin_cos_deg(180.0);
assert_range_tuple2!((0.0, -1.0), value);
let value = Math::sin_cos_deg(270.0);
assert_range_tuple2!((-1.0, 0.0), value);
let value = Math::sin_cos_deg(360.0);
assert_range_tuple2!((0.0, 1.0), value);
let value = Math::sin_cos_deg(45.0);
assert_range_tuple2!((0.707106781, 0.707106781), value);
let value = Math::sin_cos_deg(57.29577951);
assert_range_tuple2!((0.841470985, 0.540302306), value);
let value = Math::sin_cos_deg(-5729.577951);
assert_range_tuple2!((0.506365641, 0.862318872), value);
Source

pub fn sinh(value: f32) -> f32

Computes the hyperbolic sine function

  • value: The value to compute the hyperbolic sine function with

Returns: Returns the computed hyperbolic sine function

§Examples
let value = Math::sinh(0.0);
assert_range!(0.0, value);
let value = Math::sinh(1.0);
assert_range!(1.1752012, value);
let value = Math::sinh(-1.0);
assert_range!(-1.1752012, value);
let value = Math::sinh(Math::PI);
assert_range!(11.54874, value);
let value = Math::sinh(Math::E);
assert_range!(7.5441365, value);
Source

pub fn smoothstep(value: f32, left_edge: f32, right_edge: f32) -> f32

Computes a smooth Hermite interpolation that returns a number between 0.0 and 1.0

  • value: The value for the interpolation, where left_edge < value < right_edge
  • left_edge: The leftmost edge to where 0.0 would start at
  • right_edge: The rightmost edge where 1.0 would start at

Returns: Returns a smooth Hermite interpolation that returns a number between 0.0 and 1.0

§Examples
let value = Math::smoothstep(-1.0, 0.0, 1.5);
assert_eq!(0.0, value);
let value = Math::smoothstep(1.0, 0.0, 1.5);
assert_eq!(0.7407408, value);
let value = Math::smoothstep(2.0, 0.0, 1.5);
assert_eq!(1.0, value);
let value = Math::smoothstep(0.5, -1.0, 3.0);
assert_eq!(0.31640625, value);
Source

pub fn sqrt(value: f32) -> f32

Gets the square root of the given number

  • value: The number to square root

Returns: Returns the square root of the number, returns NaN if value is negative

§Examples
let value = Math::sqrt(16.0);
assert_range!(4.0, value);
let value = Math::sqrt(1023.835);
assert_range!(31.9974217711, value);
let value = Math::sqrt(-102.0);
assert_eq!(true, f32::is_nan(value));
let value = Math::sqrt(-0.0);
assert_range!(0.0, value);
let value = Math::sqrt(0.2146018);
assert_range!(0.46325132, value);
Source

pub fn tan(angle: f32) -> f32

Gets the tangent of the angle in radians

  • angle: The angle to compute the tangent with in radians

Returns: Returns the value from the computed tangent

§Examples
let value = Math::tan(0.0);
assert_range!(0.0, value);
let value = Math::tan(Math::PI);
assert_range!(0.0, value);
let value = Math::tan(Math::TWO_PI);
assert_range!(0.0, value);
let value = Math::tan(Math::PI_OVER_4);
assert_range!(1.0, value);
let value = Math::tan(1.0);
assert_range!(1.557407725, value);
let value = Math::tan(-100.0);
assert_range!(0.587213915, value);
Source

pub fn tan_deg(angle: f32) -> f32

Gets the tangent of the angle in degrees

  • angle: The angle to compute the tangent with in degrees

Returns: Returns the value from the computed tangent

§Examples
let value = Math::tan_deg(0.0);
assert_range!(0.0, value);
let value = Math::tan_deg(180.0);
assert_range!(0.0, value);
let value = Math::tan_deg(360.0);
assert_range!(0.0, value);
let value = Math::tan_deg(45.0);
assert_range!(1.0, value);
let value = Math::tan_deg(57.29577951);
assert_range!(1.557407725, value);
let value = Math::tan_deg(-5729.577951);
assert_range!(0.587213915, value);
Source

pub fn tanh(value: f32) -> f32

Computes the hyperbolic tangent function

  • value: The value to compute the hyperbolic tangent function with

Returns: Returns the computed hyperbolic tangent function

§Examples
let value = Math::tanh(0.0);
assert_range!(0.0, value);
let value = Math::tanh(1.0);
assert_range!(0.7615942, value);
let value = Math::tanh(-1.0);
assert_range!(-0.7615942, value);
let value = Math::tanh(Math::PI);
assert_range!(0.9962721, value);
let value = Math::tanh(Math::E);
assert_range!(0.9913289, value);
Source

pub fn trunc(value: f32) -> f32

Truncates the value of the floating point number

  • value: The number to truncate

Returns: Returns the truncated number

§Examples
let value = Math::trunc(123.456);
assert_eq!(123.0, value);
let value = Math::trunc(-5.4);
assert_eq!(-5.0, value);
let value = Math::trunc(6.0);
assert_eq!(6.0, value);
let value = Math::trunc(-0.0);
assert_eq!(0.0, value);

Auto Trait Implementations§

§

impl Freeze for Math

§

impl RefUnwindSafe for Math

§

impl Send for Math

§

impl Sync for Math

§

impl Unpin for Math

§

impl UnwindSafe for Math

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.