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 //! The 64-bit floating point type.
13 //! *[See also the `f64` primitive type](../primitive.f64.html).*
15 #![stable(feature = "rust1", since = "1.0.0")]
16 #![allow(missing_docs)]
24 use num::{FpCategory, ParseFloatError};
26 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
27 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
28 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
29 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
30 pub use core::f64::consts;
34 use libc::{c_double, c_int};
38 pub fn acos(n: c_double) -> c_double;
39 pub fn asin(n: c_double) -> c_double;
40 pub fn atan(n: c_double) -> c_double;
41 pub fn atan2(a: c_double, b: c_double) -> c_double;
42 pub fn cbrt(n: c_double) -> c_double;
43 pub fn cosh(n: c_double) -> c_double;
44 pub fn erf(n: c_double) -> c_double;
45 pub fn erfc(n: c_double) -> c_double;
46 pub fn expm1(n: c_double) -> c_double;
47 pub fn fdim(a: c_double, b: c_double) -> c_double;
48 pub fn fmax(a: c_double, b: c_double) -> c_double;
49 pub fn fmin(a: c_double, b: c_double) -> c_double;
50 pub fn fmod(a: c_double, b: c_double) -> c_double;
51 pub fn nextafter(x: c_double, y: c_double) -> c_double;
52 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
53 pub fn ldexp(x: c_double, n: c_int) -> c_double;
54 pub fn logb(n: c_double) -> c_double;
55 pub fn log1p(n: c_double) -> c_double;
56 pub fn ilogb(n: c_double) -> c_int;
57 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
58 pub fn sinh(n: c_double) -> c_double;
59 pub fn tan(n: c_double) -> c_double;
60 pub fn tanh(n: c_double) -> c_double;
61 pub fn tgamma(n: c_double) -> c_double;
63 // These are commonly only available for doubles
65 pub fn j0(n: c_double) -> c_double;
66 pub fn j1(n: c_double) -> c_double;
67 pub fn jn(i: c_int, n: c_double) -> c_double;
69 pub fn y0(n: c_double) -> c_double;
70 pub fn y1(n: c_double) -> c_double;
71 pub fn yn(i: c_int, n: c_double) -> c_double;
73 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
74 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
76 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
77 pub fn hypot(x: c_double, y: c_double) -> c_double;
83 #[stable(feature = "rust1", since = "1.0.0")]
85 /// Parses a float as with a given radix
86 #[unstable(feature = "float_from_str_radix", reason = "recently moved API")]
87 pub fn from_str_radix(s: &str, radix: u32) -> Result<f64, ParseFloatError> {
88 num::Float::from_str_radix(s, radix)
91 /// Returns `true` if this value is `NaN` and false otherwise.
96 /// let nan = f64::NAN;
99 /// assert!(nan.is_nan());
100 /// assert!(!f.is_nan());
102 #[stable(feature = "rust1", since = "1.0.0")]
104 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
106 /// Returns `true` if this value is positive infinity or negative infinity and
113 /// let inf = f64::INFINITY;
114 /// let neg_inf = f64::NEG_INFINITY;
115 /// let nan = f64::NAN;
117 /// assert!(!f.is_infinite());
118 /// assert!(!nan.is_infinite());
120 /// assert!(inf.is_infinite());
121 /// assert!(neg_inf.is_infinite());
123 #[stable(feature = "rust1", since = "1.0.0")]
125 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
127 /// Returns `true` if this number is neither infinite nor `NaN`.
133 /// let inf: f64 = f64::INFINITY;
134 /// let neg_inf: f64 = f64::NEG_INFINITY;
135 /// let nan: f64 = f64::NAN;
137 /// assert!(f.is_finite());
139 /// assert!(!nan.is_finite());
140 /// assert!(!inf.is_finite());
141 /// assert!(!neg_inf.is_finite());
143 #[stable(feature = "rust1", since = "1.0.0")]
145 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
147 /// Returns `true` if the number is neither zero, infinite,
148 /// [subnormal][subnormal], or `NaN`.
153 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f64
154 /// let max = f32::MAX;
155 /// let lower_than_min = 1.0e-40_f32;
156 /// let zero = 0.0f32;
158 /// assert!(min.is_normal());
159 /// assert!(max.is_normal());
161 /// assert!(!zero.is_normal());
162 /// assert!(!f32::NAN.is_normal());
163 /// assert!(!f32::INFINITY.is_normal());
164 /// // Values between `0` and `min` are Subnormal.
165 /// assert!(!lower_than_min.is_normal());
167 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
168 #[stable(feature = "rust1", since = "1.0.0")]
170 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
172 /// Returns the floating point category of the number. If only one property
173 /// is going to be tested, it is generally faster to use the specific
174 /// predicate instead.
177 /// use std::num::FpCategory;
180 /// let num = 12.4_f64;
181 /// let inf = f64::INFINITY;
183 /// assert_eq!(num.classify(), FpCategory::Normal);
184 /// assert_eq!(inf.classify(), FpCategory::Infinite);
186 #[stable(feature = "rust1", since = "1.0.0")]
188 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
190 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
191 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
192 /// The floating point encoding is documented in the [Reference][floating-point].
195 /// #![feature(float_extras)]
197 /// let num = 2.0f64;
199 /// // (8388608, -22, 1)
200 /// let (mantissa, exponent, sign) = num.integer_decode();
201 /// let sign_f = sign as f64;
202 /// let mantissa_f = mantissa as f64;
203 /// let exponent_f = num.powf(exponent as f64);
205 /// // 1 * 8388608 * 2^(-22) == 2
206 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
208 /// assert!(abs_difference < 1e-10);
210 /// [floating-point]: ../../../../../reference.html#machine-types
211 #[unstable(feature = "float_extras", reason = "signature is undecided")]
213 pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
215 /// Returns the largest integer less than or equal to a number.
218 /// let f = 3.99_f64;
221 /// assert_eq!(f.floor(), 3.0);
222 /// assert_eq!(g.floor(), 3.0);
224 #[stable(feature = "rust1", since = "1.0.0")]
226 pub fn floor(self) -> f64 { num::Float::floor(self) }
228 /// Returns the smallest integer greater than or equal to a number.
231 /// let f = 3.01_f64;
234 /// assert_eq!(f.ceil(), 4.0);
235 /// assert_eq!(g.ceil(), 4.0);
237 #[stable(feature = "rust1", since = "1.0.0")]
239 pub fn ceil(self) -> f64 { num::Float::ceil(self) }
241 /// Returns the nearest integer to a number. Round half-way cases away from
246 /// let g = -3.3_f64;
248 /// assert_eq!(f.round(), 3.0);
249 /// assert_eq!(g.round(), -3.0);
251 #[stable(feature = "rust1", since = "1.0.0")]
253 pub fn round(self) -> f64 { num::Float::round(self) }
255 /// Returns the integer part of a number.
259 /// let g = -3.7_f64;
261 /// assert_eq!(f.trunc(), 3.0);
262 /// assert_eq!(g.trunc(), -3.0);
264 #[stable(feature = "rust1", since = "1.0.0")]
266 pub fn trunc(self) -> f64 { num::Float::trunc(self) }
268 /// Returns the fractional part of a number.
272 /// let y = -3.5_f64;
273 /// let abs_difference_x = (x.fract() - 0.5).abs();
274 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
276 /// assert!(abs_difference_x < 1e-10);
277 /// assert!(abs_difference_y < 1e-10);
279 #[stable(feature = "rust1", since = "1.0.0")]
281 pub fn fract(self) -> f64 { num::Float::fract(self) }
283 /// Computes the absolute value of `self`. Returns `NAN` if the
290 /// let y = -3.5_f64;
292 /// let abs_difference_x = (x.abs() - x).abs();
293 /// let abs_difference_y = (y.abs() - (-y)).abs();
295 /// assert!(abs_difference_x < 1e-10);
296 /// assert!(abs_difference_y < 1e-10);
298 /// assert!(f64::NAN.abs().is_nan());
300 #[stable(feature = "rust1", since = "1.0.0")]
302 pub fn abs(self) -> f64 { num::Float::abs(self) }
304 /// Returns a number that represents the sign of `self`.
306 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
307 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
308 /// - `NAN` if the number is `NAN`
315 /// assert_eq!(f.signum(), 1.0);
316 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
318 /// assert!(f64::NAN.signum().is_nan());
320 #[stable(feature = "rust1", since = "1.0.0")]
322 pub fn signum(self) -> f64 { num::Float::signum(self) }
324 /// Returns `true` if `self`'s sign bit is positive, including
325 /// `+0.0` and `INFINITY`.
330 /// let nan: f64 = f64::NAN;
333 /// let g = -7.0_f64;
335 /// assert!(f.is_sign_positive());
336 /// assert!(!g.is_sign_positive());
337 /// // Requires both tests to determine if is `NaN`
338 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
340 #[stable(feature = "rust1", since = "1.0.0")]
342 pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) }
344 #[stable(feature = "rust1", since = "1.0.0")]
345 #[deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
347 pub fn is_positive(self) -> bool { num::Float::is_positive(self) }
349 /// Returns `true` if `self`'s sign is negative, including `-0.0`
350 /// and `NEG_INFINITY`.
355 /// let nan = f64::NAN;
358 /// let g = -7.0_f64;
360 /// assert!(!f.is_sign_negative());
361 /// assert!(g.is_sign_negative());
362 /// // Requires both tests to determine if is `NaN`.
363 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
365 #[stable(feature = "rust1", since = "1.0.0")]
367 pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) }
369 #[stable(feature = "rust1", since = "1.0.0")]
370 #[deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
372 pub fn is_negative(self) -> bool { num::Float::is_negative(self) }
374 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
375 /// error. This produces a more accurate result with better performance than
376 /// a separate multiplication operation followed by an add.
379 /// let m = 10.0_f64;
381 /// let b = 60.0_f64;
384 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
386 /// assert!(abs_difference < 1e-10);
388 #[stable(feature = "rust1", since = "1.0.0")]
390 pub fn mul_add(self, a: f64, b: f64) -> f64 { num::Float::mul_add(self, a, b) }
392 /// Takes the reciprocal (inverse) of a number, `1/x`.
396 /// let abs_difference = (x.recip() - (1.0/x)).abs();
398 /// assert!(abs_difference < 1e-10);
400 #[stable(feature = "rust1", since = "1.0.0")]
402 pub fn recip(self) -> f64 { num::Float::recip(self) }
404 /// Raises a number to an integer power.
406 /// Using this function is generally faster than using `powf`
410 /// let abs_difference = (x.powi(2) - x*x).abs();
412 /// assert!(abs_difference < 1e-10);
414 #[stable(feature = "rust1", since = "1.0.0")]
416 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
418 /// Raises a number to a floating point power.
422 /// let abs_difference = (x.powf(2.0) - x*x).abs();
424 /// assert!(abs_difference < 1e-10);
426 #[stable(feature = "rust1", since = "1.0.0")]
428 pub fn powf(self, n: f64) -> f64 { num::Float::powf(self, n) }
430 /// Takes the square root of a number.
432 /// Returns NaN if `self` is a negative number.
435 /// let positive = 4.0_f64;
436 /// let negative = -4.0_f64;
438 /// let abs_difference = (positive.sqrt() - 2.0).abs();
440 /// assert!(abs_difference < 1e-10);
441 /// assert!(negative.sqrt().is_nan());
443 #[stable(feature = "rust1", since = "1.0.0")]
445 pub fn sqrt(self) -> f64 { num::Float::sqrt(self) }
447 /// Returns `e^(self)`, (the exponential function).
450 /// let one = 1.0_f64;
452 /// let e = one.exp();
454 /// // ln(e) - 1 == 0
455 /// let abs_difference = (e.ln() - 1.0).abs();
457 /// assert!(abs_difference < 1e-10);
459 #[stable(feature = "rust1", since = "1.0.0")]
461 pub fn exp(self) -> f64 { num::Float::exp(self) }
463 /// Returns `2^(self)`.
469 /// let abs_difference = (f.exp2() - 4.0).abs();
471 /// assert!(abs_difference < 1e-10);
473 #[stable(feature = "rust1", since = "1.0.0")]
475 pub fn exp2(self) -> f64 { num::Float::exp2(self) }
477 /// Returns the natural logarithm of the number.
480 /// let one = 1.0_f64;
482 /// let e = one.exp();
484 /// // ln(e) - 1 == 0
485 /// let abs_difference = (e.ln() - 1.0).abs();
487 /// assert!(abs_difference < 1e-10);
489 #[stable(feature = "rust1", since = "1.0.0")]
491 pub fn ln(self) -> f64 { num::Float::ln(self) }
493 /// Returns the logarithm of the number with respect to an arbitrary base.
496 /// let ten = 10.0_f64;
497 /// let two = 2.0_f64;
499 /// // log10(10) - 1 == 0
500 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
502 /// // log2(2) - 1 == 0
503 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
505 /// assert!(abs_difference_10 < 1e-10);
506 /// assert!(abs_difference_2 < 1e-10);
508 #[stable(feature = "rust1", since = "1.0.0")]
510 pub fn log(self, base: f64) -> f64 { num::Float::log(self, base) }
512 /// Returns the base 2 logarithm of the number.
515 /// let two = 2.0_f64;
517 /// // log2(2) - 1 == 0
518 /// let abs_difference = (two.log2() - 1.0).abs();
520 /// assert!(abs_difference < 1e-10);
522 #[stable(feature = "rust1", since = "1.0.0")]
524 pub fn log2(self) -> f64 { num::Float::log2(self) }
526 /// Returns the base 10 logarithm of the number.
529 /// let ten = 10.0_f64;
531 /// // log10(10) - 1 == 0
532 /// let abs_difference = (ten.log10() - 1.0).abs();
534 /// assert!(abs_difference < 1e-10);
536 #[stable(feature = "rust1", since = "1.0.0")]
538 pub fn log10(self) -> f64 { num::Float::log10(self) }
540 /// Converts radians to degrees.
543 /// use std::f64::consts;
545 /// let angle = consts::PI;
547 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
549 /// assert!(abs_difference < 1e-10);
551 #[stable(feature = "rust1", since = "1.0.0")]
553 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
555 /// Converts degrees to radians.
558 /// use std::f64::consts;
560 /// let angle = 180.0_f64;
562 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
564 /// assert!(abs_difference < 1e-10);
566 #[stable(feature = "rust1", since = "1.0.0")]
568 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
570 /// Constructs a floating point number of `x*2^exp`.
573 /// #![feature(float_extras)]
575 /// // 3*2^2 - 12 == 0
576 /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
578 /// assert!(abs_difference < 1e-10);
580 #[unstable(feature = "float_extras",
581 reason = "pending integer conventions")]
583 pub fn ldexp(x: f64, exp: isize) -> f64 {
584 unsafe { cmath::ldexp(x, exp as c_int) }
587 /// Breaks the number into a normalized fraction and a base-2 exponent,
590 /// * `self = x * 2^exp`
591 /// * `0.5 <= abs(x) < 1.0`
594 /// #![feature(float_extras)]
598 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
599 /// let f = x.frexp();
600 /// let abs_difference_0 = (f.0 - 0.5).abs();
601 /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
603 /// assert!(abs_difference_0 < 1e-10);
604 /// assert!(abs_difference_1 < 1e-10);
606 #[unstable(feature = "float_extras",
607 reason = "pending integer conventions")]
609 pub fn frexp(self) -> (f64, isize) {
612 let x = cmath::frexp(self, &mut exp);
617 /// Returns the next representable floating-point value in the direction of
621 /// #![feature(float_extras)]
625 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
627 /// assert!(abs_diff < 1e-10);
629 #[unstable(feature = "float_extras",
630 reason = "unsure about its place in the world")]
632 pub fn next_after(self, other: f64) -> f64 {
633 unsafe { cmath::nextafter(self, other) }
636 /// Returns the maximum of the two numbers.
642 /// assert_eq!(x.max(y), y);
645 /// If one of the arguments is NaN, then the other argument is returned.
646 #[stable(feature = "rust1", since = "1.0.0")]
648 pub fn max(self, other: f64) -> f64 {
649 unsafe { cmath::fmax(self, other) }
652 /// Returns the minimum of the two numbers.
658 /// assert_eq!(x.min(y), x);
661 /// If one of the arguments is NaN, then the other argument is returned.
662 #[stable(feature = "rust1", since = "1.0.0")]
664 pub fn min(self, other: f64) -> f64 {
665 unsafe { cmath::fmin(self, other) }
668 /// The positive difference of two numbers.
670 /// * If `self <= other`: `0:0`
671 /// * Else: `self - other`
675 /// let y = -3.0_f64;
677 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
678 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
680 /// assert!(abs_difference_x < 1e-10);
681 /// assert!(abs_difference_y < 1e-10);
683 #[stable(feature = "rust1", since = "1.0.0")]
685 pub fn abs_sub(self, other: f64) -> f64 {
686 unsafe { cmath::fdim(self, other) }
689 /// Takes the cubic root of a number.
694 /// // x^(1/3) - 2 == 0
695 /// let abs_difference = (x.cbrt() - 2.0).abs();
697 /// assert!(abs_difference < 1e-10);
699 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn cbrt(self) -> f64 {
702 unsafe { cmath::cbrt(self) }
705 /// Calculates the length of the hypotenuse of a right-angle triangle given
706 /// legs of length `x` and `y`.
712 /// // sqrt(x^2 + y^2)
713 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
715 /// assert!(abs_difference < 1e-10);
717 #[stable(feature = "rust1", since = "1.0.0")]
719 pub fn hypot(self, other: f64) -> f64 {
720 unsafe { cmath::hypot(self, other) }
723 /// Computes the sine of a number (in radians).
728 /// let x = f64::consts::PI/2.0;
730 /// let abs_difference = (x.sin() - 1.0).abs();
732 /// assert!(abs_difference < 1e-10);
734 #[stable(feature = "rust1", since = "1.0.0")]
736 pub fn sin(self) -> f64 {
737 unsafe { intrinsics::sinf64(self) }
740 /// Computes the cosine of a number (in radians).
745 /// let x = 2.0*f64::consts::PI;
747 /// let abs_difference = (x.cos() - 1.0).abs();
749 /// assert!(abs_difference < 1e-10);
751 #[stable(feature = "rust1", since = "1.0.0")]
753 pub fn cos(self) -> f64 {
754 unsafe { intrinsics::cosf64(self) }
757 /// Computes the tangent of a number (in radians).
762 /// let x = f64::consts::PI/4.0;
763 /// let abs_difference = (x.tan() - 1.0).abs();
765 /// assert!(abs_difference < 1e-14);
767 #[stable(feature = "rust1", since = "1.0.0")]
769 pub fn tan(self) -> f64 {
770 unsafe { cmath::tan(self) }
773 /// Computes the arcsine of a number. Return value is in radians in
774 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
780 /// let f = f64::consts::PI / 2.0;
782 /// // asin(sin(pi/2))
783 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
785 /// assert!(abs_difference < 1e-10);
787 #[stable(feature = "rust1", since = "1.0.0")]
789 pub fn asin(self) -> f64 {
790 unsafe { cmath::asin(self) }
793 /// Computes the arccosine of a number. Return value is in radians in
794 /// the range [0, pi] or NaN if the number is outside the range
800 /// let f = f64::consts::PI / 4.0;
802 /// // acos(cos(pi/4))
803 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
805 /// assert!(abs_difference < 1e-10);
807 #[stable(feature = "rust1", since = "1.0.0")]
809 pub fn acos(self) -> f64 {
810 unsafe { cmath::acos(self) }
813 /// Computes the arctangent of a number. Return value is in radians in the
814 /// range [-pi/2, pi/2];
820 /// let abs_difference = (f.tan().atan() - 1.0).abs();
822 /// assert!(abs_difference < 1e-10);
824 #[stable(feature = "rust1", since = "1.0.0")]
826 pub fn atan(self) -> f64 {
827 unsafe { cmath::atan(self) }
830 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
832 /// * `x = 0`, `y = 0`: `0`
833 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
834 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
835 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
840 /// let pi = f64::consts::PI;
841 /// // All angles from horizontal right (+x)
842 /// // 45 deg counter-clockwise
843 /// let x1 = 3.0_f64;
844 /// let y1 = -3.0_f64;
846 /// // 135 deg clockwise
847 /// let x2 = -3.0_f64;
848 /// let y2 = 3.0_f64;
850 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
851 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
853 /// assert!(abs_difference_1 < 1e-10);
854 /// assert!(abs_difference_2 < 1e-10);
856 #[stable(feature = "rust1", since = "1.0.0")]
858 pub fn atan2(self, other: f64) -> f64 {
859 unsafe { cmath::atan2(self, other) }
862 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
863 /// `(sin(x), cos(x))`.
868 /// let x = f64::consts::PI/4.0;
869 /// let f = x.sin_cos();
871 /// let abs_difference_0 = (f.0 - x.sin()).abs();
872 /// let abs_difference_1 = (f.1 - x.cos()).abs();
874 /// assert!(abs_difference_0 < 1e-10);
875 /// assert!(abs_difference_0 < 1e-10);
877 #[stable(feature = "rust1", since = "1.0.0")]
879 pub fn sin_cos(self) -> (f64, f64) {
880 (self.sin(), self.cos())
883 /// Returns `e^(self) - 1` in a way that is accurate even if the
884 /// number is close to zero.
890 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
892 /// assert!(abs_difference < 1e-10);
894 #[stable(feature = "rust1", since = "1.0.0")]
896 pub fn exp_m1(self) -> f64 {
897 unsafe { cmath::expm1(self) }
900 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
901 /// the operations were performed separately.
906 /// let x = f64::consts::E - 1.0;
908 /// // ln(1 + (e - 1)) == ln(e) == 1
909 /// let abs_difference = (x.ln_1p() - 1.0).abs();
911 /// assert!(abs_difference < 1e-10);
913 #[stable(feature = "rust1", since = "1.0.0")]
915 pub fn ln_1p(self) -> f64 {
916 unsafe { cmath::log1p(self) }
919 /// Hyperbolic sine function.
924 /// let e = f64::consts::E;
927 /// let f = x.sinh();
928 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
929 /// let g = (e*e - 1.0)/(2.0*e);
930 /// let abs_difference = (f - g).abs();
932 /// assert!(abs_difference < 1e-10);
934 #[stable(feature = "rust1", since = "1.0.0")]
936 pub fn sinh(self) -> f64 {
937 unsafe { cmath::sinh(self) }
940 /// Hyperbolic cosine function.
945 /// let e = f64::consts::E;
947 /// let f = x.cosh();
948 /// // Solving cosh() at 1 gives this result
949 /// let g = (e*e + 1.0)/(2.0*e);
950 /// let abs_difference = (f - g).abs();
953 /// assert!(abs_difference < 1.0e-10);
955 #[stable(feature = "rust1", since = "1.0.0")]
957 pub fn cosh(self) -> f64 {
958 unsafe { cmath::cosh(self) }
961 /// Hyperbolic tangent function.
966 /// let e = f64::consts::E;
969 /// let f = x.tanh();
970 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
971 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
972 /// let abs_difference = (f - g).abs();
974 /// assert!(abs_difference < 1.0e-10);
976 #[stable(feature = "rust1", since = "1.0.0")]
978 pub fn tanh(self) -> f64 {
979 unsafe { cmath::tanh(self) }
982 /// Inverse hyperbolic sine function.
986 /// let f = x.sinh().asinh();
988 /// let abs_difference = (f - x).abs();
990 /// assert!(abs_difference < 1.0e-10);
992 #[stable(feature = "rust1", since = "1.0.0")]
994 pub fn asinh(self) -> f64 {
996 NEG_INFINITY => NEG_INFINITY,
997 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1001 /// Inverse hyperbolic cosine function.
1004 /// let x = 1.0_f64;
1005 /// let f = x.cosh().acosh();
1007 /// let abs_difference = (f - x).abs();
1009 /// assert!(abs_difference < 1.0e-10);
1011 #[stable(feature = "rust1", since = "1.0.0")]
1013 pub fn acosh(self) -> f64 {
1015 x if x < 1.0 => NAN,
1016 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1020 /// Inverse hyperbolic tangent function.
1025 /// let e = f64::consts::E;
1026 /// let f = e.tanh().atanh();
1028 /// let abs_difference = (f - e).abs();
1030 /// assert!(abs_difference < 1.0e-10);
1032 #[stable(feature = "rust1", since = "1.0.0")]
1034 pub fn atanh(self) -> f64 {
1035 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1044 use num::FpCategory as Fp;
1048 test_num(10f64, 2f64);
1053 assert_eq!(NAN.min(2.0), 2.0);
1054 assert_eq!(2.0f64.min(NAN), 2.0);
1059 assert_eq!(NAN.max(2.0), 2.0);
1060 assert_eq!(2.0f64.max(NAN), 2.0);
1066 assert!(nan.is_nan());
1067 assert!(!nan.is_infinite());
1068 assert!(!nan.is_finite());
1069 assert!(!nan.is_normal());
1070 assert!(!nan.is_sign_positive());
1071 assert!(!nan.is_sign_negative());
1072 assert_eq!(Fp::Nan, nan.classify());
1076 fn test_infinity() {
1077 let inf: f64 = INFINITY;
1078 assert!(inf.is_infinite());
1079 assert!(!inf.is_finite());
1080 assert!(inf.is_sign_positive());
1081 assert!(!inf.is_sign_negative());
1082 assert!(!inf.is_nan());
1083 assert!(!inf.is_normal());
1084 assert_eq!(Fp::Infinite, inf.classify());
1088 fn test_neg_infinity() {
1089 let neg_inf: f64 = NEG_INFINITY;
1090 assert!(neg_inf.is_infinite());
1091 assert!(!neg_inf.is_finite());
1092 assert!(!neg_inf.is_sign_positive());
1093 assert!(neg_inf.is_sign_negative());
1094 assert!(!neg_inf.is_nan());
1095 assert!(!neg_inf.is_normal());
1096 assert_eq!(Fp::Infinite, neg_inf.classify());
1101 let zero: f64 = 0.0f64;
1102 assert_eq!(0.0, zero);
1103 assert!(!zero.is_infinite());
1104 assert!(zero.is_finite());
1105 assert!(zero.is_sign_positive());
1106 assert!(!zero.is_sign_negative());
1107 assert!(!zero.is_nan());
1108 assert!(!zero.is_normal());
1109 assert_eq!(Fp::Zero, zero.classify());
1113 fn test_neg_zero() {
1114 let neg_zero: f64 = -0.0;
1115 assert_eq!(0.0, neg_zero);
1116 assert!(!neg_zero.is_infinite());
1117 assert!(neg_zero.is_finite());
1118 assert!(!neg_zero.is_sign_positive());
1119 assert!(neg_zero.is_sign_negative());
1120 assert!(!neg_zero.is_nan());
1121 assert!(!neg_zero.is_normal());
1122 assert_eq!(Fp::Zero, neg_zero.classify());
1127 let one: f64 = 1.0f64;
1128 assert_eq!(1.0, one);
1129 assert!(!one.is_infinite());
1130 assert!(one.is_finite());
1131 assert!(one.is_sign_positive());
1132 assert!(!one.is_sign_negative());
1133 assert!(!one.is_nan());
1134 assert!(one.is_normal());
1135 assert_eq!(Fp::Normal, one.classify());
1141 let inf: f64 = INFINITY;
1142 let neg_inf: f64 = NEG_INFINITY;
1143 assert!(nan.is_nan());
1144 assert!(!0.0f64.is_nan());
1145 assert!(!5.3f64.is_nan());
1146 assert!(!(-10.732f64).is_nan());
1147 assert!(!inf.is_nan());
1148 assert!(!neg_inf.is_nan());
1152 fn test_is_infinite() {
1154 let inf: f64 = INFINITY;
1155 let neg_inf: f64 = NEG_INFINITY;
1156 assert!(!nan.is_infinite());
1157 assert!(inf.is_infinite());
1158 assert!(neg_inf.is_infinite());
1159 assert!(!0.0f64.is_infinite());
1160 assert!(!42.8f64.is_infinite());
1161 assert!(!(-109.2f64).is_infinite());
1165 fn test_is_finite() {
1167 let inf: f64 = INFINITY;
1168 let neg_inf: f64 = NEG_INFINITY;
1169 assert!(!nan.is_finite());
1170 assert!(!inf.is_finite());
1171 assert!(!neg_inf.is_finite());
1172 assert!(0.0f64.is_finite());
1173 assert!(42.8f64.is_finite());
1174 assert!((-109.2f64).is_finite());
1178 fn test_is_normal() {
1180 let inf: f64 = INFINITY;
1181 let neg_inf: f64 = NEG_INFINITY;
1182 let zero: f64 = 0.0f64;
1183 let neg_zero: f64 = -0.0;
1184 assert!(!nan.is_normal());
1185 assert!(!inf.is_normal());
1186 assert!(!neg_inf.is_normal());
1187 assert!(!zero.is_normal());
1188 assert!(!neg_zero.is_normal());
1189 assert!(1f64.is_normal());
1190 assert!(1e-307f64.is_normal());
1191 assert!(!1e-308f64.is_normal());
1195 fn test_classify() {
1197 let inf: f64 = INFINITY;
1198 let neg_inf: f64 = NEG_INFINITY;
1199 let zero: f64 = 0.0f64;
1200 let neg_zero: f64 = -0.0;
1201 assert_eq!(nan.classify(), Fp::Nan);
1202 assert_eq!(inf.classify(), Fp::Infinite);
1203 assert_eq!(neg_inf.classify(), Fp::Infinite);
1204 assert_eq!(zero.classify(), Fp::Zero);
1205 assert_eq!(neg_zero.classify(), Fp::Zero);
1206 assert_eq!(1e-307f64.classify(), Fp::Normal);
1207 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1211 fn test_integer_decode() {
1212 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
1213 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
1214 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
1215 assert_eq!(0f64.integer_decode(), (0, -1075, 1));
1216 assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
1217 assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
1218 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1219 assert_eq!(NAN.integer_decode(), (6755399441055744, 972, 1));
1224 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1225 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1226 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1227 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1228 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1229 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1230 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1231 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1232 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1233 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1238 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1239 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1240 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1241 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1242 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1243 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1244 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1245 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1246 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1247 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1252 assert_approx_eq!(1.0f64.round(), 1.0f64);
1253 assert_approx_eq!(1.3f64.round(), 1.0f64);
1254 assert_approx_eq!(1.5f64.round(), 2.0f64);
1255 assert_approx_eq!(1.7f64.round(), 2.0f64);
1256 assert_approx_eq!(0.0f64.round(), 0.0f64);
1257 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1258 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1259 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1260 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1261 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1266 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1267 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1268 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1269 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1270 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1271 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1272 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1273 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1274 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1275 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1280 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1281 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1282 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1283 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1284 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1285 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1286 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1287 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1288 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1289 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1294 assert_eq!(INFINITY.abs(), INFINITY);
1295 assert_eq!(1f64.abs(), 1f64);
1296 assert_eq!(0f64.abs(), 0f64);
1297 assert_eq!((-0f64).abs(), 0f64);
1298 assert_eq!((-1f64).abs(), 1f64);
1299 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1300 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1301 assert!(NAN.abs().is_nan());
1306 assert_eq!(INFINITY.signum(), 1f64);
1307 assert_eq!(1f64.signum(), 1f64);
1308 assert_eq!(0f64.signum(), 1f64);
1309 assert_eq!((-0f64).signum(), -1f64);
1310 assert_eq!((-1f64).signum(), -1f64);
1311 assert_eq!(NEG_INFINITY.signum(), -1f64);
1312 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1313 assert!(NAN.signum().is_nan());
1317 fn test_is_sign_positive() {
1318 assert!(INFINITY.is_sign_positive());
1319 assert!(1f64.is_sign_positive());
1320 assert!(0f64.is_sign_positive());
1321 assert!(!(-0f64).is_sign_positive());
1322 assert!(!(-1f64).is_sign_positive());
1323 assert!(!NEG_INFINITY.is_sign_positive());
1324 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1325 assert!(!NAN.is_sign_positive());
1329 fn test_is_sign_negative() {
1330 assert!(!INFINITY.is_sign_negative());
1331 assert!(!1f64.is_sign_negative());
1332 assert!(!0f64.is_sign_negative());
1333 assert!((-0f64).is_sign_negative());
1334 assert!((-1f64).is_sign_negative());
1335 assert!(NEG_INFINITY.is_sign_negative());
1336 assert!((1f64/NEG_INFINITY).is_sign_negative());
1337 assert!(!NAN.is_sign_negative());
1343 let inf: f64 = INFINITY;
1344 let neg_inf: f64 = NEG_INFINITY;
1345 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1346 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1347 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1348 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1349 assert!(nan.mul_add(7.8, 9.0).is_nan());
1350 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1351 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1352 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1353 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1359 let inf: f64 = INFINITY;
1360 let neg_inf: f64 = NEG_INFINITY;
1361 assert_eq!(1.0f64.recip(), 1.0);
1362 assert_eq!(2.0f64.recip(), 0.5);
1363 assert_eq!((-0.4f64).recip(), -2.5);
1364 assert_eq!(0.0f64.recip(), inf);
1365 assert!(nan.recip().is_nan());
1366 assert_eq!(inf.recip(), 0.0);
1367 assert_eq!(neg_inf.recip(), 0.0);
1373 let inf: f64 = INFINITY;
1374 let neg_inf: f64 = NEG_INFINITY;
1375 assert_eq!(1.0f64.powi(1), 1.0);
1376 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1377 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1378 assert_eq!(8.3f64.powi(0), 1.0);
1379 assert!(nan.powi(2).is_nan());
1380 assert_eq!(inf.powi(3), inf);
1381 assert_eq!(neg_inf.powi(2), inf);
1387 let inf: f64 = INFINITY;
1388 let neg_inf: f64 = NEG_INFINITY;
1389 assert_eq!(1.0f64.powf(1.0), 1.0);
1390 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1391 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1392 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1393 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1394 assert_eq!(8.3f64.powf(0.0), 1.0);
1395 assert!(nan.powf(2.0).is_nan());
1396 assert_eq!(inf.powf(2.0), inf);
1397 assert_eq!(neg_inf.powf(3.0), neg_inf);
1401 fn test_sqrt_domain() {
1402 assert!(NAN.sqrt().is_nan());
1403 assert!(NEG_INFINITY.sqrt().is_nan());
1404 assert!((-1.0f64).sqrt().is_nan());
1405 assert_eq!((-0.0f64).sqrt(), -0.0);
1406 assert_eq!(0.0f64.sqrt(), 0.0);
1407 assert_eq!(1.0f64.sqrt(), 1.0);
1408 assert_eq!(INFINITY.sqrt(), INFINITY);
1413 assert_eq!(1.0, 0.0f64.exp());
1414 assert_approx_eq!(2.718282, 1.0f64.exp());
1415 assert_approx_eq!(148.413159, 5.0f64.exp());
1417 let inf: f64 = INFINITY;
1418 let neg_inf: f64 = NEG_INFINITY;
1420 assert_eq!(inf, inf.exp());
1421 assert_eq!(0.0, neg_inf.exp());
1422 assert!(nan.exp().is_nan());
1427 assert_eq!(32.0, 5.0f64.exp2());
1428 assert_eq!(1.0, 0.0f64.exp2());
1430 let inf: f64 = INFINITY;
1431 let neg_inf: f64 = NEG_INFINITY;
1433 assert_eq!(inf, inf.exp2());
1434 assert_eq!(0.0, neg_inf.exp2());
1435 assert!(nan.exp2().is_nan());
1441 let inf: f64 = INFINITY;
1442 let neg_inf: f64 = NEG_INFINITY;
1443 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1444 assert!(nan.ln().is_nan());
1445 assert_eq!(inf.ln(), inf);
1446 assert!(neg_inf.ln().is_nan());
1447 assert!((-2.3f64).ln().is_nan());
1448 assert_eq!((-0.0f64).ln(), neg_inf);
1449 assert_eq!(0.0f64.ln(), neg_inf);
1450 assert_approx_eq!(4.0f64.ln(), 1.386294);
1456 let inf: f64 = INFINITY;
1457 let neg_inf: f64 = NEG_INFINITY;
1458 assert_eq!(10.0f64.log(10.0), 1.0);
1459 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1460 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1461 assert!(1.0f64.log(1.0).is_nan());
1462 assert!(1.0f64.log(-13.9).is_nan());
1463 assert!(nan.log(2.3).is_nan());
1464 assert_eq!(inf.log(10.0), inf);
1465 assert!(neg_inf.log(8.8).is_nan());
1466 assert!((-2.3f64).log(0.1).is_nan());
1467 assert_eq!((-0.0f64).log(2.0), neg_inf);
1468 assert_eq!(0.0f64.log(7.0), neg_inf);
1474 let inf: f64 = INFINITY;
1475 let neg_inf: f64 = NEG_INFINITY;
1476 assert_approx_eq!(10.0f64.log2(), 3.321928);
1477 assert_approx_eq!(2.3f64.log2(), 1.201634);
1478 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1479 assert!(nan.log2().is_nan());
1480 assert_eq!(inf.log2(), inf);
1481 assert!(neg_inf.log2().is_nan());
1482 assert!((-2.3f64).log2().is_nan());
1483 assert_eq!((-0.0f64).log2(), neg_inf);
1484 assert_eq!(0.0f64.log2(), neg_inf);
1490 let inf: f64 = INFINITY;
1491 let neg_inf: f64 = NEG_INFINITY;
1492 assert_eq!(10.0f64.log10(), 1.0);
1493 assert_approx_eq!(2.3f64.log10(), 0.361728);
1494 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1495 assert_eq!(1.0f64.log10(), 0.0);
1496 assert!(nan.log10().is_nan());
1497 assert_eq!(inf.log10(), inf);
1498 assert!(neg_inf.log10().is_nan());
1499 assert!((-2.3f64).log10().is_nan());
1500 assert_eq!((-0.0f64).log10(), neg_inf);
1501 assert_eq!(0.0f64.log10(), neg_inf);
1505 fn test_to_degrees() {
1506 let pi: f64 = consts::PI;
1508 let inf: f64 = INFINITY;
1509 let neg_inf: f64 = NEG_INFINITY;
1510 assert_eq!(0.0f64.to_degrees(), 0.0);
1511 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1512 assert_eq!(pi.to_degrees(), 180.0);
1513 assert!(nan.to_degrees().is_nan());
1514 assert_eq!(inf.to_degrees(), inf);
1515 assert_eq!(neg_inf.to_degrees(), neg_inf);
1519 fn test_to_radians() {
1520 let pi: f64 = consts::PI;
1522 let inf: f64 = INFINITY;
1523 let neg_inf: f64 = NEG_INFINITY;
1524 assert_eq!(0.0f64.to_radians(), 0.0);
1525 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1526 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1527 assert_eq!(180.0f64.to_radians(), pi);
1528 assert!(nan.to_radians().is_nan());
1529 assert_eq!(inf.to_radians(), inf);
1530 assert_eq!(neg_inf.to_radians(), neg_inf);
1535 // We have to use from_str until base-2 exponents
1536 // are supported in floating-point literals
1537 let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1538 let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1539 let f3: f64 = f64::from_str_radix("1.Cp-12", 16).unwrap();
1540 assert_eq!(f64::ldexp(1f64, -123), f1);
1541 assert_eq!(f64::ldexp(1f64, -111), f2);
1542 assert_eq!(f64::ldexp(1.75f64, -12), f3);
1544 assert_eq!(f64::ldexp(0f64, -123), 0f64);
1545 assert_eq!(f64::ldexp(-0f64, -123), -0f64);
1547 let inf: f64 = INFINITY;
1548 let neg_inf: f64 = NEG_INFINITY;
1550 assert_eq!(f64::ldexp(inf, -123), inf);
1551 assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
1552 assert!(f64::ldexp(nan, -123).is_nan());
1557 // We have to use from_str until base-2 exponents
1558 // are supported in floating-point literals
1559 let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1560 let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1561 let f3: f64 = f64::from_str_radix("1.Cp-123", 16).unwrap();
1562 let (x1, exp1) = f1.frexp();
1563 let (x2, exp2) = f2.frexp();
1564 let (x3, exp3) = f3.frexp();
1565 assert_eq!((x1, exp1), (0.5f64, -122));
1566 assert_eq!((x2, exp2), (0.5f64, -110));
1567 assert_eq!((x3, exp3), (0.875f64, -122));
1568 assert_eq!(f64::ldexp(x1, exp1), f1);
1569 assert_eq!(f64::ldexp(x2, exp2), f2);
1570 assert_eq!(f64::ldexp(x3, exp3), f3);
1572 assert_eq!(0f64.frexp(), (0f64, 0));
1573 assert_eq!((-0f64).frexp(), (-0f64, 0));
1576 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1577 fn test_frexp_nowin() {
1578 let inf: f64 = INFINITY;
1579 let neg_inf: f64 = NEG_INFINITY;
1581 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1582 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1583 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1588 assert_eq!((-1f64).abs_sub(1f64), 0f64);
1589 assert_eq!(1f64.abs_sub(1f64), 0f64);
1590 assert_eq!(1f64.abs_sub(0f64), 1f64);
1591 assert_eq!(1f64.abs_sub(-1f64), 2f64);
1592 assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
1593 assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
1594 assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
1595 assert_eq!(0f64.abs_sub(INFINITY), 0f64);
1599 fn test_abs_sub_nowin() {
1600 assert!(NAN.abs_sub(-1f64).is_nan());
1601 assert!(1f64.abs_sub(NAN).is_nan());
1606 assert_eq!(0.0f64.asinh(), 0.0f64);
1607 assert_eq!((-0.0f64).asinh(), -0.0f64);
1609 let inf: f64 = INFINITY;
1610 let neg_inf: f64 = NEG_INFINITY;
1612 assert_eq!(inf.asinh(), inf);
1613 assert_eq!(neg_inf.asinh(), neg_inf);
1614 assert!(nan.asinh().is_nan());
1615 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1616 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1621 assert_eq!(1.0f64.acosh(), 0.0f64);
1622 assert!(0.999f64.acosh().is_nan());
1624 let inf: f64 = INFINITY;
1625 let neg_inf: f64 = NEG_INFINITY;
1627 assert_eq!(inf.acosh(), inf);
1628 assert!(neg_inf.acosh().is_nan());
1629 assert!(nan.acosh().is_nan());
1630 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1631 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1636 assert_eq!(0.0f64.atanh(), 0.0f64);
1637 assert_eq!((-0.0f64).atanh(), -0.0f64);
1639 let inf: f64 = INFINITY;
1640 let neg_inf: f64 = NEG_INFINITY;
1642 assert_eq!(1.0f64.atanh(), inf);
1643 assert_eq!((-1.0f64).atanh(), neg_inf);
1644 assert!(2f64.atanh().atanh().is_nan());
1645 assert!((-2f64).atanh().atanh().is_nan());
1646 assert!(inf.atanh().is_nan());
1647 assert!(neg_inf.atanh().is_nan());
1648 assert!(nan.atanh().is_nan());
1649 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1650 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1654 fn test_real_consts() {
1656 let pi: f64 = consts::PI;
1657 let two_pi: f64 = consts::PI_2;
1658 let frac_pi_2: f64 = consts::FRAC_PI_2;
1659 let frac_pi_3: f64 = consts::FRAC_PI_3;
1660 let frac_pi_4: f64 = consts::FRAC_PI_4;
1661 let frac_pi_6: f64 = consts::FRAC_PI_6;
1662 let frac_pi_8: f64 = consts::FRAC_PI_8;
1663 let frac_1_pi: f64 = consts::FRAC_1_PI;
1664 let frac_2_pi: f64 = consts::FRAC_2_PI;
1665 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1666 let sqrt2: f64 = consts::SQRT_2;
1667 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1668 let e: f64 = consts::E;
1669 let log2_e: f64 = consts::LOG2_E;
1670 let log10_e: f64 = consts::LOG10_E;
1671 let ln_2: f64 = consts::LN_2;
1672 let ln_10: f64 = consts::LN_10;
1674 assert_approx_eq!(two_pi, 2.0 * pi);
1675 assert_approx_eq!(frac_pi_2, pi / 2f64);
1676 assert_approx_eq!(frac_pi_3, pi / 3f64);
1677 assert_approx_eq!(frac_pi_4, pi / 4f64);
1678 assert_approx_eq!(frac_pi_6, pi / 6f64);
1679 assert_approx_eq!(frac_pi_8, pi / 8f64);
1680 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1681 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1682 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1683 assert_approx_eq!(sqrt2, 2f64.sqrt());
1684 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1685 assert_approx_eq!(log2_e, e.log2());
1686 assert_approx_eq!(log10_e, e.log10());
1687 assert_approx_eq!(ln_2, 2f64.ln());
1688 assert_approx_eq!(ln_10, 10f64.ln());