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