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