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