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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use crate::FiniteRangeNumber;
use crate::{False, IsAtomic, IsFloat, IsInteger, IsNonZero, IsSigned, True};
use core::fmt::LowerExp;
use core::ops::Neg;
/// Common operations on floats
pub trait Float:
Neg<Output = Self>
+ FiniteRangeNumber
+ LowerExp
+ IsAtomic<Atomic = False>
+ IsFloat<Float = True>
+ IsInteger<Integer = False>
+ IsSigned<Signed = True>
+ IsNonZero<NonZero = False>
{
// TODO: figure out both bits and numerical conversions
// fn to_bits(self) ->
// fn from-bits()
/// The radix or base of the internal representation of [`Self`]
const RADIX: usize;
/// Approximate number of significant digits in base 10.
const DIGITS: usize;
/// This is the difference between 1.0 and the next larger representable number.
const EPSILON: Self;
/// Infinity (∞).
const INFINITY: Self;
/// Negative infinity (−∞).
const NEG_INFINITY: Self;
/// Not a Number (NaN).
///
/// Note that IEEE 754 doesn’t define just a single NaN value; a plethora of
/// bit patterns are considered to be NaN. Furthermore, the standard makes a
/// difference between a “signaling” and a “quiet” NaN, and allows
/// inspecting its “payload” (the unspecified bits in the bit pattern).
/// This constant isn’t guaranteed to equal to any specific NaN bitpattern,
/// and the stability of its representation over Rust versions and target
/// platforms isn’t guaranteed.
const NAN: Self;
/// Number of significant digits in base 2.
const MANTISSA_DIGITS: usize;
/// Maximum possible power of 10 exponent.
const MAX_10_EXP: usize;
/// Maximum possible power of 2 exponent.
const MAX_EXP: usize;
/// Minimum possible normal power of 10 exponent.
const MIN_10_EXP: usize;
/// One greater than the minimum possible normal power of 2 exponent.
const MIN_EXP: usize;
/// Smallest positive normal value.
const MIN_POSITIVE: Self;
/// Returns true if this value is NaN.
fn is_nan(self) -> bool;
/// Returns true if this value is positive infinity or negative infinity,
/// and false otherwise.
fn is_infinite(self) -> bool;
/// Returns true if this number is neither infinite nor NaN.
fn is_finite(self) -> bool;
/// Return `true` if the number is [subnormal](https://en.wikipedia.org/wiki/Subnormal_number)
fn is_subnormal(self) -> bool;
/// Return `true` if the number is neither zero, infinite, [subnormal](https://en.wikipedia.org/wiki/Subnormal_number), or NaN.
fn is_normal(self) -> bool;
/// Returns the floating point category of the number. If only one property
/// is going to be tested, it is generally faster to use the specific
/// predicate instead.
fn classify(self) -> core::num::FpCategory;
/// Returns true if self has a positive sign, including +0.0, NaNs with
/// positive sign bit and positive infinity. Note that IEEE 754 doesn’t
/// assign any meaning to the sign bit in case of a NaN, and as Rust doesn’t
/// guarantee that the bit pattern of NaNs are conserved over arithmetic
/// operations, the result of is_sign_positive on a NaN might produce an
/// unexpected result in some cases. See explanation of NaN as a special
/// value for more info.
fn is_sign_positive(self) -> bool;
/// Returns true if self has a negative sign, including -0.0, NaNs with
/// egative sign bit and negative infinity. Note that IEEE 754 doesn’t a
/// ssign any meaning to the sign bit in case of a NaN, and as Rust doesn’t
/// guarantee that the bit pattern of NaNs are conserved over arithmetic
/// operations, the result of is_sign_negative on a NaN might produce an
/// unexpected result in some cases. See explanation of NaN as a special
/// value for more info.
fn is_sign_negative(self) -> bool;
/// Takes the reciprocal (inverse) of a number, 1/x.
fn recip(self) -> Self;
/// Converts radians to degrees.
fn to_degrees(self) -> Self;
/// Converts degrees to radians.
fn to_radians(self) -> Self;
/// Return the ordering between `self` and `other`.
///
/// Unlike the standard partial comparison between floating point numbers,
/// this comparison always produces an ordering in accordance to the
/// `totalOrder` predicate as defined in the IEEE 754 (2008 revision)
/// floating point standard. The values are ordered in the following sequence:
/// - negative quiet NaN
/// - negative signaling NaN
/// - negative infinity
/// - negative numbers
/// - negative subnormal numbers
/// - negative zero
/// - positive zero
/// - positive subnormal numbers
/// - positive numbers
/// - positive infinity
/// - positive signaling NaN
/// - positive quiet NaN.
///
/// The ordering established by this function does not always agree with the
/// [`PartialOrd`] and [`PartialEq`] implementations of [`Self`].
/// For example, they consider negative and positive zero equal, while
/// total_cmp doesn’t.
///
/// The interpretation of the signaling NaN bit follows the definition in
/// the IEEE 754 standard, which may not match the interpretation by some
/// of the older, non-conformant (e.g. MIPS) hardware implementations.
fn total_cmp(&self, other: &Self) -> core::cmp::Ordering;
/// Performs Euclidean division.
/// Since, for the positive integers, all common definitions of division are
/// equal, this is exactly equal to self / rhs.
#[cfg(feature = "std")]
fn div_euclid(self, rhs: Self) -> Self;
/// Calculates the least remainder of self (mod rhs).
/// Since, for the positive integers, all common definitions of division are
/// equal, this is exactly equal to self % rhs.
#[cfg(feature = "std")]
fn rem_euclid(self, rhs: Self) -> Self;
/// Returns the largest integer less than or equal to self.
#[cfg(feature = "std")]
fn floor(self) -> Self;
/// Returns the smallest integer greater than or equal to self.
#[cfg(feature = "std")]
fn ceil(self) -> Self;
/// Returns the nearest integer to self. Round half-way cases away from 0.0.
#[cfg(feature = "std")]
fn round(self) -> Self;
/// Returns the integer part of self. This means that non-integer numbers
/// are always truncated towards zero.
#[cfg(feature = "std")]
fn trunc(self) -> Self;
/// Returns the fractional part of self.
#[cfg(feature = "std")]
fn fract(self) -> Self;
/// Computes the absolute value of self.
#[cfg(feature = "std")]
fn abs(self) -> Self;
/// Returns a number that represents the sign of self.
///
/// - 1.0 if the number is positive, +0.0 or INFINITY
/// - -1.0 if the number is negative, -0.0 or NEG_INFINITY
/// - NaN if the number is NaN
#[cfg(feature = "std")]
fn signum(self) -> Self;
/// Returns a number composed of the magnitude of self and the sign of sign.
///
/// Equal to self if the sign of self and sign are the same, otherwise equal
/// to -self. If self is a NaN, then a NaN with the sign bit of sign is
/// returned. Note, however, that conserving the sign bit on NaN across
/// arithmetical operations is not generally guaranteed. See explanation of
/// NaN as a special value for more info.
#[cfg(feature = "std")]
fn copysign(self, sign: Self) -> Self;
/// Raises a number to an integer power.
///
/// Using this function is generally faster than using powf. It might have a
/// different sequence of rounding operations than powf, so the results are
/// not guaranteed to agree.
#[cfg(feature = "std")]
fn powi(self, n: isize) -> Self;
/// Raises a number to a floating point power.
#[cfg(feature = "std")]
fn powf(self, n: Self) -> Self;
/// Returns the square root of a number.
///
/// Returns NaN if self is a negative number other than -0.0.
#[cfg(feature = "std")]
fn sqrt(self) -> Self;
/// Returns `e^(self)`, (the exponential function).
#[cfg(feature = "std")]
fn exp(self) -> Self;
/// Returns 2^(self).
#[cfg(feature = "std")]
fn exp2(self) -> Self;
/// Returns the natural logarithm of the number.
#[cfg(feature = "std")]
fn ln(self) -> Self;
/// Returns the logarithm of the number with respect to an arbitrary base.
///
/// The result might not be correctly rounded owing to implementation
/// details; self.log2() can produce more accurate results for base 2,
/// and self.log10() can produce more accurate results for base 10.
#[cfg(feature = "std")]
fn log(self, base: Self) -> Self;
/// Returns the base 2 logarithm of the number.
#[cfg(feature = "std")]
fn log2(self) -> Self;
/// Returns the base 10 logarithm of the number.
#[cfg(feature = "std")]
fn log10(self) -> Self;
/// Returns the cube root of a number.
#[cfg(feature = "std")]
fn cbrt(self) -> Self;
/// Calculates the length of the hypotenuse of a right-angle triangle given
/// legs of length x and y.
#[cfg(feature = "std")]
fn hypot(self, other: Self) -> Self;
/// Computes the sine of a number (in radians).
#[cfg(feature = "std")]
fn sin(self) -> Self;
/// Computes the cosine of a number (in radians).
#[cfg(feature = "std")]
fn cos(self) -> Self;
/// Computes the tangent of a number (in radians).
#[cfg(feature = "std")]
fn tan(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].
#[cfg(feature = "std")]
fn asin(self) -> 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].
#[cfg(feature = "std")]
fn acos(self) -> Self;
/// Computes the arctangent of a number. Return value is in radians in the
/// range [-pi/2, pi/2];
#[cfg(feature = "std")]
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)`
#[cfg(feature = "std")]
fn atan2(self, other: Self) -> Self;
/// Simultaneously computes the sine and cosine of the number, x. Returns
/// (sin(x), cos(x)).
#[cfg(feature = "std")]
fn sin_cos(self) -> (Self, Self);
/// Returns e^(self) - 1 in a way that is accurate even if the number is
/// close to zero.
#[cfg(feature = "std")]
fn exp_m1(self) -> Self;
/// Returns ln(1+n) (natural logarithm) more accurately than if the
/// operations were performed separately.
#[cfg(feature = "std")]
fn ln_1p(self) -> Self;
/// Hyperbolic sine function.
#[cfg(feature = "std")]
fn sinh(self) -> Self;
/// Hyperbolic cosine function.
#[cfg(feature = "std")]
fn cosh(self) -> Self;
/// Hyperbolic tangent function.
#[cfg(feature = "std")]
fn tanh(self) -> Self;
/// Inverse hyperbolic sine function.
#[cfg(feature = "std")]
fn asinh(self) -> Self;
/// Inverse hyperbolic cosine function.
#[cfg(feature = "std")]
fn acosh(self) -> Self;
/// Inverse hyperbolic tangent function.
#[cfg(feature = "std")]
fn atanh(self) -> Self;
}