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