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