1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module provides constants which are specific to the implementation
12 //! of the `f64` floating point data type.
14 //! Mathematically significant numbers are provided in the `consts` sub-module.
16 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
18 #![stable(feature = "rust1", since = "1.0.0")]
19 #![allow(missing_docs)]
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub use core::f64::consts;
44 /// Returns `true` if this value is `NaN` and false otherwise.
49 /// let nan = f64::NAN;
52 /// assert!(nan.is_nan());
53 /// assert!(!f.is_nan());
55 #[stable(feature = "rust1", since = "1.0.0")]
57 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
59 /// Returns `true` if this value is positive infinity or negative infinity and
66 /// let inf = f64::INFINITY;
67 /// let neg_inf = f64::NEG_INFINITY;
68 /// let nan = f64::NAN;
70 /// assert!(!f.is_infinite());
71 /// assert!(!nan.is_infinite());
73 /// assert!(inf.is_infinite());
74 /// assert!(neg_inf.is_infinite());
76 #[stable(feature = "rust1", since = "1.0.0")]
78 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
80 /// Returns `true` if this number is neither infinite nor `NaN`.
86 /// let inf: f64 = f64::INFINITY;
87 /// let neg_inf: f64 = f64::NEG_INFINITY;
88 /// let nan: f64 = f64::NAN;
90 /// assert!(f.is_finite());
92 /// assert!(!nan.is_finite());
93 /// assert!(!inf.is_finite());
94 /// assert!(!neg_inf.is_finite());
96 #[stable(feature = "rust1", since = "1.0.0")]
98 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
100 /// Returns `true` if the number is neither zero, infinite,
101 /// [subnormal][subnormal], or `NaN`.
106 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
107 /// let max = f64::MAX;
108 /// let lower_than_min = 1.0e-308_f64;
109 /// let zero = 0.0f64;
111 /// assert!(min.is_normal());
112 /// assert!(max.is_normal());
114 /// assert!(!zero.is_normal());
115 /// assert!(!f64::NAN.is_normal());
116 /// assert!(!f64::INFINITY.is_normal());
117 /// // Values between `0` and `min` are Subnormal.
118 /// assert!(!lower_than_min.is_normal());
120 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
121 #[stable(feature = "rust1", since = "1.0.0")]
123 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
125 /// Returns the floating point category of the number. If only one property
126 /// is going to be tested, it is generally faster to use the specific
127 /// predicate instead.
130 /// use std::num::FpCategory;
133 /// let num = 12.4_f64;
134 /// let inf = f64::INFINITY;
136 /// assert_eq!(num.classify(), FpCategory::Normal);
137 /// assert_eq!(inf.classify(), FpCategory::Infinite);
139 #[stable(feature = "rust1", since = "1.0.0")]
141 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
143 /// Returns the largest integer less than or equal to a number.
146 /// let f = 3.99_f64;
149 /// assert_eq!(f.floor(), 3.0);
150 /// assert_eq!(g.floor(), 3.0);
152 #[stable(feature = "rust1", since = "1.0.0")]
154 pub fn floor(self) -> f64 {
155 unsafe { intrinsics::floorf64(self) }
158 /// Returns the smallest integer greater than or equal to a number.
161 /// let f = 3.01_f64;
164 /// assert_eq!(f.ceil(), 4.0);
165 /// assert_eq!(g.ceil(), 4.0);
167 #[stable(feature = "rust1", since = "1.0.0")]
169 pub fn ceil(self) -> f64 {
170 unsafe { intrinsics::ceilf64(self) }
173 /// Returns the nearest integer to a number. Round half-way cases away from
178 /// let g = -3.3_f64;
180 /// assert_eq!(f.round(), 3.0);
181 /// assert_eq!(g.round(), -3.0);
183 #[stable(feature = "rust1", since = "1.0.0")]
185 pub fn round(self) -> f64 {
186 unsafe { intrinsics::roundf64(self) }
189 /// Returns the integer part of a number.
193 /// let g = -3.7_f64;
195 /// assert_eq!(f.trunc(), 3.0);
196 /// assert_eq!(g.trunc(), -3.0);
198 #[stable(feature = "rust1", since = "1.0.0")]
200 pub fn trunc(self) -> f64 {
201 unsafe { intrinsics::truncf64(self) }
204 /// Returns the fractional part of a number.
208 /// let y = -3.5_f64;
209 /// let abs_difference_x = (x.fract() - 0.5).abs();
210 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
212 /// assert!(abs_difference_x < 1e-10);
213 /// assert!(abs_difference_y < 1e-10);
215 #[stable(feature = "rust1", since = "1.0.0")]
217 pub fn fract(self) -> f64 { self - self.trunc() }
219 /// Computes the absolute value of `self`. Returns `NAN` if the
226 /// let y = -3.5_f64;
228 /// let abs_difference_x = (x.abs() - x).abs();
229 /// let abs_difference_y = (y.abs() - (-y)).abs();
231 /// assert!(abs_difference_x < 1e-10);
232 /// assert!(abs_difference_y < 1e-10);
234 /// assert!(f64::NAN.abs().is_nan());
236 #[stable(feature = "rust1", since = "1.0.0")]
238 pub fn abs(self) -> f64 { num::Float::abs(self) }
240 /// Returns a number that represents the sign of `self`.
242 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
243 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
244 /// - `NAN` if the number is `NAN`
251 /// assert_eq!(f.signum(), 1.0);
252 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
254 /// assert!(f64::NAN.signum().is_nan());
256 #[stable(feature = "rust1", since = "1.0.0")]
258 pub fn signum(self) -> f64 { num::Float::signum(self) }
260 /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
261 /// positive sign bit and positive infinity.
265 /// let g = -7.0_f64;
267 /// assert!(f.is_sign_positive());
268 /// assert!(!g.is_sign_positive());
270 #[stable(feature = "rust1", since = "1.0.0")]
272 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
274 #[stable(feature = "rust1", since = "1.0.0")]
275 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
277 pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
279 /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
280 /// negative sign bit and negative infinity.
284 /// let g = -7.0_f64;
286 /// assert!(!f.is_sign_negative());
287 /// assert!(g.is_sign_negative());
289 #[stable(feature = "rust1", since = "1.0.0")]
291 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
293 #[stable(feature = "rust1", since = "1.0.0")]
294 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
296 pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
298 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
299 /// error. This produces a more accurate result with better performance than
300 /// a separate multiplication operation followed by an add.
303 /// let m = 10.0_f64;
305 /// let b = 60.0_f64;
308 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
310 /// assert!(abs_difference < 1e-10);
312 #[stable(feature = "rust1", since = "1.0.0")]
314 pub fn mul_add(self, a: f64, b: f64) -> f64 {
315 unsafe { intrinsics::fmaf64(self, a, b) }
318 /// Takes the reciprocal (inverse) of a number, `1/x`.
322 /// let abs_difference = (x.recip() - (1.0/x)).abs();
324 /// assert!(abs_difference < 1e-10);
326 #[stable(feature = "rust1", since = "1.0.0")]
328 pub fn recip(self) -> f64 { num::Float::recip(self) }
330 /// Raises a number to an integer power.
332 /// Using this function is generally faster than using `powf`
336 /// let abs_difference = (x.powi(2) - x*x).abs();
338 /// assert!(abs_difference < 1e-10);
340 #[stable(feature = "rust1", since = "1.0.0")]
342 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
344 /// Raises a number to a floating point power.
348 /// let abs_difference = (x.powf(2.0) - x*x).abs();
350 /// assert!(abs_difference < 1e-10);
352 #[stable(feature = "rust1", since = "1.0.0")]
354 pub fn powf(self, n: f64) -> f64 {
355 unsafe { intrinsics::powf64(self, n) }
358 /// Takes the square root of a number.
360 /// Returns NaN if `self` is a negative number.
363 /// let positive = 4.0_f64;
364 /// let negative = -4.0_f64;
366 /// let abs_difference = (positive.sqrt() - 2.0).abs();
368 /// assert!(abs_difference < 1e-10);
369 /// assert!(negative.sqrt().is_nan());
371 #[stable(feature = "rust1", since = "1.0.0")]
373 pub fn sqrt(self) -> f64 {
377 unsafe { intrinsics::sqrtf64(self) }
381 /// Returns `e^(self)`, (the exponential function).
384 /// let one = 1.0_f64;
386 /// let e = one.exp();
388 /// // ln(e) - 1 == 0
389 /// let abs_difference = (e.ln() - 1.0).abs();
391 /// assert!(abs_difference < 1e-10);
393 #[stable(feature = "rust1", since = "1.0.0")]
395 pub fn exp(self) -> f64 {
396 unsafe { intrinsics::expf64(self) }
399 /// Returns `2^(self)`.
405 /// let abs_difference = (f.exp2() - 4.0).abs();
407 /// assert!(abs_difference < 1e-10);
409 #[stable(feature = "rust1", since = "1.0.0")]
411 pub fn exp2(self) -> f64 {
412 unsafe { intrinsics::exp2f64(self) }
415 /// Returns the natural logarithm of the number.
418 /// let one = 1.0_f64;
420 /// let e = one.exp();
422 /// // ln(e) - 1 == 0
423 /// let abs_difference = (e.ln() - 1.0).abs();
425 /// assert!(abs_difference < 1e-10);
427 #[stable(feature = "rust1", since = "1.0.0")]
429 pub fn ln(self) -> f64 {
430 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
433 /// Returns the logarithm of the number with respect to an arbitrary base.
435 /// The result may not be correctly rounded owing to implementation details;
436 /// `self.log2()` can produce more accurate results for base 2, and
437 /// `self.log10()` can produce more accurate results for base 10.
440 /// let five = 5.0_f64;
442 /// // log5(5) - 1 == 0
443 /// let abs_difference = (five.log(5.0) - 1.0).abs();
445 /// assert!(abs_difference < 1e-10);
447 #[stable(feature = "rust1", since = "1.0.0")]
449 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
451 /// Returns the base 2 logarithm of the number.
454 /// let two = 2.0_f64;
456 /// // log2(2) - 1 == 0
457 /// let abs_difference = (two.log2() - 1.0).abs();
459 /// assert!(abs_difference < 1e-10);
461 #[stable(feature = "rust1", since = "1.0.0")]
463 pub fn log2(self) -> f64 {
464 self.log_wrapper(|n| {
465 #[cfg(target_os = "android")]
466 return ::sys::android::log2f64(n);
467 #[cfg(not(target_os = "android"))]
468 return unsafe { intrinsics::log2f64(n) };
472 /// Returns the base 10 logarithm of the number.
475 /// let ten = 10.0_f64;
477 /// // log10(10) - 1 == 0
478 /// let abs_difference = (ten.log10() - 1.0).abs();
480 /// assert!(abs_difference < 1e-10);
482 #[stable(feature = "rust1", since = "1.0.0")]
484 pub fn log10(self) -> f64 {
485 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
488 /// Converts radians to degrees.
491 /// use std::f64::consts;
493 /// let angle = consts::PI;
495 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
497 /// assert!(abs_difference < 1e-10);
499 #[stable(feature = "rust1", since = "1.0.0")]
501 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
503 /// Converts degrees to radians.
506 /// use std::f64::consts;
508 /// let angle = 180.0_f64;
510 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
512 /// assert!(abs_difference < 1e-10);
514 #[stable(feature = "rust1", since = "1.0.0")]
516 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
518 /// Returns the maximum of the two numbers.
524 /// assert_eq!(x.max(y), y);
527 /// If one of the arguments is NaN, then the other argument is returned.
528 #[stable(feature = "rust1", since = "1.0.0")]
530 pub fn max(self, other: f64) -> f64 {
531 num::Float::max(self, other)
534 /// Returns the minimum of the two numbers.
540 /// assert_eq!(x.min(y), x);
543 /// If one of the arguments is NaN, then the other argument is returned.
544 #[stable(feature = "rust1", since = "1.0.0")]
546 pub fn min(self, other: f64) -> f64 {
547 num::Float::min(self, other)
550 /// The positive difference of two numbers.
552 /// * If `self <= other`: `0:0`
553 /// * Else: `self - other`
557 /// let y = -3.0_f64;
559 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
560 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
562 /// assert!(abs_difference_x < 1e-10);
563 /// assert!(abs_difference_y < 1e-10);
565 #[stable(feature = "rust1", since = "1.0.0")]
567 #[rustc_deprecated(since = "1.10.0",
568 reason = "you probably meant `(self - other).abs()`: \
569 this operation is `(self - other).max(0.0)` (also \
570 known as `fdim` in C). If you truly need the positive \
571 difference, consider using that expression or the C function \
572 `fdim`, depending on how you wish to handle NaN (please consider \
573 filing an issue describing your use-case too).")]
574 pub fn abs_sub(self, other: f64) -> f64 {
575 unsafe { cmath::fdim(self, other) }
578 /// Takes the cubic root of a number.
583 /// // x^(1/3) - 2 == 0
584 /// let abs_difference = (x.cbrt() - 2.0).abs();
586 /// assert!(abs_difference < 1e-10);
588 #[stable(feature = "rust1", since = "1.0.0")]
590 pub fn cbrt(self) -> f64 {
591 unsafe { cmath::cbrt(self) }
594 /// Calculates the length of the hypotenuse of a right-angle triangle given
595 /// legs of length `x` and `y`.
601 /// // sqrt(x^2 + y^2)
602 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
604 /// assert!(abs_difference < 1e-10);
606 #[stable(feature = "rust1", since = "1.0.0")]
608 pub fn hypot(self, other: f64) -> f64 {
609 unsafe { cmath::hypot(self, other) }
612 /// Computes the sine of a number (in radians).
617 /// let x = f64::consts::PI/2.0;
619 /// let abs_difference = (x.sin() - 1.0).abs();
621 /// assert!(abs_difference < 1e-10);
623 #[stable(feature = "rust1", since = "1.0.0")]
625 pub fn sin(self) -> f64 {
626 unsafe { intrinsics::sinf64(self) }
629 /// Computes the cosine of a number (in radians).
634 /// let x = 2.0*f64::consts::PI;
636 /// let abs_difference = (x.cos() - 1.0).abs();
638 /// assert!(abs_difference < 1e-10);
640 #[stable(feature = "rust1", since = "1.0.0")]
642 pub fn cos(self) -> f64 {
643 unsafe { intrinsics::cosf64(self) }
646 /// Computes the tangent of a number (in radians).
651 /// let x = f64::consts::PI/4.0;
652 /// let abs_difference = (x.tan() - 1.0).abs();
654 /// assert!(abs_difference < 1e-14);
656 #[stable(feature = "rust1", since = "1.0.0")]
658 pub fn tan(self) -> f64 {
659 unsafe { cmath::tan(self) }
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
669 /// let f = f64::consts::PI / 2.0;
671 /// // asin(sin(pi/2))
672 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
674 /// assert!(abs_difference < 1e-10);
676 #[stable(feature = "rust1", since = "1.0.0")]
678 pub fn asin(self) -> f64 {
679 unsafe { cmath::asin(self) }
682 /// Computes the arccosine of a number. Return value is in radians in
683 /// the range [0, pi] or NaN if the number is outside the range
689 /// let f = f64::consts::PI / 4.0;
691 /// // acos(cos(pi/4))
692 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
694 /// assert!(abs_difference < 1e-10);
696 #[stable(feature = "rust1", since = "1.0.0")]
698 pub fn acos(self) -> f64 {
699 unsafe { cmath::acos(self) }
702 /// Computes the arctangent of a number. Return value is in radians in the
703 /// range [-pi/2, pi/2];
709 /// let abs_difference = (f.tan().atan() - 1.0).abs();
711 /// assert!(abs_difference < 1e-10);
713 #[stable(feature = "rust1", since = "1.0.0")]
715 pub fn atan(self) -> f64 {
716 unsafe { cmath::atan(self) }
719 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
721 /// * `x = 0`, `y = 0`: `0`
722 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
723 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
724 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
729 /// let pi = f64::consts::PI;
730 /// // All angles from horizontal right (+x)
731 /// // 45 deg counter-clockwise
732 /// let x1 = 3.0_f64;
733 /// let y1 = -3.0_f64;
735 /// // 135 deg clockwise
736 /// let x2 = -3.0_f64;
737 /// let y2 = 3.0_f64;
739 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
740 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
742 /// assert!(abs_difference_1 < 1e-10);
743 /// assert!(abs_difference_2 < 1e-10);
745 #[stable(feature = "rust1", since = "1.0.0")]
747 pub fn atan2(self, other: f64) -> f64 {
748 unsafe { cmath::atan2(self, other) }
751 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
752 /// `(sin(x), cos(x))`.
757 /// let x = f64::consts::PI/4.0;
758 /// let f = x.sin_cos();
760 /// let abs_difference_0 = (f.0 - x.sin()).abs();
761 /// let abs_difference_1 = (f.1 - x.cos()).abs();
763 /// assert!(abs_difference_0 < 1e-10);
764 /// assert!(abs_difference_1 < 1e-10);
766 #[stable(feature = "rust1", since = "1.0.0")]
768 pub fn sin_cos(self) -> (f64, f64) {
769 (self.sin(), self.cos())
772 /// Returns `e^(self) - 1` in a way that is accurate even if the
773 /// number is close to zero.
779 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
781 /// assert!(abs_difference < 1e-10);
783 #[stable(feature = "rust1", since = "1.0.0")]
785 pub fn exp_m1(self) -> f64 {
786 unsafe { cmath::expm1(self) }
789 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
790 /// the operations were performed separately.
795 /// let x = f64::consts::E - 1.0;
797 /// // ln(1 + (e - 1)) == ln(e) == 1
798 /// let abs_difference = (x.ln_1p() - 1.0).abs();
800 /// assert!(abs_difference < 1e-10);
802 #[stable(feature = "rust1", since = "1.0.0")]
804 pub fn ln_1p(self) -> f64 {
805 unsafe { cmath::log1p(self) }
808 /// Hyperbolic sine function.
813 /// let e = f64::consts::E;
816 /// let f = x.sinh();
817 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
818 /// let g = (e*e - 1.0)/(2.0*e);
819 /// let abs_difference = (f - g).abs();
821 /// assert!(abs_difference < 1e-10);
823 #[stable(feature = "rust1", since = "1.0.0")]
825 pub fn sinh(self) -> f64 {
826 unsafe { cmath::sinh(self) }
829 /// Hyperbolic cosine function.
834 /// let e = f64::consts::E;
836 /// let f = x.cosh();
837 /// // Solving cosh() at 1 gives this result
838 /// let g = (e*e + 1.0)/(2.0*e);
839 /// let abs_difference = (f - g).abs();
842 /// assert!(abs_difference < 1.0e-10);
844 #[stable(feature = "rust1", since = "1.0.0")]
846 pub fn cosh(self) -> f64 {
847 unsafe { cmath::cosh(self) }
850 /// Hyperbolic tangent function.
855 /// let e = f64::consts::E;
858 /// let f = x.tanh();
859 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
860 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
861 /// let abs_difference = (f - g).abs();
863 /// assert!(abs_difference < 1.0e-10);
865 #[stable(feature = "rust1", since = "1.0.0")]
867 pub fn tanh(self) -> f64 {
868 unsafe { cmath::tanh(self) }
871 /// Inverse hyperbolic sine function.
875 /// let f = x.sinh().asinh();
877 /// let abs_difference = (f - x).abs();
879 /// assert!(abs_difference < 1.0e-10);
881 #[stable(feature = "rust1", since = "1.0.0")]
883 pub fn asinh(self) -> f64 {
884 if self == NEG_INFINITY {
887 (self + ((self * self) + 1.0).sqrt()).ln()
891 /// Inverse hyperbolic cosine function.
895 /// let f = x.cosh().acosh();
897 /// let abs_difference = (f - x).abs();
899 /// assert!(abs_difference < 1.0e-10);
901 #[stable(feature = "rust1", since = "1.0.0")]
903 pub fn acosh(self) -> f64 {
906 x => (x + ((x * x) - 1.0).sqrt()).ln(),
910 /// Inverse hyperbolic tangent function.
915 /// let e = f64::consts::E;
916 /// let f = e.tanh().atanh();
918 /// let abs_difference = (f - e).abs();
920 /// assert!(abs_difference < 1.0e-10);
922 #[stable(feature = "rust1", since = "1.0.0")]
924 pub fn atanh(self) -> f64 {
925 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
928 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
929 // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
931 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
932 if !cfg!(target_os = "solaris") {
935 if self.is_finite() {
938 } else if self == 0.0 {
939 NEG_INFINITY // log(0) = -Inf
943 } else if self.is_nan() {
944 self // log(NaN) = NaN
945 } else if self > 0.0 {
946 self // log(Inf) = Inf
948 NAN // log(-Inf) = NaN
953 /// Raw transmutation to `u64`.
955 /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
957 /// See `from_bits` for some discussion of the portability of this operation
958 /// (there are almost no issues).
960 /// Note that this function is distinct from `as` casting, which attempts to
961 /// preserve the *numeric* value, and not the bitwise value.
966 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
967 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
970 #[stable(feature = "float_bits_conv", since = "1.20.0")]
972 pub fn to_bits(self) -> u64 {
973 num::Float::to_bits(self)
976 /// Raw transmutation from `u64`.
978 /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
979 /// It turns out this is incredibly portable, for two reasons:
981 /// * Floats and Ints have the same endianess on all supported platforms.
982 /// * IEEE-754 very precisely specifies the bit layout of floats.
984 /// However there is one caveat: prior to the 2008 version of IEEE-754, how
985 /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
986 /// (notably x86 and ARM) picked the interpretation that was ultimately
987 /// standardized in 2008, but some didn't (notably MIPS). As a result, all
988 /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
990 /// Rather than trying to preserve signaling-ness cross-platform, this
991 /// implementation favours preserving the exact bits. This means that
992 /// any payloads encoded in NaNs will be preserved even if the result of
993 /// this method is sent over the network from an x86 machine to a MIPS one.
995 /// If the results of this method are only manipulated by the same
996 /// architecture that produced them, then there is no portability concern.
998 /// If the input isn't NaN, then there is no portability concern.
1000 /// If you don't care about signalingness (very likely), then there is no
1001 /// portability concern.
1003 /// Note that this function is distinct from `as` casting, which attempts to
1004 /// preserve the *numeric* value, and not the bitwise value.
1010 /// let v = f64::from_bits(0x4029000000000000);
1011 /// let difference = (v - 12.5).abs();
1012 /// assert!(difference <= 1e-5);
1014 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1016 pub fn from_bits(v: u64) -> Self {
1017 num::Float::from_bits(v)
1026 use num::FpCategory as Fp;
1030 test_num(10f64, 2f64);
1035 assert_eq!(NAN.min(2.0), 2.0);
1036 assert_eq!(2.0f64.min(NAN), 2.0);
1041 assert_eq!(NAN.max(2.0), 2.0);
1042 assert_eq!(2.0f64.max(NAN), 2.0);
1048 assert!(nan.is_nan());
1049 assert!(!nan.is_infinite());
1050 assert!(!nan.is_finite());
1051 assert!(!nan.is_normal());
1052 assert!(nan.is_sign_positive());
1053 assert!(!nan.is_sign_negative());
1054 assert_eq!(Fp::Nan, nan.classify());
1058 fn test_infinity() {
1059 let inf: f64 = INFINITY;
1060 assert!(inf.is_infinite());
1061 assert!(!inf.is_finite());
1062 assert!(inf.is_sign_positive());
1063 assert!(!inf.is_sign_negative());
1064 assert!(!inf.is_nan());
1065 assert!(!inf.is_normal());
1066 assert_eq!(Fp::Infinite, inf.classify());
1070 fn test_neg_infinity() {
1071 let neg_inf: f64 = NEG_INFINITY;
1072 assert!(neg_inf.is_infinite());
1073 assert!(!neg_inf.is_finite());
1074 assert!(!neg_inf.is_sign_positive());
1075 assert!(neg_inf.is_sign_negative());
1076 assert!(!neg_inf.is_nan());
1077 assert!(!neg_inf.is_normal());
1078 assert_eq!(Fp::Infinite, neg_inf.classify());
1083 let zero: f64 = 0.0f64;
1084 assert_eq!(0.0, zero);
1085 assert!(!zero.is_infinite());
1086 assert!(zero.is_finite());
1087 assert!(zero.is_sign_positive());
1088 assert!(!zero.is_sign_negative());
1089 assert!(!zero.is_nan());
1090 assert!(!zero.is_normal());
1091 assert_eq!(Fp::Zero, zero.classify());
1095 fn test_neg_zero() {
1096 let neg_zero: f64 = -0.0;
1097 assert_eq!(0.0, neg_zero);
1098 assert!(!neg_zero.is_infinite());
1099 assert!(neg_zero.is_finite());
1100 assert!(!neg_zero.is_sign_positive());
1101 assert!(neg_zero.is_sign_negative());
1102 assert!(!neg_zero.is_nan());
1103 assert!(!neg_zero.is_normal());
1104 assert_eq!(Fp::Zero, neg_zero.classify());
1107 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1110 let one: f64 = 1.0f64;
1111 assert_eq!(1.0, one);
1112 assert!(!one.is_infinite());
1113 assert!(one.is_finite());
1114 assert!(one.is_sign_positive());
1115 assert!(!one.is_sign_negative());
1116 assert!(!one.is_nan());
1117 assert!(one.is_normal());
1118 assert_eq!(Fp::Normal, one.classify());
1124 let inf: f64 = INFINITY;
1125 let neg_inf: f64 = NEG_INFINITY;
1126 assert!(nan.is_nan());
1127 assert!(!0.0f64.is_nan());
1128 assert!(!5.3f64.is_nan());
1129 assert!(!(-10.732f64).is_nan());
1130 assert!(!inf.is_nan());
1131 assert!(!neg_inf.is_nan());
1135 fn test_is_infinite() {
1137 let inf: f64 = INFINITY;
1138 let neg_inf: f64 = NEG_INFINITY;
1139 assert!(!nan.is_infinite());
1140 assert!(inf.is_infinite());
1141 assert!(neg_inf.is_infinite());
1142 assert!(!0.0f64.is_infinite());
1143 assert!(!42.8f64.is_infinite());
1144 assert!(!(-109.2f64).is_infinite());
1148 fn test_is_finite() {
1150 let inf: f64 = INFINITY;
1151 let neg_inf: f64 = NEG_INFINITY;
1152 assert!(!nan.is_finite());
1153 assert!(!inf.is_finite());
1154 assert!(!neg_inf.is_finite());
1155 assert!(0.0f64.is_finite());
1156 assert!(42.8f64.is_finite());
1157 assert!((-109.2f64).is_finite());
1160 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1162 fn test_is_normal() {
1164 let inf: f64 = INFINITY;
1165 let neg_inf: f64 = NEG_INFINITY;
1166 let zero: f64 = 0.0f64;
1167 let neg_zero: f64 = -0.0;
1168 assert!(!nan.is_normal());
1169 assert!(!inf.is_normal());
1170 assert!(!neg_inf.is_normal());
1171 assert!(!zero.is_normal());
1172 assert!(!neg_zero.is_normal());
1173 assert!(1f64.is_normal());
1174 assert!(1e-307f64.is_normal());
1175 assert!(!1e-308f64.is_normal());
1178 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1180 fn test_classify() {
1182 let inf: f64 = INFINITY;
1183 let neg_inf: f64 = NEG_INFINITY;
1184 let zero: f64 = 0.0f64;
1185 let neg_zero: f64 = -0.0;
1186 assert_eq!(nan.classify(), Fp::Nan);
1187 assert_eq!(inf.classify(), Fp::Infinite);
1188 assert_eq!(neg_inf.classify(), Fp::Infinite);
1189 assert_eq!(zero.classify(), Fp::Zero);
1190 assert_eq!(neg_zero.classify(), Fp::Zero);
1191 assert_eq!(1e-307f64.classify(), Fp::Normal);
1192 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1197 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1198 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1199 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1200 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1201 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1202 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1203 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1204 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1205 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1206 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1211 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1212 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1213 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1214 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1215 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1216 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1217 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1218 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1219 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1220 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1225 assert_approx_eq!(1.0f64.round(), 1.0f64);
1226 assert_approx_eq!(1.3f64.round(), 1.0f64);
1227 assert_approx_eq!(1.5f64.round(), 2.0f64);
1228 assert_approx_eq!(1.7f64.round(), 2.0f64);
1229 assert_approx_eq!(0.0f64.round(), 0.0f64);
1230 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1231 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1232 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1233 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1234 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1239 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1240 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1241 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1242 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1243 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1244 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1245 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1246 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1247 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1248 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1253 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1254 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1255 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1256 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1257 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1258 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1259 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1260 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1261 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1262 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1267 assert_eq!(INFINITY.abs(), INFINITY);
1268 assert_eq!(1f64.abs(), 1f64);
1269 assert_eq!(0f64.abs(), 0f64);
1270 assert_eq!((-0f64).abs(), 0f64);
1271 assert_eq!((-1f64).abs(), 1f64);
1272 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1273 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1274 assert!(NAN.abs().is_nan());
1279 assert_eq!(INFINITY.signum(), 1f64);
1280 assert_eq!(1f64.signum(), 1f64);
1281 assert_eq!(0f64.signum(), 1f64);
1282 assert_eq!((-0f64).signum(), -1f64);
1283 assert_eq!((-1f64).signum(), -1f64);
1284 assert_eq!(NEG_INFINITY.signum(), -1f64);
1285 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1286 assert!(NAN.signum().is_nan());
1290 fn test_is_sign_positive() {
1291 assert!(INFINITY.is_sign_positive());
1292 assert!(1f64.is_sign_positive());
1293 assert!(0f64.is_sign_positive());
1294 assert!(!(-0f64).is_sign_positive());
1295 assert!(!(-1f64).is_sign_positive());
1296 assert!(!NEG_INFINITY.is_sign_positive());
1297 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1298 assert!(NAN.is_sign_positive());
1299 assert!(!(-NAN).is_sign_positive());
1303 fn test_is_sign_negative() {
1304 assert!(!INFINITY.is_sign_negative());
1305 assert!(!1f64.is_sign_negative());
1306 assert!(!0f64.is_sign_negative());
1307 assert!((-0f64).is_sign_negative());
1308 assert!((-1f64).is_sign_negative());
1309 assert!(NEG_INFINITY.is_sign_negative());
1310 assert!((1f64/NEG_INFINITY).is_sign_negative());
1311 assert!(!NAN.is_sign_negative());
1312 assert!((-NAN).is_sign_negative());
1318 let inf: f64 = INFINITY;
1319 let neg_inf: f64 = NEG_INFINITY;
1320 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1321 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1322 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1323 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1324 assert!(nan.mul_add(7.8, 9.0).is_nan());
1325 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1326 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1327 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1328 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1334 let inf: f64 = INFINITY;
1335 let neg_inf: f64 = NEG_INFINITY;
1336 assert_eq!(1.0f64.recip(), 1.0);
1337 assert_eq!(2.0f64.recip(), 0.5);
1338 assert_eq!((-0.4f64).recip(), -2.5);
1339 assert_eq!(0.0f64.recip(), inf);
1340 assert!(nan.recip().is_nan());
1341 assert_eq!(inf.recip(), 0.0);
1342 assert_eq!(neg_inf.recip(), 0.0);
1348 let inf: f64 = INFINITY;
1349 let neg_inf: f64 = NEG_INFINITY;
1350 assert_eq!(1.0f64.powi(1), 1.0);
1351 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1352 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1353 assert_eq!(8.3f64.powi(0), 1.0);
1354 assert!(nan.powi(2).is_nan());
1355 assert_eq!(inf.powi(3), inf);
1356 assert_eq!(neg_inf.powi(2), inf);
1362 let inf: f64 = INFINITY;
1363 let neg_inf: f64 = NEG_INFINITY;
1364 assert_eq!(1.0f64.powf(1.0), 1.0);
1365 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1366 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1367 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1368 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1369 assert_eq!(8.3f64.powf(0.0), 1.0);
1370 assert!(nan.powf(2.0).is_nan());
1371 assert_eq!(inf.powf(2.0), inf);
1372 assert_eq!(neg_inf.powf(3.0), neg_inf);
1376 fn test_sqrt_domain() {
1377 assert!(NAN.sqrt().is_nan());
1378 assert!(NEG_INFINITY.sqrt().is_nan());
1379 assert!((-1.0f64).sqrt().is_nan());
1380 assert_eq!((-0.0f64).sqrt(), -0.0);
1381 assert_eq!(0.0f64.sqrt(), 0.0);
1382 assert_eq!(1.0f64.sqrt(), 1.0);
1383 assert_eq!(INFINITY.sqrt(), INFINITY);
1388 assert_eq!(1.0, 0.0f64.exp());
1389 assert_approx_eq!(2.718282, 1.0f64.exp());
1390 assert_approx_eq!(148.413159, 5.0f64.exp());
1392 let inf: f64 = INFINITY;
1393 let neg_inf: f64 = NEG_INFINITY;
1395 assert_eq!(inf, inf.exp());
1396 assert_eq!(0.0, neg_inf.exp());
1397 assert!(nan.exp().is_nan());
1402 assert_eq!(32.0, 5.0f64.exp2());
1403 assert_eq!(1.0, 0.0f64.exp2());
1405 let inf: f64 = INFINITY;
1406 let neg_inf: f64 = NEG_INFINITY;
1408 assert_eq!(inf, inf.exp2());
1409 assert_eq!(0.0, neg_inf.exp2());
1410 assert!(nan.exp2().is_nan());
1416 let inf: f64 = INFINITY;
1417 let neg_inf: f64 = NEG_INFINITY;
1418 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1419 assert!(nan.ln().is_nan());
1420 assert_eq!(inf.ln(), inf);
1421 assert!(neg_inf.ln().is_nan());
1422 assert!((-2.3f64).ln().is_nan());
1423 assert_eq!((-0.0f64).ln(), neg_inf);
1424 assert_eq!(0.0f64.ln(), neg_inf);
1425 assert_approx_eq!(4.0f64.ln(), 1.386294);
1431 let inf: f64 = INFINITY;
1432 let neg_inf: f64 = NEG_INFINITY;
1433 assert_eq!(10.0f64.log(10.0), 1.0);
1434 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1435 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1436 assert!(1.0f64.log(1.0).is_nan());
1437 assert!(1.0f64.log(-13.9).is_nan());
1438 assert!(nan.log(2.3).is_nan());
1439 assert_eq!(inf.log(10.0), inf);
1440 assert!(neg_inf.log(8.8).is_nan());
1441 assert!((-2.3f64).log(0.1).is_nan());
1442 assert_eq!((-0.0f64).log(2.0), neg_inf);
1443 assert_eq!(0.0f64.log(7.0), neg_inf);
1449 let inf: f64 = INFINITY;
1450 let neg_inf: f64 = NEG_INFINITY;
1451 assert_approx_eq!(10.0f64.log2(), 3.321928);
1452 assert_approx_eq!(2.3f64.log2(), 1.201634);
1453 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1454 assert!(nan.log2().is_nan());
1455 assert_eq!(inf.log2(), inf);
1456 assert!(neg_inf.log2().is_nan());
1457 assert!((-2.3f64).log2().is_nan());
1458 assert_eq!((-0.0f64).log2(), neg_inf);
1459 assert_eq!(0.0f64.log2(), neg_inf);
1465 let inf: f64 = INFINITY;
1466 let neg_inf: f64 = NEG_INFINITY;
1467 assert_eq!(10.0f64.log10(), 1.0);
1468 assert_approx_eq!(2.3f64.log10(), 0.361728);
1469 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1470 assert_eq!(1.0f64.log10(), 0.0);
1471 assert!(nan.log10().is_nan());
1472 assert_eq!(inf.log10(), inf);
1473 assert!(neg_inf.log10().is_nan());
1474 assert!((-2.3f64).log10().is_nan());
1475 assert_eq!((-0.0f64).log10(), neg_inf);
1476 assert_eq!(0.0f64.log10(), neg_inf);
1480 fn test_to_degrees() {
1481 let pi: f64 = consts::PI;
1483 let inf: f64 = INFINITY;
1484 let neg_inf: f64 = NEG_INFINITY;
1485 assert_eq!(0.0f64.to_degrees(), 0.0);
1486 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1487 assert_eq!(pi.to_degrees(), 180.0);
1488 assert!(nan.to_degrees().is_nan());
1489 assert_eq!(inf.to_degrees(), inf);
1490 assert_eq!(neg_inf.to_degrees(), neg_inf);
1494 fn test_to_radians() {
1495 let pi: f64 = consts::PI;
1497 let inf: f64 = INFINITY;
1498 let neg_inf: f64 = NEG_INFINITY;
1499 assert_eq!(0.0f64.to_radians(), 0.0);
1500 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1501 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1502 assert_eq!(180.0f64.to_radians(), pi);
1503 assert!(nan.to_radians().is_nan());
1504 assert_eq!(inf.to_radians(), inf);
1505 assert_eq!(neg_inf.to_radians(), neg_inf);
1510 assert_eq!(0.0f64.asinh(), 0.0f64);
1511 assert_eq!((-0.0f64).asinh(), -0.0f64);
1513 let inf: f64 = INFINITY;
1514 let neg_inf: f64 = NEG_INFINITY;
1516 assert_eq!(inf.asinh(), inf);
1517 assert_eq!(neg_inf.asinh(), neg_inf);
1518 assert!(nan.asinh().is_nan());
1519 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1520 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1525 assert_eq!(1.0f64.acosh(), 0.0f64);
1526 assert!(0.999f64.acosh().is_nan());
1528 let inf: f64 = INFINITY;
1529 let neg_inf: f64 = NEG_INFINITY;
1531 assert_eq!(inf.acosh(), inf);
1532 assert!(neg_inf.acosh().is_nan());
1533 assert!(nan.acosh().is_nan());
1534 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1535 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1540 assert_eq!(0.0f64.atanh(), 0.0f64);
1541 assert_eq!((-0.0f64).atanh(), -0.0f64);
1543 let inf: f64 = INFINITY;
1544 let neg_inf: f64 = NEG_INFINITY;
1546 assert_eq!(1.0f64.atanh(), inf);
1547 assert_eq!((-1.0f64).atanh(), neg_inf);
1548 assert!(2f64.atanh().atanh().is_nan());
1549 assert!((-2f64).atanh().atanh().is_nan());
1550 assert!(inf.atanh().is_nan());
1551 assert!(neg_inf.atanh().is_nan());
1552 assert!(nan.atanh().is_nan());
1553 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1554 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1558 fn test_real_consts() {
1560 let pi: f64 = consts::PI;
1561 let frac_pi_2: f64 = consts::FRAC_PI_2;
1562 let frac_pi_3: f64 = consts::FRAC_PI_3;
1563 let frac_pi_4: f64 = consts::FRAC_PI_4;
1564 let frac_pi_6: f64 = consts::FRAC_PI_6;
1565 let frac_pi_8: f64 = consts::FRAC_PI_8;
1566 let frac_1_pi: f64 = consts::FRAC_1_PI;
1567 let frac_2_pi: f64 = consts::FRAC_2_PI;
1568 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1569 let sqrt2: f64 = consts::SQRT_2;
1570 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1571 let e: f64 = consts::E;
1572 let log2_e: f64 = consts::LOG2_E;
1573 let log10_e: f64 = consts::LOG10_E;
1574 let ln_2: f64 = consts::LN_2;
1575 let ln_10: f64 = consts::LN_10;
1577 assert_approx_eq!(frac_pi_2, pi / 2f64);
1578 assert_approx_eq!(frac_pi_3, pi / 3f64);
1579 assert_approx_eq!(frac_pi_4, pi / 4f64);
1580 assert_approx_eq!(frac_pi_6, pi / 6f64);
1581 assert_approx_eq!(frac_pi_8, pi / 8f64);
1582 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1583 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1584 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1585 assert_approx_eq!(sqrt2, 2f64.sqrt());
1586 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1587 assert_approx_eq!(log2_e, e.log2());
1588 assert_approx_eq!(log10_e, e.log10());
1589 assert_approx_eq!(ln_2, 2f64.ln());
1590 assert_approx_eq!(ln_10, 10f64.ln());
1594 fn test_float_bits_conv() {
1595 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1596 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1597 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1598 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1599 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1600 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1601 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1602 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1604 // Check that NaNs roundtrip their bits regardless of signalingness
1605 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1606 let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1607 let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1608 assert!(f64::from_bits(masked_nan1).is_nan());
1609 assert!(f64::from_bits(masked_nan2).is_nan());
1611 assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1612 assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);