draw

source of std::math

1// Mathematics over floats (LANGUAGE_SPEC.md section 6.1a).2//3// Float is a lang item: f32 and f64 satisfy it with no marker, through the4// float_* extensions below, and the functions take any Float: `sqrt(2.0)`5// is an f32 like any untyped float, `const r: f64 = sqrt(2.0)` an f64. The externs are the runtime's wrappers over C's6// libm, which LLVM turns into single instructions where the target has7// them (sqrt, and with SSE4.1 floor, ceil and trunc); the interpreter and8// compile-time code call the same functions. min, max, clamp, lerp and9// abs take any Number, integers included.1011import std::number;1213// What f32 and f64 satisfy, with no marker.14exp interface Float {15    fn float_sqrt(self: &) -> Self;16    fn float_cbrt(self: &) -> Self;17    fn float_sin(self: &) -> Self;18    fn float_cos(self: &) -> Self;19    fn float_tan(self: &) -> Self;20    fn float_asin(self: &) -> Self;21    fn float_acos(self: &) -> Self;22    fn float_atan(self: &) -> Self;23    fn float_sinh(self: &) -> Self;24    fn float_cosh(self: &) -> Self;25    fn float_tanh(self: &) -> Self;26    fn float_exponential(self: &) -> Self;27    fn float_exp2(self: &) -> Self;28    fn float_log(self: &) -> Self;29    fn float_log2(self: &) -> Self;30    fn float_log10(self: &) -> Self;31    fn float_floor(self: &) -> Self;32    fn float_ceil(self: &) -> Self;33    fn float_round(self: &) -> Self;34    fn float_trunc(self: &) -> Self;35    fn float_abs(self: &) -> Self;36    fn float_atan2(self: &, x: &Self) -> Self;37    fn float_pow(self: &, exponent: &Self) -> Self;38    fn float_hypot(self: &, other: &Self) -> Self;39    fn float_copysign(self: &, sign: &Self) -> Self;40    fn float_min(self: &, other: &Self) -> Self;41    fn float_max(self: &, other: &Self) -> Self;42    fn float_fma(self: &, factor: &Self, addend: &Self) -> Self;43    fn float_is_nan(self: &) -> bool;44    fn float_is_infinite(self: &) -> bool;45    fn float_is_finite(self: &) -> bool;46}4748/* --- Constants --- */4950// Untyped, so each takes the type it is used at, as a literal does.51exp const PI = 3.141592653589793;52exp const TAU = 6.283185307179586;53exp const E = 2.718281828459045;54exp const SQRT_2 = 1.4142135623730951;55exp const FRAC_1_SQRT_2 = 0.7071067811865476;56exp const FRAC_PI_2 = 1.5707963267948966;57exp const FRAC_PI_4 = 0.7853981633974483;58exp const LN_2 = 0.6931471805599453;59exp const LN_10 = 2.302585092994046;60exp const LOG2_E = 1.4426950408889634;61exp const LOG10_E = 0.4342944819032518;6263// Positive infinity and a quiet NaN, as f64 (`INFINITY to f32` for f32).64exp const INFINITY: f64 = from_bits(0x7FF0000000000000);65exp const NAN: f64 = from_bits(0x7FF8000000000000);6667fn from_bits(bits: u64) -> f64 => bits as f64;6869/* --- Any float --- */7071// The square root.72exp fn sqrt<T: Float>(x: T) -> T => x.float_sqrt();7374// The cube root.75exp fn cbrt<T: Float>(x: T) -> T => x.float_cbrt();7677// The sine of an angle in radians.78exp fn sin<T: Float>(x: T) -> T => x.float_sin();7980// The cosine of an angle in radians.81exp fn cos<T: Float>(x: T) -> T => x.float_cos();8283// The tangent of an angle in radians.84exp fn tan<T: Float>(x: T) -> T => x.float_tan();8586// The arc sine, in radians.87exp fn asin<T: Float>(x: T) -> T => x.float_asin();8889// The arc cosine, in radians.90exp fn acos<T: Float>(x: T) -> T => x.float_acos();9192// The arc tangent, in radians.93exp fn atan<T: Float>(x: T) -> T => x.float_atan();9495// The hyperbolic sine.96exp fn sinh<T: Float>(x: T) -> T => x.float_sinh();9798// The hyperbolic cosine.99exp fn cosh<T: Float>(x: T) -> T => x.float_cosh();100101// The hyperbolic tangent.102exp fn tanh<T: Float>(x: T) -> T => x.float_tanh();103104// e raised to the value.105exp fn exponential<T: Float>(x: T) -> T => x.float_exponential();106107// 2 raised to the value.108exp fn exp2<T: Float>(x: T) -> T => x.float_exp2();109110// The natural logarithm.111exp fn log<T: Float>(x: T) -> T => x.float_log();112113// The base-2 logarithm.114exp fn log2<T: Float>(x: T) -> T => x.float_log2();115116// The base-10 logarithm.117exp fn log10<T: Float>(x: T) -> T => x.float_log10();118119// The largest whole number not above the value.120exp fn floor<T: Float>(x: T) -> T => x.float_floor();121122// The smallest whole number not below the value.123exp fn ceil<T: Float>(x: T) -> T => x.float_ceil();124125// The nearest whole number, halves away from zero.126exp fn round<T: Float>(x: T) -> T => x.float_round();127128// The whole number toward zero.129exp fn trunc<T: Float>(x: T) -> T => x.float_trunc();130131// The angle of the point (x, y), in radians: the arc tangent of y / x in the right quadrant.132exp fn atan2<T: Float>(y: T, x: T) -> T => y.float_atan2(&x);133134// The value raised to the exponent.135exp fn pow<T: Float>(x: T, exponent: T) -> T => x.float_pow(&exponent);136137// The square root of the sum of the squares, without overflow in between.138exp fn hypot<T: Float>(x: T, other: T) -> T => x.float_hypot(&other);139140// The magnitude with the sign of `sign`.141exp fn copysign<T: Float>(x: T, sign: T) -> T => x.float_copysign(&sign);142143// x * factor + addend, rounded once.144exp fn fma<T: Float>(x: T, factor: T, addend: T) -> T => x.float_fma(&factor, &addend);145exp fn is_nan<T: Float>(x: T) -> bool => x.float_is_nan();146exp fn is_infinite<T: Float>(x: T) -> bool => x.float_is_infinite();147exp fn is_finite<T: Float>(x: T) -> bool => x.float_is_finite();148149/* --- Any number --- */150151// The smaller value; for floats a NaN gives way to the other.152exp fn min<T: Number>(a: T, b: T) -> T {153    if (T: Float) {154        return a.float_min(&b);155    }156    return if (b < a) b else a;157}158159// The larger value; for floats a NaN gives way to the other.160exp fn max<T: Number>(a: T, b: T) -> T {161    if (T: Float) {162        return a.float_max(&b);163    }164    return if (b > a) b else a;165}166167// The value kept within [low, high].168exp fn clamp<T: Number>(x: T, low: T, high: T) -> T => min(max(x, low), high);169170// The magnitude; a float's sign bit cleared, -0.0 and NaNs included.171exp fn abs<T: Number>(x: T) -> T {172    if (T: Float) {173        return x.float_abs();174    }175    return if (x < 0) 0 - x else x;176}177178// From a at t = 0 to b at t = 1.179exp fn lerp<T: Number>(a: T, b: T, t: T) -> T => a + (b - a) * t;180181/* --- f64 --- */182183extern al_sqrt(x: f64) -> f64;184extern al_cbrt(x: f64) -> f64;185extern al_sin(x: f64) -> f64;186extern al_cos(x: f64) -> f64;187extern al_tan(x: f64) -> f64;188extern al_asin(x: f64) -> f64;189extern al_acos(x: f64) -> f64;190extern al_atan(x: f64) -> f64;191extern al_sinh(x: f64) -> f64;192extern al_cosh(x: f64) -> f64;193extern al_tanh(x: f64) -> f64;194extern al_exp(x: f64) -> f64;195extern al_exp2(x: f64) -> f64;196extern al_log(x: f64) -> f64;197extern al_log2(x: f64) -> f64;198extern al_log10(x: f64) -> f64;199extern al_floor(x: f64) -> f64;200extern al_ceil(x: f64) -> f64;201extern al_round(x: f64) -> f64;202extern al_trunc(x: f64) -> f64;203extern al_fabs(x: f64) -> f64;204extern al_atan2(y: f64, x: f64) -> f64;205extern al_pow(x: f64, y: f64) -> f64;206extern al_hypot(x: f64, y: f64) -> f64;207extern al_copysign(x: f64, y: f64) -> f64;208extern al_fmin(x: f64, y: f64) -> f64;209extern al_fmax(x: f64, y: f64) -> f64;210extern al_fma(x: f64, y: f64, z: f64) -> f64;211212fn float_sqrt(self x: &f64) -> f64 => al_sqrt(x);213fn float_cbrt(self x: &f64) -> f64 => al_cbrt(x);214fn float_sin(self x: &f64) -> f64 => al_sin(x);215fn float_cos(self x: &f64) -> f64 => al_cos(x);216fn float_tan(self x: &f64) -> f64 => al_tan(x);217fn float_asin(self x: &f64) -> f64 => al_asin(x);218fn float_acos(self x: &f64) -> f64 => al_acos(x);219fn float_atan(self x: &f64) -> f64 => al_atan(x);220fn float_sinh(self x: &f64) -> f64 => al_sinh(x);221fn float_cosh(self x: &f64) -> f64 => al_cosh(x);222fn float_tanh(self x: &f64) -> f64 => al_tanh(x);223fn float_exponential(self x: &f64) -> f64 => al_exp(x);224fn float_exp2(self x: &f64) -> f64 => al_exp2(x);225fn float_log(self x: &f64) -> f64 => al_log(x);226fn float_log2(self x: &f64) -> f64 => al_log2(x);227fn float_log10(self x: &f64) -> f64 => al_log10(x);228fn float_floor(self x: &f64) -> f64 => al_floor(x);229fn float_ceil(self x: &f64) -> f64 => al_ceil(x);230fn float_round(self x: &f64) -> f64 => al_round(x);231fn float_trunc(self x: &f64) -> f64 => al_trunc(x);232fn float_abs(self x: &f64) -> f64 => al_fabs(x);233fn float_atan2(self y: &f64, x: &f64) -> f64 => al_atan2(y, x);234fn float_pow(self x: &f64, exponent: &f64) -> f64 => al_pow(x, exponent);235fn float_hypot(self x: &f64, other: &f64) -> f64 => al_hypot(x, other);236fn float_copysign(self x: &f64, sign: &f64) -> f64 => al_copysign(x, sign);237fn float_min(self x: &f64, other: &f64) -> f64 => al_fmin(x, other);238fn float_max(self x: &f64, other: &f64) -> f64 => al_fmax(x, other);239fn float_fma(self x: &f64, factor: &f64, addend: &f64) -> f64 => al_fma(x, factor, addend);240241fn float_is_nan(self x: &f64) -> bool {242    const v: f64 = x;243    return v != v;244}245246fn float_is_infinite(self x: &f64) -> bool {247    const v: f64 = x;248    const infinity: f64 = INFINITY;249    return v == infinity || v == -infinity;250}251252fn float_is_finite(self x: &f64) -> bool {253    const v: f64 = x;254    return v - v == 0.0;255}256257/* --- f32 --- */258259extern al_sqrtf(x: f32) -> f32;260extern al_cbrtf(x: f32) -> f32;261extern al_sinf(x: f32) -> f32;262extern al_cosf(x: f32) -> f32;263extern al_tanf(x: f32) -> f32;264extern al_asinf(x: f32) -> f32;265extern al_acosf(x: f32) -> f32;266extern al_atanf(x: f32) -> f32;267extern al_sinhf(x: f32) -> f32;268extern al_coshf(x: f32) -> f32;269extern al_tanhf(x: f32) -> f32;270extern al_expf(x: f32) -> f32;271extern al_exp2f(x: f32) -> f32;272extern al_logf(x: f32) -> f32;273extern al_log2f(x: f32) -> f32;274extern al_log10f(x: f32) -> f32;275extern al_floorf(x: f32) -> f32;276extern al_ceilf(x: f32) -> f32;277extern al_roundf(x: f32) -> f32;278extern al_truncf(x: f32) -> f32;279extern al_fabsf(x: f32) -> f32;280extern al_atan2f(y: f32, x: f32) -> f32;281extern al_powf(x: f32, y: f32) -> f32;282extern al_hypotf(x: f32, y: f32) -> f32;283extern al_copysignf(x: f32, y: f32) -> f32;284extern al_fminf(x: f32, y: f32) -> f32;285extern al_fmaxf(x: f32, y: f32) -> f32;286extern al_fmaf(x: f32, y: f32, z: f32) -> f32;287288fn float_sqrt(self x: &f32) -> f32 => al_sqrtf(x);289fn float_cbrt(self x: &f32) -> f32 => al_cbrtf(x);290fn float_sin(self x: &f32) -> f32 => al_sinf(x);291fn float_cos(self x: &f32) -> f32 => al_cosf(x);292fn float_tan(self x: &f32) -> f32 => al_tanf(x);293fn float_asin(self x: &f32) -> f32 => al_asinf(x);294fn float_acos(self x: &f32) -> f32 => al_acosf(x);295fn float_atan(self x: &f32) -> f32 => al_atanf(x);296fn float_sinh(self x: &f32) -> f32 => al_sinhf(x);297fn float_cosh(self x: &f32) -> f32 => al_coshf(x);298fn float_tanh(self x: &f32) -> f32 => al_tanhf(x);299fn float_exponential(self x: &f32) -> f32 => al_expf(x);300fn float_exp2(self x: &f32) -> f32 => al_exp2f(x);301fn float_log(self x: &f32) -> f32 => al_logf(x);302fn float_log2(self x: &f32) -> f32 => al_log2f(x);303fn float_log10(self x: &f32) -> f32 => al_log10f(x);304fn float_floor(self x: &f32) -> f32 => al_floorf(x);305fn float_ceil(self x: &f32) -> f32 => al_ceilf(x);306fn float_round(self x: &f32) -> f32 => al_roundf(x);307fn float_trunc(self x: &f32) -> f32 => al_truncf(x);308fn float_abs(self x: &f32) -> f32 => al_fabsf(x);309fn float_atan2(self y: &f32, x: &f32) -> f32 => al_atan2f(y, x);310fn float_pow(self x: &f32, exponent: &f32) -> f32 => al_powf(x, exponent);311fn float_hypot(self x: &f32, other: &f32) -> f32 => al_hypotf(x, other);312fn float_copysign(self x: &f32, sign: &f32) -> f32 => al_copysignf(x, sign);313fn float_min(self x: &f32, other: &f32) -> f32 => al_fminf(x, other);314fn float_max(self x: &f32, other: &f32) -> f32 => al_fmaxf(x, other);315fn float_fma(self x: &f32, factor: &f32, addend: &f32) -> f32 => al_fmaf(x, factor, addend);316317fn float_is_nan(self x: &f32) -> bool {318    const v: f32 = x;319    return v != v;320}321322fn float_is_infinite(self x: &f32) -> bool {323    const v: f32 = x;324    const infinity: f32 = INFINITY to f32;325    return v == infinity || v == -infinity;326}327328fn float_is_finite(self x: &f32) -> bool {329    const v: f32 = x;330    return v - v == 0.0;331}