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 fmod(a: c_double, b: c_double) -> c_double;
55 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
56 pub fn ilogb(n: c_double) -> c_int;
57 pub fn ldexp(x: c_double, n: c_int) -> c_double;
58 pub fn logb(n: c_double) -> c_double;
59 pub fn log1p(n: c_double) -> c_double;
60 pub fn nextafter(x: c_double, y: c_double) -> c_double;
61 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
62 pub fn sinh(n: c_double) -> c_double;
63 pub fn tan(n: c_double) -> c_double;
64 pub fn tanh(n: c_double) -> c_double;
65 pub fn tgamma(n: c_double) -> c_double;
67 // These are commonly only available for doubles
69 pub fn j0(n: c_double) -> c_double;
70 pub fn j1(n: c_double) -> c_double;
71 pub fn jn(i: c_int, n: c_double) -> c_double;
73 pub fn y0(n: c_double) -> c_double;
74 pub fn y1(n: c_double) -> c_double;
75 pub fn yn(i: c_int, n: c_double) -> c_double;
77 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
78 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
80 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
81 pub fn hypot(x: c_double, y: c_double) -> c_double;
88 /// Returns `true` if this value is `NaN` and false otherwise.
93 /// let nan = f64::NAN;
96 /// assert!(nan.is_nan());
97 /// assert!(!f.is_nan());
99 #[stable(feature = "rust1", since = "1.0.0")]
101 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
103 /// Returns `true` if this value is positive infinity or negative infinity and
110 /// let inf = f64::INFINITY;
111 /// let neg_inf = f64::NEG_INFINITY;
112 /// let nan = f64::NAN;
114 /// assert!(!f.is_infinite());
115 /// assert!(!nan.is_infinite());
117 /// assert!(inf.is_infinite());
118 /// assert!(neg_inf.is_infinite());
120 #[stable(feature = "rust1", since = "1.0.0")]
122 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
124 /// Returns `true` if this number is neither infinite nor `NaN`.
130 /// let inf: f64 = f64::INFINITY;
131 /// let neg_inf: f64 = f64::NEG_INFINITY;
132 /// let nan: f64 = f64::NAN;
134 /// assert!(f.is_finite());
136 /// assert!(!nan.is_finite());
137 /// assert!(!inf.is_finite());
138 /// assert!(!neg_inf.is_finite());
140 #[stable(feature = "rust1", since = "1.0.0")]
142 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
144 /// Returns `true` if the number is neither zero, infinite,
145 /// [subnormal][subnormal], or `NaN`.
150 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
151 /// let max = f64::MAX;
152 /// let lower_than_min = 1.0e-308_f64;
153 /// let zero = 0.0f64;
155 /// assert!(min.is_normal());
156 /// assert!(max.is_normal());
158 /// assert!(!zero.is_normal());
159 /// assert!(!f64::NAN.is_normal());
160 /// assert!(!f64::INFINITY.is_normal());
161 /// // Values between `0` and `min` are Subnormal.
162 /// assert!(!lower_than_min.is_normal());
164 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
165 #[stable(feature = "rust1", since = "1.0.0")]
167 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
169 /// Returns the floating point category of the number. If only one property
170 /// is going to be tested, it is generally faster to use the specific
171 /// predicate instead.
174 /// use std::num::FpCategory;
177 /// let num = 12.4_f64;
178 /// let inf = f64::INFINITY;
180 /// assert_eq!(num.classify(), FpCategory::Normal);
181 /// assert_eq!(inf.classify(), FpCategory::Infinite);
183 #[stable(feature = "rust1", since = "1.0.0")]
185 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
187 /// Returns the largest integer less than or equal to a number.
190 /// let f = 3.99_f64;
193 /// assert_eq!(f.floor(), 3.0);
194 /// assert_eq!(g.floor(), 3.0);
196 #[stable(feature = "rust1", since = "1.0.0")]
198 pub fn floor(self) -> f64 {
199 unsafe { intrinsics::floorf64(self) }
202 /// Returns the smallest integer greater than or equal to a number.
205 /// let f = 3.01_f64;
208 /// assert_eq!(f.ceil(), 4.0);
209 /// assert_eq!(g.ceil(), 4.0);
211 #[stable(feature = "rust1", since = "1.0.0")]
213 pub fn ceil(self) -> f64 {
214 unsafe { intrinsics::ceilf64(self) }
217 /// Returns the nearest integer to a number. Round half-way cases away from
222 /// let g = -3.3_f64;
224 /// assert_eq!(f.round(), 3.0);
225 /// assert_eq!(g.round(), -3.0);
227 #[stable(feature = "rust1", since = "1.0.0")]
229 pub fn round(self) -> f64 {
230 unsafe { intrinsics::roundf64(self) }
233 /// Returns the integer part of a number.
237 /// let g = -3.7_f64;
239 /// assert_eq!(f.trunc(), 3.0);
240 /// assert_eq!(g.trunc(), -3.0);
242 #[stable(feature = "rust1", since = "1.0.0")]
244 pub fn trunc(self) -> f64 {
245 unsafe { intrinsics::truncf64(self) }
248 /// Returns the fractional part of a number.
252 /// let y = -3.5_f64;
253 /// let abs_difference_x = (x.fract() - 0.5).abs();
254 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
256 /// assert!(abs_difference_x < 1e-10);
257 /// assert!(abs_difference_y < 1e-10);
259 #[stable(feature = "rust1", since = "1.0.0")]
261 pub fn fract(self) -> f64 { self - self.trunc() }
263 /// Computes the absolute value of `self`. Returns `NAN` if the
270 /// let y = -3.5_f64;
272 /// let abs_difference_x = (x.abs() - x).abs();
273 /// let abs_difference_y = (y.abs() - (-y)).abs();
275 /// assert!(abs_difference_x < 1e-10);
276 /// assert!(abs_difference_y < 1e-10);
278 /// assert!(f64::NAN.abs().is_nan());
280 #[stable(feature = "rust1", since = "1.0.0")]
282 pub fn abs(self) -> f64 { num::Float::abs(self) }
284 /// Returns a number that represents the sign of `self`.
286 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
287 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
288 /// - `NAN` if the number is `NAN`
295 /// assert_eq!(f.signum(), 1.0);
296 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
298 /// assert!(f64::NAN.signum().is_nan());
300 #[stable(feature = "rust1", since = "1.0.0")]
302 pub fn signum(self) -> f64 { num::Float::signum(self) }
304 /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
305 /// positive sign bit and positive infinity.
309 /// let g = -7.0_f64;
311 /// assert!(f.is_sign_positive());
312 /// assert!(!g.is_sign_positive());
314 #[stable(feature = "rust1", since = "1.0.0")]
316 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
318 #[stable(feature = "rust1", since = "1.0.0")]
319 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
321 pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
323 /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
324 /// negative sign bit and negative infinity.
328 /// let g = -7.0_f64;
330 /// assert!(!f.is_sign_negative());
331 /// assert!(g.is_sign_negative());
333 #[stable(feature = "rust1", since = "1.0.0")]
335 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
337 #[stable(feature = "rust1", since = "1.0.0")]
338 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
340 pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
342 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
343 /// error. This produces a more accurate result with better performance than
344 /// a separate multiplication operation followed by an add.
347 /// let m = 10.0_f64;
349 /// let b = 60.0_f64;
352 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
354 /// assert!(abs_difference < 1e-10);
356 #[stable(feature = "rust1", since = "1.0.0")]
358 pub fn mul_add(self, a: f64, b: f64) -> f64 {
359 unsafe { intrinsics::fmaf64(self, a, b) }
362 /// Takes the reciprocal (inverse) of a number, `1/x`.
366 /// let abs_difference = (x.recip() - (1.0/x)).abs();
368 /// assert!(abs_difference < 1e-10);
370 #[stable(feature = "rust1", since = "1.0.0")]
372 pub fn recip(self) -> f64 { num::Float::recip(self) }
374 /// Raises a number to an integer power.
376 /// Using this function is generally faster than using `powf`
380 /// let abs_difference = (x.powi(2) - x*x).abs();
382 /// assert!(abs_difference < 1e-10);
384 #[stable(feature = "rust1", since = "1.0.0")]
386 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
388 /// Raises a number to a floating point power.
392 /// let abs_difference = (x.powf(2.0) - x*x).abs();
394 /// assert!(abs_difference < 1e-10);
396 #[stable(feature = "rust1", since = "1.0.0")]
398 pub fn powf(self, n: f64) -> f64 {
399 unsafe { intrinsics::powf64(self, n) }
402 /// Takes the square root of a number.
404 /// Returns NaN if `self` is a negative number.
407 /// let positive = 4.0_f64;
408 /// let negative = -4.0_f64;
410 /// let abs_difference = (positive.sqrt() - 2.0).abs();
412 /// assert!(abs_difference < 1e-10);
413 /// assert!(negative.sqrt().is_nan());
415 #[stable(feature = "rust1", since = "1.0.0")]
417 pub fn sqrt(self) -> f64 {
421 unsafe { intrinsics::sqrtf64(self) }
425 /// Returns `e^(self)`, (the exponential function).
428 /// let one = 1.0_f64;
430 /// let e = one.exp();
432 /// // ln(e) - 1 == 0
433 /// let abs_difference = (e.ln() - 1.0).abs();
435 /// assert!(abs_difference < 1e-10);
437 #[stable(feature = "rust1", since = "1.0.0")]
439 pub fn exp(self) -> f64 {
440 unsafe { intrinsics::expf64(self) }
443 /// Returns `2^(self)`.
449 /// let abs_difference = (f.exp2() - 4.0).abs();
451 /// assert!(abs_difference < 1e-10);
453 #[stable(feature = "rust1", since = "1.0.0")]
455 pub fn exp2(self) -> f64 {
456 unsafe { intrinsics::exp2f64(self) }
459 /// Returns the natural logarithm of the number.
462 /// let one = 1.0_f64;
464 /// let e = one.exp();
466 /// // ln(e) - 1 == 0
467 /// let abs_difference = (e.ln() - 1.0).abs();
469 /// assert!(abs_difference < 1e-10);
471 #[stable(feature = "rust1", since = "1.0.0")]
473 pub fn ln(self) -> f64 {
474 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
477 /// Returns the logarithm of the number with respect to an arbitrary base.
480 /// let ten = 10.0_f64;
481 /// let two = 2.0_f64;
483 /// // log10(10) - 1 == 0
484 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
486 /// // log2(2) - 1 == 0
487 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
489 /// assert!(abs_difference_10 < 1e-10);
490 /// assert!(abs_difference_2 < 1e-10);
492 #[stable(feature = "rust1", since = "1.0.0")]
494 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
496 /// Returns the base 2 logarithm of the number.
499 /// let two = 2.0_f64;
501 /// // log2(2) - 1 == 0
502 /// let abs_difference = (two.log2() - 1.0).abs();
504 /// assert!(abs_difference < 1e-10);
506 #[stable(feature = "rust1", since = "1.0.0")]
508 pub fn log2(self) -> f64 {
509 self.log_wrapper(|n| {
510 #[cfg(target_os = "android")]
511 return ::sys::android::log2f64(n);
512 #[cfg(not(target_os = "android"))]
513 return unsafe { intrinsics::log2f64(n) };
517 /// Returns the base 10 logarithm of the number.
520 /// let ten = 10.0_f64;
522 /// // log10(10) - 1 == 0
523 /// let abs_difference = (ten.log10() - 1.0).abs();
525 /// assert!(abs_difference < 1e-10);
527 #[stable(feature = "rust1", since = "1.0.0")]
529 pub fn log10(self) -> f64 {
530 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
533 /// Converts radians to degrees.
536 /// use std::f64::consts;
538 /// let angle = consts::PI;
540 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
542 /// assert!(abs_difference < 1e-10);
544 #[stable(feature = "rust1", since = "1.0.0")]
546 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
548 /// Converts degrees to radians.
551 /// use std::f64::consts;
553 /// let angle = 180.0_f64;
555 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
557 /// assert!(abs_difference < 1e-10);
559 #[stable(feature = "rust1", since = "1.0.0")]
561 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
563 /// Returns the maximum of the two numbers.
569 /// assert_eq!(x.max(y), y);
572 /// If one of the arguments is NaN, then the other argument is returned.
573 #[stable(feature = "rust1", since = "1.0.0")]
575 pub fn max(self, other: f64) -> f64 {
576 num::Float::max(self, other)
579 /// Returns the minimum of the two numbers.
585 /// assert_eq!(x.min(y), x);
588 /// If one of the arguments is NaN, then the other argument is returned.
589 #[stable(feature = "rust1", since = "1.0.0")]
591 pub fn min(self, other: f64) -> f64 {
592 num::Float::min(self, other)
595 /// The positive difference of two numbers.
597 /// * If `self <= other`: `0:0`
598 /// * Else: `self - other`
602 /// let y = -3.0_f64;
604 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
605 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
607 /// assert!(abs_difference_x < 1e-10);
608 /// assert!(abs_difference_y < 1e-10);
610 #[stable(feature = "rust1", since = "1.0.0")]
612 #[rustc_deprecated(since = "1.10.0",
613 reason = "you probably meant `(self - other).abs()`: \
614 this operation is `(self - other).max(0.0)` (also \
615 known as `fdim` in C). If you truly need the positive \
616 difference, consider using that expression or the C function \
617 `fdim`, depending on how you wish to handle NaN (please consider \
618 filing an issue describing your use-case too).")]
619 pub fn abs_sub(self, other: f64) -> f64 {
620 unsafe { cmath::fdim(self, other) }
623 /// Takes the cubic root of a number.
628 /// // x^(1/3) - 2 == 0
629 /// let abs_difference = (x.cbrt() - 2.0).abs();
631 /// assert!(abs_difference < 1e-10);
633 #[stable(feature = "rust1", since = "1.0.0")]
635 pub fn cbrt(self) -> f64 {
636 unsafe { cmath::cbrt(self) }
639 /// Calculates the length of the hypotenuse of a right-angle triangle given
640 /// legs of length `x` and `y`.
646 /// // sqrt(x^2 + y^2)
647 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
649 /// assert!(abs_difference < 1e-10);
651 #[stable(feature = "rust1", since = "1.0.0")]
653 pub fn hypot(self, other: f64) -> f64 {
654 unsafe { cmath::hypot(self, other) }
657 /// Computes the sine of a number (in radians).
662 /// let x = f64::consts::PI/2.0;
664 /// let abs_difference = (x.sin() - 1.0).abs();
666 /// assert!(abs_difference < 1e-10);
668 #[stable(feature = "rust1", since = "1.0.0")]
670 pub fn sin(self) -> f64 {
671 unsafe { intrinsics::sinf64(self) }
674 /// Computes the cosine of a number (in radians).
679 /// let x = 2.0*f64::consts::PI;
681 /// let abs_difference = (x.cos() - 1.0).abs();
683 /// assert!(abs_difference < 1e-10);
685 #[stable(feature = "rust1", since = "1.0.0")]
687 pub fn cos(self) -> f64 {
688 unsafe { intrinsics::cosf64(self) }
691 /// Computes the tangent of a number (in radians).
696 /// let x = f64::consts::PI/4.0;
697 /// let abs_difference = (x.tan() - 1.0).abs();
699 /// assert!(abs_difference < 1e-14);
701 #[stable(feature = "rust1", since = "1.0.0")]
703 pub fn tan(self) -> f64 {
704 unsafe { cmath::tan(self) }
707 /// Computes the arcsine of a number. Return value is in radians in
708 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
714 /// let f = f64::consts::PI / 2.0;
716 /// // asin(sin(pi/2))
717 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
719 /// assert!(abs_difference < 1e-10);
721 #[stable(feature = "rust1", since = "1.0.0")]
723 pub fn asin(self) -> f64 {
724 unsafe { cmath::asin(self) }
727 /// Computes the arccosine of a number. Return value is in radians in
728 /// the range [0, pi] or NaN if the number is outside the range
734 /// let f = f64::consts::PI / 4.0;
736 /// // acos(cos(pi/4))
737 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
739 /// assert!(abs_difference < 1e-10);
741 #[stable(feature = "rust1", since = "1.0.0")]
743 pub fn acos(self) -> f64 {
744 unsafe { cmath::acos(self) }
747 /// Computes the arctangent of a number. Return value is in radians in the
748 /// range [-pi/2, pi/2];
754 /// let abs_difference = (f.tan().atan() - 1.0).abs();
756 /// assert!(abs_difference < 1e-10);
758 #[stable(feature = "rust1", since = "1.0.0")]
760 pub fn atan(self) -> f64 {
761 unsafe { cmath::atan(self) }
764 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
766 /// * `x = 0`, `y = 0`: `0`
767 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
768 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
769 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
774 /// let pi = f64::consts::PI;
775 /// // All angles from horizontal right (+x)
776 /// // 45 deg counter-clockwise
777 /// let x1 = 3.0_f64;
778 /// let y1 = -3.0_f64;
780 /// // 135 deg clockwise
781 /// let x2 = -3.0_f64;
782 /// let y2 = 3.0_f64;
784 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
785 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
787 /// assert!(abs_difference_1 < 1e-10);
788 /// assert!(abs_difference_2 < 1e-10);
790 #[stable(feature = "rust1", since = "1.0.0")]
792 pub fn atan2(self, other: f64) -> f64 {
793 unsafe { cmath::atan2(self, other) }
796 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
797 /// `(sin(x), cos(x))`.
802 /// let x = f64::consts::PI/4.0;
803 /// let f = x.sin_cos();
805 /// let abs_difference_0 = (f.0 - x.sin()).abs();
806 /// let abs_difference_1 = (f.1 - x.cos()).abs();
808 /// assert!(abs_difference_0 < 1e-10);
809 /// assert!(abs_difference_1 < 1e-10);
811 #[stable(feature = "rust1", since = "1.0.0")]
813 pub fn sin_cos(self) -> (f64, f64) {
814 (self.sin(), self.cos())
817 /// Returns `e^(self) - 1` in a way that is accurate even if the
818 /// number is close to zero.
824 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
826 /// assert!(abs_difference < 1e-10);
828 #[stable(feature = "rust1", since = "1.0.0")]
830 pub fn exp_m1(self) -> f64 {
831 unsafe { cmath::expm1(self) }
834 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
835 /// the operations were performed separately.
840 /// let x = f64::consts::E - 1.0;
842 /// // ln(1 + (e - 1)) == ln(e) == 1
843 /// let abs_difference = (x.ln_1p() - 1.0).abs();
845 /// assert!(abs_difference < 1e-10);
847 #[stable(feature = "rust1", since = "1.0.0")]
849 pub fn ln_1p(self) -> f64 {
850 unsafe { cmath::log1p(self) }
853 /// Hyperbolic sine function.
858 /// let e = f64::consts::E;
861 /// let f = x.sinh();
862 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
863 /// let g = (e*e - 1.0)/(2.0*e);
864 /// let abs_difference = (f - g).abs();
866 /// assert!(abs_difference < 1e-10);
868 #[stable(feature = "rust1", since = "1.0.0")]
870 pub fn sinh(self) -> f64 {
871 unsafe { cmath::sinh(self) }
874 /// Hyperbolic cosine function.
879 /// let e = f64::consts::E;
881 /// let f = x.cosh();
882 /// // Solving cosh() at 1 gives this result
883 /// let g = (e*e + 1.0)/(2.0*e);
884 /// let abs_difference = (f - g).abs();
887 /// assert!(abs_difference < 1.0e-10);
889 #[stable(feature = "rust1", since = "1.0.0")]
891 pub fn cosh(self) -> f64 {
892 unsafe { cmath::cosh(self) }
895 /// Hyperbolic tangent function.
900 /// let e = f64::consts::E;
903 /// let f = x.tanh();
904 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
905 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
906 /// let abs_difference = (f - g).abs();
908 /// assert!(abs_difference < 1.0e-10);
910 #[stable(feature = "rust1", since = "1.0.0")]
912 pub fn tanh(self) -> f64 {
913 unsafe { cmath::tanh(self) }
916 /// Inverse hyperbolic sine function.
920 /// let f = x.sinh().asinh();
922 /// let abs_difference = (f - x).abs();
924 /// assert!(abs_difference < 1.0e-10);
926 #[stable(feature = "rust1", since = "1.0.0")]
928 pub fn asinh(self) -> f64 {
929 if self == NEG_INFINITY {
932 (self + ((self * self) + 1.0).sqrt()).ln()
936 /// Inverse hyperbolic cosine function.
940 /// let f = x.cosh().acosh();
942 /// let abs_difference = (f - x).abs();
944 /// assert!(abs_difference < 1.0e-10);
946 #[stable(feature = "rust1", since = "1.0.0")]
948 pub fn acosh(self) -> f64 {
951 x => (x + ((x * x) - 1.0).sqrt()).ln(),
955 /// Inverse hyperbolic tangent function.
960 /// let e = f64::consts::E;
961 /// let f = e.tanh().atanh();
963 /// let abs_difference = (f - e).abs();
965 /// assert!(abs_difference < 1.0e-10);
967 #[stable(feature = "rust1", since = "1.0.0")]
969 pub fn atanh(self) -> f64 {
970 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
973 /// Returns max if self is greater than max, and min if self is less than min.
974 /// Otherwise this returns self. Panics if min > max, min is NaN, or max is NaN.
979 /// #![feature(clamp)]
980 /// use std::f64::NAN;
981 /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
982 /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
983 /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
984 /// assert!((NAN).clamp(-2.0f64, 1.0f64).is_nan());
986 #[unstable(feature = "clamp", issue = "44095")]
988 pub fn clamp(self, min: f64, max: f64) -> f64 {
991 if x < min { x = min; }
992 if x > max { x = max; }
996 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
997 // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
999 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
1000 if !cfg!(target_os = "solaris") {
1003 if self.is_finite() {
1006 } else if self == 0.0 {
1007 NEG_INFINITY // log(0) = -Inf
1009 NAN // log(-n) = NaN
1011 } else if self.is_nan() {
1012 self // log(NaN) = NaN
1013 } else if self > 0.0 {
1014 self // log(Inf) = Inf
1016 NAN // log(-Inf) = NaN
1021 /// Raw transmutation to `u64`.
1023 /// Converts the `f64` into its raw memory representation,
1024 /// similar to the `transmute` function.
1026 /// Note that this function is distinct from casting.
1031 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
1032 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1035 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1037 pub fn to_bits(self) -> u64 {
1038 unsafe { ::mem::transmute(self) }
1041 /// Raw transmutation from `u64`.
1043 /// Converts the given `u64` containing the float's raw memory
1044 /// representation into the `f64` type, similar to the
1045 /// `transmute` function.
1047 /// There is only one difference to a bare `transmute`:
1048 /// Due to the implications onto Rust's safety promises being
1049 /// uncertain, if the representation of a signaling NaN "sNaN" float
1050 /// is passed to the function, the implementation is allowed to
1051 /// return a quiet NaN instead.
1053 /// Note that this function is distinct from casting.
1059 /// let v = f64::from_bits(0x4029000000000000);
1060 /// let difference = (v - 12.5).abs();
1061 /// assert!(difference <= 1e-5);
1062 /// // Example for a signaling NaN value:
1063 /// let snan = 0x7FF0000000000001;
1064 /// assert_ne!(f64::from_bits(snan).to_bits(), snan);
1066 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1068 pub fn from_bits(mut v: u64) -> Self {
1069 const EXP_MASK: u64 = 0x7FF0000000000000;
1070 const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
1071 if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1072 // While IEEE 754-2008 specifies encodings for quiet NaNs
1073 // and signaling ones, certain MIPS and PA-RISC
1074 // CPUs treat signaling NaNs differently.
1075 // Therefore to be safe, we pass a known quiet NaN
1076 // if v is any kind of NaN.
1077 // The check above only assumes IEEE 754-1985 to be
1079 v = unsafe { ::mem::transmute(NAN) };
1081 unsafe { ::mem::transmute(v) }
1090 use num::FpCategory as Fp;
1094 test_num(10f64, 2f64);
1099 assert_eq!(NAN.min(2.0), 2.0);
1100 assert_eq!(2.0f64.min(NAN), 2.0);
1105 assert_eq!(NAN.max(2.0), 2.0);
1106 assert_eq!(2.0f64.max(NAN), 2.0);
1112 assert!(nan.is_nan());
1113 assert!(!nan.is_infinite());
1114 assert!(!nan.is_finite());
1115 assert!(!nan.is_normal());
1116 assert!(nan.is_sign_positive());
1117 assert!(!nan.is_sign_negative());
1118 assert_eq!(Fp::Nan, nan.classify());
1122 fn test_infinity() {
1123 let inf: f64 = INFINITY;
1124 assert!(inf.is_infinite());
1125 assert!(!inf.is_finite());
1126 assert!(inf.is_sign_positive());
1127 assert!(!inf.is_sign_negative());
1128 assert!(!inf.is_nan());
1129 assert!(!inf.is_normal());
1130 assert_eq!(Fp::Infinite, inf.classify());
1134 fn test_neg_infinity() {
1135 let neg_inf: f64 = NEG_INFINITY;
1136 assert!(neg_inf.is_infinite());
1137 assert!(!neg_inf.is_finite());
1138 assert!(!neg_inf.is_sign_positive());
1139 assert!(neg_inf.is_sign_negative());
1140 assert!(!neg_inf.is_nan());
1141 assert!(!neg_inf.is_normal());
1142 assert_eq!(Fp::Infinite, neg_inf.classify());
1147 let zero: f64 = 0.0f64;
1148 assert_eq!(0.0, zero);
1149 assert!(!zero.is_infinite());
1150 assert!(zero.is_finite());
1151 assert!(zero.is_sign_positive());
1152 assert!(!zero.is_sign_negative());
1153 assert!(!zero.is_nan());
1154 assert!(!zero.is_normal());
1155 assert_eq!(Fp::Zero, zero.classify());
1159 fn test_neg_zero() {
1160 let neg_zero: f64 = -0.0;
1161 assert_eq!(0.0, neg_zero);
1162 assert!(!neg_zero.is_infinite());
1163 assert!(neg_zero.is_finite());
1164 assert!(!neg_zero.is_sign_positive());
1165 assert!(neg_zero.is_sign_negative());
1166 assert!(!neg_zero.is_nan());
1167 assert!(!neg_zero.is_normal());
1168 assert_eq!(Fp::Zero, neg_zero.classify());
1171 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1174 let one: f64 = 1.0f64;
1175 assert_eq!(1.0, one);
1176 assert!(!one.is_infinite());
1177 assert!(one.is_finite());
1178 assert!(one.is_sign_positive());
1179 assert!(!one.is_sign_negative());
1180 assert!(!one.is_nan());
1181 assert!(one.is_normal());
1182 assert_eq!(Fp::Normal, one.classify());
1188 let inf: f64 = INFINITY;
1189 let neg_inf: f64 = NEG_INFINITY;
1190 assert!(nan.is_nan());
1191 assert!(!0.0f64.is_nan());
1192 assert!(!5.3f64.is_nan());
1193 assert!(!(-10.732f64).is_nan());
1194 assert!(!inf.is_nan());
1195 assert!(!neg_inf.is_nan());
1199 fn test_is_infinite() {
1201 let inf: f64 = INFINITY;
1202 let neg_inf: f64 = NEG_INFINITY;
1203 assert!(!nan.is_infinite());
1204 assert!(inf.is_infinite());
1205 assert!(neg_inf.is_infinite());
1206 assert!(!0.0f64.is_infinite());
1207 assert!(!42.8f64.is_infinite());
1208 assert!(!(-109.2f64).is_infinite());
1212 fn test_is_finite() {
1214 let inf: f64 = INFINITY;
1215 let neg_inf: f64 = NEG_INFINITY;
1216 assert!(!nan.is_finite());
1217 assert!(!inf.is_finite());
1218 assert!(!neg_inf.is_finite());
1219 assert!(0.0f64.is_finite());
1220 assert!(42.8f64.is_finite());
1221 assert!((-109.2f64).is_finite());
1224 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1226 fn test_is_normal() {
1228 let inf: f64 = INFINITY;
1229 let neg_inf: f64 = NEG_INFINITY;
1230 let zero: f64 = 0.0f64;
1231 let neg_zero: f64 = -0.0;
1232 assert!(!nan.is_normal());
1233 assert!(!inf.is_normal());
1234 assert!(!neg_inf.is_normal());
1235 assert!(!zero.is_normal());
1236 assert!(!neg_zero.is_normal());
1237 assert!(1f64.is_normal());
1238 assert!(1e-307f64.is_normal());
1239 assert!(!1e-308f64.is_normal());
1242 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1244 fn test_classify() {
1246 let inf: f64 = INFINITY;
1247 let neg_inf: f64 = NEG_INFINITY;
1248 let zero: f64 = 0.0f64;
1249 let neg_zero: f64 = -0.0;
1250 assert_eq!(nan.classify(), Fp::Nan);
1251 assert_eq!(inf.classify(), Fp::Infinite);
1252 assert_eq!(neg_inf.classify(), Fp::Infinite);
1253 assert_eq!(zero.classify(), Fp::Zero);
1254 assert_eq!(neg_zero.classify(), Fp::Zero);
1255 assert_eq!(1e-307f64.classify(), Fp::Normal);
1256 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1261 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1262 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1263 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1264 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1265 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1266 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1267 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1268 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1269 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1270 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1275 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1276 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1277 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1278 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1279 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1280 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1281 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1282 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1283 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1284 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1289 assert_approx_eq!(1.0f64.round(), 1.0f64);
1290 assert_approx_eq!(1.3f64.round(), 1.0f64);
1291 assert_approx_eq!(1.5f64.round(), 2.0f64);
1292 assert_approx_eq!(1.7f64.round(), 2.0f64);
1293 assert_approx_eq!(0.0f64.round(), 0.0f64);
1294 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1295 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1296 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1297 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1298 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1303 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1304 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1305 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1306 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1307 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1308 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1309 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1310 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1311 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1312 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1317 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1318 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1319 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1320 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1321 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1322 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1323 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1324 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1325 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1326 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1331 assert_eq!(INFINITY.abs(), INFINITY);
1332 assert_eq!(1f64.abs(), 1f64);
1333 assert_eq!(0f64.abs(), 0f64);
1334 assert_eq!((-0f64).abs(), 0f64);
1335 assert_eq!((-1f64).abs(), 1f64);
1336 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1337 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1338 assert!(NAN.abs().is_nan());
1343 assert_eq!(INFINITY.signum(), 1f64);
1344 assert_eq!(1f64.signum(), 1f64);
1345 assert_eq!(0f64.signum(), 1f64);
1346 assert_eq!((-0f64).signum(), -1f64);
1347 assert_eq!((-1f64).signum(), -1f64);
1348 assert_eq!(NEG_INFINITY.signum(), -1f64);
1349 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1350 assert!(NAN.signum().is_nan());
1354 fn test_is_sign_positive() {
1355 assert!(INFINITY.is_sign_positive());
1356 assert!(1f64.is_sign_positive());
1357 assert!(0f64.is_sign_positive());
1358 assert!(!(-0f64).is_sign_positive());
1359 assert!(!(-1f64).is_sign_positive());
1360 assert!(!NEG_INFINITY.is_sign_positive());
1361 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1362 assert!(NAN.is_sign_positive());
1363 assert!(!(-NAN).is_sign_positive());
1367 fn test_is_sign_negative() {
1368 assert!(!INFINITY.is_sign_negative());
1369 assert!(!1f64.is_sign_negative());
1370 assert!(!0f64.is_sign_negative());
1371 assert!((-0f64).is_sign_negative());
1372 assert!((-1f64).is_sign_negative());
1373 assert!(NEG_INFINITY.is_sign_negative());
1374 assert!((1f64/NEG_INFINITY).is_sign_negative());
1375 assert!(!NAN.is_sign_negative());
1376 assert!((-NAN).is_sign_negative());
1382 let inf: f64 = INFINITY;
1383 let neg_inf: f64 = NEG_INFINITY;
1384 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1385 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1386 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1387 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1388 assert!(nan.mul_add(7.8, 9.0).is_nan());
1389 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1390 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1391 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1392 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1398 let inf: f64 = INFINITY;
1399 let neg_inf: f64 = NEG_INFINITY;
1400 assert_eq!(1.0f64.recip(), 1.0);
1401 assert_eq!(2.0f64.recip(), 0.5);
1402 assert_eq!((-0.4f64).recip(), -2.5);
1403 assert_eq!(0.0f64.recip(), inf);
1404 assert!(nan.recip().is_nan());
1405 assert_eq!(inf.recip(), 0.0);
1406 assert_eq!(neg_inf.recip(), 0.0);
1412 let inf: f64 = INFINITY;
1413 let neg_inf: f64 = NEG_INFINITY;
1414 assert_eq!(1.0f64.powi(1), 1.0);
1415 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1416 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1417 assert_eq!(8.3f64.powi(0), 1.0);
1418 assert!(nan.powi(2).is_nan());
1419 assert_eq!(inf.powi(3), inf);
1420 assert_eq!(neg_inf.powi(2), inf);
1426 let inf: f64 = INFINITY;
1427 let neg_inf: f64 = NEG_INFINITY;
1428 assert_eq!(1.0f64.powf(1.0), 1.0);
1429 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1430 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1431 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1432 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1433 assert_eq!(8.3f64.powf(0.0), 1.0);
1434 assert!(nan.powf(2.0).is_nan());
1435 assert_eq!(inf.powf(2.0), inf);
1436 assert_eq!(neg_inf.powf(3.0), neg_inf);
1440 fn test_sqrt_domain() {
1441 assert!(NAN.sqrt().is_nan());
1442 assert!(NEG_INFINITY.sqrt().is_nan());
1443 assert!((-1.0f64).sqrt().is_nan());
1444 assert_eq!((-0.0f64).sqrt(), -0.0);
1445 assert_eq!(0.0f64.sqrt(), 0.0);
1446 assert_eq!(1.0f64.sqrt(), 1.0);
1447 assert_eq!(INFINITY.sqrt(), INFINITY);
1452 assert_eq!(1.0, 0.0f64.exp());
1453 assert_approx_eq!(2.718282, 1.0f64.exp());
1454 assert_approx_eq!(148.413159, 5.0f64.exp());
1456 let inf: f64 = INFINITY;
1457 let neg_inf: f64 = NEG_INFINITY;
1459 assert_eq!(inf, inf.exp());
1460 assert_eq!(0.0, neg_inf.exp());
1461 assert!(nan.exp().is_nan());
1466 assert_eq!(32.0, 5.0f64.exp2());
1467 assert_eq!(1.0, 0.0f64.exp2());
1469 let inf: f64 = INFINITY;
1470 let neg_inf: f64 = NEG_INFINITY;
1472 assert_eq!(inf, inf.exp2());
1473 assert_eq!(0.0, neg_inf.exp2());
1474 assert!(nan.exp2().is_nan());
1480 let inf: f64 = INFINITY;
1481 let neg_inf: f64 = NEG_INFINITY;
1482 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1483 assert!(nan.ln().is_nan());
1484 assert_eq!(inf.ln(), inf);
1485 assert!(neg_inf.ln().is_nan());
1486 assert!((-2.3f64).ln().is_nan());
1487 assert_eq!((-0.0f64).ln(), neg_inf);
1488 assert_eq!(0.0f64.ln(), neg_inf);
1489 assert_approx_eq!(4.0f64.ln(), 1.386294);
1495 let inf: f64 = INFINITY;
1496 let neg_inf: f64 = NEG_INFINITY;
1497 assert_eq!(10.0f64.log(10.0), 1.0);
1498 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1499 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1500 assert!(1.0f64.log(1.0).is_nan());
1501 assert!(1.0f64.log(-13.9).is_nan());
1502 assert!(nan.log(2.3).is_nan());
1503 assert_eq!(inf.log(10.0), inf);
1504 assert!(neg_inf.log(8.8).is_nan());
1505 assert!((-2.3f64).log(0.1).is_nan());
1506 assert_eq!((-0.0f64).log(2.0), neg_inf);
1507 assert_eq!(0.0f64.log(7.0), neg_inf);
1513 let inf: f64 = INFINITY;
1514 let neg_inf: f64 = NEG_INFINITY;
1515 assert_approx_eq!(10.0f64.log2(), 3.321928);
1516 assert_approx_eq!(2.3f64.log2(), 1.201634);
1517 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1518 assert!(nan.log2().is_nan());
1519 assert_eq!(inf.log2(), inf);
1520 assert!(neg_inf.log2().is_nan());
1521 assert!((-2.3f64).log2().is_nan());
1522 assert_eq!((-0.0f64).log2(), neg_inf);
1523 assert_eq!(0.0f64.log2(), neg_inf);
1529 let inf: f64 = INFINITY;
1530 let neg_inf: f64 = NEG_INFINITY;
1531 assert_eq!(10.0f64.log10(), 1.0);
1532 assert_approx_eq!(2.3f64.log10(), 0.361728);
1533 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1534 assert_eq!(1.0f64.log10(), 0.0);
1535 assert!(nan.log10().is_nan());
1536 assert_eq!(inf.log10(), inf);
1537 assert!(neg_inf.log10().is_nan());
1538 assert!((-2.3f64).log10().is_nan());
1539 assert_eq!((-0.0f64).log10(), neg_inf);
1540 assert_eq!(0.0f64.log10(), neg_inf);
1544 fn test_to_degrees() {
1545 let pi: f64 = consts::PI;
1547 let inf: f64 = INFINITY;
1548 let neg_inf: f64 = NEG_INFINITY;
1549 assert_eq!(0.0f64.to_degrees(), 0.0);
1550 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1551 assert_eq!(pi.to_degrees(), 180.0);
1552 assert!(nan.to_degrees().is_nan());
1553 assert_eq!(inf.to_degrees(), inf);
1554 assert_eq!(neg_inf.to_degrees(), neg_inf);
1558 fn test_to_radians() {
1559 let pi: f64 = consts::PI;
1561 let inf: f64 = INFINITY;
1562 let neg_inf: f64 = NEG_INFINITY;
1563 assert_eq!(0.0f64.to_radians(), 0.0);
1564 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1565 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1566 assert_eq!(180.0f64.to_radians(), pi);
1567 assert!(nan.to_radians().is_nan());
1568 assert_eq!(inf.to_radians(), inf);
1569 assert_eq!(neg_inf.to_radians(), neg_inf);
1574 assert_eq!(0.0f64.asinh(), 0.0f64);
1575 assert_eq!((-0.0f64).asinh(), -0.0f64);
1577 let inf: f64 = INFINITY;
1578 let neg_inf: f64 = NEG_INFINITY;
1580 assert_eq!(inf.asinh(), inf);
1581 assert_eq!(neg_inf.asinh(), neg_inf);
1582 assert!(nan.asinh().is_nan());
1583 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1584 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1589 assert_eq!(1.0f64.acosh(), 0.0f64);
1590 assert!(0.999f64.acosh().is_nan());
1592 let inf: f64 = INFINITY;
1593 let neg_inf: f64 = NEG_INFINITY;
1595 assert_eq!(inf.acosh(), inf);
1596 assert!(neg_inf.acosh().is_nan());
1597 assert!(nan.acosh().is_nan());
1598 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1599 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1604 assert_eq!(0.0f64.atanh(), 0.0f64);
1605 assert_eq!((-0.0f64).atanh(), -0.0f64);
1607 let inf: f64 = INFINITY;
1608 let neg_inf: f64 = NEG_INFINITY;
1610 assert_eq!(1.0f64.atanh(), inf);
1611 assert_eq!((-1.0f64).atanh(), neg_inf);
1612 assert!(2f64.atanh().atanh().is_nan());
1613 assert!((-2f64).atanh().atanh().is_nan());
1614 assert!(inf.atanh().is_nan());
1615 assert!(neg_inf.atanh().is_nan());
1616 assert!(nan.atanh().is_nan());
1617 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1618 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1622 fn test_real_consts() {
1624 let pi: f64 = consts::PI;
1625 let frac_pi_2: f64 = consts::FRAC_PI_2;
1626 let frac_pi_3: f64 = consts::FRAC_PI_3;
1627 let frac_pi_4: f64 = consts::FRAC_PI_4;
1628 let frac_pi_6: f64 = consts::FRAC_PI_6;
1629 let frac_pi_8: f64 = consts::FRAC_PI_8;
1630 let frac_1_pi: f64 = consts::FRAC_1_PI;
1631 let frac_2_pi: f64 = consts::FRAC_2_PI;
1632 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1633 let sqrt2: f64 = consts::SQRT_2;
1634 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1635 let e: f64 = consts::E;
1636 let log2_e: f64 = consts::LOG2_E;
1637 let log10_e: f64 = consts::LOG10_E;
1638 let ln_2: f64 = consts::LN_2;
1639 let ln_10: f64 = consts::LN_10;
1641 assert_approx_eq!(frac_pi_2, pi / 2f64);
1642 assert_approx_eq!(frac_pi_3, pi / 3f64);
1643 assert_approx_eq!(frac_pi_4, pi / 4f64);
1644 assert_approx_eq!(frac_pi_6, pi / 6f64);
1645 assert_approx_eq!(frac_pi_8, pi / 8f64);
1646 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1647 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1648 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1649 assert_approx_eq!(sqrt2, 2f64.sqrt());
1650 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1651 assert_approx_eq!(log2_e, e.log2());
1652 assert_approx_eq!(log10_e, e.log10());
1653 assert_approx_eq!(ln_2, 2f64.ln());
1654 assert_approx_eq!(ln_10, 10f64.ln());
1658 fn test_float_bits_conv() {
1659 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1660 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1661 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1662 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1663 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1664 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1665 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1666 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);