batbox_num/
float.rs

1use super::*;
2
3/// Floating point number, including NaN/Inf
4pub trait Float: Num {
5    /// Archimedes’ constant (π)
6    const PI: Self;
7
8    /// Computes the arccosine of a number.
9    ///
10    /// Return value is in radians in the range [0, pi] or NaN if the number is outside the range [-1, 1].
11    fn acos(self) -> Self;
12
13    /// Computes the arcsine of a number.
14    ///
15    /// Return value is in radians in the range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1].
16    fn asin(self) -> Self;
17
18    /// Computes the arctangent of a number.
19    ///
20    /// Return value is in radians in the range [-pi/2, pi/2];
21    fn atan(self) -> Self;
22
23    /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.  
24    ///
25    /// * `x = 0`, `y = 0`: `0`
26    /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
27    /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
28    /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
29    fn atan2(y: Self, x: Self) -> Self;
30
31    /// Returns the smallest integer greater than or equal to a number.
32    fn ceil(self) -> Self;
33
34    /// Computes the cosine of a number (in radians).
35    fn cos(self) -> Self;
36
37    /// Calculates Euclidean division, the matching method for rem_euclid.
38    ///
39    /// This computes the integer n such that self = n * rhs + self.rem_euclid(rhs).
40    /// In other words, the result is self / rhs rounded to the integer n such that self >= n * rhs.
41    fn div_euclid(self, other: Self) -> Self;
42
43    /// Returns `e^(self)`, (the exponential function).
44    fn exp(self) -> Self;
45
46    /// Returns the largest integer less than or equal to `self`.
47    fn floor(self) -> Self;
48
49    /// Returns the fractional part of `self`.
50    fn fract(self) -> Self;
51
52    /// Returns `true` if this number is neither infinite nor NaN.
53    fn is_finite(self) -> bool;
54
55    /// Returns the natural logarithm of the number.
56    fn ln(self) -> Self;
57
58    /// Returns the logarithm of the number with respect to an arbitrary base.
59    fn log(self, base: Self) -> Self;
60
61    /// Returns the base 10 logarithm of the number.
62    fn log10(self) -> Self;
63
64    /// Returns the base 2 logarithm of the number.
65    fn log2(self) -> Self;
66
67    /// Raises a number to a floating point power.
68    fn powf(self, n: Self) -> Self;
69
70    /// Raises a number to an integer power.
71    fn powi(self, n: i32) -> Self;
72
73    /// Takes the reciprocal (inverse) of a number, 1/x
74    fn recip(self) -> Self;
75
76    /// Calculates the least nonnegative remainder of `self (mod rhs)`.
77    ///
78    /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
79    /// most cases. However, due to a floating point round-off error it can
80    /// result in `r == rhs.abs()`, violating the mathematical definition, if
81    /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
82    /// This result is not an element of the function's codomain, but it is the
83    /// closest floating point number in the real numbers and thus fulfills the
84    /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
85    /// approximately.
86    fn rem_euclid(self, other: Self) -> Self;
87
88    /// Returns the nearest integer to `self`.
89    /// Round half-way cases away from `0.0`.
90    fn round(self) -> Self;
91
92    /// Computes the sine of a number (in radians).
93    fn sin(self) -> Self;
94
95    /// Simultaneously computes the sine and cosine of the number, `x`.
96    /// Returns `(sin(x), cos(x))`.
97    fn sin_cos(self) -> (Self, Self);
98
99    /// Returns the square root of a number.
100    ///
101    /// Returns NaN if `self` is a negative number other than `-0.0`.
102    fn sqrt(self) -> Self;
103
104    /// Computes the tangent of a number (in radians).
105    fn tan(self) -> Self;
106
107    /// Convert an [f32] into [Self]
108    fn from_f32(x: f32) -> Self;
109
110    /// Convert self into an [f32]
111    fn as_f32(self) -> f32;
112}
113
114macro_rules! impl_float {
115    ($t:ident) => {
116        impl UNum for $t {
117            const ZERO: Self = 0.0;
118            const ONE: Self = 1.0;
119        }
120        impl Num for $t {
121            fn signum(self) -> Self {
122                $t::signum(self)
123            }
124        }
125        impl Float for $t {
126            const PI: Self = std::$t::consts::PI;
127            fn acos(self) -> Self {
128                $t::acos(self)
129            }
130            fn asin(self) -> Self {
131                $t::asin(self)
132            }
133            fn atan(self) -> Self {
134                $t::atan(self)
135            }
136            fn atan2(y: Self, x: Self) -> Self {
137                $t::atan2(y, x)
138            }
139            fn ceil(self) -> Self {
140                $t::ceil(self)
141            }
142            fn cos(self) -> Self {
143                $t::cos(self)
144            }
145            fn div_euclid(self, other: Self) -> Self {
146                $t::div_euclid(self, other)
147            }
148            fn exp(self) -> Self {
149                $t::exp(self)
150            }
151            fn floor(self) -> Self {
152                $t::floor(self)
153            }
154            fn fract(self) -> Self {
155                $t::fract(self)
156            }
157            fn is_finite(self) -> bool {
158                $t::is_finite(self)
159            }
160            fn ln(self) -> Self {
161                $t::ln(self)
162            }
163            fn log(self, base: Self) -> Self {
164                $t::log(self, base)
165            }
166            fn log10(self) -> Self {
167                $t::log10(self)
168            }
169            fn log2(self) -> Self {
170                $t::log2(self)
171            }
172            fn powf(self, n: Self) -> Self {
173                $t::powf(self, n)
174            }
175            fn powi(self, n: i32) -> Self {
176                $t::powi(self, n)
177            }
178            fn recip(self) -> Self {
179                $t::recip(self)
180            }
181            fn rem_euclid(self, other: Self) -> Self {
182                $t::rem_euclid(self, other)
183            }
184            fn round(self) -> Self {
185                $t::round(self)
186            }
187            fn sin(self) -> Self {
188                $t::sin(self)
189            }
190            fn sin_cos(self) -> (Self, Self) {
191                $t::sin_cos(self)
192            }
193            fn sqrt(self) -> Self {
194                $t::sqrt(self)
195            }
196            fn tan(self) -> Self {
197                $t::tan(self)
198            }
199            fn from_f32(x: f32) -> Self {
200                x as Self
201            }
202            fn as_f32(self) -> f32 {
203                self as f32
204            }
205        }
206    };
207}
208
209impl_float!(f32);
210impl_float!(f64);