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