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