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