]> git.lizzy.rs Git - rust.git/blob - library/std/src/f32.rs
Merge commit 'f2cdd4a78d89c009342197cf5844a21f8aa813df' into sync_cg_clif-2022-04-22
[rust.git] / library / std / src / f32.rs
1 //! Constants specific to the `f32` single-precision floating point type.
2 //!
3 //! *[See also the `f32` primitive type](primitive@f32).*
4 //!
5 //! Mathematically significant numbers are provided in the `consts` sub-module.
6 //!
7 //! For the constants defined directly in this module
8 //! (as distinct from those defined in the `consts` sub-module),
9 //! new code should instead use the associated constants
10 //! defined directly on the `f32` type.
11
12 #![stable(feature = "rust1", since = "1.0.0")]
13 #![allow(missing_docs)]
14
15 #[cfg(test)]
16 mod tests;
17
18 #[cfg(not(test))]
19 use crate::intrinsics;
20 #[cfg(not(test))]
21 use crate::sys::cmath;
22
23 #[stable(feature = "rust1", since = "1.0.0")]
24 #[allow(deprecated, deprecated_in_future)]
25 pub use core::f32::{
26     consts, DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP,
27     MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX,
28 };
29
30 #[cfg(not(test))]
31 impl f32 {
32     /// Returns the largest integer less than or equal to a number.
33     ///
34     /// # Examples
35     ///
36     /// ```
37     /// let f = 3.7_f32;
38     /// let g = 3.0_f32;
39     /// let h = -3.7_f32;
40     ///
41     /// assert_eq!(f.floor(), 3.0);
42     /// assert_eq!(g.floor(), 3.0);
43     /// assert_eq!(h.floor(), -4.0);
44     /// ```
45     #[rustc_allow_incoherent_impl]
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     #[rustc_allow_incoherent_impl]
65     #[must_use = "method returns a new number and does not mutate the original value"]
66     #[stable(feature = "rust1", since = "1.0.0")]
67     #[inline]
68     pub fn ceil(self) -> f32 {
69         unsafe { intrinsics::ceilf32(self) }
70     }
71
72     /// Returns the nearest integer to a number. Round half-way cases away from
73     /// `0.0`.
74     ///
75     /// # Examples
76     ///
77     /// ```
78     /// let f = 3.3_f32;
79     /// let g = -3.3_f32;
80     ///
81     /// assert_eq!(f.round(), 3.0);
82     /// assert_eq!(g.round(), -3.0);
83     /// ```
84     #[rustc_allow_incoherent_impl]
85     #[must_use = "method returns a new number and does not mutate the original value"]
86     #[stable(feature = "rust1", since = "1.0.0")]
87     #[inline]
88     pub fn round(self) -> f32 {
89         unsafe { intrinsics::roundf32(self) }
90     }
91
92     /// Returns the integer part of a number.
93     ///
94     /// # Examples
95     ///
96     /// ```
97     /// let f = 3.7_f32;
98     /// let g = 3.0_f32;
99     /// let h = -3.7_f32;
100     ///
101     /// assert_eq!(f.trunc(), 3.0);
102     /// assert_eq!(g.trunc(), 3.0);
103     /// assert_eq!(h.trunc(), -3.0);
104     /// ```
105     #[rustc_allow_incoherent_impl]
106     #[must_use = "method returns a new number and does not mutate the original value"]
107     #[stable(feature = "rust1", since = "1.0.0")]
108     #[inline]
109     pub fn trunc(self) -> f32 {
110         unsafe { intrinsics::truncf32(self) }
111     }
112
113     /// Returns the fractional part of a number.
114     ///
115     /// # Examples
116     ///
117     /// ```
118     /// let x = 3.6_f32;
119     /// let y = -3.6_f32;
120     /// let abs_difference_x = (x.fract() - 0.6).abs();
121     /// let abs_difference_y = (y.fract() - (-0.6)).abs();
122     ///
123     /// assert!(abs_difference_x <= f32::EPSILON);
124     /// assert!(abs_difference_y <= f32::EPSILON);
125     /// ```
126     #[rustc_allow_incoherent_impl]
127     #[must_use = "method returns a new number and does not mutate the original value"]
128     #[stable(feature = "rust1", since = "1.0.0")]
129     #[inline]
130     pub fn fract(self) -> f32 {
131         self - self.trunc()
132     }
133
134     /// Computes the absolute value of `self`. Returns `NAN` if the
135     /// number is `NAN`.
136     ///
137     /// # Examples
138     ///
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     #[rustc_allow_incoherent_impl]
152     #[must_use = "method returns a new number and does not mutate the original value"]
153     #[stable(feature = "rust1", since = "1.0.0")]
154     #[inline]
155     pub fn abs(self) -> f32 {
156         unsafe { intrinsics::fabsf32(self) }
157     }
158
159     /// Returns a number that represents the sign of `self`.
160     ///
161     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
162     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
163     /// - `NAN` if the number is `NAN`
164     ///
165     /// # Examples
166     ///
167     /// ```
168     /// let f = 3.5_f32;
169     ///
170     /// assert_eq!(f.signum(), 1.0);
171     /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
172     ///
173     /// assert!(f32::NAN.signum().is_nan());
174     /// ```
175     #[rustc_allow_incoherent_impl]
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() { Self::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     /// let f = 3.5_f32;
194     ///
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     /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
199     ///
200     /// assert!(f32::NAN.copysign(1.0).is_nan());
201     /// ```
202     #[rustc_allow_incoherent_impl]
203     #[must_use = "method returns a new number and does not mutate the original value"]
204     #[inline]
205     #[stable(feature = "copysign", since = "1.35.0")]
206     pub fn copysign(self, sign: f32) -> f32 {
207         unsafe { intrinsics::copysignf32(self, sign) }
208     }
209
210     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
211     /// error, yielding a more accurate result than an unfused multiply-add.
212     ///
213     /// Using `mul_add` *may* be more performant than an unfused multiply-add if
214     /// the target architecture has a dedicated `fma` CPU instruction. However,
215     /// this is not always true, and will be heavily dependant on designing
216     /// algorithms with specific target hardware in mind.
217     ///
218     /// # Examples
219     ///
220     /// ```
221     /// let m = 10.0_f32;
222     /// let x = 4.0_f32;
223     /// let b = 60.0_f32;
224     ///
225     /// // 100.0
226     /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
227     ///
228     /// assert!(abs_difference <= f32::EPSILON);
229     /// ```
230     #[rustc_allow_incoherent_impl]
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     #[rustc_allow_incoherent_impl]
256     #[must_use = "method returns a new number and does not mutate the original value"]
257     #[inline]
258     #[stable(feature = "euclidean_division", since = "1.38.0")]
259     pub fn div_euclid(self, rhs: f32) -> f32 {
260         let q = (self / rhs).trunc();
261         if self % rhs < 0.0 {
262             return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
263         }
264         q
265     }
266
267     /// Calculates the least nonnegative remainder of `self (mod rhs)`.
268     ///
269     /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
270     /// most cases. However, due to a floating point round-off error it can
271     /// result in `r == rhs.abs()`, violating the mathematical definition, if
272     /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
273     /// This result is not an element of the function's codomain, but it is the
274     /// closest floating point number in the real numbers and thus fulfills the
275     /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
276     /// approximatively.
277     ///
278     /// # Examples
279     ///
280     /// ```
281     /// let a: f32 = 7.0;
282     /// let b = 4.0;
283     /// assert_eq!(a.rem_euclid(b), 3.0);
284     /// assert_eq!((-a).rem_euclid(b), 1.0);
285     /// assert_eq!(a.rem_euclid(-b), 3.0);
286     /// assert_eq!((-a).rem_euclid(-b), 1.0);
287     /// // limitation due to round-off error
288     /// assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
289     /// ```
290     #[rustc_allow_incoherent_impl]
291     #[must_use = "method returns a new number and does not mutate the original value"]
292     #[inline]
293     #[stable(feature = "euclidean_division", since = "1.38.0")]
294     pub fn rem_euclid(self, rhs: f32) -> f32 {
295         let r = self % rhs;
296         if r < 0.0 { r + rhs.abs() } else { r }
297     }
298
299     /// Raises a number to an integer power.
300     ///
301     /// Using this function is generally faster than using `powf`
302     ///
303     /// # Examples
304     ///
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     #[rustc_allow_incoherent_impl]
312     #[must_use = "method returns a new number and does not mutate the original value"]
313     #[stable(feature = "rust1", since = "1.0.0")]
314     #[inline]
315     pub fn powi(self, n: i32) -> f32 {
316         unsafe { intrinsics::powif32(self, n) }
317     }
318
319     /// Raises a number to a floating point power.
320     ///
321     /// # Examples
322     ///
323     /// ```
324     /// let x = 2.0_f32;
325     /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
326     ///
327     /// assert!(abs_difference <= f32::EPSILON);
328     /// ```
329     #[rustc_allow_incoherent_impl]
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 other than `-0.0`.
340     ///
341     /// # Examples
342     ///
343     /// ```
344     /// let positive = 4.0_f32;
345     /// let negative = -4.0_f32;
346     /// let negative_zero = -0.0_f32;
347     ///
348     /// let abs_difference = (positive.sqrt() - 2.0).abs();
349     ///
350     /// assert!(abs_difference <= f32::EPSILON);
351     /// assert!(negative.sqrt().is_nan());
352     /// assert!(negative_zero.sqrt() == negative_zero);
353     /// ```
354     #[rustc_allow_incoherent_impl]
355     #[must_use = "method returns a new number and does not mutate the original value"]
356     #[stable(feature = "rust1", since = "1.0.0")]
357     #[inline]
358     pub fn sqrt(self) -> f32 {
359         unsafe { intrinsics::sqrtf32(self) }
360     }
361
362     /// Returns `e^(self)`, (the exponential function).
363     ///
364     /// # Examples
365     ///
366     /// ```
367     /// let one = 1.0f32;
368     /// // e^1
369     /// let e = one.exp();
370     ///
371     /// // ln(e) - 1 == 0
372     /// let abs_difference = (e.ln() - 1.0).abs();
373     ///
374     /// assert!(abs_difference <= f32::EPSILON);
375     /// ```
376     #[rustc_allow_incoherent_impl]
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     /// let f = 2.0f32;
390     ///
391     /// // 2^2 - 4 == 0
392     /// let abs_difference = (f.exp2() - 4.0).abs();
393     ///
394     /// assert!(abs_difference <= f32::EPSILON);
395     /// ```
396     #[rustc_allow_incoherent_impl]
397     #[must_use = "method returns a new number and does not mutate the original value"]
398     #[stable(feature = "rust1", since = "1.0.0")]
399     #[inline]
400     pub fn exp2(self) -> f32 {
401         unsafe { intrinsics::exp2f32(self) }
402     }
403
404     /// Returns the natural logarithm of the number.
405     ///
406     /// # Examples
407     ///
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     #[rustc_allow_incoherent_impl]
419     #[must_use = "method returns a new number and does not mutate the original value"]
420     #[stable(feature = "rust1", since = "1.0.0")]
421     #[inline]
422     pub fn ln(self) -> f32 {
423         unsafe { intrinsics::logf32(self) }
424     }
425
426     /// Returns the logarithm of the number with respect to an arbitrary base.
427     ///
428     /// The result might not be correctly rounded owing to implementation details;
429     /// `self.log2()` can produce more accurate results for base 2, and
430     /// `self.log10()` can produce more accurate results for base 10.
431     ///
432     /// # Examples
433     ///
434     /// ```
435     /// let five = 5.0f32;
436     ///
437     /// // log5(5) - 1 == 0
438     /// let abs_difference = (five.log(5.0) - 1.0).abs();
439     ///
440     /// assert!(abs_difference <= f32::EPSILON);
441     /// ```
442     #[rustc_allow_incoherent_impl]
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     /// let two = 2.0f32;
456     ///
457     /// // log2(2) - 1 == 0
458     /// let abs_difference = (two.log2() - 1.0).abs();
459     ///
460     /// assert!(abs_difference <= f32::EPSILON);
461     /// ```
462     #[rustc_allow_incoherent_impl]
463     #[must_use = "method returns a new number and does not mutate the original value"]
464     #[stable(feature = "rust1", since = "1.0.0")]
465     #[inline]
466     pub fn log2(self) -> f32 {
467         #[cfg(target_os = "android")]
468         return crate::sys::android::log2f32(self);
469         #[cfg(not(target_os = "android"))]
470         return unsafe { intrinsics::log2f32(self) };
471     }
472
473     /// Returns the base 10 logarithm of the number.
474     ///
475     /// # Examples
476     ///
477     /// ```
478     /// let ten = 10.0f32;
479     ///
480     /// // log10(10) - 1 == 0
481     /// let abs_difference = (ten.log10() - 1.0).abs();
482     ///
483     /// assert!(abs_difference <= f32::EPSILON);
484     /// ```
485     #[rustc_allow_incoherent_impl]
486     #[must_use = "method returns a new number and does not mutate the original value"]
487     #[stable(feature = "rust1", since = "1.0.0")]
488     #[inline]
489     pub fn log10(self) -> f32 {
490         unsafe { intrinsics::log10f32(self) }
491     }
492
493     /// The positive difference of two numbers.
494     ///
495     /// * If `self <= other`: `0:0`
496     /// * Else: `self - other`
497     ///
498     /// # Examples
499     ///
500     /// ```
501     /// let x = 3.0f32;
502     /// let y = -3.0f32;
503     ///
504     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
505     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
506     ///
507     /// assert!(abs_difference_x <= f32::EPSILON);
508     /// assert!(abs_difference_y <= f32::EPSILON);
509     /// ```
510     #[rustc_allow_incoherent_impl]
511     #[must_use = "method returns a new number and does not mutate the original value"]
512     #[stable(feature = "rust1", since = "1.0.0")]
513     #[inline]
514     #[rustc_deprecated(
515         since = "1.10.0",
516         reason = "you probably meant `(self - other).abs()`: \
517                   this operation is `(self - other).max(0.0)` \
518                   except that `abs_sub` also propagates NaNs (also \
519                   known as `fdimf` in C). If you truly need the positive \
520                   difference, consider using that expression or the C function \
521                   `fdimf`, depending on how you wish to handle NaN (please consider \
522                   filing an issue describing your use-case too)."
523     )]
524     pub fn abs_sub(self, other: f32) -> f32 {
525         unsafe { cmath::fdimf(self, other) }
526     }
527
528     /// Returns the cube root of a number.
529     ///
530     /// # Examples
531     ///
532     /// ```
533     /// let x = 8.0f32;
534     ///
535     /// // x^(1/3) - 2 == 0
536     /// let abs_difference = (x.cbrt() - 2.0).abs();
537     ///
538     /// assert!(abs_difference <= f32::EPSILON);
539     /// ```
540     #[rustc_allow_incoherent_impl]
541     #[must_use = "method returns a new number and does not mutate the original value"]
542     #[stable(feature = "rust1", since = "1.0.0")]
543     #[inline]
544     pub fn cbrt(self) -> f32 {
545         unsafe { cmath::cbrtf(self) }
546     }
547
548     /// Calculates the length of the hypotenuse of a right-angle triangle given
549     /// legs of length `x` and `y`.
550     ///
551     /// # Examples
552     ///
553     /// ```
554     /// let x = 2.0f32;
555     /// let y = 3.0f32;
556     ///
557     /// // sqrt(x^2 + y^2)
558     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
559     ///
560     /// assert!(abs_difference <= f32::EPSILON);
561     /// ```
562     #[rustc_allow_incoherent_impl]
563     #[must_use = "method returns a new number and does not mutate the original value"]
564     #[stable(feature = "rust1", since = "1.0.0")]
565     #[inline]
566     pub fn hypot(self, other: f32) -> f32 {
567         unsafe { cmath::hypotf(self, other) }
568     }
569
570     /// Computes the sine of a number (in radians).
571     ///
572     /// # Examples
573     ///
574     /// ```
575     /// let x = std::f32::consts::FRAC_PI_2;
576     ///
577     /// let abs_difference = (x.sin() - 1.0).abs();
578     ///
579     /// assert!(abs_difference <= f32::EPSILON);
580     /// ```
581     #[rustc_allow_incoherent_impl]
582     #[must_use = "method returns a new number and does not mutate the original value"]
583     #[stable(feature = "rust1", since = "1.0.0")]
584     #[inline]
585     pub fn sin(self) -> f32 {
586         unsafe { intrinsics::sinf32(self) }
587     }
588
589     /// Computes the cosine of a number (in radians).
590     ///
591     /// # Examples
592     ///
593     /// ```
594     /// let x = 2.0 * std::f32::consts::PI;
595     ///
596     /// let abs_difference = (x.cos() - 1.0).abs();
597     ///
598     /// assert!(abs_difference <= f32::EPSILON);
599     /// ```
600     #[rustc_allow_incoherent_impl]
601     #[must_use = "method returns a new number and does not mutate the original value"]
602     #[stable(feature = "rust1", since = "1.0.0")]
603     #[inline]
604     pub fn cos(self) -> f32 {
605         unsafe { intrinsics::cosf32(self) }
606     }
607
608     /// Computes the tangent of a number (in radians).
609     ///
610     /// # Examples
611     ///
612     /// ```
613     /// let x = std::f32::consts::FRAC_PI_4;
614     /// let abs_difference = (x.tan() - 1.0).abs();
615     ///
616     /// assert!(abs_difference <= f32::EPSILON);
617     /// ```
618     #[rustc_allow_incoherent_impl]
619     #[must_use = "method returns a new number and does not mutate the original value"]
620     #[stable(feature = "rust1", since = "1.0.0")]
621     #[inline]
622     pub fn tan(self) -> f32 {
623         unsafe { cmath::tanf(self) }
624     }
625
626     /// Computes the arcsine of a number. Return value is in radians in
627     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
628     /// [-1, 1].
629     ///
630     /// # Examples
631     ///
632     /// ```
633     /// let f = std::f32::consts::FRAC_PI_2;
634     ///
635     /// // asin(sin(pi/2))
636     /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
637     ///
638     /// assert!(abs_difference <= f32::EPSILON);
639     /// ```
640     #[rustc_allow_incoherent_impl]
641     #[must_use = "method returns a new number and does not mutate the original value"]
642     #[stable(feature = "rust1", since = "1.0.0")]
643     #[inline]
644     pub fn asin(self) -> f32 {
645         unsafe { cmath::asinf(self) }
646     }
647
648     /// Computes the arccosine of a number. Return value is in radians in
649     /// the range [0, pi] or NaN if the number is outside the range
650     /// [-1, 1].
651     ///
652     /// # Examples
653     ///
654     /// ```
655     /// let f = std::f32::consts::FRAC_PI_4;
656     ///
657     /// // acos(cos(pi/4))
658     /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
659     ///
660     /// assert!(abs_difference <= f32::EPSILON);
661     /// ```
662     #[rustc_allow_incoherent_impl]
663     #[must_use = "method returns a new number and does not mutate the original value"]
664     #[stable(feature = "rust1", since = "1.0.0")]
665     #[inline]
666     pub fn acos(self) -> f32 {
667         unsafe { cmath::acosf(self) }
668     }
669
670     /// Computes the arctangent of a number. Return value is in radians in the
671     /// range [-pi/2, pi/2];
672     ///
673     /// # Examples
674     ///
675     /// ```
676     /// let f = 1.0f32;
677     ///
678     /// // atan(tan(1))
679     /// let abs_difference = (f.tan().atan() - 1.0).abs();
680     ///
681     /// assert!(abs_difference <= f32::EPSILON);
682     /// ```
683     #[rustc_allow_incoherent_impl]
684     #[must_use = "method returns a new number and does not mutate the original value"]
685     #[stable(feature = "rust1", since = "1.0.0")]
686     #[inline]
687     pub fn atan(self) -> f32 {
688         unsafe { cmath::atanf(self) }
689     }
690
691     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
692     ///
693     /// * `x = 0`, `y = 0`: `0`
694     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
695     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
696     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
697     ///
698     /// # Examples
699     ///
700     /// ```
701     /// // Positive angles measured counter-clockwise
702     /// // from positive x axis
703     /// // -pi/4 radians (45 deg clockwise)
704     /// let x1 = 3.0f32;
705     /// let y1 = -3.0f32;
706     ///
707     /// // 3pi/4 radians (135 deg counter-clockwise)
708     /// let x2 = -3.0f32;
709     /// let y2 = 3.0f32;
710     ///
711     /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
712     /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
713     ///
714     /// assert!(abs_difference_1 <= f32::EPSILON);
715     /// assert!(abs_difference_2 <= f32::EPSILON);
716     /// ```
717     #[rustc_allow_incoherent_impl]
718     #[must_use = "method returns a new number and does not mutate the original value"]
719     #[stable(feature = "rust1", since = "1.0.0")]
720     #[inline]
721     pub fn atan2(self, other: f32) -> f32 {
722         unsafe { cmath::atan2f(self, other) }
723     }
724
725     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
726     /// `(sin(x), cos(x))`.
727     ///
728     /// # Examples
729     ///
730     /// ```
731     /// let x = std::f32::consts::FRAC_PI_4;
732     /// let f = x.sin_cos();
733     ///
734     /// let abs_difference_0 = (f.0 - x.sin()).abs();
735     /// let abs_difference_1 = (f.1 - x.cos()).abs();
736     ///
737     /// assert!(abs_difference_0 <= f32::EPSILON);
738     /// assert!(abs_difference_1 <= f32::EPSILON);
739     /// ```
740     #[rustc_allow_incoherent_impl]
741     #[stable(feature = "rust1", since = "1.0.0")]
742     #[inline]
743     pub fn sin_cos(self) -> (f32, f32) {
744         (self.sin(), self.cos())
745     }
746
747     /// Returns `e^(self) - 1` in a way that is accurate even if the
748     /// number is close to zero.
749     ///
750     /// # Examples
751     ///
752     /// ```
753     /// let x = 1e-8_f32;
754     ///
755     /// // for very small x, e^x is approximately 1 + x + x^2 / 2
756     /// let approx = x + x * x / 2.0;
757     /// let abs_difference = (x.exp_m1() - approx).abs();
758     ///
759     /// assert!(abs_difference < 1e-10);
760     /// ```
761     #[rustc_allow_incoherent_impl]
762     #[must_use = "method returns a new number and does not mutate the original value"]
763     #[stable(feature = "rust1", since = "1.0.0")]
764     #[inline]
765     pub fn exp_m1(self) -> f32 {
766         unsafe { cmath::expm1f(self) }
767     }
768
769     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
770     /// the operations were performed separately.
771     ///
772     /// # Examples
773     ///
774     /// ```
775     /// let x = 1e-8_f32;
776     ///
777     /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
778     /// let approx = x - x * x / 2.0;
779     /// let abs_difference = (x.ln_1p() - approx).abs();
780     ///
781     /// assert!(abs_difference < 1e-10);
782     /// ```
783     #[rustc_allow_incoherent_impl]
784     #[must_use = "method returns a new number and does not mutate the original value"]
785     #[stable(feature = "rust1", since = "1.0.0")]
786     #[inline]
787     pub fn ln_1p(self) -> f32 {
788         unsafe { cmath::log1pf(self) }
789     }
790
791     /// Hyperbolic sine function.
792     ///
793     /// # Examples
794     ///
795     /// ```
796     /// let e = std::f32::consts::E;
797     /// let x = 1.0f32;
798     ///
799     /// let f = x.sinh();
800     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
801     /// let g = ((e * e) - 1.0) / (2.0 * e);
802     /// let abs_difference = (f - g).abs();
803     ///
804     /// assert!(abs_difference <= f32::EPSILON);
805     /// ```
806     #[rustc_allow_incoherent_impl]
807     #[must_use = "method returns a new number and does not mutate the original value"]
808     #[stable(feature = "rust1", since = "1.0.0")]
809     #[inline]
810     pub fn sinh(self) -> f32 {
811         unsafe { cmath::sinhf(self) }
812     }
813
814     /// Hyperbolic cosine function.
815     ///
816     /// # Examples
817     ///
818     /// ```
819     /// let e = std::f32::consts::E;
820     /// let x = 1.0f32;
821     /// let f = x.cosh();
822     /// // Solving cosh() at 1 gives this result
823     /// let g = ((e * e) + 1.0) / (2.0 * e);
824     /// let abs_difference = (f - g).abs();
825     ///
826     /// // Same result
827     /// assert!(abs_difference <= f32::EPSILON);
828     /// ```
829     #[rustc_allow_incoherent_impl]
830     #[must_use = "method returns a new number and does not mutate the original value"]
831     #[stable(feature = "rust1", since = "1.0.0")]
832     #[inline]
833     pub fn cosh(self) -> f32 {
834         unsafe { cmath::coshf(self) }
835     }
836
837     /// Hyperbolic tangent function.
838     ///
839     /// # Examples
840     ///
841     /// ```
842     /// let e = std::f32::consts::E;
843     /// let x = 1.0f32;
844     ///
845     /// let f = x.tanh();
846     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
847     /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
848     /// let abs_difference = (f - g).abs();
849     ///
850     /// assert!(abs_difference <= f32::EPSILON);
851     /// ```
852     #[rustc_allow_incoherent_impl]
853     #[must_use = "method returns a new number and does not mutate the original value"]
854     #[stable(feature = "rust1", since = "1.0.0")]
855     #[inline]
856     pub fn tanh(self) -> f32 {
857         unsafe { cmath::tanhf(self) }
858     }
859
860     /// Inverse hyperbolic sine function.
861     ///
862     /// # Examples
863     ///
864     /// ```
865     /// let x = 1.0f32;
866     /// let f = x.sinh().asinh();
867     ///
868     /// let abs_difference = (f - x).abs();
869     ///
870     /// assert!(abs_difference <= f32::EPSILON);
871     /// ```
872     #[rustc_allow_incoherent_impl]
873     #[must_use = "method returns a new number and does not mutate the original value"]
874     #[stable(feature = "rust1", since = "1.0.0")]
875     #[inline]
876     pub fn asinh(self) -> f32 {
877         (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
878     }
879
880     /// Inverse hyperbolic cosine function.
881     ///
882     /// # Examples
883     ///
884     /// ```
885     /// let x = 1.0f32;
886     /// let f = x.cosh().acosh();
887     ///
888     /// let abs_difference = (f - x).abs();
889     ///
890     /// assert!(abs_difference <= f32::EPSILON);
891     /// ```
892     #[rustc_allow_incoherent_impl]
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 acosh(self) -> f32 {
897         if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
898     }
899
900     /// Inverse hyperbolic tangent function.
901     ///
902     /// # Examples
903     ///
904     /// ```
905     /// let e = std::f32::consts::E;
906     /// let f = e.tanh().atanh();
907     ///
908     /// let abs_difference = (f - e).abs();
909     ///
910     /// assert!(abs_difference <= 1e-5);
911     /// ```
912     #[rustc_allow_incoherent_impl]
913     #[must_use = "method returns a new number and does not mutate the original value"]
914     #[stable(feature = "rust1", since = "1.0.0")]
915     #[inline]
916     pub fn atanh(self) -> f32 {
917         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
918     }
919 }