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)]
21 use num::{FpCategory, ParseFloatError};
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
25 #[stable(feature = "rust1", since = "1.0.0")]
26 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
27 #[stable(feature = "rust1", since = "1.0.0")]
28 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub use core::f64::consts;
36 use libc::{c_double, c_int};
40 pub fn acos(n: c_double) -> c_double;
41 pub fn asin(n: c_double) -> c_double;
42 pub fn atan(n: c_double) -> c_double;
43 pub fn atan2(a: c_double, b: c_double) -> c_double;
44 pub fn cbrt(n: c_double) -> c_double;
45 pub fn cosh(n: c_double) -> c_double;
46 pub fn erf(n: c_double) -> c_double;
47 pub fn erfc(n: c_double) -> c_double;
48 pub fn expm1(n: c_double) -> c_double;
49 pub fn fdim(a: c_double, b: c_double) -> c_double;
50 pub fn fmax(a: c_double, b: c_double) -> c_double;
51 pub fn fmin(a: c_double, b: c_double) -> c_double;
52 pub fn fmod(a: c_double, b: c_double) -> c_double;
53 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
54 pub fn ilogb(n: c_double) -> c_int;
55 pub fn ldexp(x: c_double, n: c_int) -> c_double;
56 pub fn logb(n: c_double) -> c_double;
57 pub fn log1p(n: c_double) -> c_double;
58 pub fn nextafter(x: c_double, y: c_double) -> c_double;
59 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
60 pub fn sinh(n: c_double) -> c_double;
61 pub fn tan(n: c_double) -> c_double;
62 pub fn tanh(n: c_double) -> c_double;
63 pub fn tgamma(n: c_double) -> c_double;
65 // These are commonly only available for doubles
67 pub fn j0(n: c_double) -> c_double;
68 pub fn j1(n: c_double) -> c_double;
69 pub fn jn(i: c_int, n: c_double) -> c_double;
71 pub fn y0(n: c_double) -> c_double;
72 pub fn y1(n: c_double) -> c_double;
73 pub fn yn(i: c_int, n: c_double) -> c_double;
75 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
76 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
78 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
79 pub fn hypot(x: c_double, y: c_double) -> c_double;
86 /// Parses a float as with a given radix
87 #[unstable(feature = "float_from_str_radix", reason = "recently moved API",
89 #[rustc_deprecated(since = "1.4.0",
90 reason = "unclear how useful or correct this is")]
92 pub fn from_str_radix(s: &str, radix: u32) -> Result<f64, ParseFloatError> {
93 num::Float::from_str_radix(s, radix)
96 /// Returns `true` if this value is `NaN` and false otherwise.
101 /// let nan = f64::NAN;
104 /// assert!(nan.is_nan());
105 /// assert!(!f.is_nan());
107 #[stable(feature = "rust1", since = "1.0.0")]
109 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
111 /// Returns `true` if this value is positive infinity or negative infinity and
118 /// let inf = f64::INFINITY;
119 /// let neg_inf = f64::NEG_INFINITY;
120 /// let nan = f64::NAN;
122 /// assert!(!f.is_infinite());
123 /// assert!(!nan.is_infinite());
125 /// assert!(inf.is_infinite());
126 /// assert!(neg_inf.is_infinite());
128 #[stable(feature = "rust1", since = "1.0.0")]
130 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
132 /// Returns `true` if this number is neither infinite nor `NaN`.
138 /// let inf: f64 = f64::INFINITY;
139 /// let neg_inf: f64 = f64::NEG_INFINITY;
140 /// let nan: f64 = f64::NAN;
142 /// assert!(f.is_finite());
144 /// assert!(!nan.is_finite());
145 /// assert!(!inf.is_finite());
146 /// assert!(!neg_inf.is_finite());
148 #[stable(feature = "rust1", since = "1.0.0")]
150 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
152 /// Returns `true` if the number is neither zero, infinite,
153 /// [subnormal][subnormal], or `NaN`.
158 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f64
159 /// let max = f32::MAX;
160 /// let lower_than_min = 1.0e-40_f32;
161 /// let zero = 0.0f32;
163 /// assert!(min.is_normal());
164 /// assert!(max.is_normal());
166 /// assert!(!zero.is_normal());
167 /// assert!(!f32::NAN.is_normal());
168 /// assert!(!f32::INFINITY.is_normal());
169 /// // Values between `0` and `min` are Subnormal.
170 /// assert!(!lower_than_min.is_normal());
172 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
173 #[stable(feature = "rust1", since = "1.0.0")]
175 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
177 /// Returns the floating point category of the number. If only one property
178 /// is going to be tested, it is generally faster to use the specific
179 /// predicate instead.
182 /// use std::num::FpCategory;
185 /// let num = 12.4_f64;
186 /// let inf = f64::INFINITY;
188 /// assert_eq!(num.classify(), FpCategory::Normal);
189 /// assert_eq!(inf.classify(), FpCategory::Infinite);
191 #[stable(feature = "rust1", since = "1.0.0")]
193 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
195 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
196 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
197 /// The floating point encoding is documented in the [Reference][floating-point].
200 /// #![feature(float_extras)]
202 /// let num = 2.0f64;
204 /// // (8388608, -22, 1)
205 /// let (mantissa, exponent, sign) = num.integer_decode();
206 /// let sign_f = sign as f64;
207 /// let mantissa_f = mantissa as f64;
208 /// let exponent_f = num.powf(exponent as f64);
210 /// // 1 * 8388608 * 2^(-22) == 2
211 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
213 /// assert!(abs_difference < 1e-10);
215 /// [floating-point]: ../../../../../reference.html#machine-types
216 #[unstable(feature = "float_extras", reason = "signature is undecided",
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 unsafe { intrinsics::logf64(self) }
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 unsafe { intrinsics::log2f64(self) }
558 /// Returns the base 10 logarithm of the number.
561 /// let ten = 10.0_f64;
563 /// // log10(10) - 1 == 0
564 /// let abs_difference = (ten.log10() - 1.0).abs();
566 /// assert!(abs_difference < 1e-10);
568 #[stable(feature = "rust1", since = "1.0.0")]
570 pub fn log10(self) -> f64 {
571 unsafe { intrinsics::log10f64(self) }
574 /// Converts radians to degrees.
577 /// use std::f64::consts;
579 /// let angle = consts::PI;
581 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
583 /// assert!(abs_difference < 1e-10);
585 #[stable(feature = "rust1", since = "1.0.0")]
587 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
589 /// Converts degrees to radians.
592 /// use std::f64::consts;
594 /// let angle = 180.0_f64;
596 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
598 /// assert!(abs_difference < 1e-10);
600 #[stable(feature = "rust1", since = "1.0.0")]
602 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
604 /// Constructs a floating point number of `x*2^exp`.
607 /// #![feature(float_extras)]
609 /// // 3*2^2 - 12 == 0
610 /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
612 /// assert!(abs_difference < 1e-10);
614 #[unstable(feature = "float_extras",
615 reason = "pending integer conventions",
618 pub fn ldexp(x: f64, exp: isize) -> f64 {
619 unsafe { cmath::ldexp(x, exp as c_int) }
622 /// Breaks the number into a normalized fraction and a base-2 exponent,
625 /// * `self = x * 2^exp`
626 /// * `0.5 <= abs(x) < 1.0`
629 /// #![feature(float_extras)]
633 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
634 /// let f = x.frexp();
635 /// let abs_difference_0 = (f.0 - 0.5).abs();
636 /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
638 /// assert!(abs_difference_0 < 1e-10);
639 /// assert!(abs_difference_1 < 1e-10);
641 #[unstable(feature = "float_extras",
642 reason = "pending integer conventions",
645 pub fn frexp(self) -> (f64, isize) {
648 let x = cmath::frexp(self, &mut exp);
653 /// Returns the next representable floating-point value in the direction of
657 /// #![feature(float_extras)]
661 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
663 /// assert!(abs_diff < 1e-10);
665 #[unstable(feature = "float_extras",
666 reason = "unsure about its place in the world",
669 pub fn next_after(self, other: f64) -> f64 {
670 unsafe { cmath::nextafter(self, other) }
673 /// Returns the maximum of the two numbers.
679 /// assert_eq!(x.max(y), y);
682 /// If one of the arguments is NaN, then the other argument is returned.
683 #[stable(feature = "rust1", since = "1.0.0")]
685 pub fn max(self, other: f64) -> f64 {
686 unsafe { cmath::fmax(self, other) }
689 /// Returns the minimum of the two numbers.
695 /// assert_eq!(x.min(y), x);
698 /// If one of the arguments is NaN, then the other argument is returned.
699 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn min(self, other: f64) -> f64 {
702 unsafe { cmath::fmin(self, other) }
705 /// The positive difference of two numbers.
707 /// * If `self <= other`: `0:0`
708 /// * Else: `self - other`
712 /// let y = -3.0_f64;
714 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
715 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
717 /// assert!(abs_difference_x < 1e-10);
718 /// assert!(abs_difference_y < 1e-10);
720 #[stable(feature = "rust1", since = "1.0.0")]
722 pub fn abs_sub(self, other: f64) -> f64 {
723 unsafe { cmath::fdim(self, other) }
726 /// Takes the cubic root of a number.
731 /// // x^(1/3) - 2 == 0
732 /// let abs_difference = (x.cbrt() - 2.0).abs();
734 /// assert!(abs_difference < 1e-10);
736 #[stable(feature = "rust1", since = "1.0.0")]
738 pub fn cbrt(self) -> f64 {
739 unsafe { cmath::cbrt(self) }
742 /// Calculates the length of the hypotenuse of a right-angle triangle given
743 /// legs of length `x` and `y`.
749 /// // sqrt(x^2 + y^2)
750 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
752 /// assert!(abs_difference < 1e-10);
754 #[stable(feature = "rust1", since = "1.0.0")]
756 pub fn hypot(self, other: f64) -> f64 {
757 unsafe { cmath::hypot(self, other) }
760 /// Computes the sine of a number (in radians).
765 /// let x = f64::consts::PI/2.0;
767 /// let abs_difference = (x.sin() - 1.0).abs();
769 /// assert!(abs_difference < 1e-10);
771 #[stable(feature = "rust1", since = "1.0.0")]
773 pub fn sin(self) -> f64 {
774 unsafe { intrinsics::sinf64(self) }
777 /// Computes the cosine of a number (in radians).
782 /// let x = 2.0*f64::consts::PI;
784 /// let abs_difference = (x.cos() - 1.0).abs();
786 /// assert!(abs_difference < 1e-10);
788 #[stable(feature = "rust1", since = "1.0.0")]
790 pub fn cos(self) -> f64 {
791 unsafe { intrinsics::cosf64(self) }
794 /// Computes the tangent of a number (in radians).
799 /// let x = f64::consts::PI/4.0;
800 /// let abs_difference = (x.tan() - 1.0).abs();
802 /// assert!(abs_difference < 1e-14);
804 #[stable(feature = "rust1", since = "1.0.0")]
806 pub fn tan(self) -> f64 {
807 unsafe { cmath::tan(self) }
810 /// Computes the arcsine of a number. Return value is in radians in
811 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
817 /// let f = f64::consts::PI / 2.0;
819 /// // asin(sin(pi/2))
820 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
822 /// assert!(abs_difference < 1e-10);
824 #[stable(feature = "rust1", since = "1.0.0")]
826 pub fn asin(self) -> f64 {
827 unsafe { cmath::asin(self) }
830 /// Computes the arccosine of a number. Return value is in radians in
831 /// the range [0, pi] or NaN if the number is outside the range
837 /// let f = f64::consts::PI / 4.0;
839 /// // acos(cos(pi/4))
840 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
842 /// assert!(abs_difference < 1e-10);
844 #[stable(feature = "rust1", since = "1.0.0")]
846 pub fn acos(self) -> f64 {
847 unsafe { cmath::acos(self) }
850 /// Computes the arctangent of a number. Return value is in radians in the
851 /// range [-pi/2, pi/2];
857 /// let abs_difference = (f.tan().atan() - 1.0).abs();
859 /// assert!(abs_difference < 1e-10);
861 #[stable(feature = "rust1", since = "1.0.0")]
863 pub fn atan(self) -> f64 {
864 unsafe { cmath::atan(self) }
867 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
869 /// * `x = 0`, `y = 0`: `0`
870 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
871 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
872 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
877 /// let pi = f64::consts::PI;
878 /// // All angles from horizontal right (+x)
879 /// // 45 deg counter-clockwise
880 /// let x1 = 3.0_f64;
881 /// let y1 = -3.0_f64;
883 /// // 135 deg clockwise
884 /// let x2 = -3.0_f64;
885 /// let y2 = 3.0_f64;
887 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
888 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
890 /// assert!(abs_difference_1 < 1e-10);
891 /// assert!(abs_difference_2 < 1e-10);
893 #[stable(feature = "rust1", since = "1.0.0")]
895 pub fn atan2(self, other: f64) -> f64 {
896 unsafe { cmath::atan2(self, other) }
899 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
900 /// `(sin(x), cos(x))`.
905 /// let x = f64::consts::PI/4.0;
906 /// let f = x.sin_cos();
908 /// let abs_difference_0 = (f.0 - x.sin()).abs();
909 /// let abs_difference_1 = (f.1 - x.cos()).abs();
911 /// assert!(abs_difference_0 < 1e-10);
912 /// assert!(abs_difference_0 < 1e-10);
914 #[stable(feature = "rust1", since = "1.0.0")]
916 pub fn sin_cos(self) -> (f64, f64) {
917 (self.sin(), self.cos())
920 /// Returns `e^(self) - 1` in a way that is accurate even if the
921 /// number is close to zero.
927 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
929 /// assert!(abs_difference < 1e-10);
931 #[stable(feature = "rust1", since = "1.0.0")]
933 pub fn exp_m1(self) -> f64 {
934 unsafe { cmath::expm1(self) }
937 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
938 /// the operations were performed separately.
943 /// let x = f64::consts::E - 1.0;
945 /// // ln(1 + (e - 1)) == ln(e) == 1
946 /// let abs_difference = (x.ln_1p() - 1.0).abs();
948 /// assert!(abs_difference < 1e-10);
950 #[stable(feature = "rust1", since = "1.0.0")]
952 pub fn ln_1p(self) -> f64 {
953 unsafe { cmath::log1p(self) }
956 /// Hyperbolic sine function.
961 /// let e = f64::consts::E;
964 /// let f = x.sinh();
965 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
966 /// let g = (e*e - 1.0)/(2.0*e);
967 /// let abs_difference = (f - g).abs();
969 /// assert!(abs_difference < 1e-10);
971 #[stable(feature = "rust1", since = "1.0.0")]
973 pub fn sinh(self) -> f64 {
974 unsafe { cmath::sinh(self) }
977 /// Hyperbolic cosine function.
982 /// let e = f64::consts::E;
984 /// let f = x.cosh();
985 /// // Solving cosh() at 1 gives this result
986 /// let g = (e*e + 1.0)/(2.0*e);
987 /// let abs_difference = (f - g).abs();
990 /// assert!(abs_difference < 1.0e-10);
992 #[stable(feature = "rust1", since = "1.0.0")]
994 pub fn cosh(self) -> f64 {
995 unsafe { cmath::cosh(self) }
998 /// Hyperbolic tangent function.
1003 /// let e = f64::consts::E;
1004 /// let x = 1.0_f64;
1006 /// let f = x.tanh();
1007 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1008 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1009 /// let abs_difference = (f - g).abs();
1011 /// assert!(abs_difference < 1.0e-10);
1013 #[stable(feature = "rust1", since = "1.0.0")]
1015 pub fn tanh(self) -> f64 {
1016 unsafe { cmath::tanh(self) }
1019 /// Inverse hyperbolic sine function.
1022 /// let x = 1.0_f64;
1023 /// let f = x.sinh().asinh();
1025 /// let abs_difference = (f - x).abs();
1027 /// assert!(abs_difference < 1.0e-10);
1029 #[stable(feature = "rust1", since = "1.0.0")]
1031 pub fn asinh(self) -> f64 {
1033 NEG_INFINITY => NEG_INFINITY,
1034 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1038 /// Inverse hyperbolic cosine function.
1041 /// let x = 1.0_f64;
1042 /// let f = x.cosh().acosh();
1044 /// let abs_difference = (f - x).abs();
1046 /// assert!(abs_difference < 1.0e-10);
1048 #[stable(feature = "rust1", since = "1.0.0")]
1050 pub fn acosh(self) -> f64 {
1052 x if x < 1.0 => NAN,
1053 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1057 /// Inverse hyperbolic tangent function.
1062 /// let e = f64::consts::E;
1063 /// let f = e.tanh().atanh();
1065 /// let abs_difference = (f - e).abs();
1067 /// assert!(abs_difference < 1.0e-10);
1069 #[stable(feature = "rust1", since = "1.0.0")]
1071 pub fn atanh(self) -> f64 {
1072 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1081 use num::FpCategory as Fp;
1085 test_num(10f64, 2f64);
1090 assert_eq!(NAN.min(2.0), 2.0);
1091 assert_eq!(2.0f64.min(NAN), 2.0);
1096 assert_eq!(NAN.max(2.0), 2.0);
1097 assert_eq!(2.0f64.max(NAN), 2.0);
1103 assert!(nan.is_nan());
1104 assert!(!nan.is_infinite());
1105 assert!(!nan.is_finite());
1106 assert!(!nan.is_normal());
1107 assert!(!nan.is_sign_positive());
1108 assert!(!nan.is_sign_negative());
1109 assert_eq!(Fp::Nan, nan.classify());
1113 fn test_infinity() {
1114 let inf: f64 = INFINITY;
1115 assert!(inf.is_infinite());
1116 assert!(!inf.is_finite());
1117 assert!(inf.is_sign_positive());
1118 assert!(!inf.is_sign_negative());
1119 assert!(!inf.is_nan());
1120 assert!(!inf.is_normal());
1121 assert_eq!(Fp::Infinite, inf.classify());
1125 fn test_neg_infinity() {
1126 let neg_inf: f64 = NEG_INFINITY;
1127 assert!(neg_inf.is_infinite());
1128 assert!(!neg_inf.is_finite());
1129 assert!(!neg_inf.is_sign_positive());
1130 assert!(neg_inf.is_sign_negative());
1131 assert!(!neg_inf.is_nan());
1132 assert!(!neg_inf.is_normal());
1133 assert_eq!(Fp::Infinite, neg_inf.classify());
1138 let zero: f64 = 0.0f64;
1139 assert_eq!(0.0, zero);
1140 assert!(!zero.is_infinite());
1141 assert!(zero.is_finite());
1142 assert!(zero.is_sign_positive());
1143 assert!(!zero.is_sign_negative());
1144 assert!(!zero.is_nan());
1145 assert!(!zero.is_normal());
1146 assert_eq!(Fp::Zero, zero.classify());
1150 fn test_neg_zero() {
1151 let neg_zero: f64 = -0.0;
1152 assert_eq!(0.0, neg_zero);
1153 assert!(!neg_zero.is_infinite());
1154 assert!(neg_zero.is_finite());
1155 assert!(!neg_zero.is_sign_positive());
1156 assert!(neg_zero.is_sign_negative());
1157 assert!(!neg_zero.is_nan());
1158 assert!(!neg_zero.is_normal());
1159 assert_eq!(Fp::Zero, neg_zero.classify());
1164 let one: f64 = 1.0f64;
1165 assert_eq!(1.0, one);
1166 assert!(!one.is_infinite());
1167 assert!(one.is_finite());
1168 assert!(one.is_sign_positive());
1169 assert!(!one.is_sign_negative());
1170 assert!(!one.is_nan());
1171 assert!(one.is_normal());
1172 assert_eq!(Fp::Normal, one.classify());
1178 let inf: f64 = INFINITY;
1179 let neg_inf: f64 = NEG_INFINITY;
1180 assert!(nan.is_nan());
1181 assert!(!0.0f64.is_nan());
1182 assert!(!5.3f64.is_nan());
1183 assert!(!(-10.732f64).is_nan());
1184 assert!(!inf.is_nan());
1185 assert!(!neg_inf.is_nan());
1189 fn test_is_infinite() {
1191 let inf: f64 = INFINITY;
1192 let neg_inf: f64 = NEG_INFINITY;
1193 assert!(!nan.is_infinite());
1194 assert!(inf.is_infinite());
1195 assert!(neg_inf.is_infinite());
1196 assert!(!0.0f64.is_infinite());
1197 assert!(!42.8f64.is_infinite());
1198 assert!(!(-109.2f64).is_infinite());
1202 fn test_is_finite() {
1204 let inf: f64 = INFINITY;
1205 let neg_inf: f64 = NEG_INFINITY;
1206 assert!(!nan.is_finite());
1207 assert!(!inf.is_finite());
1208 assert!(!neg_inf.is_finite());
1209 assert!(0.0f64.is_finite());
1210 assert!(42.8f64.is_finite());
1211 assert!((-109.2f64).is_finite());
1215 fn test_is_normal() {
1217 let inf: f64 = INFINITY;
1218 let neg_inf: f64 = NEG_INFINITY;
1219 let zero: f64 = 0.0f64;
1220 let neg_zero: f64 = -0.0;
1221 assert!(!nan.is_normal());
1222 assert!(!inf.is_normal());
1223 assert!(!neg_inf.is_normal());
1224 assert!(!zero.is_normal());
1225 assert!(!neg_zero.is_normal());
1226 assert!(1f64.is_normal());
1227 assert!(1e-307f64.is_normal());
1228 assert!(!1e-308f64.is_normal());
1232 fn test_classify() {
1234 let inf: f64 = INFINITY;
1235 let neg_inf: f64 = NEG_INFINITY;
1236 let zero: f64 = 0.0f64;
1237 let neg_zero: f64 = -0.0;
1238 assert_eq!(nan.classify(), Fp::Nan);
1239 assert_eq!(inf.classify(), Fp::Infinite);
1240 assert_eq!(neg_inf.classify(), Fp::Infinite);
1241 assert_eq!(zero.classify(), Fp::Zero);
1242 assert_eq!(neg_zero.classify(), Fp::Zero);
1243 assert_eq!(1e-307f64.classify(), Fp::Normal);
1244 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1248 fn test_integer_decode() {
1249 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
1250 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
1251 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
1252 assert_eq!(0f64.integer_decode(), (0, -1075, 1));
1253 assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
1254 assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
1255 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1256 assert_eq!(NAN.integer_decode(), (6755399441055744, 972, 1));
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());
1366 fn test_is_sign_negative() {
1367 assert!(!INFINITY.is_sign_negative());
1368 assert!(!1f64.is_sign_negative());
1369 assert!(!0f64.is_sign_negative());
1370 assert!((-0f64).is_sign_negative());
1371 assert!((-1f64).is_sign_negative());
1372 assert!(NEG_INFINITY.is_sign_negative());
1373 assert!((1f64/NEG_INFINITY).is_sign_negative());
1374 assert!(!NAN.is_sign_negative());
1380 let inf: f64 = INFINITY;
1381 let neg_inf: f64 = NEG_INFINITY;
1382 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1383 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1384 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1385 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1386 assert!(nan.mul_add(7.8, 9.0).is_nan());
1387 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1388 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1389 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1390 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1396 let inf: f64 = INFINITY;
1397 let neg_inf: f64 = NEG_INFINITY;
1398 assert_eq!(1.0f64.recip(), 1.0);
1399 assert_eq!(2.0f64.recip(), 0.5);
1400 assert_eq!((-0.4f64).recip(), -2.5);
1401 assert_eq!(0.0f64.recip(), inf);
1402 assert!(nan.recip().is_nan());
1403 assert_eq!(inf.recip(), 0.0);
1404 assert_eq!(neg_inf.recip(), 0.0);
1410 let inf: f64 = INFINITY;
1411 let neg_inf: f64 = NEG_INFINITY;
1412 assert_eq!(1.0f64.powi(1), 1.0);
1413 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1414 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1415 assert_eq!(8.3f64.powi(0), 1.0);
1416 assert!(nan.powi(2).is_nan());
1417 assert_eq!(inf.powi(3), inf);
1418 assert_eq!(neg_inf.powi(2), inf);
1424 let inf: f64 = INFINITY;
1425 let neg_inf: f64 = NEG_INFINITY;
1426 assert_eq!(1.0f64.powf(1.0), 1.0);
1427 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1428 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1429 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1430 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1431 assert_eq!(8.3f64.powf(0.0), 1.0);
1432 assert!(nan.powf(2.0).is_nan());
1433 assert_eq!(inf.powf(2.0), inf);
1434 assert_eq!(neg_inf.powf(3.0), neg_inf);
1438 fn test_sqrt_domain() {
1439 assert!(NAN.sqrt().is_nan());
1440 assert!(NEG_INFINITY.sqrt().is_nan());
1441 assert!((-1.0f64).sqrt().is_nan());
1442 assert_eq!((-0.0f64).sqrt(), -0.0);
1443 assert_eq!(0.0f64.sqrt(), 0.0);
1444 assert_eq!(1.0f64.sqrt(), 1.0);
1445 assert_eq!(INFINITY.sqrt(), INFINITY);
1450 assert_eq!(1.0, 0.0f64.exp());
1451 assert_approx_eq!(2.718282, 1.0f64.exp());
1452 assert_approx_eq!(148.413159, 5.0f64.exp());
1454 let inf: f64 = INFINITY;
1455 let neg_inf: f64 = NEG_INFINITY;
1457 assert_eq!(inf, inf.exp());
1458 assert_eq!(0.0, neg_inf.exp());
1459 assert!(nan.exp().is_nan());
1464 assert_eq!(32.0, 5.0f64.exp2());
1465 assert_eq!(1.0, 0.0f64.exp2());
1467 let inf: f64 = INFINITY;
1468 let neg_inf: f64 = NEG_INFINITY;
1470 assert_eq!(inf, inf.exp2());
1471 assert_eq!(0.0, neg_inf.exp2());
1472 assert!(nan.exp2().is_nan());
1478 let inf: f64 = INFINITY;
1479 let neg_inf: f64 = NEG_INFINITY;
1480 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1481 assert!(nan.ln().is_nan());
1482 assert_eq!(inf.ln(), inf);
1483 assert!(neg_inf.ln().is_nan());
1484 assert!((-2.3f64).ln().is_nan());
1485 assert_eq!((-0.0f64).ln(), neg_inf);
1486 assert_eq!(0.0f64.ln(), neg_inf);
1487 assert_approx_eq!(4.0f64.ln(), 1.386294);
1493 let inf: f64 = INFINITY;
1494 let neg_inf: f64 = NEG_INFINITY;
1495 assert_eq!(10.0f64.log(10.0), 1.0);
1496 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1497 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1498 assert!(1.0f64.log(1.0).is_nan());
1499 assert!(1.0f64.log(-13.9).is_nan());
1500 assert!(nan.log(2.3).is_nan());
1501 assert_eq!(inf.log(10.0), inf);
1502 assert!(neg_inf.log(8.8).is_nan());
1503 assert!((-2.3f64).log(0.1).is_nan());
1504 assert_eq!((-0.0f64).log(2.0), neg_inf);
1505 assert_eq!(0.0f64.log(7.0), neg_inf);
1511 let inf: f64 = INFINITY;
1512 let neg_inf: f64 = NEG_INFINITY;
1513 assert_approx_eq!(10.0f64.log2(), 3.321928);
1514 assert_approx_eq!(2.3f64.log2(), 1.201634);
1515 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1516 assert!(nan.log2().is_nan());
1517 assert_eq!(inf.log2(), inf);
1518 assert!(neg_inf.log2().is_nan());
1519 assert!((-2.3f64).log2().is_nan());
1520 assert_eq!((-0.0f64).log2(), neg_inf);
1521 assert_eq!(0.0f64.log2(), neg_inf);
1527 let inf: f64 = INFINITY;
1528 let neg_inf: f64 = NEG_INFINITY;
1529 assert_eq!(10.0f64.log10(), 1.0);
1530 assert_approx_eq!(2.3f64.log10(), 0.361728);
1531 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1532 assert_eq!(1.0f64.log10(), 0.0);
1533 assert!(nan.log10().is_nan());
1534 assert_eq!(inf.log10(), inf);
1535 assert!(neg_inf.log10().is_nan());
1536 assert!((-2.3f64).log10().is_nan());
1537 assert_eq!((-0.0f64).log10(), neg_inf);
1538 assert_eq!(0.0f64.log10(), neg_inf);
1542 fn test_to_degrees() {
1543 let pi: f64 = consts::PI;
1545 let inf: f64 = INFINITY;
1546 let neg_inf: f64 = NEG_INFINITY;
1547 assert_eq!(0.0f64.to_degrees(), 0.0);
1548 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1549 assert_eq!(pi.to_degrees(), 180.0);
1550 assert!(nan.to_degrees().is_nan());
1551 assert_eq!(inf.to_degrees(), inf);
1552 assert_eq!(neg_inf.to_degrees(), neg_inf);
1556 fn test_to_radians() {
1557 let pi: f64 = consts::PI;
1559 let inf: f64 = INFINITY;
1560 let neg_inf: f64 = NEG_INFINITY;
1561 assert_eq!(0.0f64.to_radians(), 0.0);
1562 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1563 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1564 assert_eq!(180.0f64.to_radians(), pi);
1565 assert!(nan.to_radians().is_nan());
1566 assert_eq!(inf.to_radians(), inf);
1567 assert_eq!(neg_inf.to_radians(), neg_inf);
1572 // We have to use from_str until base-2 exponents
1573 // are supported in floating-point literals
1574 let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1575 let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1576 let f3: f64 = f64::from_str_radix("1.Cp-12", 16).unwrap();
1577 assert_eq!(f64::ldexp(1f64, -123), f1);
1578 assert_eq!(f64::ldexp(1f64, -111), f2);
1579 assert_eq!(f64::ldexp(1.75f64, -12), f3);
1581 assert_eq!(f64::ldexp(0f64, -123), 0f64);
1582 assert_eq!(f64::ldexp(-0f64, -123), -0f64);
1584 let inf: f64 = INFINITY;
1585 let neg_inf: f64 = NEG_INFINITY;
1587 assert_eq!(f64::ldexp(inf, -123), inf);
1588 assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
1589 assert!(f64::ldexp(nan, -123).is_nan());
1594 // We have to use from_str until base-2 exponents
1595 // are supported in floating-point literals
1596 let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1597 let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1598 let f3: f64 = f64::from_str_radix("1.Cp-123", 16).unwrap();
1599 let (x1, exp1) = f1.frexp();
1600 let (x2, exp2) = f2.frexp();
1601 let (x3, exp3) = f3.frexp();
1602 assert_eq!((x1, exp1), (0.5f64, -122));
1603 assert_eq!((x2, exp2), (0.5f64, -110));
1604 assert_eq!((x3, exp3), (0.875f64, -122));
1605 assert_eq!(f64::ldexp(x1, exp1), f1);
1606 assert_eq!(f64::ldexp(x2, exp2), f2);
1607 assert_eq!(f64::ldexp(x3, exp3), f3);
1609 assert_eq!(0f64.frexp(), (0f64, 0));
1610 assert_eq!((-0f64).frexp(), (-0f64, 0));
1613 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1614 fn test_frexp_nowin() {
1615 let inf: f64 = INFINITY;
1616 let neg_inf: f64 = NEG_INFINITY;
1618 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1619 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1620 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1625 assert_eq!((-1f64).abs_sub(1f64), 0f64);
1626 assert_eq!(1f64.abs_sub(1f64), 0f64);
1627 assert_eq!(1f64.abs_sub(0f64), 1f64);
1628 assert_eq!(1f64.abs_sub(-1f64), 2f64);
1629 assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
1630 assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
1631 assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
1632 assert_eq!(0f64.abs_sub(INFINITY), 0f64);
1636 fn test_abs_sub_nowin() {
1637 assert!(NAN.abs_sub(-1f64).is_nan());
1638 assert!(1f64.abs_sub(NAN).is_nan());
1643 assert_eq!(0.0f64.asinh(), 0.0f64);
1644 assert_eq!((-0.0f64).asinh(), -0.0f64);
1646 let inf: f64 = INFINITY;
1647 let neg_inf: f64 = NEG_INFINITY;
1649 assert_eq!(inf.asinh(), inf);
1650 assert_eq!(neg_inf.asinh(), neg_inf);
1651 assert!(nan.asinh().is_nan());
1652 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1653 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1658 assert_eq!(1.0f64.acosh(), 0.0f64);
1659 assert!(0.999f64.acosh().is_nan());
1661 let inf: f64 = INFINITY;
1662 let neg_inf: f64 = NEG_INFINITY;
1664 assert_eq!(inf.acosh(), inf);
1665 assert!(neg_inf.acosh().is_nan());
1666 assert!(nan.acosh().is_nan());
1667 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1668 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1673 assert_eq!(0.0f64.atanh(), 0.0f64);
1674 assert_eq!((-0.0f64).atanh(), -0.0f64);
1676 let inf: f64 = INFINITY;
1677 let neg_inf: f64 = NEG_INFINITY;
1679 assert_eq!(1.0f64.atanh(), inf);
1680 assert_eq!((-1.0f64).atanh(), neg_inf);
1681 assert!(2f64.atanh().atanh().is_nan());
1682 assert!((-2f64).atanh().atanh().is_nan());
1683 assert!(inf.atanh().is_nan());
1684 assert!(neg_inf.atanh().is_nan());
1685 assert!(nan.atanh().is_nan());
1686 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1687 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1691 fn test_real_consts() {
1693 let pi: f64 = consts::PI;
1694 let frac_pi_2: f64 = consts::FRAC_PI_2;
1695 let frac_pi_3: f64 = consts::FRAC_PI_3;
1696 let frac_pi_4: f64 = consts::FRAC_PI_4;
1697 let frac_pi_6: f64 = consts::FRAC_PI_6;
1698 let frac_pi_8: f64 = consts::FRAC_PI_8;
1699 let frac_1_pi: f64 = consts::FRAC_1_PI;
1700 let frac_2_pi: f64 = consts::FRAC_2_PI;
1701 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1702 let sqrt2: f64 = consts::SQRT_2;
1703 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1704 let e: f64 = consts::E;
1705 let log2_e: f64 = consts::LOG2_E;
1706 let log10_e: f64 = consts::LOG10_E;
1707 let ln_2: f64 = consts::LN_2;
1708 let ln_10: f64 = consts::LN_10;
1710 assert_approx_eq!(frac_pi_2, pi / 2f64);
1711 assert_approx_eq!(frac_pi_3, pi / 3f64);
1712 assert_approx_eq!(frac_pi_4, pi / 4f64);
1713 assert_approx_eq!(frac_pi_6, pi / 6f64);
1714 assert_approx_eq!(frac_pi_8, pi / 8f64);
1715 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1716 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1717 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1718 assert_approx_eq!(sqrt2, 2f64.sqrt());
1719 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1720 assert_approx_eq!(log2_e, e.log2());
1721 assert_approx_eq!(log10_e, e.log10());
1722 assert_approx_eq!(ln_2, 2f64.ln());
1723 assert_approx_eq!(ln_10, 10f64.ln());