]> git.lizzy.rs Git - rust.git/blob - src/libstd/f32.rs
Error when generator trait is not found
[rust.git] / src / libstd / f32.rs
1 //! This module provides constants which are specific to the implementation
2 //! of the `f32` floating point data type.
3 //!
4 //! *[See also the `f32` primitive type](../../std/primitive.f32.html).*
5 //!
6 //! Mathematically significant numbers are provided in the `consts` sub-module.
7
8 #![stable(feature = "rust1", since = "1.0.0")]
9 #![allow(missing_docs)]
10
11 #[cfg(not(test))]
12 use crate::intrinsics;
13 #[cfg(not(test))]
14 use crate::sys::cmath;
15
16 #[stable(feature = "rust1", since = "1.0.0")]
17 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
18 #[stable(feature = "rust1", since = "1.0.0")]
19 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
20 #[stable(feature = "rust1", since = "1.0.0")]
21 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub use core::f32::consts;
26
27 #[cfg(not(test))]
28 #[lang = "f32_runtime"]
29 impl f32 {
30     /// Returns the largest integer less than or equal to a number.
31     ///
32     /// # Examples
33     ///
34     /// ```
35     /// let f = 3.7_f32;
36     /// let g = 3.0_f32;
37     /// let h = -3.7_f32;
38     ///
39     /// assert_eq!(f.floor(), 3.0);
40     /// assert_eq!(g.floor(), 3.0);
41     /// assert_eq!(h.floor(), -4.0);
42     /// ```
43     #[stable(feature = "rust1", since = "1.0.0")]
44     #[inline]
45     pub fn floor(self) -> f32 {
46         // On MSVC LLVM will lower many math intrinsics to a call to the
47         // corresponding function. On MSVC, however, many of these functions
48         // aren't actually available as symbols to call, but rather they are all
49         // `static inline` functions in header files. This means that from a C
50         // perspective it's "compatible", but not so much from an ABI
51         // perspective (which we're worried about).
52         //
53         // The inline header functions always just cast to a f64 and do their
54         // operation, so we do that here as well, but only for MSVC targets.
55         //
56         // Note that there are many MSVC-specific float operations which
57         // redirect to this comment, so `floorf` is just one case of a missing
58         // function on MSVC, but there are many others elsewhere.
59         #[cfg(target_env = "msvc")]
60         return (self as f64).floor() as f32;
61         #[cfg(not(target_env = "msvc"))]
62         return unsafe { intrinsics::floorf32(self) };
63     }
64
65     /// Returns the smallest integer greater than or equal to a number.
66     ///
67     /// # Examples
68     ///
69     /// ```
70     /// let f = 3.01_f32;
71     /// let g = 4.0_f32;
72     ///
73     /// assert_eq!(f.ceil(), 4.0);
74     /// assert_eq!(g.ceil(), 4.0);
75     /// ```
76     #[stable(feature = "rust1", since = "1.0.0")]
77     #[inline]
78     pub fn ceil(self) -> f32 {
79         // see notes above in `floor`
80         #[cfg(target_env = "msvc")]
81         return (self as f64).ceil() as f32;
82         #[cfg(not(target_env = "msvc"))]
83         return unsafe { intrinsics::ceilf32(self) };
84     }
85
86     /// Returns the nearest integer to a number. Round half-way cases away from
87     /// `0.0`.
88     ///
89     /// # Examples
90     ///
91     /// ```
92     /// let f = 3.3_f32;
93     /// let g = -3.3_f32;
94     ///
95     /// assert_eq!(f.round(), 3.0);
96     /// assert_eq!(g.round(), -3.0);
97     /// ```
98     #[stable(feature = "rust1", since = "1.0.0")]
99     #[inline]
100     pub fn round(self) -> f32 {
101         unsafe { intrinsics::roundf32(self) }
102     }
103
104     /// Returns the integer part of a number.
105     ///
106     /// # Examples
107     ///
108     /// ```
109     /// let f = 3.7_f32;
110     /// let g = 3.0_f32;
111     /// let h = -3.7_f32;
112     ///
113     /// assert_eq!(f.trunc(), 3.0);
114     /// assert_eq!(g.trunc(), 3.0);
115     /// assert_eq!(h.trunc(), -3.0);
116     /// ```
117     #[stable(feature = "rust1", since = "1.0.0")]
118     #[inline]
119     pub fn trunc(self) -> f32 {
120         unsafe { intrinsics::truncf32(self) }
121     }
122
123     /// Returns the fractional part of a number.
124     ///
125     /// # Examples
126     ///
127     /// ```
128     /// use std::f32;
129     ///
130     /// let x = 3.5_f32;
131     /// let y = -3.5_f32;
132     /// let abs_difference_x = (x.fract() - 0.5).abs();
133     /// let abs_difference_y = (y.fract() - (-0.5)).abs();
134     ///
135     /// assert!(abs_difference_x <= f32::EPSILON);
136     /// assert!(abs_difference_y <= f32::EPSILON);
137     /// ```
138     #[stable(feature = "rust1", since = "1.0.0")]
139     #[inline]
140     pub fn fract(self) -> f32 { self - self.trunc() }
141
142     /// Computes the absolute value of `self`. Returns `NAN` if the
143     /// number is `NAN`.
144     ///
145     /// # Examples
146     ///
147     /// ```
148     /// use std::f32;
149     ///
150     /// let x = 3.5_f32;
151     /// let y = -3.5_f32;
152     ///
153     /// let abs_difference_x = (x.abs() - x).abs();
154     /// let abs_difference_y = (y.abs() - (-y)).abs();
155     ///
156     /// assert!(abs_difference_x <= f32::EPSILON);
157     /// assert!(abs_difference_y <= f32::EPSILON);
158     ///
159     /// assert!(f32::NAN.abs().is_nan());
160     /// ```
161     #[stable(feature = "rust1", since = "1.0.0")]
162     #[inline]
163     pub fn abs(self) -> f32 {
164         unsafe { intrinsics::fabsf32(self) }
165     }
166
167     /// Returns a number that represents the sign of `self`.
168     ///
169     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
170     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
171     /// - `NAN` if the number is `NAN`
172     ///
173     /// # Examples
174     ///
175     /// ```
176     /// use std::f32;
177     ///
178     /// let f = 3.5_f32;
179     ///
180     /// assert_eq!(f.signum(), 1.0);
181     /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
182     ///
183     /// assert!(f32::NAN.signum().is_nan());
184     /// ```
185     #[stable(feature = "rust1", since = "1.0.0")]
186     #[inline]
187     pub fn signum(self) -> f32 {
188         if self.is_nan() {
189             NAN
190         } else {
191             1.0_f32.copysign(self)
192         }
193     }
194
195     /// Returns a number composed of the magnitude of `self` and the sign of
196     /// `sign`.
197     ///
198     /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
199     /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
200     /// `sign` is returned.
201     ///
202     /// # Examples
203     ///
204     /// ```
205     /// use std::f32;
206     ///
207     /// let f = 3.5_f32;
208     ///
209     /// assert_eq!(f.copysign(0.42), 3.5_f32);
210     /// assert_eq!(f.copysign(-0.42), -3.5_f32);
211     /// assert_eq!((-f).copysign(0.42), 3.5_f32);
212     /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
213     ///
214     /// assert!(f32::NAN.copysign(1.0).is_nan());
215     /// ```
216     #[inline]
217     #[must_use]
218     #[stable(feature = "copysign", since = "1.35.0")]
219     pub fn copysign(self, sign: f32) -> f32 {
220         unsafe { intrinsics::copysignf32(self, sign) }
221     }
222
223     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
224     /// error, yielding a more accurate result than an unfused multiply-add.
225     ///
226     /// Using `mul_add` can be more performant than an unfused multiply-add if
227     /// the target architecture has a dedicated `fma` CPU instruction.
228     ///
229     /// # Examples
230     ///
231     /// ```
232     /// use std::f32;
233     ///
234     /// let m = 10.0_f32;
235     /// let x = 4.0_f32;
236     /// let b = 60.0_f32;
237     ///
238     /// // 100.0
239     /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
240     ///
241     /// assert!(abs_difference <= f32::EPSILON);
242     /// ```
243     #[stable(feature = "rust1", since = "1.0.0")]
244     #[inline]
245     pub fn mul_add(self, a: f32, b: f32) -> f32 {
246         unsafe { intrinsics::fmaf32(self, a, b) }
247     }
248
249     /// Calculates Euclidean division, the matching method for `rem_euclid`.
250     ///
251     /// This computes the integer `n` such that
252     /// `self = n * rhs + self.rem_euclid(rhs)`.
253     /// In other words, the result is `self / rhs` rounded to the integer `n`
254     /// such that `self >= n * rhs`.
255     ///
256     /// # Examples
257     ///
258     /// ```
259     /// let a: f32 = 7.0;
260     /// let b = 4.0;
261     /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
262     /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
263     /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
264     /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
265     /// ```
266     #[inline]
267     #[stable(feature = "euclidean_division", since = "1.38.0")]
268     pub fn div_euclid(self, rhs: f32) -> f32 {
269         let q = (self / rhs).trunc();
270         if self % rhs < 0.0 {
271             return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
272         }
273         q
274     }
275
276     /// Calculates the least nonnegative remainder of `self (mod rhs)`.
277     ///
278     /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
279     /// most cases. However, due to a floating point round-off error it can
280     /// result in `r == rhs.abs()`, violating the mathematical definition, if
281     /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
282     /// This result is not an element of the function's codomain, but it is the
283     /// closest floating point number in the real numbers and thus fulfills the
284     /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
285     /// approximatively.
286     ///
287     /// # Examples
288     ///
289     /// ```
290     /// let a: f32 = 7.0;
291     /// let b = 4.0;
292     /// assert_eq!(a.rem_euclid(b), 3.0);
293     /// assert_eq!((-a).rem_euclid(b), 1.0);
294     /// assert_eq!(a.rem_euclid(-b), 3.0);
295     /// assert_eq!((-a).rem_euclid(-b), 1.0);
296     /// // limitation due to round-off error
297     /// assert!((-std::f32::EPSILON).rem_euclid(3.0) != 0.0);
298     /// ```
299     #[inline]
300     #[stable(feature = "euclidean_division", since = "1.38.0")]
301     pub fn rem_euclid(self, rhs: f32) -> f32 {
302         let r = self % rhs;
303         if r < 0.0 {
304             r + rhs.abs()
305         } else {
306             r
307         }
308     }
309
310
311     /// Raises a number to an integer power.
312     ///
313     /// Using this function is generally faster than using `powf`
314     ///
315     /// # Examples
316     ///
317     /// ```
318     /// use std::f32;
319     ///
320     /// let x = 2.0_f32;
321     /// let abs_difference = (x.powi(2) - (x * x)).abs();
322     ///
323     /// assert!(abs_difference <= f32::EPSILON);
324     /// ```
325     #[stable(feature = "rust1", since = "1.0.0")]
326     #[inline]
327     pub fn powi(self, n: i32) -> f32 {
328         unsafe { intrinsics::powif32(self, n) }
329     }
330
331     /// Raises a number to a floating point power.
332     ///
333     /// # Examples
334     ///
335     /// ```
336     /// use std::f32;
337     ///
338     /// let x = 2.0_f32;
339     /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
340     ///
341     /// assert!(abs_difference <= f32::EPSILON);
342     /// ```
343     #[stable(feature = "rust1", since = "1.0.0")]
344     #[inline]
345     pub fn powf(self, n: f32) -> f32 {
346         // see notes above in `floor`
347         #[cfg(target_env = "msvc")]
348         return (self as f64).powf(n as f64) as f32;
349         #[cfg(not(target_env = "msvc"))]
350         return unsafe { intrinsics::powf32(self, n) };
351     }
352
353     /// Takes the square root of a number.
354     ///
355     /// Returns NaN if `self` is a negative number.
356     ///
357     /// # Examples
358     ///
359     /// ```
360     /// use std::f32;
361     ///
362     /// let positive = 4.0_f32;
363     /// let negative = -4.0_f32;
364     ///
365     /// let abs_difference = (positive.sqrt() - 2.0).abs();
366     ///
367     /// assert!(abs_difference <= f32::EPSILON);
368     /// assert!(negative.sqrt().is_nan());
369     /// ```
370     #[stable(feature = "rust1", since = "1.0.0")]
371     #[inline]
372     pub fn sqrt(self) -> f32 {
373         if self < 0.0 {
374             NAN
375         } else {
376             unsafe { intrinsics::sqrtf32(self) }
377         }
378     }
379
380     /// Returns `e^(self)`, (the exponential function).
381     ///
382     /// # Examples
383     ///
384     /// ```
385     /// use std::f32;
386     ///
387     /// let one = 1.0f32;
388     /// // e^1
389     /// let e = one.exp();
390     ///
391     /// // ln(e) - 1 == 0
392     /// let abs_difference = (e.ln() - 1.0).abs();
393     ///
394     /// assert!(abs_difference <= f32::EPSILON);
395     /// ```
396     #[stable(feature = "rust1", since = "1.0.0")]
397     #[inline]
398     pub fn exp(self) -> f32 {
399         // see notes above in `floor`
400         #[cfg(target_env = "msvc")]
401         return (self as f64).exp() as f32;
402         #[cfg(not(target_env = "msvc"))]
403         return unsafe { intrinsics::expf32(self) };
404     }
405
406     /// Returns `2^(self)`.
407     ///
408     /// # Examples
409     ///
410     /// ```
411     /// use std::f32;
412     ///
413     /// let f = 2.0f32;
414     ///
415     /// // 2^2 - 4 == 0
416     /// let abs_difference = (f.exp2() - 4.0).abs();
417     ///
418     /// assert!(abs_difference <= f32::EPSILON);
419     /// ```
420     #[stable(feature = "rust1", since = "1.0.0")]
421     #[inline]
422     pub fn exp2(self) -> f32 {
423         unsafe { intrinsics::exp2f32(self) }
424     }
425
426     /// Returns the natural logarithm of the number.
427     ///
428     /// # Examples
429     ///
430     /// ```
431     /// use std::f32;
432     ///
433     /// let one = 1.0f32;
434     /// // e^1
435     /// let e = one.exp();
436     ///
437     /// // ln(e) - 1 == 0
438     /// let abs_difference = (e.ln() - 1.0).abs();
439     ///
440     /// assert!(abs_difference <= f32::EPSILON);
441     /// ```
442     #[stable(feature = "rust1", since = "1.0.0")]
443     #[inline]
444     pub fn ln(self) -> f32 {
445         // see notes above in `floor`
446         #[cfg(target_env = "msvc")]
447         return (self as f64).ln() as f32;
448         #[cfg(not(target_env = "msvc"))]
449         return unsafe { intrinsics::logf32(self) };
450     }
451
452     /// Returns the logarithm of the number with respect to an arbitrary base.
453     ///
454     /// The result may not be correctly rounded owing to implementation details;
455     /// `self.log2()` can produce more accurate results for base 2, and
456     /// `self.log10()` can produce more accurate results for base 10.
457     ///
458     /// # Examples
459     ///
460     /// ```
461     /// use std::f32;
462     ///
463     /// let five = 5.0f32;
464     ///
465     /// // log5(5) - 1 == 0
466     /// let abs_difference = (five.log(5.0) - 1.0).abs();
467     ///
468     /// assert!(abs_difference <= f32::EPSILON);
469     /// ```
470     #[stable(feature = "rust1", since = "1.0.0")]
471     #[inline]
472     pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
473
474     /// Returns the base 2 logarithm of the number.
475     ///
476     /// # Examples
477     ///
478     /// ```
479     /// use std::f32;
480     ///
481     /// let two = 2.0f32;
482     ///
483     /// // log2(2) - 1 == 0
484     /// let abs_difference = (two.log2() - 1.0).abs();
485     ///
486     /// assert!(abs_difference <= f32::EPSILON);
487     /// ```
488     #[stable(feature = "rust1", since = "1.0.0")]
489     #[inline]
490     pub fn log2(self) -> f32 {
491         #[cfg(target_os = "android")]
492         return crate::sys::android::log2f32(self);
493         #[cfg(not(target_os = "android"))]
494         return unsafe { intrinsics::log2f32(self) };
495     }
496
497     /// Returns the base 10 logarithm of the number.
498     ///
499     /// # Examples
500     ///
501     /// ```
502     /// use std::f32;
503     ///
504     /// let ten = 10.0f32;
505     ///
506     /// // log10(10) - 1 == 0
507     /// let abs_difference = (ten.log10() - 1.0).abs();
508     ///
509     /// assert!(abs_difference <= f32::EPSILON);
510     /// ```
511     #[stable(feature = "rust1", since = "1.0.0")]
512     #[inline]
513     pub fn log10(self) -> f32 {
514         // see notes above in `floor`
515         #[cfg(target_env = "msvc")]
516         return (self as f64).log10() as f32;
517         #[cfg(not(target_env = "msvc"))]
518         return unsafe { intrinsics::log10f32(self) };
519     }
520
521     /// The positive difference of two numbers.
522     ///
523     /// * If `self <= other`: `0:0`
524     /// * Else: `self - other`
525     ///
526     /// # Examples
527     ///
528     /// ```
529     /// use std::f32;
530     ///
531     /// let x = 3.0f32;
532     /// let y = -3.0f32;
533     ///
534     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
535     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
536     ///
537     /// assert!(abs_difference_x <= f32::EPSILON);
538     /// assert!(abs_difference_y <= f32::EPSILON);
539     /// ```
540     #[stable(feature = "rust1", since = "1.0.0")]
541     #[inline]
542     #[rustc_deprecated(since = "1.10.0",
543                        reason = "you probably meant `(self - other).abs()`: \
544                                  this operation is `(self - other).max(0.0)` \
545                                  except that `abs_sub` also propagates NaNs (also \
546                                  known as `fdimf` in C). If you truly need the positive \
547                                  difference, consider using that expression or the C function \
548                                  `fdimf`, depending on how you wish to handle NaN (please consider \
549                                  filing an issue describing your use-case too).")]
550     pub fn abs_sub(self, other: f32) -> f32 {
551         unsafe { cmath::fdimf(self, other) }
552     }
553
554     /// Takes the cubic root of a number.
555     ///
556     /// # Examples
557     ///
558     /// ```
559     /// use std::f32;
560     ///
561     /// let x = 8.0f32;
562     ///
563     /// // x^(1/3) - 2 == 0
564     /// let abs_difference = (x.cbrt() - 2.0).abs();
565     ///
566     /// assert!(abs_difference <= f32::EPSILON);
567     /// ```
568     #[stable(feature = "rust1", since = "1.0.0")]
569     #[inline]
570     pub fn cbrt(self) -> f32 {
571         unsafe { cmath::cbrtf(self) }
572     }
573
574     /// Calculates the length of the hypotenuse of a right-angle triangle given
575     /// legs of length `x` and `y`.
576     ///
577     /// # Examples
578     ///
579     /// ```
580     /// use std::f32;
581     ///
582     /// let x = 2.0f32;
583     /// let y = 3.0f32;
584     ///
585     /// // sqrt(x^2 + y^2)
586     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
587     ///
588     /// assert!(abs_difference <= f32::EPSILON);
589     /// ```
590     #[stable(feature = "rust1", since = "1.0.0")]
591     #[inline]
592     pub fn hypot(self, other: f32) -> f32 {
593         unsafe { cmath::hypotf(self, other) }
594     }
595
596     /// Computes the sine of a number (in radians).
597     ///
598     /// # Examples
599     ///
600     /// ```
601     /// use std::f32;
602     ///
603     /// let x = f32::consts::FRAC_PI_2;
604     ///
605     /// let abs_difference = (x.sin() - 1.0).abs();
606     ///
607     /// assert!(abs_difference <= f32::EPSILON);
608     /// ```
609     #[stable(feature = "rust1", since = "1.0.0")]
610     #[inline]
611     pub fn sin(self) -> f32 {
612         // see notes in `core::f32::Float::floor`
613         #[cfg(target_env = "msvc")]
614         return (self as f64).sin() as f32;
615         #[cfg(not(target_env = "msvc"))]
616         return unsafe { intrinsics::sinf32(self) };
617     }
618
619     /// Computes the cosine of a number (in radians).
620     ///
621     /// # Examples
622     ///
623     /// ```
624     /// use std::f32;
625     ///
626     /// let x = 2.0 * f32::consts::PI;
627     ///
628     /// let abs_difference = (x.cos() - 1.0).abs();
629     ///
630     /// assert!(abs_difference <= f32::EPSILON);
631     /// ```
632     #[stable(feature = "rust1", since = "1.0.0")]
633     #[inline]
634     pub fn cos(self) -> f32 {
635         // see notes in `core::f32::Float::floor`
636         #[cfg(target_env = "msvc")]
637         return (self as f64).cos() as f32;
638         #[cfg(not(target_env = "msvc"))]
639         return unsafe { intrinsics::cosf32(self) };
640     }
641
642     /// Computes the tangent of a number (in radians).
643     ///
644     /// # Examples
645     ///
646     /// ```
647     /// use std::f32;
648     ///
649     /// let x = f32::consts::FRAC_PI_4;
650     /// let abs_difference = (x.tan() - 1.0).abs();
651     ///
652     /// assert!(abs_difference <= f32::EPSILON);
653     /// ```
654     #[stable(feature = "rust1", since = "1.0.0")]
655     #[inline]
656     pub fn tan(self) -> f32 {
657         unsafe { cmath::tanf(self) }
658     }
659
660     /// Computes the arcsine of a number. Return value is in radians in
661     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
662     /// [-1, 1].
663     ///
664     /// # Examples
665     ///
666     /// ```
667     /// use std::f32;
668     ///
669     /// let f = f32::consts::FRAC_PI_2;
670     ///
671     /// // asin(sin(pi/2))
672     /// let abs_difference = (f.sin().asin() - f32::consts::FRAC_PI_2).abs();
673     ///
674     /// assert!(abs_difference <= f32::EPSILON);
675     /// ```
676     #[stable(feature = "rust1", since = "1.0.0")]
677     #[inline]
678     pub fn asin(self) -> f32 {
679         unsafe { cmath::asinf(self) }
680     }
681
682     /// Computes the arccosine of a number. Return value is in radians in
683     /// the range [0, pi] or NaN if the number is outside the range
684     /// [-1, 1].
685     ///
686     /// # Examples
687     ///
688     /// ```
689     /// use std::f32;
690     ///
691     /// let f = f32::consts::FRAC_PI_4;
692     ///
693     /// // acos(cos(pi/4))
694     /// let abs_difference = (f.cos().acos() - f32::consts::FRAC_PI_4).abs();
695     ///
696     /// assert!(abs_difference <= f32::EPSILON);
697     /// ```
698     #[stable(feature = "rust1", since = "1.0.0")]
699     #[inline]
700     pub fn acos(self) -> f32 {
701         unsafe { cmath::acosf(self) }
702     }
703
704     /// Computes the arctangent of a number. Return value is in radians in the
705     /// range [-pi/2, pi/2];
706     ///
707     /// # Examples
708     ///
709     /// ```
710     /// use std::f32;
711     ///
712     /// let f = 1.0f32;
713     ///
714     /// // atan(tan(1))
715     /// let abs_difference = (f.tan().atan() - 1.0).abs();
716     ///
717     /// assert!(abs_difference <= f32::EPSILON);
718     /// ```
719     #[stable(feature = "rust1", since = "1.0.0")]
720     #[inline]
721     pub fn atan(self) -> f32 {
722         unsafe { cmath::atanf(self) }
723     }
724
725     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
726     ///
727     /// * `x = 0`, `y = 0`: `0`
728     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
729     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
730     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
731     ///
732     /// # Examples
733     ///
734     /// ```
735     /// use std::f32;
736     ///
737     /// // Positive angles measured counter-clockwise
738     /// // from positive x axis
739     /// // -pi/4 radians (45 deg clockwise)
740     /// let x1 = 3.0f32;
741     /// let y1 = -3.0f32;
742     ///
743     /// // 3pi/4 radians (135 deg counter-clockwise)
744     /// let x2 = -3.0f32;
745     /// let y2 = 3.0f32;
746     ///
747     /// let abs_difference_1 = (y1.atan2(x1) - (-f32::consts::FRAC_PI_4)).abs();
748     /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * f32::consts::FRAC_PI_4)).abs();
749     ///
750     /// assert!(abs_difference_1 <= f32::EPSILON);
751     /// assert!(abs_difference_2 <= f32::EPSILON);
752     /// ```
753     #[stable(feature = "rust1", since = "1.0.0")]
754     #[inline]
755     pub fn atan2(self, other: f32) -> f32 {
756         unsafe { cmath::atan2f(self, other) }
757     }
758
759     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
760     /// `(sin(x), cos(x))`.
761     ///
762     /// # Examples
763     ///
764     /// ```
765     /// use std::f32;
766     ///
767     /// let x = f32::consts::FRAC_PI_4;
768     /// let f = x.sin_cos();
769     ///
770     /// let abs_difference_0 = (f.0 - x.sin()).abs();
771     /// let abs_difference_1 = (f.1 - x.cos()).abs();
772     ///
773     /// assert!(abs_difference_0 <= f32::EPSILON);
774     /// assert!(abs_difference_1 <= f32::EPSILON);
775     /// ```
776     #[stable(feature = "rust1", since = "1.0.0")]
777     #[inline]
778     pub fn sin_cos(self) -> (f32, f32) {
779         (self.sin(), self.cos())
780     }
781
782     /// Returns `e^(self) - 1` in a way that is accurate even if the
783     /// number is close to zero.
784     ///
785     /// # Examples
786     ///
787     /// ```
788     /// use std::f32;
789     ///
790     /// let x = 6.0f32;
791     ///
792     /// // e^(ln(6)) - 1
793     /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
794     ///
795     /// assert!(abs_difference <= f32::EPSILON);
796     /// ```
797     #[stable(feature = "rust1", since = "1.0.0")]
798     #[inline]
799     pub fn exp_m1(self) -> f32 {
800         unsafe { cmath::expm1f(self) }
801     }
802
803     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
804     /// the operations were performed separately.
805     ///
806     /// # Examples
807     ///
808     /// ```
809     /// use std::f32;
810     ///
811     /// let x = f32::consts::E - 1.0;
812     ///
813     /// // ln(1 + (e - 1)) == ln(e) == 1
814     /// let abs_difference = (x.ln_1p() - 1.0).abs();
815     ///
816     /// assert!(abs_difference <= f32::EPSILON);
817     /// ```
818     #[stable(feature = "rust1", since = "1.0.0")]
819     #[inline]
820     pub fn ln_1p(self) -> f32 {
821         unsafe { cmath::log1pf(self) }
822     }
823
824     /// Hyperbolic sine function.
825     ///
826     /// # Examples
827     ///
828     /// ```
829     /// use std::f32;
830     ///
831     /// let e = f32::consts::E;
832     /// let x = 1.0f32;
833     ///
834     /// let f = x.sinh();
835     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
836     /// let g = ((e * e) - 1.0) / (2.0 * e);
837     /// let abs_difference = (f - g).abs();
838     ///
839     /// assert!(abs_difference <= f32::EPSILON);
840     /// ```
841     #[stable(feature = "rust1", since = "1.0.0")]
842     #[inline]
843     pub fn sinh(self) -> f32 {
844         unsafe { cmath::sinhf(self) }
845     }
846
847     /// Hyperbolic cosine function.
848     ///
849     /// # Examples
850     ///
851     /// ```
852     /// use std::f32;
853     ///
854     /// let e = f32::consts::E;
855     /// let x = 1.0f32;
856     /// let f = x.cosh();
857     /// // Solving cosh() at 1 gives this result
858     /// let g = ((e * e) + 1.0) / (2.0 * e);
859     /// let abs_difference = (f - g).abs();
860     ///
861     /// // Same result
862     /// assert!(abs_difference <= f32::EPSILON);
863     /// ```
864     #[stable(feature = "rust1", since = "1.0.0")]
865     #[inline]
866     pub fn cosh(self) -> f32 {
867         unsafe { cmath::coshf(self) }
868     }
869
870     /// Hyperbolic tangent function.
871     ///
872     /// # Examples
873     ///
874     /// ```
875     /// use std::f32;
876     ///
877     /// let e = f32::consts::E;
878     /// let x = 1.0f32;
879     ///
880     /// let f = x.tanh();
881     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
882     /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
883     /// let abs_difference = (f - g).abs();
884     ///
885     /// assert!(abs_difference <= f32::EPSILON);
886     /// ```
887     #[stable(feature = "rust1", since = "1.0.0")]
888     #[inline]
889     pub fn tanh(self) -> f32 {
890         unsafe { cmath::tanhf(self) }
891     }
892
893     /// Inverse hyperbolic sine function.
894     ///
895     /// # Examples
896     ///
897     /// ```
898     /// use std::f32;
899     ///
900     /// let x = 1.0f32;
901     /// let f = x.sinh().asinh();
902     ///
903     /// let abs_difference = (f - x).abs();
904     ///
905     /// assert!(abs_difference <= f32::EPSILON);
906     /// ```
907     #[stable(feature = "rust1", since = "1.0.0")]
908     #[inline]
909     pub fn asinh(self) -> f32 {
910         if self == NEG_INFINITY {
911             NEG_INFINITY
912         } else {
913             (self + ((self * self) + 1.0).sqrt()).ln()
914         }
915     }
916
917     /// Inverse hyperbolic cosine function.
918     ///
919     /// # Examples
920     ///
921     /// ```
922     /// use std::f32;
923     ///
924     /// let x = 1.0f32;
925     /// let f = x.cosh().acosh();
926     ///
927     /// let abs_difference = (f - x).abs();
928     ///
929     /// assert!(abs_difference <= f32::EPSILON);
930     /// ```
931     #[stable(feature = "rust1", since = "1.0.0")]
932     #[inline]
933     pub fn acosh(self) -> f32 {
934         match self {
935             x if x < 1.0 => crate::f32::NAN,
936             x => (x + ((x * x) - 1.0).sqrt()).ln(),
937         }
938     }
939
940     /// Inverse hyperbolic tangent function.
941     ///
942     /// # Examples
943     ///
944     /// ```
945     /// use std::f32;
946     ///
947     /// let e = f32::consts::E;
948     /// let f = e.tanh().atanh();
949     ///
950     /// let abs_difference = (f - e).abs();
951     ///
952     /// assert!(abs_difference <= 1e-5);
953     /// ```
954     #[stable(feature = "rust1", since = "1.0.0")]
955     #[inline]
956     pub fn atanh(self) -> f32 {
957         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
958     }
959
960     /// Restrict a value to a certain interval unless it is NaN.
961     ///
962     /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
963     /// less than `min`. Otherwise this returns `self`.
964     ///
965     /// Not that this function returns NaN if the initial value was NaN as
966     /// well.
967     ///
968     /// # Panics
969     ///
970     /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
971     ///
972     /// # Examples
973     ///
974     /// ```
975     /// #![feature(clamp)]
976     /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
977     /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
978     /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
979     /// assert!((std::f32::NAN).clamp(-2.0, 1.0).is_nan());
980     /// ```
981     #[unstable(feature = "clamp", issue = "44095")]
982     #[inline]
983     pub fn clamp(self, min: f32, max: f32) -> f32 {
984         assert!(min <= max);
985         let mut x = self;
986         if x < min { x = min; }
987         if x > max { x = max; }
988         x
989     }
990
991 }
992
993 #[cfg(test)]
994 mod tests {
995     use crate::f32;
996     use crate::f32::*;
997     use crate::num::*;
998     use crate::num::FpCategory as Fp;
999
1000     #[test]
1001     fn test_num_f32() {
1002         test_num(10f32, 2f32);
1003     }
1004
1005     #[test]
1006     fn test_min_nan() {
1007         assert_eq!(NAN.min(2.0), 2.0);
1008         assert_eq!(2.0f32.min(NAN), 2.0);
1009     }
1010
1011     #[test]
1012     fn test_max_nan() {
1013         assert_eq!(NAN.max(2.0), 2.0);
1014         assert_eq!(2.0f32.max(NAN), 2.0);
1015     }
1016
1017     #[test]
1018     fn test_nan() {
1019         let nan: f32 = f32::NAN;
1020         assert!(nan.is_nan());
1021         assert!(!nan.is_infinite());
1022         assert!(!nan.is_finite());
1023         assert!(!nan.is_normal());
1024         assert!(nan.is_sign_positive());
1025         assert!(!nan.is_sign_negative());
1026         assert_eq!(Fp::Nan, nan.classify());
1027     }
1028
1029     #[test]
1030     fn test_infinity() {
1031         let inf: f32 = f32::INFINITY;
1032         assert!(inf.is_infinite());
1033         assert!(!inf.is_finite());
1034         assert!(inf.is_sign_positive());
1035         assert!(!inf.is_sign_negative());
1036         assert!(!inf.is_nan());
1037         assert!(!inf.is_normal());
1038         assert_eq!(Fp::Infinite, inf.classify());
1039     }
1040
1041     #[test]
1042     fn test_neg_infinity() {
1043         let neg_inf: f32 = f32::NEG_INFINITY;
1044         assert!(neg_inf.is_infinite());
1045         assert!(!neg_inf.is_finite());
1046         assert!(!neg_inf.is_sign_positive());
1047         assert!(neg_inf.is_sign_negative());
1048         assert!(!neg_inf.is_nan());
1049         assert!(!neg_inf.is_normal());
1050         assert_eq!(Fp::Infinite, neg_inf.classify());
1051     }
1052
1053     #[test]
1054     fn test_zero() {
1055         let zero: f32 = 0.0f32;
1056         assert_eq!(0.0, zero);
1057         assert!(!zero.is_infinite());
1058         assert!(zero.is_finite());
1059         assert!(zero.is_sign_positive());
1060         assert!(!zero.is_sign_negative());
1061         assert!(!zero.is_nan());
1062         assert!(!zero.is_normal());
1063         assert_eq!(Fp::Zero, zero.classify());
1064     }
1065
1066     #[test]
1067     fn test_neg_zero() {
1068         let neg_zero: f32 = -0.0;
1069         assert_eq!(0.0, neg_zero);
1070         assert!(!neg_zero.is_infinite());
1071         assert!(neg_zero.is_finite());
1072         assert!(!neg_zero.is_sign_positive());
1073         assert!(neg_zero.is_sign_negative());
1074         assert!(!neg_zero.is_nan());
1075         assert!(!neg_zero.is_normal());
1076         assert_eq!(Fp::Zero, neg_zero.classify());
1077     }
1078
1079     #[test]
1080     fn test_one() {
1081         let one: f32 = 1.0f32;
1082         assert_eq!(1.0, one);
1083         assert!(!one.is_infinite());
1084         assert!(one.is_finite());
1085         assert!(one.is_sign_positive());
1086         assert!(!one.is_sign_negative());
1087         assert!(!one.is_nan());
1088         assert!(one.is_normal());
1089         assert_eq!(Fp::Normal, one.classify());
1090     }
1091
1092     #[test]
1093     fn test_is_nan() {
1094         let nan: f32 = f32::NAN;
1095         let inf: f32 = f32::INFINITY;
1096         let neg_inf: f32 = f32::NEG_INFINITY;
1097         assert!(nan.is_nan());
1098         assert!(!0.0f32.is_nan());
1099         assert!(!5.3f32.is_nan());
1100         assert!(!(-10.732f32).is_nan());
1101         assert!(!inf.is_nan());
1102         assert!(!neg_inf.is_nan());
1103     }
1104
1105     #[test]
1106     fn test_is_infinite() {
1107         let nan: f32 = f32::NAN;
1108         let inf: f32 = f32::INFINITY;
1109         let neg_inf: f32 = f32::NEG_INFINITY;
1110         assert!(!nan.is_infinite());
1111         assert!(inf.is_infinite());
1112         assert!(neg_inf.is_infinite());
1113         assert!(!0.0f32.is_infinite());
1114         assert!(!42.8f32.is_infinite());
1115         assert!(!(-109.2f32).is_infinite());
1116     }
1117
1118     #[test]
1119     fn test_is_finite() {
1120         let nan: f32 = f32::NAN;
1121         let inf: f32 = f32::INFINITY;
1122         let neg_inf: f32 = f32::NEG_INFINITY;
1123         assert!(!nan.is_finite());
1124         assert!(!inf.is_finite());
1125         assert!(!neg_inf.is_finite());
1126         assert!(0.0f32.is_finite());
1127         assert!(42.8f32.is_finite());
1128         assert!((-109.2f32).is_finite());
1129     }
1130
1131     #[test]
1132     fn test_is_normal() {
1133         let nan: f32 = f32::NAN;
1134         let inf: f32 = f32::INFINITY;
1135         let neg_inf: f32 = f32::NEG_INFINITY;
1136         let zero: f32 = 0.0f32;
1137         let neg_zero: f32 = -0.0;
1138         assert!(!nan.is_normal());
1139         assert!(!inf.is_normal());
1140         assert!(!neg_inf.is_normal());
1141         assert!(!zero.is_normal());
1142         assert!(!neg_zero.is_normal());
1143         assert!(1f32.is_normal());
1144         assert!(1e-37f32.is_normal());
1145         assert!(!1e-38f32.is_normal());
1146     }
1147
1148     #[test]
1149     fn test_classify() {
1150         let nan: f32 = f32::NAN;
1151         let inf: f32 = f32::INFINITY;
1152         let neg_inf: f32 = f32::NEG_INFINITY;
1153         let zero: f32 = 0.0f32;
1154         let neg_zero: f32 = -0.0;
1155         assert_eq!(nan.classify(), Fp::Nan);
1156         assert_eq!(inf.classify(), Fp::Infinite);
1157         assert_eq!(neg_inf.classify(), Fp::Infinite);
1158         assert_eq!(zero.classify(), Fp::Zero);
1159         assert_eq!(neg_zero.classify(), Fp::Zero);
1160         assert_eq!(1f32.classify(), Fp::Normal);
1161         assert_eq!(1e-37f32.classify(), Fp::Normal);
1162         assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1163     }
1164
1165     #[test]
1166     fn test_floor() {
1167         assert_approx_eq!(1.0f32.floor(), 1.0f32);
1168         assert_approx_eq!(1.3f32.floor(), 1.0f32);
1169         assert_approx_eq!(1.5f32.floor(), 1.0f32);
1170         assert_approx_eq!(1.7f32.floor(), 1.0f32);
1171         assert_approx_eq!(0.0f32.floor(), 0.0f32);
1172         assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1173         assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1174         assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1175         assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1176         assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1177     }
1178
1179     #[test]
1180     fn test_ceil() {
1181         assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1182         assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1183         assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1184         assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1185         assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1186         assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1187         assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1188         assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1189         assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1190         assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1191     }
1192
1193     #[test]
1194     fn test_round() {
1195         assert_approx_eq!(1.0f32.round(), 1.0f32);
1196         assert_approx_eq!(1.3f32.round(), 1.0f32);
1197         assert_approx_eq!(1.5f32.round(), 2.0f32);
1198         assert_approx_eq!(1.7f32.round(), 2.0f32);
1199         assert_approx_eq!(0.0f32.round(), 0.0f32);
1200         assert_approx_eq!((-0.0f32).round(), -0.0f32);
1201         assert_approx_eq!((-1.0f32).round(), -1.0f32);
1202         assert_approx_eq!((-1.3f32).round(), -1.0f32);
1203         assert_approx_eq!((-1.5f32).round(), -2.0f32);
1204         assert_approx_eq!((-1.7f32).round(), -2.0f32);
1205     }
1206
1207     #[test]
1208     fn test_trunc() {
1209         assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1210         assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1211         assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1212         assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1213         assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1214         assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1215         assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1216         assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1217         assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1218         assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1219     }
1220
1221     #[test]
1222     fn test_fract() {
1223         assert_approx_eq!(1.0f32.fract(), 0.0f32);
1224         assert_approx_eq!(1.3f32.fract(), 0.3f32);
1225         assert_approx_eq!(1.5f32.fract(), 0.5f32);
1226         assert_approx_eq!(1.7f32.fract(), 0.7f32);
1227         assert_approx_eq!(0.0f32.fract(), 0.0f32);
1228         assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1229         assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1230         assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1231         assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1232         assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1233     }
1234
1235     #[test]
1236     fn test_abs() {
1237         assert_eq!(INFINITY.abs(), INFINITY);
1238         assert_eq!(1f32.abs(), 1f32);
1239         assert_eq!(0f32.abs(), 0f32);
1240         assert_eq!((-0f32).abs(), 0f32);
1241         assert_eq!((-1f32).abs(), 1f32);
1242         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1243         assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1244         assert!(NAN.abs().is_nan());
1245     }
1246
1247     #[test]
1248     fn test_signum() {
1249         assert_eq!(INFINITY.signum(), 1f32);
1250         assert_eq!(1f32.signum(), 1f32);
1251         assert_eq!(0f32.signum(), 1f32);
1252         assert_eq!((-0f32).signum(), -1f32);
1253         assert_eq!((-1f32).signum(), -1f32);
1254         assert_eq!(NEG_INFINITY.signum(), -1f32);
1255         assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1256         assert!(NAN.signum().is_nan());
1257     }
1258
1259     #[test]
1260     fn test_is_sign_positive() {
1261         assert!(INFINITY.is_sign_positive());
1262         assert!(1f32.is_sign_positive());
1263         assert!(0f32.is_sign_positive());
1264         assert!(!(-0f32).is_sign_positive());
1265         assert!(!(-1f32).is_sign_positive());
1266         assert!(!NEG_INFINITY.is_sign_positive());
1267         assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1268         assert!(NAN.is_sign_positive());
1269         assert!(!(-NAN).is_sign_positive());
1270     }
1271
1272     #[test]
1273     fn test_is_sign_negative() {
1274         assert!(!INFINITY.is_sign_negative());
1275         assert!(!1f32.is_sign_negative());
1276         assert!(!0f32.is_sign_negative());
1277         assert!((-0f32).is_sign_negative());
1278         assert!((-1f32).is_sign_negative());
1279         assert!(NEG_INFINITY.is_sign_negative());
1280         assert!((1f32/NEG_INFINITY).is_sign_negative());
1281         assert!(!NAN.is_sign_negative());
1282         assert!((-NAN).is_sign_negative());
1283     }
1284
1285     #[test]
1286     fn test_mul_add() {
1287         let nan: f32 = f32::NAN;
1288         let inf: f32 = f32::INFINITY;
1289         let neg_inf: f32 = f32::NEG_INFINITY;
1290         assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1291         assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1292         assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1293         assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1294         assert!(nan.mul_add(7.8, 9.0).is_nan());
1295         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1296         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1297         assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1298         assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1299     }
1300
1301     #[test]
1302     fn test_recip() {
1303         let nan: f32 = f32::NAN;
1304         let inf: f32 = f32::INFINITY;
1305         let neg_inf: f32 = f32::NEG_INFINITY;
1306         assert_eq!(1.0f32.recip(), 1.0);
1307         assert_eq!(2.0f32.recip(), 0.5);
1308         assert_eq!((-0.4f32).recip(), -2.5);
1309         assert_eq!(0.0f32.recip(), inf);
1310         assert!(nan.recip().is_nan());
1311         assert_eq!(inf.recip(), 0.0);
1312         assert_eq!(neg_inf.recip(), 0.0);
1313     }
1314
1315     #[test]
1316     fn test_powi() {
1317         let nan: f32 = f32::NAN;
1318         let inf: f32 = f32::INFINITY;
1319         let neg_inf: f32 = f32::NEG_INFINITY;
1320         assert_eq!(1.0f32.powi(1), 1.0);
1321         assert_approx_eq!((-3.1f32).powi(2), 9.61);
1322         assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1323         assert_eq!(8.3f32.powi(0), 1.0);
1324         assert!(nan.powi(2).is_nan());
1325         assert_eq!(inf.powi(3), inf);
1326         assert_eq!(neg_inf.powi(2), inf);
1327     }
1328
1329     #[test]
1330     fn test_powf() {
1331         let nan: f32 = f32::NAN;
1332         let inf: f32 = f32::INFINITY;
1333         let neg_inf: f32 = f32::NEG_INFINITY;
1334         assert_eq!(1.0f32.powf(1.0), 1.0);
1335         assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1336         assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1337         assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1338         assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1339         assert_eq!(8.3f32.powf(0.0), 1.0);
1340         assert!(nan.powf(2.0).is_nan());
1341         assert_eq!(inf.powf(2.0), inf);
1342         assert_eq!(neg_inf.powf(3.0), neg_inf);
1343     }
1344
1345     #[test]
1346     fn test_sqrt_domain() {
1347         assert!(NAN.sqrt().is_nan());
1348         assert!(NEG_INFINITY.sqrt().is_nan());
1349         assert!((-1.0f32).sqrt().is_nan());
1350         assert_eq!((-0.0f32).sqrt(), -0.0);
1351         assert_eq!(0.0f32.sqrt(), 0.0);
1352         assert_eq!(1.0f32.sqrt(), 1.0);
1353         assert_eq!(INFINITY.sqrt(), INFINITY);
1354     }
1355
1356     #[test]
1357     fn test_exp() {
1358         assert_eq!(1.0, 0.0f32.exp());
1359         assert_approx_eq!(2.718282, 1.0f32.exp());
1360         assert_approx_eq!(148.413162, 5.0f32.exp());
1361
1362         let inf: f32 = f32::INFINITY;
1363         let neg_inf: f32 = f32::NEG_INFINITY;
1364         let nan: f32 = f32::NAN;
1365         assert_eq!(inf, inf.exp());
1366         assert_eq!(0.0, neg_inf.exp());
1367         assert!(nan.exp().is_nan());
1368     }
1369
1370     #[test]
1371     fn test_exp2() {
1372         assert_eq!(32.0, 5.0f32.exp2());
1373         assert_eq!(1.0, 0.0f32.exp2());
1374
1375         let inf: f32 = f32::INFINITY;
1376         let neg_inf: f32 = f32::NEG_INFINITY;
1377         let nan: f32 = f32::NAN;
1378         assert_eq!(inf, inf.exp2());
1379         assert_eq!(0.0, neg_inf.exp2());
1380         assert!(nan.exp2().is_nan());
1381     }
1382
1383     #[test]
1384     fn test_ln() {
1385         let nan: f32 = f32::NAN;
1386         let inf: f32 = f32::INFINITY;
1387         let neg_inf: f32 = f32::NEG_INFINITY;
1388         assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1389         assert!(nan.ln().is_nan());
1390         assert_eq!(inf.ln(), inf);
1391         assert!(neg_inf.ln().is_nan());
1392         assert!((-2.3f32).ln().is_nan());
1393         assert_eq!((-0.0f32).ln(), neg_inf);
1394         assert_eq!(0.0f32.ln(), neg_inf);
1395         assert_approx_eq!(4.0f32.ln(), 1.386294);
1396     }
1397
1398     #[test]
1399     fn test_log() {
1400         let nan: f32 = f32::NAN;
1401         let inf: f32 = f32::INFINITY;
1402         let neg_inf: f32 = f32::NEG_INFINITY;
1403         assert_eq!(10.0f32.log(10.0), 1.0);
1404         assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1405         assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1406         assert!(1.0f32.log(1.0).is_nan());
1407         assert!(1.0f32.log(-13.9).is_nan());
1408         assert!(nan.log(2.3).is_nan());
1409         assert_eq!(inf.log(10.0), inf);
1410         assert!(neg_inf.log(8.8).is_nan());
1411         assert!((-2.3f32).log(0.1).is_nan());
1412         assert_eq!((-0.0f32).log(2.0), neg_inf);
1413         assert_eq!(0.0f32.log(7.0), neg_inf);
1414     }
1415
1416     #[test]
1417     fn test_log2() {
1418         let nan: f32 = f32::NAN;
1419         let inf: f32 = f32::INFINITY;
1420         let neg_inf: f32 = f32::NEG_INFINITY;
1421         assert_approx_eq!(10.0f32.log2(), 3.321928);
1422         assert_approx_eq!(2.3f32.log2(), 1.201634);
1423         assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1424         assert!(nan.log2().is_nan());
1425         assert_eq!(inf.log2(), inf);
1426         assert!(neg_inf.log2().is_nan());
1427         assert!((-2.3f32).log2().is_nan());
1428         assert_eq!((-0.0f32).log2(), neg_inf);
1429         assert_eq!(0.0f32.log2(), neg_inf);
1430     }
1431
1432     #[test]
1433     fn test_log10() {
1434         let nan: f32 = f32::NAN;
1435         let inf: f32 = f32::INFINITY;
1436         let neg_inf: f32 = f32::NEG_INFINITY;
1437         assert_eq!(10.0f32.log10(), 1.0);
1438         assert_approx_eq!(2.3f32.log10(), 0.361728);
1439         assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1440         assert_eq!(1.0f32.log10(), 0.0);
1441         assert!(nan.log10().is_nan());
1442         assert_eq!(inf.log10(), inf);
1443         assert!(neg_inf.log10().is_nan());
1444         assert!((-2.3f32).log10().is_nan());
1445         assert_eq!((-0.0f32).log10(), neg_inf);
1446         assert_eq!(0.0f32.log10(), neg_inf);
1447     }
1448
1449     #[test]
1450     fn test_to_degrees() {
1451         let pi: f32 = consts::PI;
1452         let nan: f32 = f32::NAN;
1453         let inf: f32 = f32::INFINITY;
1454         let neg_inf: f32 = f32::NEG_INFINITY;
1455         assert_eq!(0.0f32.to_degrees(), 0.0);
1456         assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1457         assert_eq!(pi.to_degrees(), 180.0);
1458         assert!(nan.to_degrees().is_nan());
1459         assert_eq!(inf.to_degrees(), inf);
1460         assert_eq!(neg_inf.to_degrees(), neg_inf);
1461         assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1462     }
1463
1464     #[test]
1465     fn test_to_radians() {
1466         let pi: f32 = consts::PI;
1467         let nan: f32 = f32::NAN;
1468         let inf: f32 = f32::INFINITY;
1469         let neg_inf: f32 = f32::NEG_INFINITY;
1470         assert_eq!(0.0f32.to_radians(), 0.0);
1471         assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1472         assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1473         assert_eq!(180.0f32.to_radians(), pi);
1474         assert!(nan.to_radians().is_nan());
1475         assert_eq!(inf.to_radians(), inf);
1476         assert_eq!(neg_inf.to_radians(), neg_inf);
1477     }
1478
1479     #[test]
1480     fn test_asinh() {
1481         assert_eq!(0.0f32.asinh(), 0.0f32);
1482         assert_eq!((-0.0f32).asinh(), -0.0f32);
1483
1484         let inf: f32 = f32::INFINITY;
1485         let neg_inf: f32 = f32::NEG_INFINITY;
1486         let nan: f32 = f32::NAN;
1487         assert_eq!(inf.asinh(), inf);
1488         assert_eq!(neg_inf.asinh(), neg_inf);
1489         assert!(nan.asinh().is_nan());
1490         assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1491         assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1492     }
1493
1494     #[test]
1495     fn test_acosh() {
1496         assert_eq!(1.0f32.acosh(), 0.0f32);
1497         assert!(0.999f32.acosh().is_nan());
1498
1499         let inf: f32 = f32::INFINITY;
1500         let neg_inf: f32 = f32::NEG_INFINITY;
1501         let nan: f32 = f32::NAN;
1502         assert_eq!(inf.acosh(), inf);
1503         assert!(neg_inf.acosh().is_nan());
1504         assert!(nan.acosh().is_nan());
1505         assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1506         assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1507     }
1508
1509     #[test]
1510     fn test_atanh() {
1511         assert_eq!(0.0f32.atanh(), 0.0f32);
1512         assert_eq!((-0.0f32).atanh(), -0.0f32);
1513
1514         let inf32: f32 = f32::INFINITY;
1515         let neg_inf32: f32 = f32::NEG_INFINITY;
1516         assert_eq!(1.0f32.atanh(), inf32);
1517         assert_eq!((-1.0f32).atanh(), neg_inf32);
1518
1519         assert!(2f64.atanh().atanh().is_nan());
1520         assert!((-2f64).atanh().atanh().is_nan());
1521
1522         let inf64: f32 = f32::INFINITY;
1523         let neg_inf64: f32 = f32::NEG_INFINITY;
1524         let nan32: f32 = f32::NAN;
1525         assert!(inf64.atanh().is_nan());
1526         assert!(neg_inf64.atanh().is_nan());
1527         assert!(nan32.atanh().is_nan());
1528
1529         assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1530         assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1531     }
1532
1533     #[test]
1534     fn test_real_consts() {
1535         use super::consts;
1536
1537         let pi: f32 = consts::PI;
1538         let frac_pi_2: f32 = consts::FRAC_PI_2;
1539         let frac_pi_3: f32 = consts::FRAC_PI_3;
1540         let frac_pi_4: f32 = consts::FRAC_PI_4;
1541         let frac_pi_6: f32 = consts::FRAC_PI_6;
1542         let frac_pi_8: f32 = consts::FRAC_PI_8;
1543         let frac_1_pi: f32 = consts::FRAC_1_PI;
1544         let frac_2_pi: f32 = consts::FRAC_2_PI;
1545         let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1546         let sqrt2: f32 = consts::SQRT_2;
1547         let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1548         let e: f32 = consts::E;
1549         let log2_e: f32 = consts::LOG2_E;
1550         let log10_e: f32 = consts::LOG10_E;
1551         let ln_2: f32 = consts::LN_2;
1552         let ln_10: f32 = consts::LN_10;
1553
1554         assert_approx_eq!(frac_pi_2, pi / 2f32);
1555         assert_approx_eq!(frac_pi_3, pi / 3f32);
1556         assert_approx_eq!(frac_pi_4, pi / 4f32);
1557         assert_approx_eq!(frac_pi_6, pi / 6f32);
1558         assert_approx_eq!(frac_pi_8, pi / 8f32);
1559         assert_approx_eq!(frac_1_pi, 1f32 / pi);
1560         assert_approx_eq!(frac_2_pi, 2f32 / pi);
1561         assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1562         assert_approx_eq!(sqrt2, 2f32.sqrt());
1563         assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1564         assert_approx_eq!(log2_e, e.log2());
1565         assert_approx_eq!(log10_e, e.log10());
1566         assert_approx_eq!(ln_2, 2f32.ln());
1567         assert_approx_eq!(ln_10, 10f32.ln());
1568     }
1569
1570     #[test]
1571     fn test_float_bits_conv() {
1572         assert_eq!((1f32).to_bits(), 0x3f800000);
1573         assert_eq!((12.5f32).to_bits(), 0x41480000);
1574         assert_eq!((1337f32).to_bits(), 0x44a72000);
1575         assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1576         assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1577         assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1578         assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1579         assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1580
1581         // Check that NaNs roundtrip their bits regardless of signalingness
1582         // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1583         let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1584         let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1585         assert!(f32::from_bits(masked_nan1).is_nan());
1586         assert!(f32::from_bits(masked_nan2).is_nan());
1587
1588         assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1589         assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1590     }
1591
1592     #[test]
1593     #[should_panic]
1594     fn test_clamp_min_greater_than_max() {
1595         1.0f32.clamp(3.0, 1.0);
1596     }
1597
1598     #[test]
1599     #[should_panic]
1600     fn test_clamp_min_is_nan() {
1601         1.0f32.clamp(NAN, 1.0);
1602     }
1603
1604     #[test]
1605     #[should_panic]
1606     fn test_clamp_max_is_nan() {
1607         1.0f32.clamp(3.0, NAN);
1608     }
1609 }