1
- use core:: { num , intrinsics} ;
1
+ use core:: intrinsics;
2
2
use core:: f32:: { NAN , NEG_INFINITY } ;
3
3
4
4
pub trait FloatImpls {
5
- fn is_nan ( self ) -> bool ;
6
- fn is_infinite ( self ) -> bool ;
7
- fn is_finite ( self ) -> bool ;
8
- fn is_normal ( self ) -> bool ;
9
5
fn floor ( self ) -> f32 ;
10
6
fn ceil ( self ) -> f32 ;
11
7
fn round ( self ) -> f32 ;
12
8
fn trunc ( self ) -> f32 ;
13
9
fn fract ( self ) -> f32 ;
14
- fn signum ( self ) -> f32 ;
15
- fn is_sign_positive ( self ) -> bool ;
16
- fn is_sign_negative ( self ) -> bool ;
17
10
fn mul_add ( self , a : f32 , b : f32 ) -> f32 ;
18
- fn powi ( self , n : i32 ) -> f32 ;
19
11
fn powf ( self , n : f32 ) -> f32 ;
20
12
fn sqrt ( self ) -> f32 ;
21
13
fn exp ( self ) -> f32 ;
@@ -24,99 +16,13 @@ pub trait FloatImpls {
24
16
fn log ( self , base : f32 ) -> f32 ;
25
17
fn log2 ( self ) -> f32 ;
26
18
fn log10 ( self ) -> f32 ;
27
- fn to_degrees ( self ) -> f32 ;
28
19
fn max ( self , other : f32 ) -> f32 ;
29
20
fn min ( self , other : f32 ) -> f32 ;
30
21
fn asinh ( self ) -> f32 ;
31
22
fn acosh ( self ) -> f32 ;
32
23
}
33
24
34
25
impl FloatImpls for f32 {
35
- /// Returns `true` if this value is `NaN` and false otherwise.
36
- ///
37
- /// ```
38
- /// use std::f32;
39
- ///
40
- /// let nan = f32::NAN;
41
- /// let f = 7.0_f32;
42
- ///
43
- /// assert!(nan.is_nan());
44
- /// assert!(!f.is_nan());
45
- /// ```
46
- #[ inline]
47
- fn is_nan ( self ) -> bool {
48
- num:: Float :: is_nan ( self )
49
- }
50
-
51
- /// Returns `true` if this value is positive infinity or negative infinity and
52
- /// false otherwise.
53
- ///
54
- /// ```
55
- /// use std::f32;
56
- ///
57
- /// let f = 7.0f32;
58
- /// let inf = f32::INFINITY;
59
- /// let neg_inf = f32::NEG_INFINITY;
60
- /// let nan = f32::NAN;
61
- ///
62
- /// assert!(!f.is_infinite());
63
- /// assert!(!nan.is_infinite());
64
- ///
65
- /// assert!(inf.is_infinite());
66
- /// assert!(neg_inf.is_infinite());
67
- /// ```
68
- #[ inline]
69
- fn is_infinite ( self ) -> bool {
70
- num:: Float :: is_infinite ( self )
71
- }
72
-
73
- /// Returns `true` if this number is neither infinite nor `NaN`.
74
- ///
75
- /// ```
76
- /// use std::f32;
77
- ///
78
- /// let f = 7.0f32;
79
- /// let inf = f32::INFINITY;
80
- /// let neg_inf = f32::NEG_INFINITY;
81
- /// let nan = f32::NAN;
82
- ///
83
- /// assert!(f.is_finite());
84
- ///
85
- /// assert!(!nan.is_finite());
86
- /// assert!(!inf.is_finite());
87
- /// assert!(!neg_inf.is_finite());
88
- /// ```
89
- #[ inline]
90
- fn is_finite ( self ) -> bool {
91
- num:: Float :: is_finite ( self )
92
- }
93
-
94
- /// Returns `true` if the number is neither zero, infinite,
95
- /// [subnormal][subnormal], or `NaN`.
96
- ///
97
- /// ```
98
- /// use std::f32;
99
- ///
100
- /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
101
- /// let max = f32::MAX;
102
- /// let lower_than_min = 1.0e-40_f32;
103
- /// let zero = 0.0_f32;
104
- ///
105
- /// assert!(min.is_normal());
106
- /// assert!(max.is_normal());
107
- ///
108
- /// assert!(!zero.is_normal());
109
- /// assert!(!f32::NAN.is_normal());
110
- /// assert!(!f32::INFINITY.is_normal());
111
- /// // Values between `0` and `min` are Subnormal.
112
- /// assert!(!lower_than_min.is_normal());
113
- /// ```
114
- /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
115
- #[ inline]
116
- fn is_normal ( self ) -> bool {
117
- num:: Float :: is_normal ( self )
118
- }
119
-
120
26
/// Returns the largest integer less than or equal to a number.
121
27
///
122
28
/// ```
@@ -215,67 +121,6 @@ impl FloatImpls for f32 {
215
121
self - self . trunc ( )
216
122
}
217
123
218
- /// Returns a number that represents the sign of `self`.
219
- ///
220
- /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
221
- /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
222
- /// - `NAN` if the number is `NAN`
223
- ///
224
- /// ```
225
- /// use std::f32;
226
- ///
227
- /// let f = 3.5_f32;
228
- ///
229
- /// assert_eq!(f.signum(), 1.0);
230
- /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
231
- ///
232
- /// assert!(f32::NAN.signum().is_nan());
233
- /// ```
234
- #[ inline]
235
- fn signum ( self ) -> f32 {
236
- num:: Float :: signum ( self )
237
- }
238
-
239
- /// Returns `true` if `self`'s sign bit is positive, including
240
- /// `+0.0` and `INFINITY`.
241
- ///
242
- /// ```
243
- /// use std::f32;
244
- ///
245
- /// let nan = f32::NAN;
246
- /// let f = 7.0_f32;
247
- /// let g = -7.0_f32;
248
- ///
249
- /// assert!(f.is_sign_positive());
250
- /// assert!(!g.is_sign_positive());
251
- /// // Requires both tests to determine if is `NaN`
252
- /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
253
- /// ```
254
- #[ inline]
255
- fn is_sign_positive ( self ) -> bool {
256
- num:: Float :: is_sign_positive ( self )
257
- }
258
-
259
- /// Returns `true` if `self`'s sign is negative, including `-0.0`
260
- /// and `NEG_INFINITY`.
261
- ///
262
- /// ```
263
- /// use std::f32;
264
- ///
265
- /// let nan = f32::NAN;
266
- /// let f = 7.0f32;
267
- /// let g = -7.0f32;
268
- ///
269
- /// assert!(!f.is_sign_negative());
270
- /// assert!(g.is_sign_negative());
271
- /// // Requires both tests to determine if is `NaN`.
272
- /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
273
- /// ```
274
- #[ inline]
275
- fn is_sign_negative ( self ) -> bool {
276
- num:: Float :: is_sign_negative ( self )
277
- }
278
-
279
124
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
280
125
/// error. This produces a more accurate result with better performance than
281
126
/// a separate multiplication operation followed by an add.
@@ -297,23 +142,6 @@ impl FloatImpls for f32 {
297
142
unsafe { intrinsics:: fmaf32 ( self , a, b) }
298
143
}
299
144
300
- /// Raises a number to an integer power.
301
- ///
302
- /// Using this function is generally faster than using `powf`
303
- ///
304
- /// ```
305
- /// use std::f32;
306
- ///
307
- /// let x = 2.0_f32;
308
- /// let abs_difference = (x.powi(2) - x*x).abs();
309
- ///
310
- /// assert!(abs_difference <= f32::EPSILON);
311
- /// ```
312
- #[ inline]
313
- fn powi ( self , n : i32 ) -> f32 {
314
- num:: Float :: powi ( self , n)
315
- }
316
-
317
145
/// Raises a number to a floating point power.
318
146
///
319
147
/// ```
@@ -483,22 +311,6 @@ impl FloatImpls for f32 {
483
311
return unsafe { intrinsics:: log10f32 ( self ) } ;
484
312
}
485
313
486
- /// Converts radians to degrees.
487
- ///
488
- /// ```
489
- /// use std::f32::{self, consts};
490
- ///
491
- /// let angle = consts::PI;
492
- ///
493
- /// let abs_difference = (angle.to_degrees() - 180.0).abs();
494
- ///
495
- /// assert!(abs_difference <= f32::EPSILON);
496
- /// ```
497
- #[ inline]
498
- fn to_degrees ( self ) -> f32 {
499
- num:: Float :: to_degrees ( self )
500
- }
501
-
502
314
/// Returns the maximum of the two numbers.
503
315
///
504
316
/// ```
0 commit comments