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. Mathematically significant
13 //! numbers are provided in the `consts` sub-module.
15 //! *[See also the `f64` primitive type](../primitive.f64.html).*
17 #![stable(feature = "rust1", since = "1.0.0")]
18 #![allow(missing_docs)]
27 #[stable(feature = "rust1", since = "1.0.0")]
28 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
35 #[stable(feature = "rust1", since = "1.0.0")]
36 pub use core::f64::consts;
40 use libc::{c_double, c_int};
44 pub fn acos(n: c_double) -> c_double;
45 pub fn asin(n: c_double) -> c_double;
46 pub fn atan(n: c_double) -> c_double;
47 pub fn atan2(a: c_double, b: c_double) -> c_double;
48 pub fn cbrt(n: c_double) -> c_double;
49 pub fn cosh(n: c_double) -> c_double;
50 pub fn erf(n: c_double) -> c_double;
51 pub fn erfc(n: c_double) -> c_double;
52 pub fn expm1(n: c_double) -> c_double;
53 pub fn fdim(a: c_double, b: c_double) -> c_double;
54 pub fn fmax(a: c_double, b: c_double) -> c_double;
55 pub fn fmin(a: c_double, b: c_double) -> c_double;
56 pub fn fmod(a: c_double, b: c_double) -> c_double;
57 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
58 pub fn ilogb(n: c_double) -> c_int;
59 pub fn ldexp(x: c_double, n: c_int) -> c_double;
60 pub fn logb(n: c_double) -> c_double;
61 pub fn log1p(n: c_double) -> c_double;
62 pub fn nextafter(x: c_double, y: c_double) -> c_double;
63 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
64 pub fn sinh(n: c_double) -> c_double;
65 pub fn tan(n: c_double) -> c_double;
66 pub fn tanh(n: c_double) -> c_double;
67 pub fn tgamma(n: c_double) -> c_double;
69 // These are commonly only available for doubles
71 pub fn j0(n: c_double) -> c_double;
72 pub fn j1(n: c_double) -> c_double;
73 pub fn jn(i: c_int, n: c_double) -> c_double;
75 pub fn y0(n: c_double) -> c_double;
76 pub fn y1(n: c_double) -> c_double;
77 pub fn yn(i: c_int, n: c_double) -> c_double;
79 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
80 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
82 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
83 pub fn hypot(x: c_double, y: c_double) -> c_double;
90 /// Returns `true` if this value is `NaN` and false otherwise.
95 /// let nan = f64::NAN;
98 /// assert!(nan.is_nan());
99 /// assert!(!f.is_nan());
101 #[stable(feature = "rust1", since = "1.0.0")]
103 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
105 /// Returns `true` if this value is positive infinity or negative infinity and
112 /// let inf = f64::INFINITY;
113 /// let neg_inf = f64::NEG_INFINITY;
114 /// let nan = f64::NAN;
116 /// assert!(!f.is_infinite());
117 /// assert!(!nan.is_infinite());
119 /// assert!(inf.is_infinite());
120 /// assert!(neg_inf.is_infinite());
122 #[stable(feature = "rust1", since = "1.0.0")]
124 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
126 /// Returns `true` if this number is neither infinite nor `NaN`.
132 /// let inf: f64 = f64::INFINITY;
133 /// let neg_inf: f64 = f64::NEG_INFINITY;
134 /// let nan: f64 = f64::NAN;
136 /// assert!(f.is_finite());
138 /// assert!(!nan.is_finite());
139 /// assert!(!inf.is_finite());
140 /// assert!(!neg_inf.is_finite());
142 #[stable(feature = "rust1", since = "1.0.0")]
144 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
146 /// Returns `true` if the number is neither zero, infinite,
147 /// [subnormal][subnormal], or `NaN`.
152 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
153 /// let max = f64::MAX;
154 /// let lower_than_min = 1.0e-308_f64;
155 /// let zero = 0.0f64;
157 /// assert!(min.is_normal());
158 /// assert!(max.is_normal());
160 /// assert!(!zero.is_normal());
161 /// assert!(!f64::NAN.is_normal());
162 /// assert!(!f64::INFINITY.is_normal());
163 /// // Values between `0` and `min` are Subnormal.
164 /// assert!(!lower_than_min.is_normal());
166 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
167 #[stable(feature = "rust1", since = "1.0.0")]
169 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
171 /// Returns the floating point category of the number. If only one property
172 /// is going to be tested, it is generally faster to use the specific
173 /// predicate instead.
176 /// use std::num::FpCategory;
179 /// let num = 12.4_f64;
180 /// let inf = f64::INFINITY;
182 /// assert_eq!(num.classify(), FpCategory::Normal);
183 /// assert_eq!(inf.classify(), FpCategory::Infinite);
185 #[stable(feature = "rust1", since = "1.0.0")]
187 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
189 /// Returns the largest integer less than or equal to a number.
192 /// let f = 3.99_f64;
195 /// assert_eq!(f.floor(), 3.0);
196 /// assert_eq!(g.floor(), 3.0);
198 #[stable(feature = "rust1", since = "1.0.0")]
200 pub fn floor(self) -> f64 {
201 unsafe { intrinsics::floorf64(self) }
204 /// Returns the smallest integer greater than or equal to a number.
207 /// let f = 3.01_f64;
210 /// assert_eq!(f.ceil(), 4.0);
211 /// assert_eq!(g.ceil(), 4.0);
213 #[stable(feature = "rust1", since = "1.0.0")]
215 pub fn ceil(self) -> f64 {
216 unsafe { intrinsics::ceilf64(self) }
219 /// Returns the nearest integer to a number. Round half-way cases away from
224 /// let g = -3.3_f64;
226 /// assert_eq!(f.round(), 3.0);
227 /// assert_eq!(g.round(), -3.0);
229 #[stable(feature = "rust1", since = "1.0.0")]
231 pub fn round(self) -> f64 {
232 unsafe { intrinsics::roundf64(self) }
235 /// Returns the integer part of a number.
239 /// let g = -3.7_f64;
241 /// assert_eq!(f.trunc(), 3.0);
242 /// assert_eq!(g.trunc(), -3.0);
244 #[stable(feature = "rust1", since = "1.0.0")]
246 pub fn trunc(self) -> f64 {
247 unsafe { intrinsics::truncf64(self) }
250 /// Returns the fractional part of a number.
254 /// let y = -3.5_f64;
255 /// let abs_difference_x = (x.fract() - 0.5).abs();
256 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
258 /// assert!(abs_difference_x < 1e-10);
259 /// assert!(abs_difference_y < 1e-10);
261 #[stable(feature = "rust1", since = "1.0.0")]
263 pub fn fract(self) -> f64 { self - self.trunc() }
265 /// Computes the absolute value of `self`. Returns `NAN` if the
272 /// let y = -3.5_f64;
274 /// let abs_difference_x = (x.abs() - x).abs();
275 /// let abs_difference_y = (y.abs() - (-y)).abs();
277 /// assert!(abs_difference_x < 1e-10);
278 /// assert!(abs_difference_y < 1e-10);
280 /// assert!(f64::NAN.abs().is_nan());
282 #[stable(feature = "rust1", since = "1.0.0")]
284 pub fn abs(self) -> f64 { num::Float::abs(self) }
286 /// Returns a number that represents the sign of `self`.
288 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
289 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
290 /// - `NAN` if the number is `NAN`
297 /// assert_eq!(f.signum(), 1.0);
298 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
300 /// assert!(f64::NAN.signum().is_nan());
302 #[stable(feature = "rust1", since = "1.0.0")]
304 pub fn signum(self) -> f64 { num::Float::signum(self) }
306 /// Returns `true` if `self`'s sign bit is positive, including
307 /// `+0.0` and `INFINITY`.
312 /// let nan: f64 = f64::NAN;
315 /// let g = -7.0_f64;
317 /// assert!(f.is_sign_positive());
318 /// assert!(!g.is_sign_positive());
319 /// // Requires both tests to determine if is `NaN`
320 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
322 #[stable(feature = "rust1", since = "1.0.0")]
324 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
326 #[stable(feature = "rust1", since = "1.0.0")]
327 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
329 pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
331 /// Returns `true` if `self`'s sign is negative, including `-0.0`
332 /// and `NEG_INFINITY`.
337 /// let nan = f64::NAN;
340 /// let g = -7.0_f64;
342 /// assert!(!f.is_sign_negative());
343 /// assert!(g.is_sign_negative());
344 /// // Requires both tests to determine if is `NaN`.
345 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
347 #[stable(feature = "rust1", since = "1.0.0")]
349 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
351 #[stable(feature = "rust1", since = "1.0.0")]
352 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
354 pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
356 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
357 /// error. This produces a more accurate result with better performance than
358 /// a separate multiplication operation followed by an add.
361 /// let m = 10.0_f64;
363 /// let b = 60.0_f64;
366 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
368 /// assert!(abs_difference < 1e-10);
370 #[stable(feature = "rust1", since = "1.0.0")]
372 pub fn mul_add(self, a: f64, b: f64) -> f64 {
373 unsafe { intrinsics::fmaf64(self, a, b) }
376 /// Takes the reciprocal (inverse) of a number, `1/x`.
380 /// let abs_difference = (x.recip() - (1.0/x)).abs();
382 /// assert!(abs_difference < 1e-10);
384 #[stable(feature = "rust1", since = "1.0.0")]
386 pub fn recip(self) -> f64 { num::Float::recip(self) }
388 /// Raises a number to an integer power.
390 /// Using this function is generally faster than using `powf`
394 /// let abs_difference = (x.powi(2) - x*x).abs();
396 /// assert!(abs_difference < 1e-10);
398 #[stable(feature = "rust1", since = "1.0.0")]
400 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
402 /// Raises a number to a floating point power.
406 /// let abs_difference = (x.powf(2.0) - x*x).abs();
408 /// assert!(abs_difference < 1e-10);
410 #[stable(feature = "rust1", since = "1.0.0")]
412 pub fn powf(self, n: f64) -> f64 {
413 unsafe { intrinsics::powf64(self, n) }
416 /// Takes the square root of a number.
418 /// Returns NaN if `self` is a negative number.
421 /// let positive = 4.0_f64;
422 /// let negative = -4.0_f64;
424 /// let abs_difference = (positive.sqrt() - 2.0).abs();
426 /// assert!(abs_difference < 1e-10);
427 /// assert!(negative.sqrt().is_nan());
429 #[stable(feature = "rust1", since = "1.0.0")]
431 pub fn sqrt(self) -> f64 {
435 unsafe { intrinsics::sqrtf64(self) }
439 /// Returns `e^(self)`, (the exponential function).
442 /// let one = 1.0_f64;
444 /// let e = one.exp();
446 /// // ln(e) - 1 == 0
447 /// let abs_difference = (e.ln() - 1.0).abs();
449 /// assert!(abs_difference < 1e-10);
451 #[stable(feature = "rust1", since = "1.0.0")]
453 pub fn exp(self) -> f64 {
454 unsafe { intrinsics::expf64(self) }
457 /// Returns `2^(self)`.
463 /// let abs_difference = (f.exp2() - 4.0).abs();
465 /// assert!(abs_difference < 1e-10);
467 #[stable(feature = "rust1", since = "1.0.0")]
469 pub fn exp2(self) -> f64 {
470 unsafe { intrinsics::exp2f64(self) }
473 /// Returns the natural logarithm of the number.
476 /// let one = 1.0_f64;
478 /// let e = one.exp();
480 /// // ln(e) - 1 == 0
481 /// let abs_difference = (e.ln() - 1.0).abs();
483 /// assert!(abs_difference < 1e-10);
485 #[stable(feature = "rust1", since = "1.0.0")]
487 pub fn ln(self) -> f64 {
488 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
491 /// Returns the logarithm of the number with respect to an arbitrary base.
494 /// let ten = 10.0_f64;
495 /// let two = 2.0_f64;
497 /// // log10(10) - 1 == 0
498 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
500 /// // log2(2) - 1 == 0
501 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
503 /// assert!(abs_difference_10 < 1e-10);
504 /// assert!(abs_difference_2 < 1e-10);
506 #[stable(feature = "rust1", since = "1.0.0")]
508 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
510 /// Returns the base 2 logarithm of the number.
513 /// let two = 2.0_f64;
515 /// // log2(2) - 1 == 0
516 /// let abs_difference = (two.log2() - 1.0).abs();
518 /// assert!(abs_difference < 1e-10);
520 #[stable(feature = "rust1", since = "1.0.0")]
522 pub fn log2(self) -> f64 {
523 self.log_wrapper(|n| {
524 #[cfg(target_os = "android")]
525 return ::sys::android::log2f64(n);
526 #[cfg(not(target_os = "android"))]
527 return unsafe { intrinsics::log2f64(n) };
531 /// Returns the base 10 logarithm of the number.
534 /// let ten = 10.0_f64;
536 /// // log10(10) - 1 == 0
537 /// let abs_difference = (ten.log10() - 1.0).abs();
539 /// assert!(abs_difference < 1e-10);
541 #[stable(feature = "rust1", since = "1.0.0")]
543 pub fn log10(self) -> f64 {
544 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
547 /// Converts radians to degrees.
550 /// use std::f64::consts;
552 /// let angle = consts::PI;
554 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
556 /// assert!(abs_difference < 1e-10);
558 #[stable(feature = "rust1", since = "1.0.0")]
560 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
562 /// Converts degrees to radians.
565 /// use std::f64::consts;
567 /// let angle = 180.0_f64;
569 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
571 /// assert!(abs_difference < 1e-10);
573 #[stable(feature = "rust1", since = "1.0.0")]
575 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
577 /// Returns the maximum of the two numbers.
583 /// assert_eq!(x.max(y), y);
586 /// If one of the arguments is NaN, then the other argument is returned.
587 #[stable(feature = "rust1", since = "1.0.0")]
589 pub fn max(self, other: f64) -> f64 {
590 unsafe { cmath::fmax(self, other) }
593 /// Returns the minimum of the two numbers.
599 /// assert_eq!(x.min(y), x);
602 /// If one of the arguments is NaN, then the other argument is returned.
603 #[stable(feature = "rust1", since = "1.0.0")]
605 pub fn min(self, other: f64) -> f64 {
606 unsafe { cmath::fmin(self, other) }
609 /// The positive difference of two numbers.
611 /// * If `self <= other`: `0:0`
612 /// * Else: `self - other`
616 /// let y = -3.0_f64;
618 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
619 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
621 /// assert!(abs_difference_x < 1e-10);
622 /// assert!(abs_difference_y < 1e-10);
624 #[stable(feature = "rust1", since = "1.0.0")]
626 #[rustc_deprecated(since = "1.10.0",
627 reason = "you probably meant `(self - other).abs()`: \
628 this operation is `(self - other).max(0.0)` (also \
629 known as `fdim` in C). If you truly need the positive \
630 difference, consider using that expression or the C function \
631 `fdim`, depending on how you wish to handle NaN (please consider \
632 filing an issue describing your use-case too).")]
633 pub fn abs_sub(self, other: f64) -> f64 {
634 unsafe { cmath::fdim(self, other) }
637 /// Takes the cubic root of a number.
642 /// // x^(1/3) - 2 == 0
643 /// let abs_difference = (x.cbrt() - 2.0).abs();
645 /// assert!(abs_difference < 1e-10);
647 #[stable(feature = "rust1", since = "1.0.0")]
649 pub fn cbrt(self) -> f64 {
650 unsafe { cmath::cbrt(self) }
653 /// Calculates the length of the hypotenuse of a right-angle triangle given
654 /// legs of length `x` and `y`.
660 /// // sqrt(x^2 + y^2)
661 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
663 /// assert!(abs_difference < 1e-10);
665 #[stable(feature = "rust1", since = "1.0.0")]
667 pub fn hypot(self, other: f64) -> f64 {
668 unsafe { cmath::hypot(self, other) }
671 /// Computes the sine of a number (in radians).
676 /// let x = f64::consts::PI/2.0;
678 /// let abs_difference = (x.sin() - 1.0).abs();
680 /// assert!(abs_difference < 1e-10);
682 #[stable(feature = "rust1", since = "1.0.0")]
684 pub fn sin(self) -> f64 {
685 unsafe { intrinsics::sinf64(self) }
688 /// Computes the cosine of a number (in radians).
693 /// let x = 2.0*f64::consts::PI;
695 /// let abs_difference = (x.cos() - 1.0).abs();
697 /// assert!(abs_difference < 1e-10);
699 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn cos(self) -> f64 {
702 unsafe { intrinsics::cosf64(self) }
705 /// Computes the tangent of a number (in radians).
710 /// let x = f64::consts::PI/4.0;
711 /// let abs_difference = (x.tan() - 1.0).abs();
713 /// assert!(abs_difference < 1e-14);
715 #[stable(feature = "rust1", since = "1.0.0")]
717 pub fn tan(self) -> f64 {
718 unsafe { cmath::tan(self) }
721 /// Computes the arcsine of a number. Return value is in radians in
722 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
728 /// let f = f64::consts::PI / 2.0;
730 /// // asin(sin(pi/2))
731 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
733 /// assert!(abs_difference < 1e-10);
735 #[stable(feature = "rust1", since = "1.0.0")]
737 pub fn asin(self) -> f64 {
738 unsafe { cmath::asin(self) }
741 /// Computes the arccosine of a number. Return value is in radians in
742 /// the range [0, pi] or NaN if the number is outside the range
748 /// let f = f64::consts::PI / 4.0;
750 /// // acos(cos(pi/4))
751 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
753 /// assert!(abs_difference < 1e-10);
755 #[stable(feature = "rust1", since = "1.0.0")]
757 pub fn acos(self) -> f64 {
758 unsafe { cmath::acos(self) }
761 /// Computes the arctangent of a number. Return value is in radians in the
762 /// range [-pi/2, pi/2];
768 /// let abs_difference = (f.tan().atan() - 1.0).abs();
770 /// assert!(abs_difference < 1e-10);
772 #[stable(feature = "rust1", since = "1.0.0")]
774 pub fn atan(self) -> f64 {
775 unsafe { cmath::atan(self) }
778 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
780 /// * `x = 0`, `y = 0`: `0`
781 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
782 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
783 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
788 /// let pi = f64::consts::PI;
789 /// // All angles from horizontal right (+x)
790 /// // 45 deg counter-clockwise
791 /// let x1 = 3.0_f64;
792 /// let y1 = -3.0_f64;
794 /// // 135 deg clockwise
795 /// let x2 = -3.0_f64;
796 /// let y2 = 3.0_f64;
798 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
799 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
801 /// assert!(abs_difference_1 < 1e-10);
802 /// assert!(abs_difference_2 < 1e-10);
804 #[stable(feature = "rust1", since = "1.0.0")]
806 pub fn atan2(self, other: f64) -> f64 {
807 unsafe { cmath::atan2(self, other) }
810 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
811 /// `(sin(x), cos(x))`.
816 /// let x = f64::consts::PI/4.0;
817 /// let f = x.sin_cos();
819 /// let abs_difference_0 = (f.0 - x.sin()).abs();
820 /// let abs_difference_1 = (f.1 - x.cos()).abs();
822 /// assert!(abs_difference_0 < 1e-10);
823 /// assert!(abs_difference_1 < 1e-10);
825 #[stable(feature = "rust1", since = "1.0.0")]
827 pub fn sin_cos(self) -> (f64, f64) {
828 (self.sin(), self.cos())
831 /// Returns `e^(self) - 1` in a way that is accurate even if the
832 /// number is close to zero.
838 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
840 /// assert!(abs_difference < 1e-10);
842 #[stable(feature = "rust1", since = "1.0.0")]
844 pub fn exp_m1(self) -> f64 {
845 unsafe { cmath::expm1(self) }
848 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
849 /// the operations were performed separately.
854 /// let x = f64::consts::E - 1.0;
856 /// // ln(1 + (e - 1)) == ln(e) == 1
857 /// let abs_difference = (x.ln_1p() - 1.0).abs();
859 /// assert!(abs_difference < 1e-10);
861 #[stable(feature = "rust1", since = "1.0.0")]
863 pub fn ln_1p(self) -> f64 {
864 unsafe { cmath::log1p(self) }
867 /// Hyperbolic sine function.
872 /// let e = f64::consts::E;
875 /// let f = x.sinh();
876 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
877 /// let g = (e*e - 1.0)/(2.0*e);
878 /// let abs_difference = (f - g).abs();
880 /// assert!(abs_difference < 1e-10);
882 #[stable(feature = "rust1", since = "1.0.0")]
884 pub fn sinh(self) -> f64 {
885 unsafe { cmath::sinh(self) }
888 /// Hyperbolic cosine function.
893 /// let e = f64::consts::E;
895 /// let f = x.cosh();
896 /// // Solving cosh() at 1 gives this result
897 /// let g = (e*e + 1.0)/(2.0*e);
898 /// let abs_difference = (f - g).abs();
901 /// assert!(abs_difference < 1.0e-10);
903 #[stable(feature = "rust1", since = "1.0.0")]
905 pub fn cosh(self) -> f64 {
906 unsafe { cmath::cosh(self) }
909 /// Hyperbolic tangent function.
914 /// let e = f64::consts::E;
917 /// let f = x.tanh();
918 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
919 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
920 /// let abs_difference = (f - g).abs();
922 /// assert!(abs_difference < 1.0e-10);
924 #[stable(feature = "rust1", since = "1.0.0")]
926 pub fn tanh(self) -> f64 {
927 unsafe { cmath::tanh(self) }
930 /// Inverse hyperbolic sine function.
934 /// let f = x.sinh().asinh();
936 /// let abs_difference = (f - x).abs();
938 /// assert!(abs_difference < 1.0e-10);
940 #[stable(feature = "rust1", since = "1.0.0")]
942 pub fn asinh(self) -> f64 {
943 if self == NEG_INFINITY {
946 (self + ((self * self) + 1.0).sqrt()).ln()
950 /// Inverse hyperbolic cosine function.
954 /// let f = x.cosh().acosh();
956 /// let abs_difference = (f - x).abs();
958 /// assert!(abs_difference < 1.0e-10);
960 #[stable(feature = "rust1", since = "1.0.0")]
962 pub fn acosh(self) -> f64 {
965 x => (x + ((x * x) - 1.0).sqrt()).ln(),
969 /// Inverse hyperbolic tangent function.
974 /// let e = f64::consts::E;
975 /// let f = e.tanh().atanh();
977 /// let abs_difference = (f - e).abs();
979 /// assert!(abs_difference < 1.0e-10);
981 #[stable(feature = "rust1", since = "1.0.0")]
983 pub fn atanh(self) -> f64 {
984 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
987 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
988 // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
990 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
991 if !cfg!(target_os = "solaris") {
994 if self.is_finite() {
997 } else if self == 0.0 {
998 NEG_INFINITY // log(0) = -Inf
1000 NAN // log(-n) = NaN
1002 } else if self.is_nan() {
1003 self // log(NaN) = NaN
1004 } else if self > 0.0 {
1005 self // log(Inf) = Inf
1007 NAN // log(-Inf) = NaN
1012 /// Raw transmutation to `u64`.
1014 /// Converts the `f64` into its raw memory representation,
1015 /// similar to the `transmute` function.
1017 /// Note that this function is distinct from casting.
1022 /// #![feature(float_bits_conv)]
1023 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
1024 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1027 #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "40470")]
1029 pub fn to_bits(self) -> u64 {
1030 unsafe { ::mem::transmute(self) }
1033 /// Raw transmutation from `u64`.
1035 /// Converts the given `u64` containing the float's raw memory
1036 /// representation into the `f64` type, similar to the
1037 /// `transmute` function.
1039 /// There is only one difference to a bare `transmute`:
1040 /// Due to the implications onto Rust's safety promises being
1041 /// uncertain, if the representation of a signaling NaN "sNaN" float
1042 /// is passed to the function, the implementation is allowed to
1043 /// return a quiet NaN instead.
1045 /// Note that this function is distinct from casting.
1050 /// #![feature(float_bits_conv)]
1052 /// let v = f64::from_bits(0x4029000000000000);
1053 /// let difference = (v - 12.5).abs();
1054 /// assert!(difference <= 1e-5);
1055 /// // Example for a signaling NaN value:
1056 /// let snan = 0x7FF0000000000001;
1057 /// assert_ne!(f64::from_bits(snan).to_bits(), snan);
1059 #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "40470")]
1061 pub fn from_bits(mut v: u64) -> Self {
1062 const EXP_MASK: u64 = 0x7FF0000000000000;
1063 const QNAN_MASK: u64 = 0x0001000000000000;
1064 const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
1065 if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1066 // If we have a NaN value, we
1067 // convert signaling NaN values to quiet NaN
1068 // by setting the the highest bit of the fraction
1071 unsafe { ::mem::transmute(v) }
1080 use num::FpCategory as Fp;
1084 test_num(10f64, 2f64);
1089 assert_eq!(NAN.min(2.0), 2.0);
1090 assert_eq!(2.0f64.min(NAN), 2.0);
1095 assert_eq!(NAN.max(2.0), 2.0);
1096 assert_eq!(2.0f64.max(NAN), 2.0);
1102 assert!(nan.is_nan());
1103 assert!(!nan.is_infinite());
1104 assert!(!nan.is_finite());
1105 assert!(!nan.is_normal());
1106 assert!(!nan.is_sign_positive());
1107 assert!(!nan.is_sign_negative());
1108 assert_eq!(Fp::Nan, nan.classify());
1112 fn test_infinity() {
1113 let inf: f64 = INFINITY;
1114 assert!(inf.is_infinite());
1115 assert!(!inf.is_finite());
1116 assert!(inf.is_sign_positive());
1117 assert!(!inf.is_sign_negative());
1118 assert!(!inf.is_nan());
1119 assert!(!inf.is_normal());
1120 assert_eq!(Fp::Infinite, inf.classify());
1124 fn test_neg_infinity() {
1125 let neg_inf: f64 = NEG_INFINITY;
1126 assert!(neg_inf.is_infinite());
1127 assert!(!neg_inf.is_finite());
1128 assert!(!neg_inf.is_sign_positive());
1129 assert!(neg_inf.is_sign_negative());
1130 assert!(!neg_inf.is_nan());
1131 assert!(!neg_inf.is_normal());
1132 assert_eq!(Fp::Infinite, neg_inf.classify());
1137 let zero: f64 = 0.0f64;
1138 assert_eq!(0.0, zero);
1139 assert!(!zero.is_infinite());
1140 assert!(zero.is_finite());
1141 assert!(zero.is_sign_positive());
1142 assert!(!zero.is_sign_negative());
1143 assert!(!zero.is_nan());
1144 assert!(!zero.is_normal());
1145 assert_eq!(Fp::Zero, zero.classify());
1149 fn test_neg_zero() {
1150 let neg_zero: f64 = -0.0;
1151 assert_eq!(0.0, neg_zero);
1152 assert!(!neg_zero.is_infinite());
1153 assert!(neg_zero.is_finite());
1154 assert!(!neg_zero.is_sign_positive());
1155 assert!(neg_zero.is_sign_negative());
1156 assert!(!neg_zero.is_nan());
1157 assert!(!neg_zero.is_normal());
1158 assert_eq!(Fp::Zero, neg_zero.classify());
1163 let one: f64 = 1.0f64;
1164 assert_eq!(1.0, one);
1165 assert!(!one.is_infinite());
1166 assert!(one.is_finite());
1167 assert!(one.is_sign_positive());
1168 assert!(!one.is_sign_negative());
1169 assert!(!one.is_nan());
1170 assert!(one.is_normal());
1171 assert_eq!(Fp::Normal, one.classify());
1177 let inf: f64 = INFINITY;
1178 let neg_inf: f64 = NEG_INFINITY;
1179 assert!(nan.is_nan());
1180 assert!(!0.0f64.is_nan());
1181 assert!(!5.3f64.is_nan());
1182 assert!(!(-10.732f64).is_nan());
1183 assert!(!inf.is_nan());
1184 assert!(!neg_inf.is_nan());
1188 fn test_is_infinite() {
1190 let inf: f64 = INFINITY;
1191 let neg_inf: f64 = NEG_INFINITY;
1192 assert!(!nan.is_infinite());
1193 assert!(inf.is_infinite());
1194 assert!(neg_inf.is_infinite());
1195 assert!(!0.0f64.is_infinite());
1196 assert!(!42.8f64.is_infinite());
1197 assert!(!(-109.2f64).is_infinite());
1201 fn test_is_finite() {
1203 let inf: f64 = INFINITY;
1204 let neg_inf: f64 = NEG_INFINITY;
1205 assert!(!nan.is_finite());
1206 assert!(!inf.is_finite());
1207 assert!(!neg_inf.is_finite());
1208 assert!(0.0f64.is_finite());
1209 assert!(42.8f64.is_finite());
1210 assert!((-109.2f64).is_finite());
1214 fn test_is_normal() {
1216 let inf: f64 = INFINITY;
1217 let neg_inf: f64 = NEG_INFINITY;
1218 let zero: f64 = 0.0f64;
1219 let neg_zero: f64 = -0.0;
1220 assert!(!nan.is_normal());
1221 assert!(!inf.is_normal());
1222 assert!(!neg_inf.is_normal());
1223 assert!(!zero.is_normal());
1224 assert!(!neg_zero.is_normal());
1225 assert!(1f64.is_normal());
1226 assert!(1e-307f64.is_normal());
1227 assert!(!1e-308f64.is_normal());
1231 fn test_classify() {
1233 let inf: f64 = INFINITY;
1234 let neg_inf: f64 = NEG_INFINITY;
1235 let zero: f64 = 0.0f64;
1236 let neg_zero: f64 = -0.0;
1237 assert_eq!(nan.classify(), Fp::Nan);
1238 assert_eq!(inf.classify(), Fp::Infinite);
1239 assert_eq!(neg_inf.classify(), Fp::Infinite);
1240 assert_eq!(zero.classify(), Fp::Zero);
1241 assert_eq!(neg_zero.classify(), Fp::Zero);
1242 assert_eq!(1e-307f64.classify(), Fp::Normal);
1243 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1248 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1249 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1250 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1251 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1252 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1253 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1254 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1255 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1256 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1257 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1262 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1263 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1264 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1265 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1266 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1267 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1268 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1269 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1270 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1271 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1276 assert_approx_eq!(1.0f64.round(), 1.0f64);
1277 assert_approx_eq!(1.3f64.round(), 1.0f64);
1278 assert_approx_eq!(1.5f64.round(), 2.0f64);
1279 assert_approx_eq!(1.7f64.round(), 2.0f64);
1280 assert_approx_eq!(0.0f64.round(), 0.0f64);
1281 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1282 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1283 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1284 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1285 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1290 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1291 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1292 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1293 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1294 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1295 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1296 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1297 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1298 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1299 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1304 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1305 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1306 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1307 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1308 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1309 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1310 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1311 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1312 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1313 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1318 assert_eq!(INFINITY.abs(), INFINITY);
1319 assert_eq!(1f64.abs(), 1f64);
1320 assert_eq!(0f64.abs(), 0f64);
1321 assert_eq!((-0f64).abs(), 0f64);
1322 assert_eq!((-1f64).abs(), 1f64);
1323 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1324 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1325 assert!(NAN.abs().is_nan());
1330 assert_eq!(INFINITY.signum(), 1f64);
1331 assert_eq!(1f64.signum(), 1f64);
1332 assert_eq!(0f64.signum(), 1f64);
1333 assert_eq!((-0f64).signum(), -1f64);
1334 assert_eq!((-1f64).signum(), -1f64);
1335 assert_eq!(NEG_INFINITY.signum(), -1f64);
1336 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1337 assert!(NAN.signum().is_nan());
1341 fn test_is_sign_positive() {
1342 assert!(INFINITY.is_sign_positive());
1343 assert!(1f64.is_sign_positive());
1344 assert!(0f64.is_sign_positive());
1345 assert!(!(-0f64).is_sign_positive());
1346 assert!(!(-1f64).is_sign_positive());
1347 assert!(!NEG_INFINITY.is_sign_positive());
1348 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1349 assert!(!NAN.is_sign_positive());
1353 fn test_is_sign_negative() {
1354 assert!(!INFINITY.is_sign_negative());
1355 assert!(!1f64.is_sign_negative());
1356 assert!(!0f64.is_sign_negative());
1357 assert!((-0f64).is_sign_negative());
1358 assert!((-1f64).is_sign_negative());
1359 assert!(NEG_INFINITY.is_sign_negative());
1360 assert!((1f64/NEG_INFINITY).is_sign_negative());
1361 assert!(!NAN.is_sign_negative());
1367 let inf: f64 = INFINITY;
1368 let neg_inf: f64 = NEG_INFINITY;
1369 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1370 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1371 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1372 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1373 assert!(nan.mul_add(7.8, 9.0).is_nan());
1374 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1375 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1376 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1377 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1383 let inf: f64 = INFINITY;
1384 let neg_inf: f64 = NEG_INFINITY;
1385 assert_eq!(1.0f64.recip(), 1.0);
1386 assert_eq!(2.0f64.recip(), 0.5);
1387 assert_eq!((-0.4f64).recip(), -2.5);
1388 assert_eq!(0.0f64.recip(), inf);
1389 assert!(nan.recip().is_nan());
1390 assert_eq!(inf.recip(), 0.0);
1391 assert_eq!(neg_inf.recip(), 0.0);
1397 let inf: f64 = INFINITY;
1398 let neg_inf: f64 = NEG_INFINITY;
1399 assert_eq!(1.0f64.powi(1), 1.0);
1400 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1401 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1402 assert_eq!(8.3f64.powi(0), 1.0);
1403 assert!(nan.powi(2).is_nan());
1404 assert_eq!(inf.powi(3), inf);
1405 assert_eq!(neg_inf.powi(2), inf);
1411 let inf: f64 = INFINITY;
1412 let neg_inf: f64 = NEG_INFINITY;
1413 assert_eq!(1.0f64.powf(1.0), 1.0);
1414 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1415 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1416 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1417 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1418 assert_eq!(8.3f64.powf(0.0), 1.0);
1419 assert!(nan.powf(2.0).is_nan());
1420 assert_eq!(inf.powf(2.0), inf);
1421 assert_eq!(neg_inf.powf(3.0), neg_inf);
1425 fn test_sqrt_domain() {
1426 assert!(NAN.sqrt().is_nan());
1427 assert!(NEG_INFINITY.sqrt().is_nan());
1428 assert!((-1.0f64).sqrt().is_nan());
1429 assert_eq!((-0.0f64).sqrt(), -0.0);
1430 assert_eq!(0.0f64.sqrt(), 0.0);
1431 assert_eq!(1.0f64.sqrt(), 1.0);
1432 assert_eq!(INFINITY.sqrt(), INFINITY);
1437 assert_eq!(1.0, 0.0f64.exp());
1438 assert_approx_eq!(2.718282, 1.0f64.exp());
1439 assert_approx_eq!(148.413159, 5.0f64.exp());
1441 let inf: f64 = INFINITY;
1442 let neg_inf: f64 = NEG_INFINITY;
1444 assert_eq!(inf, inf.exp());
1445 assert_eq!(0.0, neg_inf.exp());
1446 assert!(nan.exp().is_nan());
1451 assert_eq!(32.0, 5.0f64.exp2());
1452 assert_eq!(1.0, 0.0f64.exp2());
1454 let inf: f64 = INFINITY;
1455 let neg_inf: f64 = NEG_INFINITY;
1457 assert_eq!(inf, inf.exp2());
1458 assert_eq!(0.0, neg_inf.exp2());
1459 assert!(nan.exp2().is_nan());
1465 let inf: f64 = INFINITY;
1466 let neg_inf: f64 = NEG_INFINITY;
1467 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1468 assert!(nan.ln().is_nan());
1469 assert_eq!(inf.ln(), inf);
1470 assert!(neg_inf.ln().is_nan());
1471 assert!((-2.3f64).ln().is_nan());
1472 assert_eq!((-0.0f64).ln(), neg_inf);
1473 assert_eq!(0.0f64.ln(), neg_inf);
1474 assert_approx_eq!(4.0f64.ln(), 1.386294);
1480 let inf: f64 = INFINITY;
1481 let neg_inf: f64 = NEG_INFINITY;
1482 assert_eq!(10.0f64.log(10.0), 1.0);
1483 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1484 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1485 assert!(1.0f64.log(1.0).is_nan());
1486 assert!(1.0f64.log(-13.9).is_nan());
1487 assert!(nan.log(2.3).is_nan());
1488 assert_eq!(inf.log(10.0), inf);
1489 assert!(neg_inf.log(8.8).is_nan());
1490 assert!((-2.3f64).log(0.1).is_nan());
1491 assert_eq!((-0.0f64).log(2.0), neg_inf);
1492 assert_eq!(0.0f64.log(7.0), neg_inf);
1498 let inf: f64 = INFINITY;
1499 let neg_inf: f64 = NEG_INFINITY;
1500 assert_approx_eq!(10.0f64.log2(), 3.321928);
1501 assert_approx_eq!(2.3f64.log2(), 1.201634);
1502 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1503 assert!(nan.log2().is_nan());
1504 assert_eq!(inf.log2(), inf);
1505 assert!(neg_inf.log2().is_nan());
1506 assert!((-2.3f64).log2().is_nan());
1507 assert_eq!((-0.0f64).log2(), neg_inf);
1508 assert_eq!(0.0f64.log2(), neg_inf);
1514 let inf: f64 = INFINITY;
1515 let neg_inf: f64 = NEG_INFINITY;
1516 assert_eq!(10.0f64.log10(), 1.0);
1517 assert_approx_eq!(2.3f64.log10(), 0.361728);
1518 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1519 assert_eq!(1.0f64.log10(), 0.0);
1520 assert!(nan.log10().is_nan());
1521 assert_eq!(inf.log10(), inf);
1522 assert!(neg_inf.log10().is_nan());
1523 assert!((-2.3f64).log10().is_nan());
1524 assert_eq!((-0.0f64).log10(), neg_inf);
1525 assert_eq!(0.0f64.log10(), neg_inf);
1529 fn test_to_degrees() {
1530 let pi: f64 = consts::PI;
1532 let inf: f64 = INFINITY;
1533 let neg_inf: f64 = NEG_INFINITY;
1534 assert_eq!(0.0f64.to_degrees(), 0.0);
1535 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1536 assert_eq!(pi.to_degrees(), 180.0);
1537 assert!(nan.to_degrees().is_nan());
1538 assert_eq!(inf.to_degrees(), inf);
1539 assert_eq!(neg_inf.to_degrees(), neg_inf);
1543 fn test_to_radians() {
1544 let pi: f64 = consts::PI;
1546 let inf: f64 = INFINITY;
1547 let neg_inf: f64 = NEG_INFINITY;
1548 assert_eq!(0.0f64.to_radians(), 0.0);
1549 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1550 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1551 assert_eq!(180.0f64.to_radians(), pi);
1552 assert!(nan.to_radians().is_nan());
1553 assert_eq!(inf.to_radians(), inf);
1554 assert_eq!(neg_inf.to_radians(), neg_inf);
1559 assert_eq!(0.0f64.asinh(), 0.0f64);
1560 assert_eq!((-0.0f64).asinh(), -0.0f64);
1562 let inf: f64 = INFINITY;
1563 let neg_inf: f64 = NEG_INFINITY;
1565 assert_eq!(inf.asinh(), inf);
1566 assert_eq!(neg_inf.asinh(), neg_inf);
1567 assert!(nan.asinh().is_nan());
1568 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1569 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1574 assert_eq!(1.0f64.acosh(), 0.0f64);
1575 assert!(0.999f64.acosh().is_nan());
1577 let inf: f64 = INFINITY;
1578 let neg_inf: f64 = NEG_INFINITY;
1580 assert_eq!(inf.acosh(), inf);
1581 assert!(neg_inf.acosh().is_nan());
1582 assert!(nan.acosh().is_nan());
1583 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1584 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1589 assert_eq!(0.0f64.atanh(), 0.0f64);
1590 assert_eq!((-0.0f64).atanh(), -0.0f64);
1592 let inf: f64 = INFINITY;
1593 let neg_inf: f64 = NEG_INFINITY;
1595 assert_eq!(1.0f64.atanh(), inf);
1596 assert_eq!((-1.0f64).atanh(), neg_inf);
1597 assert!(2f64.atanh().atanh().is_nan());
1598 assert!((-2f64).atanh().atanh().is_nan());
1599 assert!(inf.atanh().is_nan());
1600 assert!(neg_inf.atanh().is_nan());
1601 assert!(nan.atanh().is_nan());
1602 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1603 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1607 fn test_real_consts() {
1609 let pi: f64 = consts::PI;
1610 let frac_pi_2: f64 = consts::FRAC_PI_2;
1611 let frac_pi_3: f64 = consts::FRAC_PI_3;
1612 let frac_pi_4: f64 = consts::FRAC_PI_4;
1613 let frac_pi_6: f64 = consts::FRAC_PI_6;
1614 let frac_pi_8: f64 = consts::FRAC_PI_8;
1615 let frac_1_pi: f64 = consts::FRAC_1_PI;
1616 let frac_2_pi: f64 = consts::FRAC_2_PI;
1617 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1618 let sqrt2: f64 = consts::SQRT_2;
1619 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1620 let e: f64 = consts::E;
1621 let log2_e: f64 = consts::LOG2_E;
1622 let log10_e: f64 = consts::LOG10_E;
1623 let ln_2: f64 = consts::LN_2;
1624 let ln_10: f64 = consts::LN_10;
1626 assert_approx_eq!(frac_pi_2, pi / 2f64);
1627 assert_approx_eq!(frac_pi_3, pi / 3f64);
1628 assert_approx_eq!(frac_pi_4, pi / 4f64);
1629 assert_approx_eq!(frac_pi_6, pi / 6f64);
1630 assert_approx_eq!(frac_pi_8, pi / 8f64);
1631 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1632 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1633 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1634 assert_approx_eq!(sqrt2, 2f64.sqrt());
1635 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1636 assert_approx_eq!(log2_e, e.log2());
1637 assert_approx_eq!(log10_e, e.log10());
1638 assert_approx_eq!(ln_2, 2f64.ln());
1639 assert_approx_eq!(ln_10, 10f64.ln());
1643 fn test_float_bits_conv() {
1644 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1645 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1646 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1647 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1648 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1649 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1650 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1651 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);