]> git.lizzy.rs Git - rust.git/blob - library/std/src/f32.rs
Auto merge of #103647 - lqd:osx-x64-lto, r=Mark-Simulacrum
[rust.git] / library / std / src / f32.rs
1 //! Constants for 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 `self`.
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 `self`.
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 `self`. 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     /// let h = -3.7_f32;
81     ///
82     /// assert_eq!(f.round(), 3.0);
83     /// assert_eq!(g.round(), -3.0);
84     /// assert_eq!(h.round(), -4.0);
85     /// ```
86     #[rustc_allow_incoherent_impl]
87     #[must_use = "method returns a new number and does not mutate the original value"]
88     #[stable(feature = "rust1", since = "1.0.0")]
89     #[inline]
90     pub fn round(self) -> f32 {
91         unsafe { intrinsics::roundf32(self) }
92     }
93
94     /// Returns the integer part of `self`.
95     /// This means that non-integer numbers are always truncated towards zero.
96     ///
97     /// # Examples
98     ///
99     /// ```
100     /// let f = 3.7_f32;
101     /// let g = 3.0_f32;
102     /// let h = -3.7_f32;
103     ///
104     /// assert_eq!(f.trunc(), 3.0);
105     /// assert_eq!(g.trunc(), 3.0);
106     /// assert_eq!(h.trunc(), -3.0);
107     /// ```
108     #[rustc_allow_incoherent_impl]
109     #[must_use = "method returns a new number and does not mutate the original value"]
110     #[stable(feature = "rust1", since = "1.0.0")]
111     #[inline]
112     pub fn trunc(self) -> f32 {
113         unsafe { intrinsics::truncf32(self) }
114     }
115
116     /// Returns the fractional part of `self`.
117     ///
118     /// # Examples
119     ///
120     /// ```
121     /// let x = 3.6_f32;
122     /// let y = -3.6_f32;
123     /// let abs_difference_x = (x.fract() - 0.6).abs();
124     /// let abs_difference_y = (y.fract() - (-0.6)).abs();
125     ///
126     /// assert!(abs_difference_x <= f32::EPSILON);
127     /// assert!(abs_difference_y <= f32::EPSILON);
128     /// ```
129     #[rustc_allow_incoherent_impl]
130     #[must_use = "method returns a new number and does not mutate the original value"]
131     #[stable(feature = "rust1", since = "1.0.0")]
132     #[inline]
133     pub fn fract(self) -> f32 {
134         self - self.trunc()
135     }
136
137     /// Computes the absolute value of `self`.
138     ///
139     /// # Examples
140     ///
141     /// ```
142     /// let x = 3.5_f32;
143     /// let y = -3.5_f32;
144     ///
145     /// let abs_difference_x = (x.abs() - x).abs();
146     /// let abs_difference_y = (y.abs() - (-y)).abs();
147     ///
148     /// assert!(abs_difference_x <= f32::EPSILON);
149     /// assert!(abs_difference_y <= f32::EPSILON);
150     ///
151     /// assert!(f32::NAN.abs().is_nan());
152     /// ```
153     #[rustc_allow_incoherent_impl]
154     #[must_use = "method returns a new number and does not mutate the original value"]
155     #[stable(feature = "rust1", since = "1.0.0")]
156     #[inline]
157     pub fn abs(self) -> f32 {
158         unsafe { intrinsics::fabsf32(self) }
159     }
160
161     /// Returns a number that represents the sign of `self`.
162     ///
163     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
164     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
165     /// - NaN if the number is NaN
166     ///
167     /// # Examples
168     ///
169     /// ```
170     /// let f = 3.5_f32;
171     ///
172     /// assert_eq!(f.signum(), 1.0);
173     /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
174     ///
175     /// assert!(f32::NAN.signum().is_nan());
176     /// ```
177     #[rustc_allow_incoherent_impl]
178     #[must_use = "method returns a new number and does not mutate the original value"]
179     #[stable(feature = "rust1", since = "1.0.0")]
180     #[inline]
181     pub fn signum(self) -> f32 {
182         if self.is_nan() { Self::NAN } else { 1.0_f32.copysign(self) }
183     }
184
185     /// Returns a number composed of the magnitude of `self` and the sign of
186     /// `sign`.
187     ///
188     /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
189     /// equal to `-self`. If `self` is a NaN, then a NaN with the sign bit of
190     /// `sign` is returned. Note, however, that conserving the sign bit on NaN
191     /// across arithmetical operations is not generally guaranteed.
192     /// See [explanation of NaN as a special value](primitive@f32) for more info.
193     ///
194     /// # Examples
195     ///
196     /// ```
197     /// let f = 3.5_f32;
198     ///
199     /// assert_eq!(f.copysign(0.42), 3.5_f32);
200     /// assert_eq!(f.copysign(-0.42), -3.5_f32);
201     /// assert_eq!((-f).copysign(0.42), 3.5_f32);
202     /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
203     ///
204     /// assert!(f32::NAN.copysign(1.0).is_nan());
205     /// ```
206     #[rustc_allow_incoherent_impl]
207     #[must_use = "method returns a new number and does not mutate the original value"]
208     #[inline]
209     #[stable(feature = "copysign", since = "1.35.0")]
210     pub fn copysign(self, sign: f32) -> f32 {
211         unsafe { intrinsics::copysignf32(self, sign) }
212     }
213
214     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
215     /// error, yielding a more accurate result than an unfused multiply-add.
216     ///
217     /// Using `mul_add` *may* be more performant than an unfused multiply-add if
218     /// the target architecture has a dedicated `fma` CPU instruction. However,
219     /// this is not always true, and will be heavily dependant on designing
220     /// algorithms with specific target hardware in mind.
221     ///
222     /// # Examples
223     ///
224     /// ```
225     /// let m = 10.0_f32;
226     /// let x = 4.0_f32;
227     /// let b = 60.0_f32;
228     ///
229     /// // 100.0
230     /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
231     ///
232     /// assert!(abs_difference <= f32::EPSILON);
233     /// ```
234     #[rustc_allow_incoherent_impl]
235     #[must_use = "method returns a new number and does not mutate the original value"]
236     #[stable(feature = "rust1", since = "1.0.0")]
237     #[inline]
238     pub fn mul_add(self, a: f32, b: f32) -> f32 {
239         unsafe { intrinsics::fmaf32(self, a, b) }
240     }
241
242     /// Calculates Euclidean division, the matching method for `rem_euclid`.
243     ///
244     /// This computes the integer `n` such that
245     /// `self = n * rhs + self.rem_euclid(rhs)`.
246     /// In other words, the result is `self / rhs` rounded to the integer `n`
247     /// such that `self >= n * rhs`.
248     ///
249     /// # Examples
250     ///
251     /// ```
252     /// let a: f32 = 7.0;
253     /// let b = 4.0;
254     /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
255     /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
256     /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
257     /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
258     /// ```
259     #[rustc_allow_incoherent_impl]
260     #[must_use = "method returns a new number and does not mutate the original value"]
261     #[inline]
262     #[stable(feature = "euclidean_division", since = "1.38.0")]
263     pub fn div_euclid(self, rhs: f32) -> f32 {
264         let q = (self / rhs).trunc();
265         if self % rhs < 0.0 {
266             return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
267         }
268         q
269     }
270
271     /// Calculates the least nonnegative remainder of `self (mod rhs)`.
272     ///
273     /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
274     /// most cases. However, due to a floating point round-off error it can
275     /// result in `r == rhs.abs()`, violating the mathematical definition, if
276     /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
277     /// This result is not an element of the function's codomain, but it is the
278     /// closest floating point number in the real numbers and thus fulfills the
279     /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
280     /// approximately.
281     ///
282     /// # Examples
283     ///
284     /// ```
285     /// let a: f32 = 7.0;
286     /// let b = 4.0;
287     /// assert_eq!(a.rem_euclid(b), 3.0);
288     /// assert_eq!((-a).rem_euclid(b), 1.0);
289     /// assert_eq!(a.rem_euclid(-b), 3.0);
290     /// assert_eq!((-a).rem_euclid(-b), 1.0);
291     /// // limitation due to round-off error
292     /// assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
293     /// ```
294     #[rustc_allow_incoherent_impl]
295     #[must_use = "method returns a new number and does not mutate the original value"]
296     #[inline]
297     #[stable(feature = "euclidean_division", since = "1.38.0")]
298     pub fn rem_euclid(self, rhs: f32) -> f32 {
299         let r = self % rhs;
300         if r < 0.0 { r + rhs.abs() } else { r }
301     }
302
303     /// Raises a number to an integer power.
304     ///
305     /// Using this function is generally faster than using `powf`.
306     /// It might have a different sequence of rounding operations than `powf`,
307     /// so the results are not guaranteed to agree.
308     ///
309     /// # Examples
310     ///
311     /// ```
312     /// let x = 2.0_f32;
313     /// let abs_difference = (x.powi(2) - (x * x)).abs();
314     ///
315     /// assert!(abs_difference <= f32::EPSILON);
316     /// ```
317     #[rustc_allow_incoherent_impl]
318     #[must_use = "method returns a new number and does not mutate the original value"]
319     #[stable(feature = "rust1", since = "1.0.0")]
320     #[inline]
321     pub fn powi(self, n: i32) -> f32 {
322         unsafe { intrinsics::powif32(self, n) }
323     }
324
325     /// Raises a number to a floating point power.
326     ///
327     /// # Examples
328     ///
329     /// ```
330     /// let x = 2.0_f32;
331     /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
332     ///
333     /// assert!(abs_difference <= f32::EPSILON);
334     /// ```
335     #[rustc_allow_incoherent_impl]
336     #[must_use = "method returns a new number and does not mutate the original value"]
337     #[stable(feature = "rust1", since = "1.0.0")]
338     #[inline]
339     pub fn powf(self, n: f32) -> f32 {
340         unsafe { intrinsics::powf32(self, n) }
341     }
342
343     /// Returns the square root of a number.
344     ///
345     /// Returns NaN if `self` is a negative number other than `-0.0`.
346     ///
347     /// # Examples
348     ///
349     /// ```
350     /// let positive = 4.0_f32;
351     /// let negative = -4.0_f32;
352     /// let negative_zero = -0.0_f32;
353     ///
354     /// let abs_difference = (positive.sqrt() - 2.0).abs();
355     ///
356     /// assert!(abs_difference <= f32::EPSILON);
357     /// assert!(negative.sqrt().is_nan());
358     /// assert!(negative_zero.sqrt() == negative_zero);
359     /// ```
360     #[rustc_allow_incoherent_impl]
361     #[must_use = "method returns a new number and does not mutate the original value"]
362     #[stable(feature = "rust1", since = "1.0.0")]
363     #[inline]
364     pub fn sqrt(self) -> f32 {
365         unsafe { intrinsics::sqrtf32(self) }
366     }
367
368     /// Returns `e^(self)`, (the exponential function).
369     ///
370     /// # Examples
371     ///
372     /// ```
373     /// let one = 1.0f32;
374     /// // e^1
375     /// let e = one.exp();
376     ///
377     /// // ln(e) - 1 == 0
378     /// let abs_difference = (e.ln() - 1.0).abs();
379     ///
380     /// assert!(abs_difference <= f32::EPSILON);
381     /// ```
382     #[rustc_allow_incoherent_impl]
383     #[must_use = "method returns a new number and does not mutate the original value"]
384     #[stable(feature = "rust1", since = "1.0.0")]
385     #[inline]
386     pub fn exp(self) -> f32 {
387         unsafe { intrinsics::expf32(self) }
388     }
389
390     /// Returns `2^(self)`.
391     ///
392     /// # Examples
393     ///
394     /// ```
395     /// let f = 2.0f32;
396     ///
397     /// // 2^2 - 4 == 0
398     /// let abs_difference = (f.exp2() - 4.0).abs();
399     ///
400     /// assert!(abs_difference <= f32::EPSILON);
401     /// ```
402     #[rustc_allow_incoherent_impl]
403     #[must_use = "method returns a new number and does not mutate the original value"]
404     #[stable(feature = "rust1", since = "1.0.0")]
405     #[inline]
406     pub fn exp2(self) -> f32 {
407         unsafe { intrinsics::exp2f32(self) }
408     }
409
410     /// Returns the natural logarithm of the number.
411     ///
412     /// # Examples
413     ///
414     /// ```
415     /// let one = 1.0f32;
416     /// // e^1
417     /// let e = one.exp();
418     ///
419     /// // ln(e) - 1 == 0
420     /// let abs_difference = (e.ln() - 1.0).abs();
421     ///
422     /// assert!(abs_difference <= f32::EPSILON);
423     /// ```
424     #[rustc_allow_incoherent_impl]
425     #[must_use = "method returns a new number and does not mutate the original value"]
426     #[stable(feature = "rust1", since = "1.0.0")]
427     #[inline]
428     pub fn ln(self) -> f32 {
429         unsafe { intrinsics::logf32(self) }
430     }
431
432     /// Returns the logarithm of the number with respect to an arbitrary base.
433     ///
434     /// The result might not be correctly rounded owing to implementation details;
435     /// `self.log2()` can produce more accurate results for base 2, and
436     /// `self.log10()` can produce more accurate results for base 10.
437     ///
438     /// # Examples
439     ///
440     /// ```
441     /// let five = 5.0f32;
442     ///
443     /// // log5(5) - 1 == 0
444     /// let abs_difference = (five.log(5.0) - 1.0).abs();
445     ///
446     /// assert!(abs_difference <= f32::EPSILON);
447     /// ```
448     #[rustc_allow_incoherent_impl]
449     #[must_use = "method returns a new number and does not mutate the original value"]
450     #[stable(feature = "rust1", since = "1.0.0")]
451     #[inline]
452     pub fn log(self, base: f32) -> f32 {
453         self.ln() / base.ln()
454     }
455
456     /// Returns the base 2 logarithm of the number.
457     ///
458     /// # Examples
459     ///
460     /// ```
461     /// let two = 2.0f32;
462     ///
463     /// // log2(2) - 1 == 0
464     /// let abs_difference = (two.log2() - 1.0).abs();
465     ///
466     /// assert!(abs_difference <= f32::EPSILON);
467     /// ```
468     #[rustc_allow_incoherent_impl]
469     #[must_use = "method returns a new number and does not mutate the original value"]
470     #[stable(feature = "rust1", since = "1.0.0")]
471     #[inline]
472     pub fn log2(self) -> f32 {
473         #[cfg(target_os = "android")]
474         return crate::sys::android::log2f32(self);
475         #[cfg(not(target_os = "android"))]
476         return unsafe { intrinsics::log2f32(self) };
477     }
478
479     /// Returns the base 10 logarithm of the number.
480     ///
481     /// # Examples
482     ///
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     #[rustc_allow_incoherent_impl]
492     #[must_use = "method returns a new number and does not mutate the original value"]
493     #[stable(feature = "rust1", since = "1.0.0")]
494     #[inline]
495     pub fn log10(self) -> f32 {
496         unsafe { intrinsics::log10f32(self) }
497     }
498
499     /// The positive difference of two numbers.
500     ///
501     /// * If `self <= other`: `0:0`
502     /// * Else: `self - other`
503     ///
504     /// # Examples
505     ///
506     /// ```
507     /// let x = 3.0f32;
508     /// let y = -3.0f32;
509     ///
510     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
511     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
512     ///
513     /// assert!(abs_difference_x <= f32::EPSILON);
514     /// assert!(abs_difference_y <= f32::EPSILON);
515     /// ```
516     #[rustc_allow_incoherent_impl]
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     #[deprecated(
521         since = "1.10.0",
522         note = "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 cube root of a number.
535     ///
536     /// # Examples
537     ///
538     /// ```
539     /// let x = 8.0f32;
540     ///
541     /// // x^(1/3) - 2 == 0
542     /// let abs_difference = (x.cbrt() - 2.0).abs();
543     ///
544     /// assert!(abs_difference <= f32::EPSILON);
545     /// ```
546     #[rustc_allow_incoherent_impl]
547     #[must_use = "method returns a new number and does not mutate the original value"]
548     #[stable(feature = "rust1", since = "1.0.0")]
549     #[inline]
550     pub fn cbrt(self) -> f32 {
551         unsafe { cmath::cbrtf(self) }
552     }
553
554     /// Calculates the length of the hypotenuse of a right-angle triangle given
555     /// legs of length `x` and `y`.
556     ///
557     /// # Examples
558     ///
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     #[rustc_allow_incoherent_impl]
569     #[must_use = "method returns a new number and does not mutate the original value"]
570     #[stable(feature = "rust1", since = "1.0.0")]
571     #[inline]
572     pub fn hypot(self, other: f32) -> f32 {
573         unsafe { cmath::hypotf(self, other) }
574     }
575
576     /// Computes the sine of a number (in radians).
577     ///
578     /// # Examples
579     ///
580     /// ```
581     /// let x = std::f32::consts::FRAC_PI_2;
582     ///
583     /// let abs_difference = (x.sin() - 1.0).abs();
584     ///
585     /// assert!(abs_difference <= f32::EPSILON);
586     /// ```
587     #[rustc_allow_incoherent_impl]
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     /// let x = 2.0 * std::f32::consts::PI;
601     ///
602     /// let abs_difference = (x.cos() - 1.0).abs();
603     ///
604     /// assert!(abs_difference <= f32::EPSILON);
605     /// ```
606     #[rustc_allow_incoherent_impl]
607     #[must_use = "method returns a new number and does not mutate the original value"]
608     #[stable(feature = "rust1", since = "1.0.0")]
609     #[inline]
610     pub fn cos(self) -> f32 {
611         unsafe { intrinsics::cosf32(self) }
612     }
613
614     /// Computes the tangent of a number (in radians).
615     ///
616     /// # Examples
617     ///
618     /// ```
619     /// let x = std::f32::consts::FRAC_PI_4;
620     /// let abs_difference = (x.tan() - 1.0).abs();
621     ///
622     /// assert!(abs_difference <= f32::EPSILON);
623     /// ```
624     #[rustc_allow_incoherent_impl]
625     #[must_use = "method returns a new number and does not mutate the original value"]
626     #[stable(feature = "rust1", since = "1.0.0")]
627     #[inline]
628     pub fn tan(self) -> f32 {
629         unsafe { cmath::tanf(self) }
630     }
631
632     /// Computes the arcsine of a number. Return value is in radians in
633     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
634     /// [-1, 1].
635     ///
636     /// # Examples
637     ///
638     /// ```
639     /// let f = std::f32::consts::FRAC_PI_2;
640     ///
641     /// // asin(sin(pi/2))
642     /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
643     ///
644     /// assert!(abs_difference <= f32::EPSILON);
645     /// ```
646     #[rustc_allow_incoherent_impl]
647     #[must_use = "method returns a new number and does not mutate the original value"]
648     #[stable(feature = "rust1", since = "1.0.0")]
649     #[inline]
650     pub fn asin(self) -> f32 {
651         unsafe { cmath::asinf(self) }
652     }
653
654     /// Computes the arccosine of a number. Return value is in radians in
655     /// the range [0, pi] or NaN if the number is outside the range
656     /// [-1, 1].
657     ///
658     /// # Examples
659     ///
660     /// ```
661     /// let f = std::f32::consts::FRAC_PI_4;
662     ///
663     /// // acos(cos(pi/4))
664     /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
665     ///
666     /// assert!(abs_difference <= f32::EPSILON);
667     /// ```
668     #[rustc_allow_incoherent_impl]
669     #[must_use = "method returns a new number and does not mutate the original value"]
670     #[stable(feature = "rust1", since = "1.0.0")]
671     #[inline]
672     pub fn acos(self) -> f32 {
673         unsafe { cmath::acosf(self) }
674     }
675
676     /// Computes the arctangent of a number. Return value is in radians in the
677     /// range [-pi/2, pi/2];
678     ///
679     /// # Examples
680     ///
681     /// ```
682     /// let f = 1.0f32;
683     ///
684     /// // atan(tan(1))
685     /// let abs_difference = (f.tan().atan() - 1.0).abs();
686     ///
687     /// assert!(abs_difference <= f32::EPSILON);
688     /// ```
689     #[rustc_allow_incoherent_impl]
690     #[must_use = "method returns a new number and does not mutate the original value"]
691     #[stable(feature = "rust1", since = "1.0.0")]
692     #[inline]
693     pub fn atan(self) -> f32 {
694         unsafe { cmath::atanf(self) }
695     }
696
697     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
698     ///
699     /// * `x = 0`, `y = 0`: `0`
700     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
701     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
702     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
703     ///
704     /// # Examples
705     ///
706     /// ```
707     /// // Positive angles measured counter-clockwise
708     /// // from positive x axis
709     /// // -pi/4 radians (45 deg clockwise)
710     /// let x1 = 3.0f32;
711     /// let y1 = -3.0f32;
712     ///
713     /// // 3pi/4 radians (135 deg counter-clockwise)
714     /// let x2 = -3.0f32;
715     /// let y2 = 3.0f32;
716     ///
717     /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
718     /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
719     ///
720     /// assert!(abs_difference_1 <= f32::EPSILON);
721     /// assert!(abs_difference_2 <= f32::EPSILON);
722     /// ```
723     #[rustc_allow_incoherent_impl]
724     #[must_use = "method returns a new number and does not mutate the original value"]
725     #[stable(feature = "rust1", since = "1.0.0")]
726     #[inline]
727     pub fn atan2(self, other: f32) -> f32 {
728         unsafe { cmath::atan2f(self, other) }
729     }
730
731     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
732     /// `(sin(x), cos(x))`.
733     ///
734     /// # Examples
735     ///
736     /// ```
737     /// let x = std::f32::consts::FRAC_PI_4;
738     /// let f = x.sin_cos();
739     ///
740     /// let abs_difference_0 = (f.0 - x.sin()).abs();
741     /// let abs_difference_1 = (f.1 - x.cos()).abs();
742     ///
743     /// assert!(abs_difference_0 <= f32::EPSILON);
744     /// assert!(abs_difference_1 <= f32::EPSILON);
745     /// ```
746     #[rustc_allow_incoherent_impl]
747     #[stable(feature = "rust1", since = "1.0.0")]
748     #[inline]
749     pub fn sin_cos(self) -> (f32, f32) {
750         (self.sin(), self.cos())
751     }
752
753     /// Returns `e^(self) - 1` in a way that is accurate even if the
754     /// number is close to zero.
755     ///
756     /// # Examples
757     ///
758     /// ```
759     /// let x = 1e-8_f32;
760     ///
761     /// // for very small x, e^x is approximately 1 + x + x^2 / 2
762     /// let approx = x + x * x / 2.0;
763     /// let abs_difference = (x.exp_m1() - approx).abs();
764     ///
765     /// assert!(abs_difference < 1e-10);
766     /// ```
767     #[rustc_allow_incoherent_impl]
768     #[must_use = "method returns a new number and does not mutate the original value"]
769     #[stable(feature = "rust1", since = "1.0.0")]
770     #[inline]
771     pub fn exp_m1(self) -> f32 {
772         unsafe { cmath::expm1f(self) }
773     }
774
775     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
776     /// the operations were performed separately.
777     ///
778     /// # Examples
779     ///
780     /// ```
781     /// let x = 1e-8_f32;
782     ///
783     /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
784     /// let approx = x - x * x / 2.0;
785     /// let abs_difference = (x.ln_1p() - approx).abs();
786     ///
787     /// assert!(abs_difference < 1e-10);
788     /// ```
789     #[rustc_allow_incoherent_impl]
790     #[must_use = "method returns a new number and does not mutate the original value"]
791     #[stable(feature = "rust1", since = "1.0.0")]
792     #[inline]
793     pub fn ln_1p(self) -> f32 {
794         unsafe { cmath::log1pf(self) }
795     }
796
797     /// Hyperbolic sine function.
798     ///
799     /// # Examples
800     ///
801     /// ```
802     /// let e = std::f32::consts::E;
803     /// let x = 1.0f32;
804     ///
805     /// let f = x.sinh();
806     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
807     /// let g = ((e * e) - 1.0) / (2.0 * e);
808     /// let abs_difference = (f - g).abs();
809     ///
810     /// assert!(abs_difference <= f32::EPSILON);
811     /// ```
812     #[rustc_allow_incoherent_impl]
813     #[must_use = "method returns a new number and does not mutate the original value"]
814     #[stable(feature = "rust1", since = "1.0.0")]
815     #[inline]
816     pub fn sinh(self) -> f32 {
817         unsafe { cmath::sinhf(self) }
818     }
819
820     /// Hyperbolic cosine function.
821     ///
822     /// # Examples
823     ///
824     /// ```
825     /// let e = std::f32::consts::E;
826     /// let x = 1.0f32;
827     /// let f = x.cosh();
828     /// // Solving cosh() at 1 gives this result
829     /// let g = ((e * e) + 1.0) / (2.0 * e);
830     /// let abs_difference = (f - g).abs();
831     ///
832     /// // Same result
833     /// assert!(abs_difference <= f32::EPSILON);
834     /// ```
835     #[rustc_allow_incoherent_impl]
836     #[must_use = "method returns a new number and does not mutate the original value"]
837     #[stable(feature = "rust1", since = "1.0.0")]
838     #[inline]
839     pub fn cosh(self) -> f32 {
840         unsafe { cmath::coshf(self) }
841     }
842
843     /// Hyperbolic tangent function.
844     ///
845     /// # Examples
846     ///
847     /// ```
848     /// let e = std::f32::consts::E;
849     /// let x = 1.0f32;
850     ///
851     /// let f = x.tanh();
852     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
853     /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
854     /// let abs_difference = (f - g).abs();
855     ///
856     /// assert!(abs_difference <= f32::EPSILON);
857     /// ```
858     #[rustc_allow_incoherent_impl]
859     #[must_use = "method returns a new number and does not mutate the original value"]
860     #[stable(feature = "rust1", since = "1.0.0")]
861     #[inline]
862     pub fn tanh(self) -> f32 {
863         unsafe { cmath::tanhf(self) }
864     }
865
866     /// Inverse hyperbolic sine function.
867     ///
868     /// # Examples
869     ///
870     /// ```
871     /// let x = 1.0f32;
872     /// let f = x.sinh().asinh();
873     ///
874     /// let abs_difference = (f - x).abs();
875     ///
876     /// assert!(abs_difference <= f32::EPSILON);
877     /// ```
878     #[rustc_allow_incoherent_impl]
879     #[must_use = "method returns a new number and does not mutate the original value"]
880     #[stable(feature = "rust1", since = "1.0.0")]
881     #[inline]
882     pub fn asinh(self) -> f32 {
883         let ax = self.abs();
884         let ix = 1.0 / ax;
885         (ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
886     }
887
888     /// Inverse hyperbolic cosine function.
889     ///
890     /// # Examples
891     ///
892     /// ```
893     /// let x = 1.0f32;
894     /// let f = x.cosh().acosh();
895     ///
896     /// let abs_difference = (f - x).abs();
897     ///
898     /// assert!(abs_difference <= f32::EPSILON);
899     /// ```
900     #[rustc_allow_incoherent_impl]
901     #[must_use = "method returns a new number and does not mutate the original value"]
902     #[stable(feature = "rust1", since = "1.0.0")]
903     #[inline]
904     pub fn acosh(self) -> f32 {
905         if self < 1.0 {
906             Self::NAN
907         } else {
908             (self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
909         }
910     }
911
912     /// Inverse hyperbolic tangent function.
913     ///
914     /// # Examples
915     ///
916     /// ```
917     /// let e = std::f32::consts::E;
918     /// let f = e.tanh().atanh();
919     ///
920     /// let abs_difference = (f - e).abs();
921     ///
922     /// assert!(abs_difference <= 1e-5);
923     /// ```
924     #[rustc_allow_incoherent_impl]
925     #[must_use = "method returns a new number and does not mutate the original value"]
926     #[stable(feature = "rust1", since = "1.0.0")]
927     #[inline]
928     pub fn atanh(self) -> f32 {
929         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
930     }
931 }