]> git.lizzy.rs Git - rust.git/blob - library/std/src/f64.rs
Auto merge of #100035 - workingjubilee:merge-functions, r=nikic
[rust.git] / library / std / src / f64.rs
1 //! Constants specific to the `f64` double-precision floating point type.
2 //!
3 //! *[See also the `f64` primitive type](primitive@f64).*
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 `f64` 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::f64::{
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 f64 {
32     /// Returns the largest integer less than or equal to `self`.
33     ///
34     /// # Examples
35     ///
36     /// ```
37     /// let f = 3.7_f64;
38     /// let g = 3.0_f64;
39     /// let h = -3.7_f64;
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) -> f64 {
50         unsafe { intrinsics::floorf64(self) }
51     }
52
53     /// Returns the smallest integer greater than or equal to `self`.
54     ///
55     /// # Examples
56     ///
57     /// ```
58     /// let f = 3.01_f64;
59     /// let g = 4.0_f64;
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) -> f64 {
69         unsafe { intrinsics::ceilf64(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_f64;
79     /// let g = -3.3_f64;
80     ///
81     /// assert_eq!(f.round(), 3.0);
82     /// assert_eq!(g.round(), -3.0);
83     /// ```
84     #[rustc_allow_incoherent_impl]
85     #[must_use = "method returns a new number and does not mutate the original value"]
86     #[stable(feature = "rust1", since = "1.0.0")]
87     #[inline]
88     pub fn round(self) -> f64 {
89         unsafe { intrinsics::roundf64(self) }
90     }
91
92     /// Returns the integer part of `self`.
93     /// This means that non-integer numbers are always truncated towards zero.
94     ///
95     /// # Examples
96     ///
97     /// ```
98     /// let f = 3.7_f64;
99     /// let g = 3.0_f64;
100     /// let h = -3.7_f64;
101     ///
102     /// assert_eq!(f.trunc(), 3.0);
103     /// assert_eq!(g.trunc(), 3.0);
104     /// assert_eq!(h.trunc(), -3.0);
105     /// ```
106     #[rustc_allow_incoherent_impl]
107     #[must_use = "method returns a new number and does not mutate the original value"]
108     #[stable(feature = "rust1", since = "1.0.0")]
109     #[inline]
110     pub fn trunc(self) -> f64 {
111         unsafe { intrinsics::truncf64(self) }
112     }
113
114     /// Returns the fractional part of `self`.
115     ///
116     /// # Examples
117     ///
118     /// ```
119     /// let x = 3.6_f64;
120     /// let y = -3.6_f64;
121     /// let abs_difference_x = (x.fract() - 0.6).abs();
122     /// let abs_difference_y = (y.fract() - (-0.6)).abs();
123     ///
124     /// assert!(abs_difference_x < 1e-10);
125     /// assert!(abs_difference_y < 1e-10);
126     /// ```
127     #[rustc_allow_incoherent_impl]
128     #[must_use = "method returns a new number and does not mutate the original value"]
129     #[stable(feature = "rust1", since = "1.0.0")]
130     #[inline]
131     pub fn fract(self) -> f64 {
132         self - self.trunc()
133     }
134
135     /// Computes the absolute value of `self`.
136     ///
137     /// # Examples
138     ///
139     /// ```
140     /// let x = 3.5_f64;
141     /// let y = -3.5_f64;
142     ///
143     /// let abs_difference_x = (x.abs() - x).abs();
144     /// let abs_difference_y = (y.abs() - (-y)).abs();
145     ///
146     /// assert!(abs_difference_x < 1e-10);
147     /// assert!(abs_difference_y < 1e-10);
148     ///
149     /// assert!(f64::NAN.abs().is_nan());
150     /// ```
151     #[rustc_allow_incoherent_impl]
152     #[must_use = "method returns a new number and does not mutate the original value"]
153     #[stable(feature = "rust1", since = "1.0.0")]
154     #[inline]
155     pub fn abs(self) -> f64 {
156         unsafe { intrinsics::fabsf64(self) }
157     }
158
159     /// Returns a number that represents the sign of `self`.
160     ///
161     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
162     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
163     /// - NaN if the number is NaN
164     ///
165     /// # Examples
166     ///
167     /// ```
168     /// let f = 3.5_f64;
169     ///
170     /// assert_eq!(f.signum(), 1.0);
171     /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
172     ///
173     /// assert!(f64::NAN.signum().is_nan());
174     /// ```
175     #[rustc_allow_incoherent_impl]
176     #[must_use = "method returns a new number and does not mutate the original value"]
177     #[stable(feature = "rust1", since = "1.0.0")]
178     #[inline]
179     pub fn signum(self) -> f64 {
180         if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) }
181     }
182
183     /// Returns a number composed of the magnitude of `self` and the sign of
184     /// `sign`.
185     ///
186     /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
187     /// equal to `-self`. If `self` is a NaN, then a NaN with the sign bit of
188     /// `sign` is returned. Note, however, that conserving the sign bit on NaN
189     /// across arithmetical operations is not generally guaranteed.
190     /// See [explanation of NaN as a special value](primitive@f32) for more info.
191     ///
192     /// # Examples
193     ///
194     /// ```
195     /// let f = 3.5_f64;
196     ///
197     /// assert_eq!(f.copysign(0.42), 3.5_f64);
198     /// assert_eq!(f.copysign(-0.42), -3.5_f64);
199     /// assert_eq!((-f).copysign(0.42), 3.5_f64);
200     /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
201     ///
202     /// assert!(f64::NAN.copysign(1.0).is_nan());
203     /// ```
204     #[rustc_allow_incoherent_impl]
205     #[must_use = "method returns a new number and does not mutate the original value"]
206     #[stable(feature = "copysign", since = "1.35.0")]
207     #[inline]
208     pub fn copysign(self, sign: f64) -> f64 {
209         unsafe { intrinsics::copysignf64(self, sign) }
210     }
211
212     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
213     /// error, yielding a more accurate result than an unfused multiply-add.
214     ///
215     /// Using `mul_add` *may* be more performant than an unfused multiply-add if
216     /// the target architecture has a dedicated `fma` CPU instruction. However,
217     /// this is not always true, and will be heavily dependant on designing
218     /// algorithms with specific target hardware in mind.
219     ///
220     /// # Examples
221     ///
222     /// ```
223     /// let m = 10.0_f64;
224     /// let x = 4.0_f64;
225     /// let b = 60.0_f64;
226     ///
227     /// // 100.0
228     /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
229     ///
230     /// assert!(abs_difference < 1e-10);
231     /// ```
232     #[rustc_allow_incoherent_impl]
233     #[must_use = "method returns a new number and does not mutate the original value"]
234     #[stable(feature = "rust1", since = "1.0.0")]
235     #[inline]
236     pub fn mul_add(self, a: f64, b: f64) -> f64 {
237         unsafe { intrinsics::fmaf64(self, a, b) }
238     }
239
240     /// Calculates Euclidean division, the matching method for `rem_euclid`.
241     ///
242     /// This computes the integer `n` such that
243     /// `self = n * rhs + self.rem_euclid(rhs)`.
244     /// In other words, the result is `self / rhs` rounded to the integer `n`
245     /// such that `self >= n * rhs`.
246     ///
247     /// # Examples
248     ///
249     /// ```
250     /// let a: f64 = 7.0;
251     /// let b = 4.0;
252     /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
253     /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
254     /// 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     /// ```
257     #[rustc_allow_incoherent_impl]
258     #[must_use = "method returns a new number and does not mutate the original value"]
259     #[inline]
260     #[stable(feature = "euclidean_division", since = "1.38.0")]
261     pub fn div_euclid(self, rhs: f64) -> f64 {
262         let q = (self / rhs).trunc();
263         if self % rhs < 0.0 {
264             return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
265         }
266         q
267     }
268
269     /// Calculates the least nonnegative remainder of `self (mod rhs)`.
270     ///
271     /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
272     /// most cases. However, due to a floating point round-off error it can
273     /// result in `r == rhs.abs()`, violating the mathematical definition, if
274     /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
275     /// This result is not an element of the function's codomain, but it is the
276     /// closest floating point number in the real numbers and thus fulfills the
277     /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
278     /// approximatively.
279     ///
280     /// # Examples
281     ///
282     /// ```
283     /// let a: f64 = 7.0;
284     /// let b = 4.0;
285     /// assert_eq!(a.rem_euclid(b), 3.0);
286     /// assert_eq!((-a).rem_euclid(b), 1.0);
287     /// assert_eq!(a.rem_euclid(-b), 3.0);
288     /// assert_eq!((-a).rem_euclid(-b), 1.0);
289     /// // limitation due to round-off error
290     /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
291     /// ```
292     #[rustc_allow_incoherent_impl]
293     #[must_use = "method returns a new number and does not mutate the original value"]
294     #[inline]
295     #[stable(feature = "euclidean_division", since = "1.38.0")]
296     pub fn rem_euclid(self, rhs: f64) -> f64 {
297         let r = self % rhs;
298         if r < 0.0 { r + rhs.abs() } else { r }
299     }
300
301     /// Raises a number to an integer power.
302     ///
303     /// Using this function is generally faster than using `powf`.
304     /// It might have a different sequence of rounding operations than `powf`,
305     /// so the results are not guaranteed to agree.
306     ///
307     /// # Examples
308     ///
309     /// ```
310     /// let x = 2.0_f64;
311     /// let abs_difference = (x.powi(2) - (x * x)).abs();
312     ///
313     /// assert!(abs_difference < 1e-10);
314     /// ```
315     #[rustc_allow_incoherent_impl]
316     #[must_use = "method returns a new number and does not mutate the original value"]
317     #[stable(feature = "rust1", since = "1.0.0")]
318     #[inline]
319     pub fn powi(self, n: i32) -> f64 {
320         unsafe { intrinsics::powif64(self, n) }
321     }
322
323     /// Raises a number to a floating point power.
324     ///
325     /// # Examples
326     ///
327     /// ```
328     /// let x = 2.0_f64;
329     /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
330     ///
331     /// assert!(abs_difference < 1e-10);
332     /// ```
333     #[rustc_allow_incoherent_impl]
334     #[must_use = "method returns a new number and does not mutate the original value"]
335     #[stable(feature = "rust1", since = "1.0.0")]
336     #[inline]
337     pub fn powf(self, n: f64) -> f64 {
338         unsafe { intrinsics::powf64(self, n) }
339     }
340
341     /// Returns the square root of a number.
342     ///
343     /// Returns NaN if `self` is a negative number other than `-0.0`.
344     ///
345     /// # Examples
346     ///
347     /// ```
348     /// let positive = 4.0_f64;
349     /// let negative = -4.0_f64;
350     /// let negative_zero = -0.0_f64;
351     ///
352     /// let abs_difference = (positive.sqrt() - 2.0).abs();
353     ///
354     /// assert!(abs_difference < 1e-10);
355     /// assert!(negative.sqrt().is_nan());
356     /// assert!(negative_zero.sqrt() == negative_zero);
357     /// ```
358     #[rustc_allow_incoherent_impl]
359     #[must_use = "method returns a new number and does not mutate the original value"]
360     #[stable(feature = "rust1", since = "1.0.0")]
361     #[inline]
362     pub fn sqrt(self) -> f64 {
363         unsafe { intrinsics::sqrtf64(self) }
364     }
365
366     /// Returns `e^(self)`, (the exponential function).
367     ///
368     /// # Examples
369     ///
370     /// ```
371     /// let one = 1.0_f64;
372     /// // e^1
373     /// let e = one.exp();
374     ///
375     /// // ln(e) - 1 == 0
376     /// let abs_difference = (e.ln() - 1.0).abs();
377     ///
378     /// assert!(abs_difference < 1e-10);
379     /// ```
380     #[rustc_allow_incoherent_impl]
381     #[must_use = "method returns a new number and does not mutate the original value"]
382     #[stable(feature = "rust1", since = "1.0.0")]
383     #[inline]
384     pub fn exp(self) -> f64 {
385         unsafe { intrinsics::expf64(self) }
386     }
387
388     /// Returns `2^(self)`.
389     ///
390     /// # Examples
391     ///
392     /// ```
393     /// let f = 2.0_f64;
394     ///
395     /// // 2^2 - 4 == 0
396     /// let abs_difference = (f.exp2() - 4.0).abs();
397     ///
398     /// assert!(abs_difference < 1e-10);
399     /// ```
400     #[rustc_allow_incoherent_impl]
401     #[must_use = "method returns a new number and does not mutate the original value"]
402     #[stable(feature = "rust1", since = "1.0.0")]
403     #[inline]
404     pub fn exp2(self) -> f64 {
405         unsafe { intrinsics::exp2f64(self) }
406     }
407
408     /// Returns the natural logarithm of the number.
409     ///
410     /// # Examples
411     ///
412     /// ```
413     /// let one = 1.0_f64;
414     /// // e^1
415     /// let e = one.exp();
416     ///
417     /// // ln(e) - 1 == 0
418     /// let abs_difference = (e.ln() - 1.0).abs();
419     ///
420     /// assert!(abs_difference < 1e-10);
421     /// ```
422     #[rustc_allow_incoherent_impl]
423     #[must_use = "method returns a new number and does not mutate the original value"]
424     #[stable(feature = "rust1", since = "1.0.0")]
425     #[inline]
426     pub fn ln(self) -> f64 {
427         self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
428     }
429
430     /// Returns the logarithm of the number with respect to an arbitrary base.
431     ///
432     /// The result might not be correctly rounded owing to implementation details;
433     /// `self.log2()` can produce more accurate results for base 2, and
434     /// `self.log10()` can produce more accurate results for base 10.
435     ///
436     /// # Examples
437     ///
438     /// ```
439     /// let twenty_five = 25.0_f64;
440     ///
441     /// // log5(25) - 2 == 0
442     /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
443     ///
444     /// assert!(abs_difference < 1e-10);
445     /// ```
446     #[rustc_allow_incoherent_impl]
447     #[must_use = "method returns a new number and does not mutate the original value"]
448     #[stable(feature = "rust1", since = "1.0.0")]
449     #[inline]
450     pub fn log(self, base: f64) -> f64 {
451         self.ln() / base.ln()
452     }
453
454     /// Returns the base 2 logarithm of the number.
455     ///
456     /// # Examples
457     ///
458     /// ```
459     /// let four = 4.0_f64;
460     ///
461     /// // log2(4) - 2 == 0
462     /// let abs_difference = (four.log2() - 2.0).abs();
463     ///
464     /// assert!(abs_difference < 1e-10);
465     /// ```
466     #[rustc_allow_incoherent_impl]
467     #[must_use = "method returns a new number and does not mutate the original value"]
468     #[stable(feature = "rust1", since = "1.0.0")]
469     #[inline]
470     pub fn log2(self) -> f64 {
471         self.log_wrapper(|n| {
472             #[cfg(target_os = "android")]
473             return crate::sys::android::log2f64(n);
474             #[cfg(not(target_os = "android"))]
475             return unsafe { intrinsics::log2f64(n) };
476         })
477     }
478
479     /// Returns the base 10 logarithm of the number.
480     ///
481     /// # Examples
482     ///
483     /// ```
484     /// let hundred = 100.0_f64;
485     ///
486     /// // log10(100) - 2 == 0
487     /// let abs_difference = (hundred.log10() - 2.0).abs();
488     ///
489     /// assert!(abs_difference < 1e-10);
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) -> f64 {
496         self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
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.0_f64;
508     /// let y = -3.0_f64;
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 < 1e-10);
514     /// assert!(abs_difference_y < 1e-10);
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 `fdim` in C). If you truly need the positive \
526                 difference, consider using that expression or the C function \
527                 `fdim`, 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: f64) -> f64 {
531         unsafe { cmath::fdim(self, other) }
532     }
533
534     /// Returns the cube root of a number.
535     ///
536     /// # Examples
537     ///
538     /// ```
539     /// let x = 8.0_f64;
540     ///
541     /// // x^(1/3) - 2 == 0
542     /// let abs_difference = (x.cbrt() - 2.0).abs();
543     ///
544     /// assert!(abs_difference < 1e-10);
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) -> f64 {
551         unsafe { cmath::cbrt(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.0_f64;
561     /// let y = 3.0_f64;
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 < 1e-10);
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: f64) -> f64 {
573         unsafe { cmath::hypot(self, other) }
574     }
575
576     /// Computes the sine of a number (in radians).
577     ///
578     /// # Examples
579     ///
580     /// ```
581     /// let x = std::f64::consts::FRAC_PI_2;
582     ///
583     /// let abs_difference = (x.sin() - 1.0).abs();
584     ///
585     /// assert!(abs_difference < 1e-10);
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) -> f64 {
592         unsafe { intrinsics::sinf64(self) }
593     }
594
595     /// Computes the cosine of a number (in radians).
596     ///
597     /// # Examples
598     ///
599     /// ```
600     /// let x = 2.0 * std::f64::consts::PI;
601     ///
602     /// let abs_difference = (x.cos() - 1.0).abs();
603     ///
604     /// assert!(abs_difference < 1e-10);
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) -> f64 {
611         unsafe { intrinsics::cosf64(self) }
612     }
613
614     /// Computes the tangent of a number (in radians).
615     ///
616     /// # Examples
617     ///
618     /// ```
619     /// let x = std::f64::consts::FRAC_PI_4;
620     /// let abs_difference = (x.tan() - 1.0).abs();
621     ///
622     /// assert!(abs_difference < 1e-14);
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) -> f64 {
629         unsafe { cmath::tan(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::f64::consts::FRAC_PI_2;
640     ///
641     /// // asin(sin(pi/2))
642     /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
643     ///
644     /// assert!(abs_difference < 1e-10);
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) -> f64 {
651         unsafe { cmath::asin(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::f64::consts::FRAC_PI_4;
662     ///
663     /// // acos(cos(pi/4))
664     /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
665     ///
666     /// assert!(abs_difference < 1e-10);
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) -> f64 {
673         unsafe { cmath::acos(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.0_f64;
683     ///
684     /// // atan(tan(1))
685     /// let abs_difference = (f.tan().atan() - 1.0).abs();
686     ///
687     /// assert!(abs_difference < 1e-10);
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) -> f64 {
694         unsafe { cmath::atan(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.0_f64;
711     /// let y1 = -3.0_f64;
712     ///
713     /// // 3pi/4 radians (135 deg counter-clockwise)
714     /// let x2 = -3.0_f64;
715     /// let y2 = 3.0_f64;
716     ///
717     /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
718     /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
719     ///
720     /// assert!(abs_difference_1 < 1e-10);
721     /// assert!(abs_difference_2 < 1e-10);
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: f64) -> f64 {
728         unsafe { cmath::atan2(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::f64::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 < 1e-10);
744     /// assert!(abs_difference_1 < 1e-10);
745     /// ```
746     #[rustc_allow_incoherent_impl]
747     #[stable(feature = "rust1", since = "1.0.0")]
748     #[inline]
749     pub fn sin_cos(self) -> (f64, f64) {
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-16_f64;
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-20);
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) -> f64 {
772         unsafe { cmath::expm1(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-16_f64;
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-20);
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) -> f64 {
794         unsafe { cmath::log1p(self) }
795     }
796
797     /// Hyperbolic sine function.
798     ///
799     /// # Examples
800     ///
801     /// ```
802     /// let e = std::f64::consts::E;
803     /// let x = 1.0_f64;
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 < 1e-10);
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) -> f64 {
817         unsafe { cmath::sinh(self) }
818     }
819
820     /// Hyperbolic cosine function.
821     ///
822     /// # Examples
823     ///
824     /// ```
825     /// let e = std::f64::consts::E;
826     /// let x = 1.0_f64;
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 < 1.0e-10);
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) -> f64 {
840         unsafe { cmath::cosh(self) }
841     }
842
843     /// Hyperbolic tangent function.
844     ///
845     /// # Examples
846     ///
847     /// ```
848     /// let e = std::f64::consts::E;
849     /// let x = 1.0_f64;
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 < 1.0e-10);
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) -> f64 {
863         unsafe { cmath::tanh(self) }
864     }
865
866     /// Inverse hyperbolic sine function.
867     ///
868     /// # Examples
869     ///
870     /// ```
871     /// let x = 1.0_f64;
872     /// let f = x.sinh().asinh();
873     ///
874     /// let abs_difference = (f - x).abs();
875     ///
876     /// assert!(abs_difference < 1.0e-10);
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) -> f64 {
883         (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
884     }
885
886     /// Inverse hyperbolic cosine function.
887     ///
888     /// # Examples
889     ///
890     /// ```
891     /// let x = 1.0_f64;
892     /// let f = x.cosh().acosh();
893     ///
894     /// let abs_difference = (f - x).abs();
895     ///
896     /// assert!(abs_difference < 1.0e-10);
897     /// ```
898     #[rustc_allow_incoherent_impl]
899     #[must_use = "method returns a new number and does not mutate the original value"]
900     #[stable(feature = "rust1", since = "1.0.0")]
901     #[inline]
902     pub fn acosh(self) -> f64 {
903         if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
904     }
905
906     /// Inverse hyperbolic tangent function.
907     ///
908     /// # Examples
909     ///
910     /// ```
911     /// let e = std::f64::consts::E;
912     /// let f = e.tanh().atanh();
913     ///
914     /// let abs_difference = (f - e).abs();
915     ///
916     /// assert!(abs_difference < 1.0e-10);
917     /// ```
918     #[rustc_allow_incoherent_impl]
919     #[must_use = "method returns a new number and does not mutate the original value"]
920     #[stable(feature = "rust1", since = "1.0.0")]
921     #[inline]
922     pub fn atanh(self) -> f64 {
923         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
924     }
925
926     // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
927     // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
928     // of expected NaN).
929     #[rustc_allow_incoherent_impl]
930     fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
931         if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
932             log_fn(self)
933         } else if self.is_finite() {
934             if self > 0.0 {
935                 log_fn(self)
936             } else if self == 0.0 {
937                 Self::NEG_INFINITY // log(0) = -Inf
938             } else {
939                 Self::NAN // log(-n) = NaN
940             }
941         } else if self.is_nan() {
942             self // log(NaN) = NaN
943         } else if self > 0.0 {
944             self // log(Inf) = Inf
945         } else {
946             Self::NAN // log(-Inf) = NaN
947         }
948     }
949 }