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