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