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