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 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
24 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
25 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
26 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
27 pub use core::f64::consts;
31 use libc::{c_double, c_int};
35 pub fn acos(n: c_double) -> c_double;
36 pub fn asin(n: c_double) -> c_double;
37 pub fn atan(n: c_double) -> c_double;
38 pub fn atan2(a: c_double, b: c_double) -> c_double;
39 pub fn cbrt(n: c_double) -> c_double;
40 pub fn cosh(n: c_double) -> c_double;
41 pub fn erf(n: c_double) -> c_double;
42 pub fn erfc(n: c_double) -> c_double;
43 pub fn expm1(n: c_double) -> c_double;
44 pub fn fdim(a: c_double, b: c_double) -> c_double;
45 pub fn fmax(a: c_double, b: c_double) -> c_double;
46 pub fn fmin(a: c_double, b: c_double) -> c_double;
47 pub fn fmod(a: c_double, b: c_double) -> c_double;
48 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
49 pub fn ilogb(n: c_double) -> c_int;
50 pub fn ldexp(x: c_double, n: c_int) -> c_double;
51 pub fn logb(n: c_double) -> c_double;
52 pub fn log1p(n: c_double) -> c_double;
53 pub fn nextafter(x: c_double, y: c_double) -> c_double;
54 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
55 pub fn sinh(n: c_double) -> c_double;
56 pub fn tan(n: c_double) -> c_double;
57 pub fn tanh(n: c_double) -> c_double;
58 pub fn tgamma(n: c_double) -> c_double;
60 // These are commonly only available for doubles
62 pub fn j0(n: c_double) -> c_double;
63 pub fn j1(n: c_double) -> c_double;
64 pub fn jn(i: c_int, n: c_double) -> c_double;
66 pub fn y0(n: c_double) -> c_double;
67 pub fn y1(n: c_double) -> c_double;
68 pub fn yn(i: c_int, n: c_double) -> c_double;
70 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
71 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
73 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
74 pub fn hypot(x: c_double, y: c_double) -> c_double;
80 #[stable(feature = "rust1", since = "1.0.0")]
82 /// Parses a float as with a given radix
83 #[unstable(feature = "float_from_str_radix", reason = "recently moved API")]
84 pub fn from_str_radix(s: &str, radix: u32) -> Result<f64, ParseFloatError> {
85 num::Float::from_str_radix(s, radix)
88 /// Returns `true` if this value is `NaN` and false otherwise.
93 /// let nan = f64::NAN;
96 /// assert!(nan.is_nan());
97 /// assert!(!f.is_nan());
99 #[stable(feature = "rust1", since = "1.0.0")]
101 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
103 /// Returns `true` if this value is positive infinity or negative infinity and
110 /// let inf = f64::INFINITY;
111 /// let neg_inf = f64::NEG_INFINITY;
112 /// let nan = f64::NAN;
114 /// assert!(!f.is_infinite());
115 /// assert!(!nan.is_infinite());
117 /// assert!(inf.is_infinite());
118 /// assert!(neg_inf.is_infinite());
120 #[stable(feature = "rust1", since = "1.0.0")]
122 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
124 /// Returns `true` if this number is neither infinite nor `NaN`.
130 /// let inf: f64 = f64::INFINITY;
131 /// let neg_inf: f64 = f64::NEG_INFINITY;
132 /// let nan: f64 = f64::NAN;
134 /// assert!(f.is_finite());
136 /// assert!(!nan.is_finite());
137 /// assert!(!inf.is_finite());
138 /// assert!(!neg_inf.is_finite());
140 #[stable(feature = "rust1", since = "1.0.0")]
142 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
144 /// Returns `true` if the number is neither zero, infinite,
145 /// [subnormal][subnormal], or `NaN`.
150 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f64
151 /// let max = f32::MAX;
152 /// let lower_than_min = 1.0e-40_f32;
153 /// let zero = 0.0f32;
155 /// assert!(min.is_normal());
156 /// assert!(max.is_normal());
158 /// assert!(!zero.is_normal());
159 /// assert!(!f32::NAN.is_normal());
160 /// assert!(!f32::INFINITY.is_normal());
161 /// // Values between `0` and `min` are Subnormal.
162 /// assert!(!lower_than_min.is_normal());
164 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
165 #[stable(feature = "rust1", since = "1.0.0")]
167 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
169 /// Returns the floating point category of the number. If only one property
170 /// is going to be tested, it is generally faster to use the specific
171 /// predicate instead.
174 /// use std::num::FpCategory;
177 /// let num = 12.4_f64;
178 /// let inf = f64::INFINITY;
180 /// assert_eq!(num.classify(), FpCategory::Normal);
181 /// assert_eq!(inf.classify(), FpCategory::Infinite);
183 #[stable(feature = "rust1", since = "1.0.0")]
185 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
187 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
188 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
189 /// The floating point encoding is documented in the [Reference][floating-point].
192 /// #![feature(float_extras)]
194 /// let num = 2.0f64;
196 /// // (8388608, -22, 1)
197 /// let (mantissa, exponent, sign) = num.integer_decode();
198 /// let sign_f = sign as f64;
199 /// let mantissa_f = mantissa as f64;
200 /// let exponent_f = num.powf(exponent as f64);
202 /// // 1 * 8388608 * 2^(-22) == 2
203 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
205 /// assert!(abs_difference < 1e-10);
207 /// [floating-point]: ../../../../../reference.html#machine-types
208 #[unstable(feature = "float_extras", reason = "signature is undecided")]
210 pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
212 /// Returns the largest integer less than or equal to a number.
215 /// let f = 3.99_f64;
218 /// assert_eq!(f.floor(), 3.0);
219 /// assert_eq!(g.floor(), 3.0);
221 #[stable(feature = "rust1", since = "1.0.0")]
223 pub fn floor(self) -> f64 {
224 unsafe { intrinsics::floorf64(self) }
227 /// Returns the smallest integer greater than or equal to a number.
230 /// let f = 3.01_f64;
233 /// assert_eq!(f.ceil(), 4.0);
234 /// assert_eq!(g.ceil(), 4.0);
236 #[stable(feature = "rust1", since = "1.0.0")]
238 pub fn ceil(self) -> f64 {
239 unsafe { intrinsics::ceilf64(self) }
242 /// Returns the nearest integer to a number. Round half-way cases away from
247 /// let g = -3.3_f64;
249 /// assert_eq!(f.round(), 3.0);
250 /// assert_eq!(g.round(), -3.0);
252 #[stable(feature = "rust1", since = "1.0.0")]
254 pub fn round(self) -> f64 {
255 unsafe { intrinsics::roundf64(self) }
258 /// Returns the integer part of a number.
262 /// let g = -3.7_f64;
264 /// assert_eq!(f.trunc(), 3.0);
265 /// assert_eq!(g.trunc(), -3.0);
267 #[stable(feature = "rust1", since = "1.0.0")]
269 pub fn trunc(self) -> f64 {
270 unsafe { intrinsics::truncf64(self) }
273 /// Returns the fractional part of a number.
277 /// let y = -3.5_f64;
278 /// let abs_difference_x = (x.fract() - 0.5).abs();
279 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
281 /// assert!(abs_difference_x < 1e-10);
282 /// assert!(abs_difference_y < 1e-10);
284 #[stable(feature = "rust1", since = "1.0.0")]
286 pub fn fract(self) -> f64 { self - self.trunc() }
288 /// Computes the absolute value of `self`. Returns `NAN` if the
295 /// let y = -3.5_f64;
297 /// let abs_difference_x = (x.abs() - x).abs();
298 /// let abs_difference_y = (y.abs() - (-y)).abs();
300 /// assert!(abs_difference_x < 1e-10);
301 /// assert!(abs_difference_y < 1e-10);
303 /// assert!(f64::NAN.abs().is_nan());
305 #[stable(feature = "rust1", since = "1.0.0")]
307 pub fn abs(self) -> f64 { num::Float::abs(self) }
309 /// Returns a number that represents the sign of `self`.
311 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
312 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
313 /// - `NAN` if the number is `NAN`
320 /// assert_eq!(f.signum(), 1.0);
321 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
323 /// assert!(f64::NAN.signum().is_nan());
325 #[stable(feature = "rust1", since = "1.0.0")]
327 pub fn signum(self) -> f64 { num::Float::signum(self) }
329 /// Returns `true` if `self`'s sign bit is positive, including
330 /// `+0.0` and `INFINITY`.
335 /// let nan: f64 = f64::NAN;
338 /// let g = -7.0_f64;
340 /// assert!(f.is_sign_positive());
341 /// assert!(!g.is_sign_positive());
342 /// // Requires both tests to determine if is `NaN`
343 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
345 #[stable(feature = "rust1", since = "1.0.0")]
347 pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) }
349 #[stable(feature = "rust1", since = "1.0.0")]
350 #[deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
352 pub fn is_positive(self) -> bool { num::Float::is_positive(self) }
354 /// Returns `true` if `self`'s sign is negative, including `-0.0`
355 /// and `NEG_INFINITY`.
360 /// let nan = f64::NAN;
363 /// let g = -7.0_f64;
365 /// assert!(!f.is_sign_negative());
366 /// assert!(g.is_sign_negative());
367 /// // Requires both tests to determine if is `NaN`.
368 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
370 #[stable(feature = "rust1", since = "1.0.0")]
372 pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) }
374 #[stable(feature = "rust1", since = "1.0.0")]
375 #[deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
377 pub fn is_negative(self) -> bool { num::Float::is_negative(self) }
379 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
380 /// error. This produces a more accurate result with better performance than
381 /// a separate multiplication operation followed by an add.
384 /// let m = 10.0_f64;
386 /// let b = 60.0_f64;
389 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
391 /// assert!(abs_difference < 1e-10);
393 #[stable(feature = "rust1", since = "1.0.0")]
395 pub fn mul_add(self, a: f64, b: f64) -> f64 {
396 unsafe { intrinsics::fmaf64(self, a, b) }
399 /// Takes the reciprocal (inverse) of a number, `1/x`.
403 /// let abs_difference = (x.recip() - (1.0/x)).abs();
405 /// assert!(abs_difference < 1e-10);
407 #[stable(feature = "rust1", since = "1.0.0")]
409 pub fn recip(self) -> f64 { num::Float::recip(self) }
411 /// Raises a number to an integer power.
413 /// Using this function is generally faster than using `powf`
417 /// let abs_difference = (x.powi(2) - x*x).abs();
419 /// assert!(abs_difference < 1e-10);
421 #[stable(feature = "rust1", since = "1.0.0")]
423 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
425 /// Raises a number to a floating point power.
429 /// let abs_difference = (x.powf(2.0) - x*x).abs();
431 /// assert!(abs_difference < 1e-10);
433 #[stable(feature = "rust1", since = "1.0.0")]
435 pub fn powf(self, n: f64) -> f64 {
436 unsafe { intrinsics::powf64(self, n) }
439 /// Takes the square root of a number.
441 /// Returns NaN if `self` is a negative number.
444 /// let positive = 4.0_f64;
445 /// let negative = -4.0_f64;
447 /// let abs_difference = (positive.sqrt() - 2.0).abs();
449 /// assert!(abs_difference < 1e-10);
450 /// assert!(negative.sqrt().is_nan());
452 #[stable(feature = "rust1", since = "1.0.0")]
454 pub fn sqrt(self) -> f64 {
455 unsafe { intrinsics::sqrtf64(self) }
458 /// Returns `e^(self)`, (the exponential function).
461 /// let one = 1.0_f64;
463 /// let e = one.exp();
465 /// // ln(e) - 1 == 0
466 /// let abs_difference = (e.ln() - 1.0).abs();
468 /// assert!(abs_difference < 1e-10);
470 #[stable(feature = "rust1", since = "1.0.0")]
472 pub fn exp(self) -> f64 {
473 unsafe { intrinsics::expf64(self) }
476 /// Returns `2^(self)`.
482 /// let abs_difference = (f.exp2() - 4.0).abs();
484 /// assert!(abs_difference < 1e-10);
486 #[stable(feature = "rust1", since = "1.0.0")]
488 pub fn exp2(self) -> f64 {
489 unsafe { intrinsics::exp2f64(self) }
492 /// Returns the natural logarithm of the number.
495 /// let one = 1.0_f64;
497 /// let e = one.exp();
499 /// // ln(e) - 1 == 0
500 /// let abs_difference = (e.ln() - 1.0).abs();
502 /// assert!(abs_difference < 1e-10);
504 #[stable(feature = "rust1", since = "1.0.0")]
506 pub fn ln(self) -> f64 {
507 unsafe { intrinsics::logf64(self) }
510 /// Returns the logarithm of the number with respect to an arbitrary base.
513 /// let ten = 10.0_f64;
514 /// let two = 2.0_f64;
516 /// // log10(10) - 1 == 0
517 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
519 /// // log2(2) - 1 == 0
520 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
522 /// assert!(abs_difference_10 < 1e-10);
523 /// assert!(abs_difference_2 < 1e-10);
525 #[stable(feature = "rust1", since = "1.0.0")]
527 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
529 /// Returns the base 2 logarithm of the number.
532 /// let two = 2.0_f64;
534 /// // log2(2) - 1 == 0
535 /// let abs_difference = (two.log2() - 1.0).abs();
537 /// assert!(abs_difference < 1e-10);
539 #[stable(feature = "rust1", since = "1.0.0")]
541 pub fn log2(self) -> f64 {
542 unsafe { intrinsics::log2f64(self) }
545 /// Returns the base 10 logarithm of the number.
548 /// let ten = 10.0_f64;
550 /// // log10(10) - 1 == 0
551 /// let abs_difference = (ten.log10() - 1.0).abs();
553 /// assert!(abs_difference < 1e-10);
555 #[stable(feature = "rust1", since = "1.0.0")]
557 pub fn log10(self) -> f64 {
558 unsafe { intrinsics::log10f64(self) }
561 /// Converts radians to degrees.
564 /// use std::f64::consts;
566 /// let angle = consts::PI;
568 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
570 /// assert!(abs_difference < 1e-10);
572 #[stable(feature = "rust1", since = "1.0.0")]
574 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
576 /// Converts degrees to radians.
579 /// use std::f64::consts;
581 /// let angle = 180.0_f64;
583 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
585 /// assert!(abs_difference < 1e-10);
587 #[stable(feature = "rust1", since = "1.0.0")]
589 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
591 /// Constructs a floating point number of `x*2^exp`.
594 /// #![feature(float_extras)]
596 /// // 3*2^2 - 12 == 0
597 /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
599 /// assert!(abs_difference < 1e-10);
601 #[unstable(feature = "float_extras",
602 reason = "pending integer conventions")]
604 pub fn ldexp(x: f64, exp: isize) -> f64 {
605 unsafe { cmath::ldexp(x, exp as c_int) }
608 /// Breaks the number into a normalized fraction and a base-2 exponent,
611 /// * `self = x * 2^exp`
612 /// * `0.5 <= abs(x) < 1.0`
615 /// #![feature(float_extras)]
619 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
620 /// let f = x.frexp();
621 /// let abs_difference_0 = (f.0 - 0.5).abs();
622 /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
624 /// assert!(abs_difference_0 < 1e-10);
625 /// assert!(abs_difference_1 < 1e-10);
627 #[unstable(feature = "float_extras",
628 reason = "pending integer conventions")]
630 pub fn frexp(self) -> (f64, isize) {
633 let x = cmath::frexp(self, &mut exp);
638 /// Returns the next representable floating-point value in the direction of
642 /// #![feature(float_extras)]
646 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
648 /// assert!(abs_diff < 1e-10);
650 #[unstable(feature = "float_extras",
651 reason = "unsure about its place in the world")]
653 pub fn next_after(self, other: f64) -> f64 {
654 unsafe { cmath::nextafter(self, other) }
657 /// Returns the maximum of the two numbers.
663 /// assert_eq!(x.max(y), y);
666 /// If one of the arguments is NaN, then the other argument is returned.
667 #[stable(feature = "rust1", since = "1.0.0")]
669 pub fn max(self, other: f64) -> f64 {
670 unsafe { cmath::fmax(self, other) }
673 /// Returns the minimum of the two numbers.
679 /// assert_eq!(x.min(y), x);
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 min(self, other: f64) -> f64 {
686 unsafe { cmath::fmin(self, other) }
689 /// The positive difference of two numbers.
691 /// * If `self <= other`: `0:0`
692 /// * Else: `self - other`
696 /// let y = -3.0_f64;
698 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
699 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
701 /// assert!(abs_difference_x < 1e-10);
702 /// assert!(abs_difference_y < 1e-10);
704 #[stable(feature = "rust1", since = "1.0.0")]
706 pub fn abs_sub(self, other: f64) -> f64 {
707 unsafe { cmath::fdim(self, other) }
710 /// Takes the cubic root of a number.
715 /// // x^(1/3) - 2 == 0
716 /// let abs_difference = (x.cbrt() - 2.0).abs();
718 /// assert!(abs_difference < 1e-10);
720 #[stable(feature = "rust1", since = "1.0.0")]
722 pub fn cbrt(self) -> f64 {
723 unsafe { cmath::cbrt(self) }
726 /// Calculates the length of the hypotenuse of a right-angle triangle given
727 /// legs of length `x` and `y`.
733 /// // sqrt(x^2 + y^2)
734 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
736 /// assert!(abs_difference < 1e-10);
738 #[stable(feature = "rust1", since = "1.0.0")]
740 pub fn hypot(self, other: f64) -> f64 {
741 unsafe { cmath::hypot(self, other) }
744 /// Computes the sine of a number (in radians).
749 /// let x = f64::consts::PI/2.0;
751 /// let abs_difference = (x.sin() - 1.0).abs();
753 /// assert!(abs_difference < 1e-10);
755 #[stable(feature = "rust1", since = "1.0.0")]
757 pub fn sin(self) -> f64 {
758 unsafe { intrinsics::sinf64(self) }
761 /// Computes the cosine of a number (in radians).
766 /// let x = 2.0*f64::consts::PI;
768 /// let abs_difference = (x.cos() - 1.0).abs();
770 /// assert!(abs_difference < 1e-10);
772 #[stable(feature = "rust1", since = "1.0.0")]
774 pub fn cos(self) -> f64 {
775 unsafe { intrinsics::cosf64(self) }
778 /// Computes the tangent of a number (in radians).
783 /// let x = f64::consts::PI/4.0;
784 /// let abs_difference = (x.tan() - 1.0).abs();
786 /// assert!(abs_difference < 1e-14);
788 #[stable(feature = "rust1", since = "1.0.0")]
790 pub fn tan(self) -> f64 {
791 unsafe { cmath::tan(self) }
794 /// Computes the arcsine of a number. Return value is in radians in
795 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
801 /// let f = f64::consts::PI / 2.0;
803 /// // asin(sin(pi/2))
804 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
806 /// assert!(abs_difference < 1e-10);
808 #[stable(feature = "rust1", since = "1.0.0")]
810 pub fn asin(self) -> f64 {
811 unsafe { cmath::asin(self) }
814 /// Computes the arccosine of a number. Return value is in radians in
815 /// the range [0, pi] or NaN if the number is outside the range
821 /// let f = f64::consts::PI / 4.0;
823 /// // acos(cos(pi/4))
824 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
826 /// assert!(abs_difference < 1e-10);
828 #[stable(feature = "rust1", since = "1.0.0")]
830 pub fn acos(self) -> f64 {
831 unsafe { cmath::acos(self) }
834 /// Computes the arctangent of a number. Return value is in radians in the
835 /// range [-pi/2, pi/2];
841 /// let abs_difference = (f.tan().atan() - 1.0).abs();
843 /// assert!(abs_difference < 1e-10);
845 #[stable(feature = "rust1", since = "1.0.0")]
847 pub fn atan(self) -> f64 {
848 unsafe { cmath::atan(self) }
851 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
853 /// * `x = 0`, `y = 0`: `0`
854 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
855 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
856 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
861 /// let pi = f64::consts::PI;
862 /// // All angles from horizontal right (+x)
863 /// // 45 deg counter-clockwise
864 /// let x1 = 3.0_f64;
865 /// let y1 = -3.0_f64;
867 /// // 135 deg clockwise
868 /// let x2 = -3.0_f64;
869 /// let y2 = 3.0_f64;
871 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
872 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
874 /// assert!(abs_difference_1 < 1e-10);
875 /// assert!(abs_difference_2 < 1e-10);
877 #[stable(feature = "rust1", since = "1.0.0")]
879 pub fn atan2(self, other: f64) -> f64 {
880 unsafe { cmath::atan2(self, other) }
883 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
884 /// `(sin(x), cos(x))`.
889 /// let x = f64::consts::PI/4.0;
890 /// let f = x.sin_cos();
892 /// let abs_difference_0 = (f.0 - x.sin()).abs();
893 /// let abs_difference_1 = (f.1 - x.cos()).abs();
895 /// assert!(abs_difference_0 < 1e-10);
896 /// assert!(abs_difference_0 < 1e-10);
898 #[stable(feature = "rust1", since = "1.0.0")]
900 pub fn sin_cos(self) -> (f64, f64) {
901 (self.sin(), self.cos())
904 /// Returns `e^(self) - 1` in a way that is accurate even if the
905 /// number is close to zero.
911 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
913 /// assert!(abs_difference < 1e-10);
915 #[stable(feature = "rust1", since = "1.0.0")]
917 pub fn exp_m1(self) -> f64 {
918 unsafe { cmath::expm1(self) }
921 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
922 /// the operations were performed separately.
927 /// let x = f64::consts::E - 1.0;
929 /// // ln(1 + (e - 1)) == ln(e) == 1
930 /// let abs_difference = (x.ln_1p() - 1.0).abs();
932 /// assert!(abs_difference < 1e-10);
934 #[stable(feature = "rust1", since = "1.0.0")]
936 pub fn ln_1p(self) -> f64 {
937 unsafe { cmath::log1p(self) }
940 /// Hyperbolic sine function.
945 /// let e = f64::consts::E;
948 /// let f = x.sinh();
949 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
950 /// let g = (e*e - 1.0)/(2.0*e);
951 /// let abs_difference = (f - g).abs();
953 /// assert!(abs_difference < 1e-10);
955 #[stable(feature = "rust1", since = "1.0.0")]
957 pub fn sinh(self) -> f64 {
958 unsafe { cmath::sinh(self) }
961 /// Hyperbolic cosine function.
966 /// let e = f64::consts::E;
968 /// let f = x.cosh();
969 /// // Solving cosh() at 1 gives this result
970 /// let g = (e*e + 1.0)/(2.0*e);
971 /// let abs_difference = (f - g).abs();
974 /// assert!(abs_difference < 1.0e-10);
976 #[stable(feature = "rust1", since = "1.0.0")]
978 pub fn cosh(self) -> f64 {
979 unsafe { cmath::cosh(self) }
982 /// Hyperbolic tangent function.
987 /// let e = f64::consts::E;
990 /// let f = x.tanh();
991 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
992 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
993 /// let abs_difference = (f - g).abs();
995 /// assert!(abs_difference < 1.0e-10);
997 #[stable(feature = "rust1", since = "1.0.0")]
999 pub fn tanh(self) -> f64 {
1000 unsafe { cmath::tanh(self) }
1003 /// Inverse hyperbolic sine function.
1006 /// let x = 1.0_f64;
1007 /// let f = x.sinh().asinh();
1009 /// let abs_difference = (f - x).abs();
1011 /// assert!(abs_difference < 1.0e-10);
1013 #[stable(feature = "rust1", since = "1.0.0")]
1015 pub fn asinh(self) -> f64 {
1017 NEG_INFINITY => NEG_INFINITY,
1018 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1022 /// Inverse hyperbolic cosine function.
1025 /// let x = 1.0_f64;
1026 /// let f = x.cosh().acosh();
1028 /// let abs_difference = (f - x).abs();
1030 /// assert!(abs_difference < 1.0e-10);
1032 #[stable(feature = "rust1", since = "1.0.0")]
1034 pub fn acosh(self) -> f64 {
1036 x if x < 1.0 => NAN,
1037 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1041 /// Inverse hyperbolic tangent function.
1046 /// let e = f64::consts::E;
1047 /// let f = e.tanh().atanh();
1049 /// let abs_difference = (f - e).abs();
1051 /// assert!(abs_difference < 1.0e-10);
1053 #[stable(feature = "rust1", since = "1.0.0")]
1055 pub fn atanh(self) -> f64 {
1056 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1065 use num::FpCategory as Fp;
1069 test_num(10f64, 2f64);
1074 assert_eq!(NAN.min(2.0), 2.0);
1075 assert_eq!(2.0f64.min(NAN), 2.0);
1080 assert_eq!(NAN.max(2.0), 2.0);
1081 assert_eq!(2.0f64.max(NAN), 2.0);
1087 assert!(nan.is_nan());
1088 assert!(!nan.is_infinite());
1089 assert!(!nan.is_finite());
1090 assert!(!nan.is_normal());
1091 assert!(!nan.is_sign_positive());
1092 assert!(!nan.is_sign_negative());
1093 assert_eq!(Fp::Nan, nan.classify());
1097 fn test_infinity() {
1098 let inf: f64 = INFINITY;
1099 assert!(inf.is_infinite());
1100 assert!(!inf.is_finite());
1101 assert!(inf.is_sign_positive());
1102 assert!(!inf.is_sign_negative());
1103 assert!(!inf.is_nan());
1104 assert!(!inf.is_normal());
1105 assert_eq!(Fp::Infinite, inf.classify());
1109 fn test_neg_infinity() {
1110 let neg_inf: f64 = NEG_INFINITY;
1111 assert!(neg_inf.is_infinite());
1112 assert!(!neg_inf.is_finite());
1113 assert!(!neg_inf.is_sign_positive());
1114 assert!(neg_inf.is_sign_negative());
1115 assert!(!neg_inf.is_nan());
1116 assert!(!neg_inf.is_normal());
1117 assert_eq!(Fp::Infinite, neg_inf.classify());
1122 let zero: f64 = 0.0f64;
1123 assert_eq!(0.0, zero);
1124 assert!(!zero.is_infinite());
1125 assert!(zero.is_finite());
1126 assert!(zero.is_sign_positive());
1127 assert!(!zero.is_sign_negative());
1128 assert!(!zero.is_nan());
1129 assert!(!zero.is_normal());
1130 assert_eq!(Fp::Zero, zero.classify());
1134 fn test_neg_zero() {
1135 let neg_zero: f64 = -0.0;
1136 assert_eq!(0.0, neg_zero);
1137 assert!(!neg_zero.is_infinite());
1138 assert!(neg_zero.is_finite());
1139 assert!(!neg_zero.is_sign_positive());
1140 assert!(neg_zero.is_sign_negative());
1141 assert!(!neg_zero.is_nan());
1142 assert!(!neg_zero.is_normal());
1143 assert_eq!(Fp::Zero, neg_zero.classify());
1148 let one: f64 = 1.0f64;
1149 assert_eq!(1.0, one);
1150 assert!(!one.is_infinite());
1151 assert!(one.is_finite());
1152 assert!(one.is_sign_positive());
1153 assert!(!one.is_sign_negative());
1154 assert!(!one.is_nan());
1155 assert!(one.is_normal());
1156 assert_eq!(Fp::Normal, one.classify());
1162 let inf: f64 = INFINITY;
1163 let neg_inf: f64 = NEG_INFINITY;
1164 assert!(nan.is_nan());
1165 assert!(!0.0f64.is_nan());
1166 assert!(!5.3f64.is_nan());
1167 assert!(!(-10.732f64).is_nan());
1168 assert!(!inf.is_nan());
1169 assert!(!neg_inf.is_nan());
1173 fn test_is_infinite() {
1175 let inf: f64 = INFINITY;
1176 let neg_inf: f64 = NEG_INFINITY;
1177 assert!(!nan.is_infinite());
1178 assert!(inf.is_infinite());
1179 assert!(neg_inf.is_infinite());
1180 assert!(!0.0f64.is_infinite());
1181 assert!(!42.8f64.is_infinite());
1182 assert!(!(-109.2f64).is_infinite());
1186 fn test_is_finite() {
1188 let inf: f64 = INFINITY;
1189 let neg_inf: f64 = NEG_INFINITY;
1190 assert!(!nan.is_finite());
1191 assert!(!inf.is_finite());
1192 assert!(!neg_inf.is_finite());
1193 assert!(0.0f64.is_finite());
1194 assert!(42.8f64.is_finite());
1195 assert!((-109.2f64).is_finite());
1199 fn test_is_normal() {
1201 let inf: f64 = INFINITY;
1202 let neg_inf: f64 = NEG_INFINITY;
1203 let zero: f64 = 0.0f64;
1204 let neg_zero: f64 = -0.0;
1205 assert!(!nan.is_normal());
1206 assert!(!inf.is_normal());
1207 assert!(!neg_inf.is_normal());
1208 assert!(!zero.is_normal());
1209 assert!(!neg_zero.is_normal());
1210 assert!(1f64.is_normal());
1211 assert!(1e-307f64.is_normal());
1212 assert!(!1e-308f64.is_normal());
1216 fn test_classify() {
1218 let inf: f64 = INFINITY;
1219 let neg_inf: f64 = NEG_INFINITY;
1220 let zero: f64 = 0.0f64;
1221 let neg_zero: f64 = -0.0;
1222 assert_eq!(nan.classify(), Fp::Nan);
1223 assert_eq!(inf.classify(), Fp::Infinite);
1224 assert_eq!(neg_inf.classify(), Fp::Infinite);
1225 assert_eq!(zero.classify(), Fp::Zero);
1226 assert_eq!(neg_zero.classify(), Fp::Zero);
1227 assert_eq!(1e-307f64.classify(), Fp::Normal);
1228 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1232 fn test_integer_decode() {
1233 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
1234 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
1235 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
1236 assert_eq!(0f64.integer_decode(), (0, -1075, 1));
1237 assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
1238 assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
1239 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1240 assert_eq!(NAN.integer_decode(), (6755399441055744, 972, 1));
1245 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1246 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1247 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1248 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1249 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1250 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1251 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1252 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1253 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1254 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1259 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1260 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1261 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1262 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1263 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1264 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1265 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1266 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1267 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1268 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1273 assert_approx_eq!(1.0f64.round(), 1.0f64);
1274 assert_approx_eq!(1.3f64.round(), 1.0f64);
1275 assert_approx_eq!(1.5f64.round(), 2.0f64);
1276 assert_approx_eq!(1.7f64.round(), 2.0f64);
1277 assert_approx_eq!(0.0f64.round(), 0.0f64);
1278 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1279 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1280 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1281 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1282 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1287 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1288 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1289 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1290 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1291 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1292 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1293 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1294 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1295 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1296 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1301 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1302 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1303 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1304 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1305 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1306 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1307 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1308 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1309 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1310 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1315 assert_eq!(INFINITY.abs(), INFINITY);
1316 assert_eq!(1f64.abs(), 1f64);
1317 assert_eq!(0f64.abs(), 0f64);
1318 assert_eq!((-0f64).abs(), 0f64);
1319 assert_eq!((-1f64).abs(), 1f64);
1320 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1321 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1322 assert!(NAN.abs().is_nan());
1327 assert_eq!(INFINITY.signum(), 1f64);
1328 assert_eq!(1f64.signum(), 1f64);
1329 assert_eq!(0f64.signum(), 1f64);
1330 assert_eq!((-0f64).signum(), -1f64);
1331 assert_eq!((-1f64).signum(), -1f64);
1332 assert_eq!(NEG_INFINITY.signum(), -1f64);
1333 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1334 assert!(NAN.signum().is_nan());
1338 fn test_is_sign_positive() {
1339 assert!(INFINITY.is_sign_positive());
1340 assert!(1f64.is_sign_positive());
1341 assert!(0f64.is_sign_positive());
1342 assert!(!(-0f64).is_sign_positive());
1343 assert!(!(-1f64).is_sign_positive());
1344 assert!(!NEG_INFINITY.is_sign_positive());
1345 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1346 assert!(!NAN.is_sign_positive());
1350 fn test_is_sign_negative() {
1351 assert!(!INFINITY.is_sign_negative());
1352 assert!(!1f64.is_sign_negative());
1353 assert!(!0f64.is_sign_negative());
1354 assert!((-0f64).is_sign_negative());
1355 assert!((-1f64).is_sign_negative());
1356 assert!(NEG_INFINITY.is_sign_negative());
1357 assert!((1f64/NEG_INFINITY).is_sign_negative());
1358 assert!(!NAN.is_sign_negative());
1364 let inf: f64 = INFINITY;
1365 let neg_inf: f64 = NEG_INFINITY;
1366 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1367 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1368 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1369 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1370 assert!(nan.mul_add(7.8, 9.0).is_nan());
1371 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1372 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1373 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1374 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1380 let inf: f64 = INFINITY;
1381 let neg_inf: f64 = NEG_INFINITY;
1382 assert_eq!(1.0f64.recip(), 1.0);
1383 assert_eq!(2.0f64.recip(), 0.5);
1384 assert_eq!((-0.4f64).recip(), -2.5);
1385 assert_eq!(0.0f64.recip(), inf);
1386 assert!(nan.recip().is_nan());
1387 assert_eq!(inf.recip(), 0.0);
1388 assert_eq!(neg_inf.recip(), 0.0);
1394 let inf: f64 = INFINITY;
1395 let neg_inf: f64 = NEG_INFINITY;
1396 assert_eq!(1.0f64.powi(1), 1.0);
1397 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1398 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1399 assert_eq!(8.3f64.powi(0), 1.0);
1400 assert!(nan.powi(2).is_nan());
1401 assert_eq!(inf.powi(3), inf);
1402 assert_eq!(neg_inf.powi(2), inf);
1408 let inf: f64 = INFINITY;
1409 let neg_inf: f64 = NEG_INFINITY;
1410 assert_eq!(1.0f64.powf(1.0), 1.0);
1411 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1412 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1413 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1414 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1415 assert_eq!(8.3f64.powf(0.0), 1.0);
1416 assert!(nan.powf(2.0).is_nan());
1417 assert_eq!(inf.powf(2.0), inf);
1418 assert_eq!(neg_inf.powf(3.0), neg_inf);
1422 fn test_sqrt_domain() {
1423 assert!(NAN.sqrt().is_nan());
1424 assert!(NEG_INFINITY.sqrt().is_nan());
1425 assert!((-1.0f64).sqrt().is_nan());
1426 assert_eq!((-0.0f64).sqrt(), -0.0);
1427 assert_eq!(0.0f64.sqrt(), 0.0);
1428 assert_eq!(1.0f64.sqrt(), 1.0);
1429 assert_eq!(INFINITY.sqrt(), INFINITY);
1434 assert_eq!(1.0, 0.0f64.exp());
1435 assert_approx_eq!(2.718282, 1.0f64.exp());
1436 assert_approx_eq!(148.413159, 5.0f64.exp());
1438 let inf: f64 = INFINITY;
1439 let neg_inf: f64 = NEG_INFINITY;
1441 assert_eq!(inf, inf.exp());
1442 assert_eq!(0.0, neg_inf.exp());
1443 assert!(nan.exp().is_nan());
1448 assert_eq!(32.0, 5.0f64.exp2());
1449 assert_eq!(1.0, 0.0f64.exp2());
1451 let inf: f64 = INFINITY;
1452 let neg_inf: f64 = NEG_INFINITY;
1454 assert_eq!(inf, inf.exp2());
1455 assert_eq!(0.0, neg_inf.exp2());
1456 assert!(nan.exp2().is_nan());
1462 let inf: f64 = INFINITY;
1463 let neg_inf: f64 = NEG_INFINITY;
1464 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1465 assert!(nan.ln().is_nan());
1466 assert_eq!(inf.ln(), inf);
1467 assert!(neg_inf.ln().is_nan());
1468 assert!((-2.3f64).ln().is_nan());
1469 assert_eq!((-0.0f64).ln(), neg_inf);
1470 assert_eq!(0.0f64.ln(), neg_inf);
1471 assert_approx_eq!(4.0f64.ln(), 1.386294);
1477 let inf: f64 = INFINITY;
1478 let neg_inf: f64 = NEG_INFINITY;
1479 assert_eq!(10.0f64.log(10.0), 1.0);
1480 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1481 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1482 assert!(1.0f64.log(1.0).is_nan());
1483 assert!(1.0f64.log(-13.9).is_nan());
1484 assert!(nan.log(2.3).is_nan());
1485 assert_eq!(inf.log(10.0), inf);
1486 assert!(neg_inf.log(8.8).is_nan());
1487 assert!((-2.3f64).log(0.1).is_nan());
1488 assert_eq!((-0.0f64).log(2.0), neg_inf);
1489 assert_eq!(0.0f64.log(7.0), neg_inf);
1495 let inf: f64 = INFINITY;
1496 let neg_inf: f64 = NEG_INFINITY;
1497 assert_approx_eq!(10.0f64.log2(), 3.321928);
1498 assert_approx_eq!(2.3f64.log2(), 1.201634);
1499 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1500 assert!(nan.log2().is_nan());
1501 assert_eq!(inf.log2(), inf);
1502 assert!(neg_inf.log2().is_nan());
1503 assert!((-2.3f64).log2().is_nan());
1504 assert_eq!((-0.0f64).log2(), neg_inf);
1505 assert_eq!(0.0f64.log2(), neg_inf);
1511 let inf: f64 = INFINITY;
1512 let neg_inf: f64 = NEG_INFINITY;
1513 assert_eq!(10.0f64.log10(), 1.0);
1514 assert_approx_eq!(2.3f64.log10(), 0.361728);
1515 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1516 assert_eq!(1.0f64.log10(), 0.0);
1517 assert!(nan.log10().is_nan());
1518 assert_eq!(inf.log10(), inf);
1519 assert!(neg_inf.log10().is_nan());
1520 assert!((-2.3f64).log10().is_nan());
1521 assert_eq!((-0.0f64).log10(), neg_inf);
1522 assert_eq!(0.0f64.log10(), neg_inf);
1526 fn test_to_degrees() {
1527 let pi: f64 = consts::PI;
1529 let inf: f64 = INFINITY;
1530 let neg_inf: f64 = NEG_INFINITY;
1531 assert_eq!(0.0f64.to_degrees(), 0.0);
1532 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1533 assert_eq!(pi.to_degrees(), 180.0);
1534 assert!(nan.to_degrees().is_nan());
1535 assert_eq!(inf.to_degrees(), inf);
1536 assert_eq!(neg_inf.to_degrees(), neg_inf);
1540 fn test_to_radians() {
1541 let pi: f64 = consts::PI;
1543 let inf: f64 = INFINITY;
1544 let neg_inf: f64 = NEG_INFINITY;
1545 assert_eq!(0.0f64.to_radians(), 0.0);
1546 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1547 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1548 assert_eq!(180.0f64.to_radians(), pi);
1549 assert!(nan.to_radians().is_nan());
1550 assert_eq!(inf.to_radians(), inf);
1551 assert_eq!(neg_inf.to_radians(), neg_inf);
1556 // We have to use from_str until base-2 exponents
1557 // are supported in floating-point literals
1558 let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1559 let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1560 let f3: f64 = f64::from_str_radix("1.Cp-12", 16).unwrap();
1561 assert_eq!(f64::ldexp(1f64, -123), f1);
1562 assert_eq!(f64::ldexp(1f64, -111), f2);
1563 assert_eq!(f64::ldexp(1.75f64, -12), f3);
1565 assert_eq!(f64::ldexp(0f64, -123), 0f64);
1566 assert_eq!(f64::ldexp(-0f64, -123), -0f64);
1568 let inf: f64 = INFINITY;
1569 let neg_inf: f64 = NEG_INFINITY;
1571 assert_eq!(f64::ldexp(inf, -123), inf);
1572 assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
1573 assert!(f64::ldexp(nan, -123).is_nan());
1578 // We have to use from_str until base-2 exponents
1579 // are supported in floating-point literals
1580 let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap();
1581 let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap();
1582 let f3: f64 = f64::from_str_radix("1.Cp-123", 16).unwrap();
1583 let (x1, exp1) = f1.frexp();
1584 let (x2, exp2) = f2.frexp();
1585 let (x3, exp3) = f3.frexp();
1586 assert_eq!((x1, exp1), (0.5f64, -122));
1587 assert_eq!((x2, exp2), (0.5f64, -110));
1588 assert_eq!((x3, exp3), (0.875f64, -122));
1589 assert_eq!(f64::ldexp(x1, exp1), f1);
1590 assert_eq!(f64::ldexp(x2, exp2), f2);
1591 assert_eq!(f64::ldexp(x3, exp3), f3);
1593 assert_eq!(0f64.frexp(), (0f64, 0));
1594 assert_eq!((-0f64).frexp(), (-0f64, 0));
1597 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1598 fn test_frexp_nowin() {
1599 let inf: f64 = INFINITY;
1600 let neg_inf: f64 = NEG_INFINITY;
1602 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1603 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1604 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1609 assert_eq!((-1f64).abs_sub(1f64), 0f64);
1610 assert_eq!(1f64.abs_sub(1f64), 0f64);
1611 assert_eq!(1f64.abs_sub(0f64), 1f64);
1612 assert_eq!(1f64.abs_sub(-1f64), 2f64);
1613 assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
1614 assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
1615 assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
1616 assert_eq!(0f64.abs_sub(INFINITY), 0f64);
1620 fn test_abs_sub_nowin() {
1621 assert!(NAN.abs_sub(-1f64).is_nan());
1622 assert!(1f64.abs_sub(NAN).is_nan());
1627 assert_eq!(0.0f64.asinh(), 0.0f64);
1628 assert_eq!((-0.0f64).asinh(), -0.0f64);
1630 let inf: f64 = INFINITY;
1631 let neg_inf: f64 = NEG_INFINITY;
1633 assert_eq!(inf.asinh(), inf);
1634 assert_eq!(neg_inf.asinh(), neg_inf);
1635 assert!(nan.asinh().is_nan());
1636 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1637 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1642 assert_eq!(1.0f64.acosh(), 0.0f64);
1643 assert!(0.999f64.acosh().is_nan());
1645 let inf: f64 = INFINITY;
1646 let neg_inf: f64 = NEG_INFINITY;
1648 assert_eq!(inf.acosh(), inf);
1649 assert!(neg_inf.acosh().is_nan());
1650 assert!(nan.acosh().is_nan());
1651 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1652 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1657 assert_eq!(0.0f64.atanh(), 0.0f64);
1658 assert_eq!((-0.0f64).atanh(), -0.0f64);
1660 let inf: f64 = INFINITY;
1661 let neg_inf: f64 = NEG_INFINITY;
1663 assert_eq!(1.0f64.atanh(), inf);
1664 assert_eq!((-1.0f64).atanh(), neg_inf);
1665 assert!(2f64.atanh().atanh().is_nan());
1666 assert!((-2f64).atanh().atanh().is_nan());
1667 assert!(inf.atanh().is_nan());
1668 assert!(neg_inf.atanh().is_nan());
1669 assert!(nan.atanh().is_nan());
1670 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1671 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1675 fn test_real_consts() {
1677 let pi: f64 = consts::PI;
1678 let two_pi: f64 = consts::PI_2;
1679 let frac_pi_2: f64 = consts::FRAC_PI_2;
1680 let frac_pi_3: f64 = consts::FRAC_PI_3;
1681 let frac_pi_4: f64 = consts::FRAC_PI_4;
1682 let frac_pi_6: f64 = consts::FRAC_PI_6;
1683 let frac_pi_8: f64 = consts::FRAC_PI_8;
1684 let frac_1_pi: f64 = consts::FRAC_1_PI;
1685 let frac_2_pi: f64 = consts::FRAC_2_PI;
1686 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1687 let sqrt2: f64 = consts::SQRT_2;
1688 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1689 let e: f64 = consts::E;
1690 let log2_e: f64 = consts::LOG2_E;
1691 let log10_e: f64 = consts::LOG10_E;
1692 let ln_2: f64 = consts::LN_2;
1693 let ln_10: f64 = consts::LN_10;
1695 assert_approx_eq!(two_pi, 2.0 * pi);
1696 assert_approx_eq!(frac_pi_2, pi / 2f64);
1697 assert_approx_eq!(frac_pi_3, pi / 3f64);
1698 assert_approx_eq!(frac_pi_4, pi / 4f64);
1699 assert_approx_eq!(frac_pi_6, pi / 6f64);
1700 assert_approx_eq!(frac_pi_8, pi / 8f64);
1701 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1702 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1703 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1704 assert_approx_eq!(sqrt2, 2f64.sqrt());
1705 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1706 assert_approx_eq!(log2_e, e.log2());
1707 assert_approx_eq!(log10_e, e.log10());
1708 assert_approx_eq!(ln_2, 2f64.ln());
1709 assert_approx_eq!(ln_10, 10f64.ln());