74 lines
2.3 KiB
Plaintext
74 lines
2.3 KiB
Plaintext
#! @file constants.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the constants of real numbers: those of mathematics, as `f64`s, which
|
||
|
|
#! `as f32` gives the nearest `f32` of, and those of each type of real, its
|
||
|
|
#! largest and least values, epsilon, infinity, and not a number.
|
||
|
|
|
||
|
|
module std::math;
|
||
|
|
|
||
|
|
|
||
|
|
#! π, the ratio of a circle's circumference to its diameter.
|
||
|
|
pub const pi: f64 = 3.141592653589793;
|
||
|
|
|
||
|
|
#! τ, which is 2π, the angle of a whole turn in radians.
|
||
|
|
pub const tau: f64 = 6.283185307179586;
|
||
|
|
|
||
|
|
#! e, the base of the natural logarithm.
|
||
|
|
pub const e: f64 = 2.718281828459045;
|
||
|
|
|
||
|
|
#! The square root of 2.
|
||
|
|
pub const sqrt_2: f64 = 1.4142135623730951;
|
||
|
|
|
||
|
|
#! The natural logarithm of 2.
|
||
|
|
pub const ln_2: f64 = 0.6931471805599453;
|
||
|
|
|
||
|
|
#! The natural logarithm of 10.
|
||
|
|
pub const ln_10: f64 = 2.302585092994046;
|
||
|
|
|
||
|
|
#! The largest finite value of a type of real.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @returns About 3.4e38 for `f32`, and 1.8e308 for `f64`.
|
||
|
|
pub def largest<T>(): T where T: real {
|
||
|
|
if const size_of::<T>() == 4 {
|
||
|
|
return 3.4028234663852886e38 as T;
|
||
|
|
}
|
||
|
|
return 1.7976931348623157e308 as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The least positive normal value of a type of real. Those closer to zero are
|
||
|
|
#! subnormal, and hold fewer bits of precision.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @returns About 1.2e-38 for `f32`, and 2.2e-308 for `f64`.
|
||
|
|
pub def smallest<T>(): T where T: real {
|
||
|
|
if const size_of::<T>() == 4 {
|
||
|
|
return 1.1754943508222875e-38 as T;
|
||
|
|
}
|
||
|
|
return 2.2250738585072014e-308 as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The difference between 1 and the next value of a type of real.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @returns 2^-23 for `f32`, and 2^-52 for `f64`.
|
||
|
|
pub def epsilon<T>(): T where T: real {
|
||
|
|
if const size_of::<T>() == 4 {
|
||
|
|
return 1.1920928955078125e-07 as T;
|
||
|
|
}
|
||
|
|
return 2.220446049250313e-16 as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Positive infinity, of a type of real.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @returns Infinity; its negation is negative infinity.
|
||
|
|
pub def infinity<T>(): T where T: real {
|
||
|
|
return real_of_bits(0x7FF0000000000000u) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Not a number, of a type of real: a quiet NaN, of positive sign.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @returns NaN, which is unequal to everything, itself included.
|
||
|
|
pub def nan<T>(): T where T: real {
|
||
|
|
return real_of_bits(0x7FF8000000000000u) as T;
|
||
|
|
}
|