numy 0.2.0

Trait boundaries for primitive Rust types
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use std::{cmp::Ordering, num::FpCategory};

use crate::{FloatConst, Num, Signed, UnsignedInt};

/// Flating point types.
///
/// Exposes:
/// - The `Num` trait
/// - Floating point constants
/// - Floating point functionality
///
/// See [`f32`] or [`f64`].
///
/// [`f32`]: https://doc.rust-lang.org/std/primitive.f32.html
/// [`f64`]: https://doc.rust-lang.org/std/primitive.f64.html
pub trait Float: Num + FloatConst + Signed {
    /// The unsigned integer variant of the type with the same size.
    ///
    /// This is not normally defined for floats and is only here to be the
    /// return type of `.to_bits` and input for `.from_bits`.
    type Bits: UnsignedInt;

    /// The byte array variant of the type with the same size.
    ///
    /// This is not normally defined for floats and is only here for the
    /// to/from endian functons.
    type Bytes;

    /// The radix or base of the internal representation.
    const RADIX: u32;

    /// Number of significant digits in base 2.
    const MANTISSA_DIGITS: u32;

    /// Approximate number of significant digits in base 10.
    const DIGITS: u32;

    /// Machine epsilon value for this floating point type.
    const EPSILON: Self;

    /// Smallest positive normal value.
    const MIN_POSITIVE: Self;

    /// One greater than the minimum possible normal power of 2 exponent.
    const MIN_EXP: i32;

    /// One greater than the maximum possible power of 2 exponent.
    const MAX_EXP: i32;

    /// Minimum x for which 10^x is normal.
    const MIN_10_EXP: i32;

    /// Maximum x for which 10^x is normal.
    const MAX_10_EXP: i32;

    /// Not a Number (NaN).
    const NAN: Self;

    /// Infinity (∞).
    const INFINITY: Self;

    /// Negative infinity (−∞).
    const NEG_INFINITY: Self;

    /// Returns the largest integer less than or equal to `self`.
    #[must_use]
    fn floor(self) -> Self;

    /// Returns the smallest integer greater than or equal to `self`.
    #[must_use]
    fn ceil(self) -> Self;

    /// Returns the nearest integer to `self`.
    #[must_use]
    fn round(self) -> Self;

    /// Returns the nearest integer to a number.
    #[must_use]
    fn round_ties_even(self) -> Self;

    /// Returns the integer part of `self`.
    #[must_use]
    fn trunc(self) -> Self;

    /// Returns the fractional part of `self`.
    #[must_use]
    fn fract(self) -> Self;

    /// Fused multiply-add.
    #[must_use]
    fn mul_add(self, a: Self, b: Self) -> Self;

    /// Calculates Euclidean division, the matching method for `rem_euclid`.
    #[must_use]
    fn div_euclid(self, rhs: Self) -> Self;

    /// Calculates the least nonnegative remainder of `self (mod rhs)`.
    #[must_use]
    fn rem_euclid(self, rhs: Self) -> Self;

    /// Raises a number to an integer power.
    #[must_use]
    fn powi(self, n: i32) -> Self;

    /// Raises a number to a floating point power.
    #[must_use]
    fn powf(self, n: Self) -> Self;

    /// Returns the square root of a number.
    #[must_use]
    fn sqrt(self) -> Self;

    /// Returns `e^(self)`.
    #[must_use]
    fn exp(self) -> Self;

    /// Returns `2^(self)`.
    #[must_use]
    fn exp2(self) -> Self;

    /// Returns the natural logarithm of a number.
    #[must_use]
    fn ln(self) -> Self;

    /// Returns the logarithm of the number with respect to an arbitrary base.
    #[must_use]
    fn log(self, base: Self) -> Self;

    /// Returns the base 2 logarithm of the number.
    #[must_use]
    fn log2(self) -> Self;

    /// Returns the base 10 logarithm of the number.
    #[must_use]
    fn log10(self) -> Self;

    /// Returns the cube root of a number.
    #[must_use]
    fn cbrt(self) -> Self;

    /// Compute the distance between the origin and a point (`x`, `y`) on the Euclidean plane.
    #[must_use]
    fn hypot(self, other: Self) -> Self;

    /// Computes the sine of a number (in radians).
    #[must_use]
    fn sin(self) -> Self;

    /// Computes the cosine of a number (in radians).
    #[must_use]
    fn cos(self) -> Self;

    /// Computes the tangent of a number (in radians).
    #[must_use]
    fn tan(self) -> Self;

    /// Computes the arcsine of a number.
    #[must_use]
    fn asin(self) -> Self;

    /// Computes the arccosine of a number.
    #[must_use]
    fn acos(self) -> Self;

    /// Computes the arctangent of a number.
    #[must_use]
    fn atan(self) -> Self;

    /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
    #[must_use]
    fn atan2(self, other: Self) -> Self;

    /// Simultaneously computes the sine and cosine of the number, `x`.
    #[must_use]
    fn sin_cos(self) -> (Self, Self);

    /// Returns `e^(self) - 1` in a way that is accurate even if the number is close to zero.
    #[must_use]
    fn exp_m1(self) -> Self;

    /// Returns `ln(1+n)` (natural logarithm) more accurately than if the operations were performed separately.
    #[must_use]
    fn ln_1p(self) -> Self;

    /// Hyperbolic sine function.
    #[must_use]
    fn sinh(self) -> Self;

    /// Hyperbolic cosine function.
    #[must_use]
    fn cosh(self) -> Self;

    /// Hyperbolic tangent function.
    #[must_use]
    fn tanh(self) -> Self;

    /// Inverse hyperbolic sine function.
    #[must_use]
    fn asinh(self) -> Self;

    /// Inverse hyperbolic cosine function.
    #[must_use]
    fn acosh(self) -> Self;

    /// Inverse hyperbolic tangent function.
    #[must_use]
    fn atanh(self) -> Self;

    /// Returns `true` if this value is NaN.
    #[must_use]
    fn is_nan(self) -> bool;

    /// Returns `true` if this value is positive or negative infinity.
    #[must_use]
    fn is_infinite(self) -> bool;

    /// Returns `true` if this number is neither infinite nor NaN.
    #[must_use]
    fn is_finite(self) -> bool;

    /// Returns `true` if the number is subnormal.
    #[must_use]
    fn is_subnormal(self) -> bool;

    /// Returns `true` if the number is neither zero, infinite, subnormal, or NaN.
    #[must_use]
    fn is_normal(self) -> bool;

    /// Returns the floating point category of the number.
    #[must_use]
    fn classify(self) -> FpCategory;

    /// Returns `true` if `self` has a positive sign.
    #[must_use]
    fn is_sign_positive(self) -> bool;

    /// Returns `true` if `self` has a negative sign.
    #[must_use]
    fn is_sign_negative(self) -> bool;

    /// Returns the least number greater than `self`.
    #[must_use]
    fn next_up(self) -> Self;

    /// Returns the greatest number less than `self`.
    #[must_use]
    fn next_down(self) -> Self;

    /// Takes the reciprocal (inverse) of a number, `1/x`.
    #[must_use]
    fn recip(self) -> Self;

    /// Converts radians to degrees.
    #[must_use]
    fn to_degrees(self) -> Self;

    /// Converts degrees to radians.
    #[must_use]
    fn to_radians(self) -> Self;

    /// Returns the maximum of the two numbers, ignoring NaN.
    #[deprecated = "Use `NumOrd::max` instead"]
    #[must_use]
    fn max(self, other: Self) -> Self;

    /// Returns the minimum of the two numbers, ignoring NaN.
    #[deprecated = "Use `NumOrd::min` instead"]
    #[must_use]
    fn min(self, other: Self) -> Self;

    /// Calculates the midpoint (average) between `self` and `rhs`.
    #[must_use]
    fn midpoint(self, other: Self) -> Self;

    /// Raw transmutation to `u32`.
    #[must_use]
    fn to_bits(self) -> Self::Bits;

    /// Raw transmutation from `u32`.
    #[must_use]
    fn from_bits(v: Self::Bits) -> Self;

    /// Returns the memory representation as a big-endian byte array.
    #[must_use]
    fn to_be_bytes(self) -> Self::Bytes;

    /// Returns the memory representation as a little-endian byte array.
    #[must_use]
    fn to_le_bytes(self) -> Self::Bytes;

    /// Returns the memory representation as a native-endian byte array.
    #[must_use]
    fn to_ne_bytes(self) -> Self::Bytes;

    /// Creates a float from a big-endian byte array.
    #[must_use]
    fn from_be_bytes(bytes: Self::Bytes) -> Self;

    /// Creates a float from a little-endian byte array.
    #[must_use]
    fn from_le_bytes(bytes: Self::Bytes) -> Self;

    /// Creates a float from a native-endian byte array.
    #[must_use]
    fn from_ne_bytes(bytes: Self::Bytes) -> Self;

    /// Returns the ordering between `self` and `other` according to total order.
    #[must_use]
    fn total_cmp(&self, other: &Self) -> Ordering;

    /// Restrict a value to a certain interval unless it is NaN.
    #[deprecated = "Use `NumOrd::clamp` instead"]
    #[must_use]
    fn clamp(self, min: Self, max: Self) -> Self;

    /// Returns a number that represents the sign of `self`.
    #[must_use]
    fn signum(self) -> Self;

    /// Returns a number composed of the magnitude of `self` and the sign of `sign`.
    #[must_use]
    fn copysign(self, sign: Self) -> Self;
}

macro_rules! impl_float {
    ($($t:ty, $b:ty);* $(;)*) => {
        $(
            impl Float for $t {
                type Bits = $b;

                type Bytes = [u8; std::mem::size_of::<Self>()];

                const RADIX: u32 = Self::RADIX;

                const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS;

                const DIGITS: u32 = Self::DIGITS;

                const EPSILON: Self = Self::EPSILON;

                const MIN_POSITIVE: Self = Self::MIN_POSITIVE;

                const MIN_EXP: i32 = Self::MIN_EXP;

                const MAX_EXP: i32 = Self::MAX_EXP;

                const MIN_10_EXP: i32 = Self::MIN_10_EXP;

                const MAX_10_EXP: i32 = Self::MAX_10_EXP;

                const NAN: Self = Self::NAN;

                const INFINITY: Self = Self::INFINITY;

                const NEG_INFINITY: Self = Self::NEG_INFINITY;

                #[inline(always)]
                fn floor(self) -> Self { Self::floor(self) }

                #[inline(always)]
                fn ceil(self) -> Self { Self::ceil(self) }

                #[inline(always)]
                fn round(self) -> Self { Self::round(self) }

                #[inline(always)]
                fn round_ties_even(self) -> Self { Self::round_ties_even(self) }

                #[inline(always)]
                fn trunc(self) -> Self { Self::trunc(self) }

                #[inline(always)]
                fn fract(self) -> Self { Self::fract(self) }

                #[inline(always)]
                fn mul_add(self, a: Self, b: Self) -> Self { Self::mul_add(self, a, b) }

                #[inline(always)]
                fn div_euclid(self, rhs: Self) -> Self { Self::div_euclid(self, rhs) }

                #[inline(always)]
                fn rem_euclid(self, rhs: Self) -> Self { Self::rem_euclid(self, rhs) }

                #[inline(always)]
                fn powi(self, n: i32) -> Self { Self::powi(self, n) }

                #[inline(always)]
                fn powf(self, n: Self) -> Self { Self::powf(self, n) }

                #[inline(always)]
                fn sqrt(self) -> Self { Self::sqrt(self) }

                #[inline(always)]
                fn exp(self) -> Self { Self::exp(self) }

                #[inline(always)]
                fn exp2(self) -> Self { Self::exp2(self) }

                #[inline(always)]
                fn ln(self) -> Self { Self::ln(self) }

                #[inline(always)]
                fn log(self, base: Self) -> Self { Self::log(self, base) }

                #[inline(always)]
                fn log2(self) -> Self { Self::log2(self) }

                #[inline(always)]
                fn log10(self) -> Self { Self::log10(self) }

                #[inline(always)]
                fn cbrt(self) -> Self { Self::cbrt(self) }

                #[inline(always)]
                fn hypot(self, other: Self) -> Self { Self::hypot(self, other) }

                #[inline(always)]
                fn sin(self) -> Self { Self::sin(self) }

                #[inline(always)]
                fn cos(self) -> Self { Self::cos(self) }

                #[inline(always)]
                fn tan(self) -> Self { Self::tan(self) }

                #[inline(always)]
                fn asin(self) -> Self { Self::asin(self) }

                #[inline(always)]
                fn acos(self) -> Self { Self::acos(self) }

                #[inline(always)]
                fn atan(self) -> Self { Self::atan(self) }

                #[inline(always)]
                fn atan2(self, other: Self) -> Self { Self::atan2(self, other) }

                #[inline(always)]
                fn sin_cos(self) -> (Self, Self) { Self::sin_cos(self) }

                #[inline(always)]
                fn exp_m1(self) -> Self { Self::exp_m1(self) }

                #[inline(always)]
                fn ln_1p(self) -> Self { Self::ln_1p(self) }

                #[inline(always)]
                fn sinh(self) -> Self { Self::sinh(self) }

                #[inline(always)]
                fn cosh(self) -> Self { Self::cosh(self) }

                #[inline(always)]
                fn tanh(self) -> Self { Self::tanh(self) }

                #[inline(always)]
                fn asinh(self) -> Self { Self::asinh(self) }

                #[inline(always)]
                fn acosh(self) -> Self { Self::acosh(self) }

                #[inline(always)]
                fn atanh(self) -> Self { Self::atanh(self) }

                #[inline(always)]
                fn is_nan(self) -> bool { Self::is_nan(self) }

                #[inline(always)]
                fn is_infinite(self) -> bool { Self::is_infinite(self) }

                #[inline(always)]
                fn is_finite(self) -> bool { Self::is_finite(self) }

                #[inline(always)]
                fn is_subnormal(self) -> bool { Self::is_subnormal(self) }

                #[inline(always)]
                fn is_normal(self) -> bool { Self::is_normal(self) }

                #[inline(always)]
                fn classify(self) -> FpCategory { Self::classify(self) }

                #[inline(always)]
                fn is_sign_positive(self) -> bool { Self::is_sign_positive(self) }

                #[inline(always)]
                fn is_sign_negative(self) -> bool { Self::is_sign_negative(self) }

                #[inline(always)]
                fn next_up(self) -> Self { Self::next_up(self) }

                #[inline(always)]
                fn next_down(self) -> Self { Self::next_down(self) }

                #[inline(always)]
                fn recip(self) -> Self { Self::recip(self) }

                #[inline(always)]
                fn to_degrees(self) -> Self { Self::to_degrees(self) }

                #[inline(always)]
                fn to_radians(self) -> Self { Self::to_radians(self) }

                #[inline(always)]
                fn max(self, other: Self) -> Self { Self::max(self, other) }

                #[inline(always)]
                fn min(self, other: Self) -> Self { Self::min(self, other) }

                #[inline(always)]
                fn midpoint(self, other: Self) -> Self { Self::midpoint(self, other) }

                #[inline(always)]
                fn to_bits(self) -> Self::Bits { Self::to_bits(self) }

                #[inline(always)]
                fn from_bits(v: Self::Bits) -> Self { Self::from_bits(v) }

                #[inline(always)]
                fn to_be_bytes(self) -> Self::Bytes { Self::to_be_bytes(self) }

                #[inline(always)]
                fn to_le_bytes(self) -> Self::Bytes { Self::to_le_bytes(self) }

                #[inline(always)]
                fn to_ne_bytes(self) -> Self::Bytes { Self::to_ne_bytes(self) }

                #[inline(always)]
                fn from_be_bytes(bytes: Self::Bytes) -> Self { Self::from_be_bytes(bytes) }

                #[inline(always)]
                fn from_le_bytes(bytes: Self::Bytes) -> Self { Self::from_le_bytes(bytes) }

                #[inline(always)]
                fn from_ne_bytes(bytes: Self::Bytes) -> Self { Self::from_ne_bytes(bytes) }

                #[inline(always)]
                fn total_cmp(&self, other: &Self) -> Ordering { Self::total_cmp(self, other) }

                #[inline(always)]
                fn clamp(self, min: Self, max: Self) -> Self { Self::clamp(self, min, max) }

                #[inline(always)]
                fn signum(self) -> Self { Self::signum(self) }

                #[inline(always)]
                fn copysign(self, sign: Self) -> Self { Self::copysign(self, sign) }
            }
        )*
    };
}

impl_float!(
    f32, u32;
    f64, u64;
);