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