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)]
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
35 #[stable(feature = "rust1", since = "1.0.0")]
36 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub use core::f64::consts;
42 use libc::{c_double, c_int};
46 pub fn acos(n: c_double) -> c_double;
47 pub fn asin(n: c_double) -> c_double;
48 pub fn atan(n: c_double) -> c_double;
49 pub fn atan2(a: c_double, b: c_double) -> c_double;
50 pub fn cbrt(n: c_double) -> c_double;
51 pub fn cosh(n: c_double) -> c_double;
52 pub fn erf(n: c_double) -> c_double;
53 pub fn erfc(n: c_double) -> c_double;
54 pub fn expm1(n: c_double) -> c_double;
55 pub fn fdim(a: c_double, b: c_double) -> c_double;
56 pub fn fmax(a: c_double, b: c_double) -> c_double;
57 pub fn fmin(a: c_double, b: c_double) -> c_double;
58 pub fn fmod(a: c_double, b: c_double) -> c_double;
59 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
60 pub fn ilogb(n: c_double) -> c_int;
61 pub fn ldexp(x: c_double, n: c_int) -> c_double;
62 pub fn logb(n: c_double) -> c_double;
63 pub fn log1p(n: c_double) -> c_double;
64 pub fn nextafter(x: c_double, y: c_double) -> c_double;
65 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
66 pub fn sinh(n: c_double) -> c_double;
67 pub fn tan(n: c_double) -> c_double;
68 pub fn tanh(n: c_double) -> c_double;
69 pub fn tgamma(n: c_double) -> c_double;
71 // These are commonly only available for doubles
73 pub fn j0(n: c_double) -> c_double;
74 pub fn j1(n: c_double) -> c_double;
75 pub fn jn(i: c_int, n: c_double) -> c_double;
77 pub fn y0(n: c_double) -> c_double;
78 pub fn y1(n: c_double) -> c_double;
79 pub fn yn(i: c_int, n: c_double) -> c_double;
81 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
82 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
84 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
85 pub fn hypot(x: c_double, y: c_double) -> c_double;
92 /// Returns `true` if this value is `NaN` and false otherwise.
97 /// let nan = f64::NAN;
100 /// assert!(nan.is_nan());
101 /// assert!(!f.is_nan());
103 #[stable(feature = "rust1", since = "1.0.0")]
105 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
107 /// Returns `true` if this value is positive infinity or negative infinity and
114 /// let inf = f64::INFINITY;
115 /// let neg_inf = f64::NEG_INFINITY;
116 /// let nan = f64::NAN;
118 /// assert!(!f.is_infinite());
119 /// assert!(!nan.is_infinite());
121 /// assert!(inf.is_infinite());
122 /// assert!(neg_inf.is_infinite());
124 #[stable(feature = "rust1", since = "1.0.0")]
126 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
128 /// Returns `true` if this number is neither infinite nor `NaN`.
134 /// let inf: f64 = f64::INFINITY;
135 /// let neg_inf: f64 = f64::NEG_INFINITY;
136 /// let nan: f64 = f64::NAN;
138 /// assert!(f.is_finite());
140 /// assert!(!nan.is_finite());
141 /// assert!(!inf.is_finite());
142 /// assert!(!neg_inf.is_finite());
144 #[stable(feature = "rust1", since = "1.0.0")]
146 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
148 /// Returns `true` if the number is neither zero, infinite,
149 /// [subnormal][subnormal], or `NaN`.
154 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
155 /// let max = f64::MAX;
156 /// let lower_than_min = 1.0e-308_f64;
157 /// let zero = 0.0f64;
159 /// assert!(min.is_normal());
160 /// assert!(max.is_normal());
162 /// assert!(!zero.is_normal());
163 /// assert!(!f64::NAN.is_normal());
164 /// assert!(!f64::INFINITY.is_normal());
165 /// // Values between `0` and `min` are Subnormal.
166 /// assert!(!lower_than_min.is_normal());
168 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
169 #[stable(feature = "rust1", since = "1.0.0")]
171 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
173 /// Returns the floating point category of the number. If only one property
174 /// is going to be tested, it is generally faster to use the specific
175 /// predicate instead.
178 /// use std::num::FpCategory;
181 /// let num = 12.4_f64;
182 /// let inf = f64::INFINITY;
184 /// assert_eq!(num.classify(), FpCategory::Normal);
185 /// assert_eq!(inf.classify(), FpCategory::Infinite);
187 #[stable(feature = "rust1", since = "1.0.0")]
189 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
191 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
192 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
193 /// The floating point encoding is documented in the [Reference][floating-point].
196 /// #![feature(float_extras)]
198 /// let num = 2.0f64;
200 /// // (8388608, -22, 1)
201 /// let (mantissa, exponent, sign) = num.integer_decode();
202 /// let sign_f = sign as f64;
203 /// let mantissa_f = mantissa as f64;
204 /// let exponent_f = num.powf(exponent as f64);
206 /// // 1 * 8388608 * 2^(-22) == 2
207 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
209 /// assert!(abs_difference < 1e-10);
211 /// [floating-point]: ../reference/types.html#machine-types
212 #[unstable(feature = "float_extras", reason = "signature is undecided",
214 #[rustc_deprecated(since = "1.11.0",
215 reason = "never really came to fruition and easily \
216 implementable outside the standard library")]
219 pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
221 /// Returns the largest integer less than or equal to a number.
224 /// let f = 3.99_f64;
227 /// assert_eq!(f.floor(), 3.0);
228 /// assert_eq!(g.floor(), 3.0);
230 #[stable(feature = "rust1", since = "1.0.0")]
232 pub fn floor(self) -> f64 {
233 unsafe { intrinsics::floorf64(self) }
236 /// Returns the smallest integer greater than or equal to a number.
239 /// let f = 3.01_f64;
242 /// assert_eq!(f.ceil(), 4.0);
243 /// assert_eq!(g.ceil(), 4.0);
245 #[stable(feature = "rust1", since = "1.0.0")]
247 pub fn ceil(self) -> f64 {
248 unsafe { intrinsics::ceilf64(self) }
251 /// Returns the nearest integer to a number. Round half-way cases away from
256 /// let g = -3.3_f64;
258 /// assert_eq!(f.round(), 3.0);
259 /// assert_eq!(g.round(), -3.0);
261 #[stable(feature = "rust1", since = "1.0.0")]
263 pub fn round(self) -> f64 {
264 unsafe { intrinsics::roundf64(self) }
267 /// Returns the integer part of a number.
271 /// let g = -3.7_f64;
273 /// assert_eq!(f.trunc(), 3.0);
274 /// assert_eq!(g.trunc(), -3.0);
276 #[stable(feature = "rust1", since = "1.0.0")]
278 pub fn trunc(self) -> f64 {
279 unsafe { intrinsics::truncf64(self) }
282 /// Returns the fractional part of a number.
286 /// let y = -3.5_f64;
287 /// let abs_difference_x = (x.fract() - 0.5).abs();
288 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
290 /// assert!(abs_difference_x < 1e-10);
291 /// assert!(abs_difference_y < 1e-10);
293 #[stable(feature = "rust1", since = "1.0.0")]
295 pub fn fract(self) -> f64 { self - self.trunc() }
297 /// Computes the absolute value of `self`. Returns `NAN` if the
304 /// let y = -3.5_f64;
306 /// let abs_difference_x = (x.abs() - x).abs();
307 /// let abs_difference_y = (y.abs() - (-y)).abs();
309 /// assert!(abs_difference_x < 1e-10);
310 /// assert!(abs_difference_y < 1e-10);
312 /// assert!(f64::NAN.abs().is_nan());
314 #[stable(feature = "rust1", since = "1.0.0")]
316 pub fn abs(self) -> f64 { num::Float::abs(self) }
318 /// Returns a number that represents the sign of `self`.
320 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
321 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
322 /// - `NAN` if the number is `NAN`
329 /// assert_eq!(f.signum(), 1.0);
330 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
332 /// assert!(f64::NAN.signum().is_nan());
334 #[stable(feature = "rust1", since = "1.0.0")]
336 pub fn signum(self) -> f64 { num::Float::signum(self) }
338 /// Returns `true` if `self`'s sign bit is positive, including
339 /// `+0.0` and `INFINITY`.
344 /// let nan: f64 = f64::NAN;
347 /// let g = -7.0_f64;
349 /// assert!(f.is_sign_positive());
350 /// assert!(!g.is_sign_positive());
351 /// // Requires both tests to determine if is `NaN`
352 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
354 #[stable(feature = "rust1", since = "1.0.0")]
356 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
358 #[stable(feature = "rust1", since = "1.0.0")]
359 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
361 pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
363 /// Returns `true` if `self`'s sign is negative, including `-0.0`
364 /// and `NEG_INFINITY`.
369 /// let nan = f64::NAN;
372 /// let g = -7.0_f64;
374 /// assert!(!f.is_sign_negative());
375 /// assert!(g.is_sign_negative());
376 /// // Requires both tests to determine if is `NaN`.
377 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
379 #[stable(feature = "rust1", since = "1.0.0")]
381 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
383 #[stable(feature = "rust1", since = "1.0.0")]
384 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
386 pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
388 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
389 /// error. This produces a more accurate result with better performance than
390 /// a separate multiplication operation followed by an add.
393 /// let m = 10.0_f64;
395 /// let b = 60.0_f64;
398 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
400 /// assert!(abs_difference < 1e-10);
402 #[stable(feature = "rust1", since = "1.0.0")]
404 pub fn mul_add(self, a: f64, b: f64) -> f64 {
405 unsafe { intrinsics::fmaf64(self, a, b) }
408 /// Takes the reciprocal (inverse) of a number, `1/x`.
412 /// let abs_difference = (x.recip() - (1.0/x)).abs();
414 /// assert!(abs_difference < 1e-10);
416 #[stable(feature = "rust1", since = "1.0.0")]
418 pub fn recip(self) -> f64 { num::Float::recip(self) }
420 /// Raises a number to an integer power.
422 /// Using this function is generally faster than using `powf`
426 /// let abs_difference = (x.powi(2) - x*x).abs();
428 /// assert!(abs_difference < 1e-10);
430 #[stable(feature = "rust1", since = "1.0.0")]
432 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
434 /// Raises a number to a floating point power.
438 /// let abs_difference = (x.powf(2.0) - x*x).abs();
440 /// assert!(abs_difference < 1e-10);
442 #[stable(feature = "rust1", since = "1.0.0")]
444 pub fn powf(self, n: f64) -> f64 {
445 unsafe { intrinsics::powf64(self, n) }
448 /// Takes the square root of a number.
450 /// Returns NaN if `self` is a negative number.
453 /// let positive = 4.0_f64;
454 /// let negative = -4.0_f64;
456 /// let abs_difference = (positive.sqrt() - 2.0).abs();
458 /// assert!(abs_difference < 1e-10);
459 /// assert!(negative.sqrt().is_nan());
461 #[stable(feature = "rust1", since = "1.0.0")]
463 pub fn sqrt(self) -> f64 {
467 unsafe { intrinsics::sqrtf64(self) }
471 /// Returns `e^(self)`, (the exponential function).
474 /// let one = 1.0_f64;
476 /// let e = one.exp();
478 /// // ln(e) - 1 == 0
479 /// let abs_difference = (e.ln() - 1.0).abs();
481 /// assert!(abs_difference < 1e-10);
483 #[stable(feature = "rust1", since = "1.0.0")]
485 pub fn exp(self) -> f64 {
486 unsafe { intrinsics::expf64(self) }
489 /// Returns `2^(self)`.
495 /// let abs_difference = (f.exp2() - 4.0).abs();
497 /// assert!(abs_difference < 1e-10);
499 #[stable(feature = "rust1", since = "1.0.0")]
501 pub fn exp2(self) -> f64 {
502 unsafe { intrinsics::exp2f64(self) }
505 /// Returns the natural logarithm of the number.
508 /// let one = 1.0_f64;
510 /// let e = one.exp();
512 /// // ln(e) - 1 == 0
513 /// let abs_difference = (e.ln() - 1.0).abs();
515 /// assert!(abs_difference < 1e-10);
517 #[stable(feature = "rust1", since = "1.0.0")]
519 pub fn ln(self) -> f64 {
520 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
523 /// Returns the logarithm of the number with respect to an arbitrary base.
526 /// let ten = 10.0_f64;
527 /// let two = 2.0_f64;
529 /// // log10(10) - 1 == 0
530 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
532 /// // log2(2) - 1 == 0
533 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
535 /// assert!(abs_difference_10 < 1e-10);
536 /// assert!(abs_difference_2 < 1e-10);
538 #[stable(feature = "rust1", since = "1.0.0")]
540 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
542 /// Returns the base 2 logarithm of the number.
545 /// let two = 2.0_f64;
547 /// // log2(2) - 1 == 0
548 /// let abs_difference = (two.log2() - 1.0).abs();
550 /// assert!(abs_difference < 1e-10);
552 #[stable(feature = "rust1", since = "1.0.0")]
554 pub fn log2(self) -> f64 {
555 self.log_wrapper(|n| {
556 #[cfg(target_os = "android")]
557 return ::sys::android::log2f64(n);
558 #[cfg(not(target_os = "android"))]
559 return unsafe { intrinsics::log2f64(n) };
563 /// Returns the base 10 logarithm of the number.
566 /// let ten = 10.0_f64;
568 /// // log10(10) - 1 == 0
569 /// let abs_difference = (ten.log10() - 1.0).abs();
571 /// assert!(abs_difference < 1e-10);
573 #[stable(feature = "rust1", since = "1.0.0")]
575 pub fn log10(self) -> f64 {
576 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
579 /// Converts radians to degrees.
582 /// use std::f64::consts;
584 /// let angle = consts::PI;
586 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
588 /// assert!(abs_difference < 1e-10);
590 #[stable(feature = "rust1", since = "1.0.0")]
592 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
594 /// Converts degrees to radians.
597 /// use std::f64::consts;
599 /// let angle = 180.0_f64;
601 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
603 /// assert!(abs_difference < 1e-10);
605 #[stable(feature = "rust1", since = "1.0.0")]
607 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
609 /// Constructs a floating point number of `x*2^exp`.
612 /// #![feature(float_extras)]
614 /// // 3*2^2 - 12 == 0
615 /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
617 /// assert!(abs_difference < 1e-10);
619 #[unstable(feature = "float_extras",
620 reason = "pending integer conventions",
622 #[rustc_deprecated(since = "1.11.0",
623 reason = "never really came to fruition and easily \
624 implementable outside the standard library")]
626 pub fn ldexp(x: f64, exp: isize) -> f64 {
627 unsafe { cmath::ldexp(x, exp as c_int) }
630 /// Breaks the number into a normalized fraction and a base-2 exponent,
633 /// * `self = x * 2^exp`
634 /// * `0.5 <= abs(x) < 1.0`
637 /// #![feature(float_extras)]
641 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
642 /// let f = x.frexp();
643 /// let abs_difference_0 = (f.0 - 0.5).abs();
644 /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
646 /// assert!(abs_difference_0 < 1e-10);
647 /// assert!(abs_difference_1 < 1e-10);
649 #[unstable(feature = "float_extras",
650 reason = "pending integer conventions",
652 #[rustc_deprecated(since = "1.11.0",
653 reason = "never really came to fruition and easily \
654 implementable outside the standard library")]
656 pub fn frexp(self) -> (f64, isize) {
659 let x = cmath::frexp(self, &mut exp);
664 /// Returns the next representable floating-point value in the direction of
668 /// #![feature(float_extras)]
672 /// let abs_diff = (x.next_after(2.0) - 1.0000000000000002220446049250313_f64).abs();
674 /// assert!(abs_diff < 1e-10);
676 #[unstable(feature = "float_extras",
677 reason = "unsure about its place in the world",
679 #[rustc_deprecated(since = "1.11.0",
680 reason = "never really came to fruition and easily \
681 implementable outside the standard library")]
683 pub fn next_after(self, other: f64) -> f64 {
684 unsafe { cmath::nextafter(self, other) }
687 /// Returns the maximum of the two numbers.
693 /// assert_eq!(x.max(y), y);
696 /// If one of the arguments is NaN, then the other argument is returned.
697 #[stable(feature = "rust1", since = "1.0.0")]
699 pub fn max(self, other: f64) -> f64 {
700 unsafe { cmath::fmax(self, other) }
703 /// Returns the minimum of the two numbers.
709 /// assert_eq!(x.min(y), x);
712 /// If one of the arguments is NaN, then the other argument is returned.
713 #[stable(feature = "rust1", since = "1.0.0")]
715 pub fn min(self, other: f64) -> f64 {
716 unsafe { cmath::fmin(self, other) }
719 /// The positive difference of two numbers.
721 /// * If `self <= other`: `0:0`
722 /// * Else: `self - other`
726 /// let y = -3.0_f64;
728 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
729 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
731 /// assert!(abs_difference_x < 1e-10);
732 /// assert!(abs_difference_y < 1e-10);
734 #[stable(feature = "rust1", since = "1.0.0")]
736 #[rustc_deprecated(since = "1.10.0",
737 reason = "you probably meant `(self - other).abs()`: \
738 this operation is `(self - other).max(0.0)` (also \
739 known as `fdim` in C). If you truly need the positive \
740 difference, consider using that expression or the C function \
741 `fdim`, depending on how you wish to handle NaN (please consider \
742 filing an issue describing your use-case too).")]
743 pub fn abs_sub(self, other: f64) -> f64 {
744 unsafe { cmath::fdim(self, other) }
747 /// Takes the cubic root of a number.
752 /// // x^(1/3) - 2 == 0
753 /// let abs_difference = (x.cbrt() - 2.0).abs();
755 /// assert!(abs_difference < 1e-10);
757 #[stable(feature = "rust1", since = "1.0.0")]
759 pub fn cbrt(self) -> f64 {
760 unsafe { cmath::cbrt(self) }
763 /// Calculates the length of the hypotenuse of a right-angle triangle given
764 /// legs of length `x` and `y`.
770 /// // sqrt(x^2 + y^2)
771 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
773 /// assert!(abs_difference < 1e-10);
775 #[stable(feature = "rust1", since = "1.0.0")]
777 pub fn hypot(self, other: f64) -> f64 {
778 unsafe { cmath::hypot(self, other) }
781 /// Computes the sine of a number (in radians).
786 /// let x = f64::consts::PI/2.0;
788 /// let abs_difference = (x.sin() - 1.0).abs();
790 /// assert!(abs_difference < 1e-10);
792 #[stable(feature = "rust1", since = "1.0.0")]
794 pub fn sin(self) -> f64 {
795 unsafe { intrinsics::sinf64(self) }
798 /// Computes the cosine of a number (in radians).
803 /// let x = 2.0*f64::consts::PI;
805 /// let abs_difference = (x.cos() - 1.0).abs();
807 /// assert!(abs_difference < 1e-10);
809 #[stable(feature = "rust1", since = "1.0.0")]
811 pub fn cos(self) -> f64 {
812 unsafe { intrinsics::cosf64(self) }
815 /// Computes the tangent of a number (in radians).
820 /// let x = f64::consts::PI/4.0;
821 /// let abs_difference = (x.tan() - 1.0).abs();
823 /// assert!(abs_difference < 1e-14);
825 #[stable(feature = "rust1", since = "1.0.0")]
827 pub fn tan(self) -> f64 {
828 unsafe { cmath::tan(self) }
831 /// Computes the arcsine of a number. Return value is in radians in
832 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
838 /// let f = f64::consts::PI / 2.0;
840 /// // asin(sin(pi/2))
841 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
843 /// assert!(abs_difference < 1e-10);
845 #[stable(feature = "rust1", since = "1.0.0")]
847 pub fn asin(self) -> f64 {
848 unsafe { cmath::asin(self) }
851 /// Computes the arccosine of a number. Return value is in radians in
852 /// the range [0, pi] or NaN if the number is outside the range
858 /// let f = f64::consts::PI / 4.0;
860 /// // acos(cos(pi/4))
861 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
863 /// assert!(abs_difference < 1e-10);
865 #[stable(feature = "rust1", since = "1.0.0")]
867 pub fn acos(self) -> f64 {
868 unsafe { cmath::acos(self) }
871 /// Computes the arctangent of a number. Return value is in radians in the
872 /// range [-pi/2, pi/2];
878 /// let abs_difference = (f.tan().atan() - 1.0).abs();
880 /// assert!(abs_difference < 1e-10);
882 #[stable(feature = "rust1", since = "1.0.0")]
884 pub fn atan(self) -> f64 {
885 unsafe { cmath::atan(self) }
888 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
890 /// * `x = 0`, `y = 0`: `0`
891 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
892 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
893 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
898 /// let pi = f64::consts::PI;
899 /// // All angles from horizontal right (+x)
900 /// // 45 deg counter-clockwise
901 /// let x1 = 3.0_f64;
902 /// let y1 = -3.0_f64;
904 /// // 135 deg clockwise
905 /// let x2 = -3.0_f64;
906 /// let y2 = 3.0_f64;
908 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
909 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
911 /// assert!(abs_difference_1 < 1e-10);
912 /// assert!(abs_difference_2 < 1e-10);
914 #[stable(feature = "rust1", since = "1.0.0")]
916 pub fn atan2(self, other: f64) -> f64 {
917 unsafe { cmath::atan2(self, other) }
920 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
921 /// `(sin(x), cos(x))`.
926 /// let x = f64::consts::PI/4.0;
927 /// let f = x.sin_cos();
929 /// let abs_difference_0 = (f.0 - x.sin()).abs();
930 /// let abs_difference_1 = (f.1 - x.cos()).abs();
932 /// assert!(abs_difference_0 < 1e-10);
933 /// assert!(abs_difference_1 < 1e-10);
935 #[stable(feature = "rust1", since = "1.0.0")]
937 pub fn sin_cos(self) -> (f64, f64) {
938 (self.sin(), self.cos())
941 /// Returns `e^(self) - 1` in a way that is accurate even if the
942 /// number is close to zero.
948 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
950 /// assert!(abs_difference < 1e-10);
952 #[stable(feature = "rust1", since = "1.0.0")]
954 pub fn exp_m1(self) -> f64 {
955 unsafe { cmath::expm1(self) }
958 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
959 /// the operations were performed separately.
964 /// let x = f64::consts::E - 1.0;
966 /// // ln(1 + (e - 1)) == ln(e) == 1
967 /// let abs_difference = (x.ln_1p() - 1.0).abs();
969 /// assert!(abs_difference < 1e-10);
971 #[stable(feature = "rust1", since = "1.0.0")]
973 pub fn ln_1p(self) -> f64 {
974 unsafe { cmath::log1p(self) }
977 /// Hyperbolic sine function.
982 /// let e = f64::consts::E;
985 /// let f = x.sinh();
986 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
987 /// let g = (e*e - 1.0)/(2.0*e);
988 /// let abs_difference = (f - g).abs();
990 /// assert!(abs_difference < 1e-10);
992 #[stable(feature = "rust1", since = "1.0.0")]
994 pub fn sinh(self) -> f64 {
995 unsafe { cmath::sinh(self) }
998 /// Hyperbolic cosine function.
1003 /// let e = f64::consts::E;
1004 /// let x = 1.0_f64;
1005 /// let f = x.cosh();
1006 /// // Solving cosh() at 1 gives this result
1007 /// let g = (e*e + 1.0)/(2.0*e);
1008 /// let abs_difference = (f - g).abs();
1011 /// assert!(abs_difference < 1.0e-10);
1013 #[stable(feature = "rust1", since = "1.0.0")]
1015 pub fn cosh(self) -> f64 {
1016 unsafe { cmath::cosh(self) }
1019 /// Hyperbolic tangent function.
1024 /// let e = f64::consts::E;
1025 /// let x = 1.0_f64;
1027 /// let f = x.tanh();
1028 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1029 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1030 /// let abs_difference = (f - g).abs();
1032 /// assert!(abs_difference < 1.0e-10);
1034 #[stable(feature = "rust1", since = "1.0.0")]
1036 pub fn tanh(self) -> f64 {
1037 unsafe { cmath::tanh(self) }
1040 /// Inverse hyperbolic sine function.
1043 /// let x = 1.0_f64;
1044 /// let f = x.sinh().asinh();
1046 /// let abs_difference = (f - x).abs();
1048 /// assert!(abs_difference < 1.0e-10);
1050 #[stable(feature = "rust1", since = "1.0.0")]
1052 pub fn asinh(self) -> f64 {
1053 if self == NEG_INFINITY {
1056 (self + ((self * self) + 1.0).sqrt()).ln()
1060 /// Inverse hyperbolic cosine function.
1063 /// let x = 1.0_f64;
1064 /// let f = x.cosh().acosh();
1066 /// let abs_difference = (f - x).abs();
1068 /// assert!(abs_difference < 1.0e-10);
1070 #[stable(feature = "rust1", since = "1.0.0")]
1072 pub fn acosh(self) -> f64 {
1074 x if x < 1.0 => NAN,
1075 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1079 /// Inverse hyperbolic tangent function.
1084 /// let e = f64::consts::E;
1085 /// let f = e.tanh().atanh();
1087 /// let abs_difference = (f - e).abs();
1089 /// assert!(abs_difference < 1.0e-10);
1091 #[stable(feature = "rust1", since = "1.0.0")]
1093 pub fn atanh(self) -> f64 {
1094 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1097 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
1098 // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
1099 // of expected NaN).
1100 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
1101 if !cfg!(target_os = "solaris") {
1104 if self.is_finite() {
1107 } else if self == 0.0 {
1108 NEG_INFINITY // log(0) = -Inf
1110 NAN // log(-n) = NaN
1112 } else if self.is_nan() {
1113 self // log(NaN) = NaN
1114 } else if self > 0.0 {
1115 self // log(Inf) = Inf
1117 NAN // log(-Inf) = NaN
1122 /// Raw transmutation to `u64`.
1124 /// Converts the `f64` into its raw memory representation,
1125 /// similar to the `transmute` function.
1127 /// Note that this function is distinct from casting.
1132 /// #![feature(float_bits_conv)]
1133 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
1134 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1137 #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "40470")]
1139 pub fn to_bits(self) -> u64 {
1140 unsafe { ::mem::transmute(self) }
1143 /// Raw transmutation from `u64`.
1145 /// Converts the given `u64` containing the float's raw memory
1146 /// representation into the `f64` type, similar to the
1147 /// `transmute` function.
1149 /// There is only one difference to a bare `transmute`:
1150 /// Due to the implications onto Rust's safety promises being
1151 /// uncertain, if the representation of a signaling NaN "sNaN" float
1152 /// is passed to the function, the implementation is allowed to
1153 /// return a quiet NaN instead.
1155 /// Note that this function is distinct from casting.
1160 /// #![feature(float_bits_conv)]
1162 /// let v = f64::from_bits(0x4029000000000000);
1163 /// let difference = (v - 12.5).abs();
1164 /// assert!(difference <= 1e-5);
1165 /// // Example for a signaling NaN value:
1166 /// let snan = 0x7FF0000000000001;
1167 /// assert_ne!(f64::from_bits(snan).to_bits(), snan);
1169 #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "40470")]
1171 pub fn from_bits(mut v: u64) -> Self {
1172 const EXP_MASK: u64 = 0x7FF0000000000000;
1173 const QNAN_MASK: u64 = 0x0001000000000000;
1174 const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
1175 if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1176 // If we have a NaN value, we
1177 // convert signaling NaN values to quiet NaN
1178 // by setting the the highest bit of the fraction
1181 unsafe { ::mem::transmute(v) }
1190 use num::FpCategory as Fp;
1194 test_num(10f64, 2f64);
1199 assert_eq!(NAN.min(2.0), 2.0);
1200 assert_eq!(2.0f64.min(NAN), 2.0);
1205 assert_eq!(NAN.max(2.0), 2.0);
1206 assert_eq!(2.0f64.max(NAN), 2.0);
1212 assert!(nan.is_nan());
1213 assert!(!nan.is_infinite());
1214 assert!(!nan.is_finite());
1215 assert!(!nan.is_normal());
1216 assert!(!nan.is_sign_positive());
1217 assert!(!nan.is_sign_negative());
1218 assert_eq!(Fp::Nan, nan.classify());
1222 fn test_infinity() {
1223 let inf: f64 = INFINITY;
1224 assert!(inf.is_infinite());
1225 assert!(!inf.is_finite());
1226 assert!(inf.is_sign_positive());
1227 assert!(!inf.is_sign_negative());
1228 assert!(!inf.is_nan());
1229 assert!(!inf.is_normal());
1230 assert_eq!(Fp::Infinite, inf.classify());
1234 fn test_neg_infinity() {
1235 let neg_inf: f64 = NEG_INFINITY;
1236 assert!(neg_inf.is_infinite());
1237 assert!(!neg_inf.is_finite());
1238 assert!(!neg_inf.is_sign_positive());
1239 assert!(neg_inf.is_sign_negative());
1240 assert!(!neg_inf.is_nan());
1241 assert!(!neg_inf.is_normal());
1242 assert_eq!(Fp::Infinite, neg_inf.classify());
1247 let zero: f64 = 0.0f64;
1248 assert_eq!(0.0, zero);
1249 assert!(!zero.is_infinite());
1250 assert!(zero.is_finite());
1251 assert!(zero.is_sign_positive());
1252 assert!(!zero.is_sign_negative());
1253 assert!(!zero.is_nan());
1254 assert!(!zero.is_normal());
1255 assert_eq!(Fp::Zero, zero.classify());
1259 fn test_neg_zero() {
1260 let neg_zero: f64 = -0.0;
1261 assert_eq!(0.0, neg_zero);
1262 assert!(!neg_zero.is_infinite());
1263 assert!(neg_zero.is_finite());
1264 assert!(!neg_zero.is_sign_positive());
1265 assert!(neg_zero.is_sign_negative());
1266 assert!(!neg_zero.is_nan());
1267 assert!(!neg_zero.is_normal());
1268 assert_eq!(Fp::Zero, neg_zero.classify());
1273 let one: f64 = 1.0f64;
1274 assert_eq!(1.0, one);
1275 assert!(!one.is_infinite());
1276 assert!(one.is_finite());
1277 assert!(one.is_sign_positive());
1278 assert!(!one.is_sign_negative());
1279 assert!(!one.is_nan());
1280 assert!(one.is_normal());
1281 assert_eq!(Fp::Normal, one.classify());
1287 let inf: f64 = INFINITY;
1288 let neg_inf: f64 = NEG_INFINITY;
1289 assert!(nan.is_nan());
1290 assert!(!0.0f64.is_nan());
1291 assert!(!5.3f64.is_nan());
1292 assert!(!(-10.732f64).is_nan());
1293 assert!(!inf.is_nan());
1294 assert!(!neg_inf.is_nan());
1298 fn test_is_infinite() {
1300 let inf: f64 = INFINITY;
1301 let neg_inf: f64 = NEG_INFINITY;
1302 assert!(!nan.is_infinite());
1303 assert!(inf.is_infinite());
1304 assert!(neg_inf.is_infinite());
1305 assert!(!0.0f64.is_infinite());
1306 assert!(!42.8f64.is_infinite());
1307 assert!(!(-109.2f64).is_infinite());
1311 fn test_is_finite() {
1313 let inf: f64 = INFINITY;
1314 let neg_inf: f64 = NEG_INFINITY;
1315 assert!(!nan.is_finite());
1316 assert!(!inf.is_finite());
1317 assert!(!neg_inf.is_finite());
1318 assert!(0.0f64.is_finite());
1319 assert!(42.8f64.is_finite());
1320 assert!((-109.2f64).is_finite());
1324 fn test_is_normal() {
1326 let inf: f64 = INFINITY;
1327 let neg_inf: f64 = NEG_INFINITY;
1328 let zero: f64 = 0.0f64;
1329 let neg_zero: f64 = -0.0;
1330 assert!(!nan.is_normal());
1331 assert!(!inf.is_normal());
1332 assert!(!neg_inf.is_normal());
1333 assert!(!zero.is_normal());
1334 assert!(!neg_zero.is_normal());
1335 assert!(1f64.is_normal());
1336 assert!(1e-307f64.is_normal());
1337 assert!(!1e-308f64.is_normal());
1341 fn test_classify() {
1343 let inf: f64 = INFINITY;
1344 let neg_inf: f64 = NEG_INFINITY;
1345 let zero: f64 = 0.0f64;
1346 let neg_zero: f64 = -0.0;
1347 assert_eq!(nan.classify(), Fp::Nan);
1348 assert_eq!(inf.classify(), Fp::Infinite);
1349 assert_eq!(neg_inf.classify(), Fp::Infinite);
1350 assert_eq!(zero.classify(), Fp::Zero);
1351 assert_eq!(neg_zero.classify(), Fp::Zero);
1352 assert_eq!(1e-307f64.classify(), Fp::Normal);
1353 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1357 #[allow(deprecated)]
1358 fn test_integer_decode() {
1359 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
1360 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
1361 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
1362 assert_eq!(0f64.integer_decode(), (0, -1075, 1));
1363 assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
1364 assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
1365 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1367 // Ignore the "sign" (quiet / signalling flag) of NAN.
1368 // It can vary between runtime operations and LLVM folding.
1369 let (nan_m, nan_e, _nan_s) = NAN.integer_decode();
1370 assert_eq!((nan_m, nan_e), (6755399441055744, 972));
1375 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1376 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1377 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1378 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1379 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1380 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1381 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1382 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1383 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1384 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1389 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1390 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1391 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1392 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1393 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1394 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1395 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1396 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1397 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1398 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1403 assert_approx_eq!(1.0f64.round(), 1.0f64);
1404 assert_approx_eq!(1.3f64.round(), 1.0f64);
1405 assert_approx_eq!(1.5f64.round(), 2.0f64);
1406 assert_approx_eq!(1.7f64.round(), 2.0f64);
1407 assert_approx_eq!(0.0f64.round(), 0.0f64);
1408 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1409 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1410 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1411 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1412 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1417 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1418 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1419 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1420 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1421 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1422 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1423 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1424 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1425 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1426 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1431 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1432 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1433 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1434 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1435 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1436 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1437 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1438 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1439 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1440 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1445 assert_eq!(INFINITY.abs(), INFINITY);
1446 assert_eq!(1f64.abs(), 1f64);
1447 assert_eq!(0f64.abs(), 0f64);
1448 assert_eq!((-0f64).abs(), 0f64);
1449 assert_eq!((-1f64).abs(), 1f64);
1450 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1451 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1452 assert!(NAN.abs().is_nan());
1457 assert_eq!(INFINITY.signum(), 1f64);
1458 assert_eq!(1f64.signum(), 1f64);
1459 assert_eq!(0f64.signum(), 1f64);
1460 assert_eq!((-0f64).signum(), -1f64);
1461 assert_eq!((-1f64).signum(), -1f64);
1462 assert_eq!(NEG_INFINITY.signum(), -1f64);
1463 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1464 assert!(NAN.signum().is_nan());
1468 fn test_is_sign_positive() {
1469 assert!(INFINITY.is_sign_positive());
1470 assert!(1f64.is_sign_positive());
1471 assert!(0f64.is_sign_positive());
1472 assert!(!(-0f64).is_sign_positive());
1473 assert!(!(-1f64).is_sign_positive());
1474 assert!(!NEG_INFINITY.is_sign_positive());
1475 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1476 assert!(!NAN.is_sign_positive());
1480 fn test_is_sign_negative() {
1481 assert!(!INFINITY.is_sign_negative());
1482 assert!(!1f64.is_sign_negative());
1483 assert!(!0f64.is_sign_negative());
1484 assert!((-0f64).is_sign_negative());
1485 assert!((-1f64).is_sign_negative());
1486 assert!(NEG_INFINITY.is_sign_negative());
1487 assert!((1f64/NEG_INFINITY).is_sign_negative());
1488 assert!(!NAN.is_sign_negative());
1494 let inf: f64 = INFINITY;
1495 let neg_inf: f64 = NEG_INFINITY;
1496 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1497 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1498 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1499 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1500 assert!(nan.mul_add(7.8, 9.0).is_nan());
1501 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1502 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1503 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1504 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1510 let inf: f64 = INFINITY;
1511 let neg_inf: f64 = NEG_INFINITY;
1512 assert_eq!(1.0f64.recip(), 1.0);
1513 assert_eq!(2.0f64.recip(), 0.5);
1514 assert_eq!((-0.4f64).recip(), -2.5);
1515 assert_eq!(0.0f64.recip(), inf);
1516 assert!(nan.recip().is_nan());
1517 assert_eq!(inf.recip(), 0.0);
1518 assert_eq!(neg_inf.recip(), 0.0);
1524 let inf: f64 = INFINITY;
1525 let neg_inf: f64 = NEG_INFINITY;
1526 assert_eq!(1.0f64.powi(1), 1.0);
1527 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1528 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1529 assert_eq!(8.3f64.powi(0), 1.0);
1530 assert!(nan.powi(2).is_nan());
1531 assert_eq!(inf.powi(3), inf);
1532 assert_eq!(neg_inf.powi(2), inf);
1538 let inf: f64 = INFINITY;
1539 let neg_inf: f64 = NEG_INFINITY;
1540 assert_eq!(1.0f64.powf(1.0), 1.0);
1541 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1542 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1543 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1544 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1545 assert_eq!(8.3f64.powf(0.0), 1.0);
1546 assert!(nan.powf(2.0).is_nan());
1547 assert_eq!(inf.powf(2.0), inf);
1548 assert_eq!(neg_inf.powf(3.0), neg_inf);
1552 fn test_sqrt_domain() {
1553 assert!(NAN.sqrt().is_nan());
1554 assert!(NEG_INFINITY.sqrt().is_nan());
1555 assert!((-1.0f64).sqrt().is_nan());
1556 assert_eq!((-0.0f64).sqrt(), -0.0);
1557 assert_eq!(0.0f64.sqrt(), 0.0);
1558 assert_eq!(1.0f64.sqrt(), 1.0);
1559 assert_eq!(INFINITY.sqrt(), INFINITY);
1564 assert_eq!(1.0, 0.0f64.exp());
1565 assert_approx_eq!(2.718282, 1.0f64.exp());
1566 assert_approx_eq!(148.413159, 5.0f64.exp());
1568 let inf: f64 = INFINITY;
1569 let neg_inf: f64 = NEG_INFINITY;
1571 assert_eq!(inf, inf.exp());
1572 assert_eq!(0.0, neg_inf.exp());
1573 assert!(nan.exp().is_nan());
1578 assert_eq!(32.0, 5.0f64.exp2());
1579 assert_eq!(1.0, 0.0f64.exp2());
1581 let inf: f64 = INFINITY;
1582 let neg_inf: f64 = NEG_INFINITY;
1584 assert_eq!(inf, inf.exp2());
1585 assert_eq!(0.0, neg_inf.exp2());
1586 assert!(nan.exp2().is_nan());
1592 let inf: f64 = INFINITY;
1593 let neg_inf: f64 = NEG_INFINITY;
1594 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1595 assert!(nan.ln().is_nan());
1596 assert_eq!(inf.ln(), inf);
1597 assert!(neg_inf.ln().is_nan());
1598 assert!((-2.3f64).ln().is_nan());
1599 assert_eq!((-0.0f64).ln(), neg_inf);
1600 assert_eq!(0.0f64.ln(), neg_inf);
1601 assert_approx_eq!(4.0f64.ln(), 1.386294);
1607 let inf: f64 = INFINITY;
1608 let neg_inf: f64 = NEG_INFINITY;
1609 assert_eq!(10.0f64.log(10.0), 1.0);
1610 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1611 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1612 assert!(1.0f64.log(1.0).is_nan());
1613 assert!(1.0f64.log(-13.9).is_nan());
1614 assert!(nan.log(2.3).is_nan());
1615 assert_eq!(inf.log(10.0), inf);
1616 assert!(neg_inf.log(8.8).is_nan());
1617 assert!((-2.3f64).log(0.1).is_nan());
1618 assert_eq!((-0.0f64).log(2.0), neg_inf);
1619 assert_eq!(0.0f64.log(7.0), neg_inf);
1625 let inf: f64 = INFINITY;
1626 let neg_inf: f64 = NEG_INFINITY;
1627 assert_approx_eq!(10.0f64.log2(), 3.321928);
1628 assert_approx_eq!(2.3f64.log2(), 1.201634);
1629 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1630 assert!(nan.log2().is_nan());
1631 assert_eq!(inf.log2(), inf);
1632 assert!(neg_inf.log2().is_nan());
1633 assert!((-2.3f64).log2().is_nan());
1634 assert_eq!((-0.0f64).log2(), neg_inf);
1635 assert_eq!(0.0f64.log2(), neg_inf);
1641 let inf: f64 = INFINITY;
1642 let neg_inf: f64 = NEG_INFINITY;
1643 assert_eq!(10.0f64.log10(), 1.0);
1644 assert_approx_eq!(2.3f64.log10(), 0.361728);
1645 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1646 assert_eq!(1.0f64.log10(), 0.0);
1647 assert!(nan.log10().is_nan());
1648 assert_eq!(inf.log10(), inf);
1649 assert!(neg_inf.log10().is_nan());
1650 assert!((-2.3f64).log10().is_nan());
1651 assert_eq!((-0.0f64).log10(), neg_inf);
1652 assert_eq!(0.0f64.log10(), neg_inf);
1656 fn test_to_degrees() {
1657 let pi: f64 = consts::PI;
1659 let inf: f64 = INFINITY;
1660 let neg_inf: f64 = NEG_INFINITY;
1661 assert_eq!(0.0f64.to_degrees(), 0.0);
1662 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1663 assert_eq!(pi.to_degrees(), 180.0);
1664 assert!(nan.to_degrees().is_nan());
1665 assert_eq!(inf.to_degrees(), inf);
1666 assert_eq!(neg_inf.to_degrees(), neg_inf);
1670 fn test_to_radians() {
1671 let pi: f64 = consts::PI;
1673 let inf: f64 = INFINITY;
1674 let neg_inf: f64 = NEG_INFINITY;
1675 assert_eq!(0.0f64.to_radians(), 0.0);
1676 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1677 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1678 assert_eq!(180.0f64.to_radians(), pi);
1679 assert!(nan.to_radians().is_nan());
1680 assert_eq!(inf.to_radians(), inf);
1681 assert_eq!(neg_inf.to_radians(), neg_inf);
1685 #[allow(deprecated)]
1687 let f1 = 2.0f64.powi(-123);
1688 let f2 = 2.0f64.powi(-111);
1689 let f3 = 1.75 * 2.0f64.powi(-12);
1690 assert_eq!(f64::ldexp(1f64, -123), f1);
1691 assert_eq!(f64::ldexp(1f64, -111), f2);
1692 assert_eq!(f64::ldexp(1.75f64, -12), f3);
1694 assert_eq!(f64::ldexp(0f64, -123), 0f64);
1695 assert_eq!(f64::ldexp(-0f64, -123), -0f64);
1697 let inf: f64 = INFINITY;
1698 let neg_inf: f64 = NEG_INFINITY;
1700 assert_eq!(f64::ldexp(inf, -123), inf);
1701 assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
1702 assert!(f64::ldexp(nan, -123).is_nan());
1706 #[allow(deprecated)]
1708 let f1 = 2.0f64.powi(-123);
1709 let f2 = 2.0f64.powi(-111);
1710 let f3 = 1.75 * 2.0f64.powi(-123);
1711 let (x1, exp1) = f1.frexp();
1712 let (x2, exp2) = f2.frexp();
1713 let (x3, exp3) = f3.frexp();
1714 assert_eq!((x1, exp1), (0.5f64, -122));
1715 assert_eq!((x2, exp2), (0.5f64, -110));
1716 assert_eq!((x3, exp3), (0.875f64, -122));
1717 assert_eq!(f64::ldexp(x1, exp1), f1);
1718 assert_eq!(f64::ldexp(x2, exp2), f2);
1719 assert_eq!(f64::ldexp(x3, exp3), f3);
1721 assert_eq!(0f64.frexp(), (0f64, 0));
1722 assert_eq!((-0f64).frexp(), (-0f64, 0));
1725 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1726 #[allow(deprecated)]
1727 fn test_frexp_nowin() {
1728 let inf: f64 = INFINITY;
1729 let neg_inf: f64 = NEG_INFINITY;
1731 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1732 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1733 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1738 assert_eq!(0.0f64.asinh(), 0.0f64);
1739 assert_eq!((-0.0f64).asinh(), -0.0f64);
1741 let inf: f64 = INFINITY;
1742 let neg_inf: f64 = NEG_INFINITY;
1744 assert_eq!(inf.asinh(), inf);
1745 assert_eq!(neg_inf.asinh(), neg_inf);
1746 assert!(nan.asinh().is_nan());
1747 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1748 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1753 assert_eq!(1.0f64.acosh(), 0.0f64);
1754 assert!(0.999f64.acosh().is_nan());
1756 let inf: f64 = INFINITY;
1757 let neg_inf: f64 = NEG_INFINITY;
1759 assert_eq!(inf.acosh(), inf);
1760 assert!(neg_inf.acosh().is_nan());
1761 assert!(nan.acosh().is_nan());
1762 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1763 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1768 assert_eq!(0.0f64.atanh(), 0.0f64);
1769 assert_eq!((-0.0f64).atanh(), -0.0f64);
1771 let inf: f64 = INFINITY;
1772 let neg_inf: f64 = NEG_INFINITY;
1774 assert_eq!(1.0f64.atanh(), inf);
1775 assert_eq!((-1.0f64).atanh(), neg_inf);
1776 assert!(2f64.atanh().atanh().is_nan());
1777 assert!((-2f64).atanh().atanh().is_nan());
1778 assert!(inf.atanh().is_nan());
1779 assert!(neg_inf.atanh().is_nan());
1780 assert!(nan.atanh().is_nan());
1781 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1782 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1786 fn test_real_consts() {
1788 let pi: f64 = consts::PI;
1789 let frac_pi_2: f64 = consts::FRAC_PI_2;
1790 let frac_pi_3: f64 = consts::FRAC_PI_3;
1791 let frac_pi_4: f64 = consts::FRAC_PI_4;
1792 let frac_pi_6: f64 = consts::FRAC_PI_6;
1793 let frac_pi_8: f64 = consts::FRAC_PI_8;
1794 let frac_1_pi: f64 = consts::FRAC_1_PI;
1795 let frac_2_pi: f64 = consts::FRAC_2_PI;
1796 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1797 let sqrt2: f64 = consts::SQRT_2;
1798 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1799 let e: f64 = consts::E;
1800 let log2_e: f64 = consts::LOG2_E;
1801 let log10_e: f64 = consts::LOG10_E;
1802 let ln_2: f64 = consts::LN_2;
1803 let ln_10: f64 = consts::LN_10;
1805 assert_approx_eq!(frac_pi_2, pi / 2f64);
1806 assert_approx_eq!(frac_pi_3, pi / 3f64);
1807 assert_approx_eq!(frac_pi_4, pi / 4f64);
1808 assert_approx_eq!(frac_pi_6, pi / 6f64);
1809 assert_approx_eq!(frac_pi_8, pi / 8f64);
1810 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1811 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1812 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1813 assert_approx_eq!(sqrt2, 2f64.sqrt());
1814 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1815 assert_approx_eq!(log2_e, e.log2());
1816 assert_approx_eq!(log10_e, e.log10());
1817 assert_approx_eq!(ln_2, 2f64.ln());
1818 assert_approx_eq!(ln_10, 10f64.ln());
1822 fn test_float_bits_conv() {
1823 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1824 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1825 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1826 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1827 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1828 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1829 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1830 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);