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)]
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 /// Returns `true` if this value is `NaN` and false otherwise.
91 /// let nan = f64::NAN;
94 /// assert!(nan.is_nan());
95 /// assert!(!f.is_nan());
97 #[stable(feature = "rust1", since = "1.0.0")]
99 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
101 /// Returns `true` if this value is positive infinity or negative infinity and
108 /// let inf = f64::INFINITY;
109 /// let neg_inf = f64::NEG_INFINITY;
110 /// let nan = f64::NAN;
112 /// assert!(!f.is_infinite());
113 /// assert!(!nan.is_infinite());
115 /// assert!(inf.is_infinite());
116 /// assert!(neg_inf.is_infinite());
118 #[stable(feature = "rust1", since = "1.0.0")]
120 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
122 /// Returns `true` if this number is neither infinite nor `NaN`.
128 /// let inf: f64 = f64::INFINITY;
129 /// let neg_inf: f64 = f64::NEG_INFINITY;
130 /// let nan: f64 = f64::NAN;
132 /// assert!(f.is_finite());
134 /// assert!(!nan.is_finite());
135 /// assert!(!inf.is_finite());
136 /// assert!(!neg_inf.is_finite());
138 #[stable(feature = "rust1", since = "1.0.0")]
140 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
142 /// Returns `true` if the number is neither zero, infinite,
143 /// [subnormal][subnormal], or `NaN`.
148 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f64
149 /// let max = f32::MAX;
150 /// let lower_than_min = 1.0e-40_f32;
151 /// let zero = 0.0f32;
153 /// assert!(min.is_normal());
154 /// assert!(max.is_normal());
156 /// assert!(!zero.is_normal());
157 /// assert!(!f32::NAN.is_normal());
158 /// assert!(!f32::INFINITY.is_normal());
159 /// // Values between `0` and `min` are Subnormal.
160 /// assert!(!lower_than_min.is_normal());
162 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
163 #[stable(feature = "rust1", since = "1.0.0")]
165 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
167 /// Returns the floating point category of the number. If only one property
168 /// is going to be tested, it is generally faster to use the specific
169 /// predicate instead.
172 /// use std::num::FpCategory;
175 /// let num = 12.4_f64;
176 /// let inf = f64::INFINITY;
178 /// assert_eq!(num.classify(), FpCategory::Normal);
179 /// assert_eq!(inf.classify(), FpCategory::Infinite);
181 #[stable(feature = "rust1", since = "1.0.0")]
183 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
185 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
186 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
187 /// The floating point encoding is documented in the [Reference][floating-point].
190 /// #![feature(float_extras)]
192 /// let num = 2.0f64;
194 /// // (8388608, -22, 1)
195 /// let (mantissa, exponent, sign) = num.integer_decode();
196 /// let sign_f = sign as f64;
197 /// let mantissa_f = mantissa as f64;
198 /// let exponent_f = num.powf(exponent as f64);
200 /// // 1 * 8388608 * 2^(-22) == 2
201 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
203 /// assert!(abs_difference < 1e-10);
205 /// [floating-point]: ../../../../../reference.html#machine-types
206 #[unstable(feature = "float_extras", reason = "signature is undecided",
209 pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
211 /// Returns the largest integer less than or equal to a number.
214 /// let f = 3.99_f64;
217 /// assert_eq!(f.floor(), 3.0);
218 /// assert_eq!(g.floor(), 3.0);
220 #[stable(feature = "rust1", since = "1.0.0")]
222 pub fn floor(self) -> f64 {
223 unsafe { intrinsics::floorf64(self) }
226 /// Returns the smallest integer greater than or equal to a number.
229 /// let f = 3.01_f64;
232 /// assert_eq!(f.ceil(), 4.0);
233 /// assert_eq!(g.ceil(), 4.0);
235 #[stable(feature = "rust1", since = "1.0.0")]
237 pub fn ceil(self) -> f64 {
238 unsafe { intrinsics::ceilf64(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 {
254 unsafe { intrinsics::roundf64(self) }
257 /// Returns the integer part of a number.
261 /// let g = -3.7_f64;
263 /// assert_eq!(f.trunc(), 3.0);
264 /// assert_eq!(g.trunc(), -3.0);
266 #[stable(feature = "rust1", since = "1.0.0")]
268 pub fn trunc(self) -> f64 {
269 unsafe { intrinsics::truncf64(self) }
272 /// Returns the fractional part of a number.
276 /// let y = -3.5_f64;
277 /// let abs_difference_x = (x.fract() - 0.5).abs();
278 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
280 /// assert!(abs_difference_x < 1e-10);
281 /// assert!(abs_difference_y < 1e-10);
283 #[stable(feature = "rust1", since = "1.0.0")]
285 pub fn fract(self) -> f64 { self - self.trunc() }
287 /// Computes the absolute value of `self`. Returns `NAN` if the
294 /// let y = -3.5_f64;
296 /// let abs_difference_x = (x.abs() - x).abs();
297 /// let abs_difference_y = (y.abs() - (-y)).abs();
299 /// assert!(abs_difference_x < 1e-10);
300 /// assert!(abs_difference_y < 1e-10);
302 /// assert!(f64::NAN.abs().is_nan());
304 #[stable(feature = "rust1", since = "1.0.0")]
306 pub fn abs(self) -> f64 { num::Float::abs(self) }
308 /// Returns a number that represents the sign of `self`.
310 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
311 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
312 /// - `NAN` if the number is `NAN`
319 /// assert_eq!(f.signum(), 1.0);
320 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
322 /// assert!(f64::NAN.signum().is_nan());
324 #[stable(feature = "rust1", since = "1.0.0")]
326 pub fn signum(self) -> f64 { num::Float::signum(self) }
328 /// Returns `true` if `self`'s sign bit is positive, including
329 /// `+0.0` and `INFINITY`.
334 /// let nan: f64 = f64::NAN;
337 /// let g = -7.0_f64;
339 /// assert!(f.is_sign_positive());
340 /// assert!(!g.is_sign_positive());
341 /// // Requires both tests to determine if is `NaN`
342 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
344 #[stable(feature = "rust1", since = "1.0.0")]
346 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
348 #[stable(feature = "rust1", since = "1.0.0")]
349 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
351 pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
353 /// Returns `true` if `self`'s sign is negative, including `-0.0`
354 /// and `NEG_INFINITY`.
359 /// let nan = f64::NAN;
362 /// let g = -7.0_f64;
364 /// assert!(!f.is_sign_negative());
365 /// assert!(g.is_sign_negative());
366 /// // Requires both tests to determine if is `NaN`.
367 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
369 #[stable(feature = "rust1", since = "1.0.0")]
371 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
373 #[stable(feature = "rust1", since = "1.0.0")]
374 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
376 pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
378 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
379 /// error. This produces a more accurate result with better performance than
380 /// a separate multiplication operation followed by an add.
383 /// let m = 10.0_f64;
385 /// let b = 60.0_f64;
388 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
390 /// assert!(abs_difference < 1e-10);
392 #[stable(feature = "rust1", since = "1.0.0")]
394 pub fn mul_add(self, a: f64, b: f64) -> f64 {
395 unsafe { intrinsics::fmaf64(self, a, b) }
398 /// Takes the reciprocal (inverse) of a number, `1/x`.
402 /// let abs_difference = (x.recip() - (1.0/x)).abs();
404 /// assert!(abs_difference < 1e-10);
406 #[stable(feature = "rust1", since = "1.0.0")]
408 pub fn recip(self) -> f64 { num::Float::recip(self) }
410 /// Raises a number to an integer power.
412 /// Using this function is generally faster than using `powf`
416 /// let abs_difference = (x.powi(2) - x*x).abs();
418 /// assert!(abs_difference < 1e-10);
420 #[stable(feature = "rust1", since = "1.0.0")]
422 pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
424 /// Raises a number to a floating point power.
428 /// let abs_difference = (x.powf(2.0) - x*x).abs();
430 /// assert!(abs_difference < 1e-10);
432 #[stable(feature = "rust1", since = "1.0.0")]
434 pub fn powf(self, n: f64) -> f64 {
435 unsafe { intrinsics::powf64(self, n) }
438 /// Takes the square root of a number.
440 /// Returns NaN if `self` is a negative number.
443 /// let positive = 4.0_f64;
444 /// let negative = -4.0_f64;
446 /// let abs_difference = (positive.sqrt() - 2.0).abs();
448 /// assert!(abs_difference < 1e-10);
449 /// assert!(negative.sqrt().is_nan());
451 #[stable(feature = "rust1", since = "1.0.0")]
453 pub fn sqrt(self) -> f64 {
457 unsafe { intrinsics::sqrtf64(self) }
461 /// Returns `e^(self)`, (the exponential function).
464 /// let one = 1.0_f64;
466 /// let e = one.exp();
468 /// // ln(e) - 1 == 0
469 /// let abs_difference = (e.ln() - 1.0).abs();
471 /// assert!(abs_difference < 1e-10);
473 #[stable(feature = "rust1", since = "1.0.0")]
475 pub fn exp(self) -> f64 {
476 unsafe { intrinsics::expf64(self) }
479 /// Returns `2^(self)`.
485 /// let abs_difference = (f.exp2() - 4.0).abs();
487 /// assert!(abs_difference < 1e-10);
489 #[stable(feature = "rust1", since = "1.0.0")]
491 pub fn exp2(self) -> f64 {
492 unsafe { intrinsics::exp2f64(self) }
495 /// Returns the natural logarithm of the number.
498 /// let one = 1.0_f64;
500 /// let e = one.exp();
502 /// // ln(e) - 1 == 0
503 /// let abs_difference = (e.ln() - 1.0).abs();
505 /// assert!(abs_difference < 1e-10);
507 #[stable(feature = "rust1", since = "1.0.0")]
509 pub fn ln(self) -> f64 {
510 unsafe { intrinsics::logf64(self) }
513 /// Returns the logarithm of the number with respect to an arbitrary base.
516 /// let ten = 10.0_f64;
517 /// let two = 2.0_f64;
519 /// // log10(10) - 1 == 0
520 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
522 /// // log2(2) - 1 == 0
523 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
525 /// assert!(abs_difference_10 < 1e-10);
526 /// assert!(abs_difference_2 < 1e-10);
528 #[stable(feature = "rust1", since = "1.0.0")]
530 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
532 /// Returns the base 2 logarithm of the number.
535 /// let two = 2.0_f64;
537 /// // log2(2) - 1 == 0
538 /// let abs_difference = (two.log2() - 1.0).abs();
540 /// assert!(abs_difference < 1e-10);
542 #[stable(feature = "rust1", since = "1.0.0")]
544 pub fn log2(self) -> f64 {
545 unsafe { intrinsics::log2f64(self) }
548 /// Returns the base 10 logarithm of the number.
551 /// let ten = 10.0_f64;
553 /// // log10(10) - 1 == 0
554 /// let abs_difference = (ten.log10() - 1.0).abs();
556 /// assert!(abs_difference < 1e-10);
558 #[stable(feature = "rust1", since = "1.0.0")]
560 pub fn log10(self) -> f64 {
561 unsafe { intrinsics::log10f64(self) }
564 /// Converts radians to degrees.
567 /// use std::f64::consts;
569 /// let angle = consts::PI;
571 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
573 /// assert!(abs_difference < 1e-10);
575 #[stable(feature = "rust1", since = "1.0.0")]
577 pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
579 /// Converts degrees to radians.
582 /// use std::f64::consts;
584 /// let angle = 180.0_f64;
586 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
588 /// assert!(abs_difference < 1e-10);
590 #[stable(feature = "rust1", since = "1.0.0")]
592 pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
594 /// Constructs a floating point number of `x*2^exp`.
597 /// #![feature(float_extras)]
599 /// // 3*2^2 - 12 == 0
600 /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
602 /// assert!(abs_difference < 1e-10);
604 #[unstable(feature = "float_extras",
605 reason = "pending integer conventions",
608 pub fn ldexp(x: f64, exp: isize) -> f64 {
609 unsafe { cmath::ldexp(x, exp as c_int) }
612 /// Breaks the number into a normalized fraction and a base-2 exponent,
615 /// * `self = x * 2^exp`
616 /// * `0.5 <= abs(x) < 1.0`
619 /// #![feature(float_extras)]
623 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
624 /// let f = x.frexp();
625 /// let abs_difference_0 = (f.0 - 0.5).abs();
626 /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
628 /// assert!(abs_difference_0 < 1e-10);
629 /// assert!(abs_difference_1 < 1e-10);
631 #[unstable(feature = "float_extras",
632 reason = "pending integer conventions",
635 pub fn frexp(self) -> (f64, isize) {
638 let x = cmath::frexp(self, &mut exp);
643 /// Returns the next representable floating-point value in the direction of
647 /// #![feature(float_extras)]
651 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
653 /// assert!(abs_diff < 1e-10);
655 #[unstable(feature = "float_extras",
656 reason = "unsure about its place in the world",
659 pub fn next_after(self, other: f64) -> f64 {
660 unsafe { cmath::nextafter(self, other) }
663 /// Returns the maximum of the two numbers.
669 /// assert_eq!(x.max(y), y);
672 /// If one of the arguments is NaN, then the other argument is returned.
673 #[stable(feature = "rust1", since = "1.0.0")]
675 pub fn max(self, other: f64) -> f64 {
676 unsafe { cmath::fmax(self, other) }
679 /// Returns the minimum of the two numbers.
685 /// assert_eq!(x.min(y), x);
688 /// If one of the arguments is NaN, then the other argument is returned.
689 #[stable(feature = "rust1", since = "1.0.0")]
691 pub fn min(self, other: f64) -> f64 {
692 unsafe { cmath::fmin(self, other) }
695 /// The positive difference of two numbers.
697 /// * If `self <= other`: `0:0`
698 /// * Else: `self - other`
702 /// let y = -3.0_f64;
704 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
705 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
707 /// assert!(abs_difference_x < 1e-10);
708 /// assert!(abs_difference_y < 1e-10);
710 #[stable(feature = "rust1", since = "1.0.0")]
712 pub fn abs_sub(self, other: f64) -> f64 {
713 unsafe { cmath::fdim(self, other) }
716 /// Takes the cubic root of a number.
721 /// // x^(1/3) - 2 == 0
722 /// let abs_difference = (x.cbrt() - 2.0).abs();
724 /// assert!(abs_difference < 1e-10);
726 #[stable(feature = "rust1", since = "1.0.0")]
728 pub fn cbrt(self) -> f64 {
729 unsafe { cmath::cbrt(self) }
732 /// Calculates the length of the hypotenuse of a right-angle triangle given
733 /// legs of length `x` and `y`.
739 /// // sqrt(x^2 + y^2)
740 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
742 /// assert!(abs_difference < 1e-10);
744 #[stable(feature = "rust1", since = "1.0.0")]
746 pub fn hypot(self, other: f64) -> f64 {
747 unsafe { cmath::hypot(self, other) }
750 /// Computes the sine of a number (in radians).
755 /// let x = f64::consts::PI/2.0;
757 /// let abs_difference = (x.sin() - 1.0).abs();
759 /// assert!(abs_difference < 1e-10);
761 #[stable(feature = "rust1", since = "1.0.0")]
763 pub fn sin(self) -> f64 {
764 unsafe { intrinsics::sinf64(self) }
767 /// Computes the cosine of a number (in radians).
772 /// let x = 2.0*f64::consts::PI;
774 /// let abs_difference = (x.cos() - 1.0).abs();
776 /// assert!(abs_difference < 1e-10);
778 #[stable(feature = "rust1", since = "1.0.0")]
780 pub fn cos(self) -> f64 {
781 unsafe { intrinsics::cosf64(self) }
784 /// Computes the tangent of a number (in radians).
789 /// let x = f64::consts::PI/4.0;
790 /// let abs_difference = (x.tan() - 1.0).abs();
792 /// assert!(abs_difference < 1e-14);
794 #[stable(feature = "rust1", since = "1.0.0")]
796 pub fn tan(self) -> f64 {
797 unsafe { cmath::tan(self) }
800 /// Computes the arcsine of a number. Return value is in radians in
801 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
807 /// let f = f64::consts::PI / 2.0;
809 /// // asin(sin(pi/2))
810 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
812 /// assert!(abs_difference < 1e-10);
814 #[stable(feature = "rust1", since = "1.0.0")]
816 pub fn asin(self) -> f64 {
817 unsafe { cmath::asin(self) }
820 /// Computes the arccosine of a number. Return value is in radians in
821 /// the range [0, pi] or NaN if the number is outside the range
827 /// let f = f64::consts::PI / 4.0;
829 /// // acos(cos(pi/4))
830 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
832 /// assert!(abs_difference < 1e-10);
834 #[stable(feature = "rust1", since = "1.0.0")]
836 pub fn acos(self) -> f64 {
837 unsafe { cmath::acos(self) }
840 /// Computes the arctangent of a number. Return value is in radians in the
841 /// range [-pi/2, pi/2];
847 /// let abs_difference = (f.tan().atan() - 1.0).abs();
849 /// assert!(abs_difference < 1e-10);
851 #[stable(feature = "rust1", since = "1.0.0")]
853 pub fn atan(self) -> f64 {
854 unsafe { cmath::atan(self) }
857 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
859 /// * `x = 0`, `y = 0`: `0`
860 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
861 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
862 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
867 /// let pi = f64::consts::PI;
868 /// // All angles from horizontal right (+x)
869 /// // 45 deg counter-clockwise
870 /// let x1 = 3.0_f64;
871 /// let y1 = -3.0_f64;
873 /// // 135 deg clockwise
874 /// let x2 = -3.0_f64;
875 /// let y2 = 3.0_f64;
877 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
878 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
880 /// assert!(abs_difference_1 < 1e-10);
881 /// assert!(abs_difference_2 < 1e-10);
883 #[stable(feature = "rust1", since = "1.0.0")]
885 pub fn atan2(self, other: f64) -> f64 {
886 unsafe { cmath::atan2(self, other) }
889 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
890 /// `(sin(x), cos(x))`.
895 /// let x = f64::consts::PI/4.0;
896 /// let f = x.sin_cos();
898 /// let abs_difference_0 = (f.0 - x.sin()).abs();
899 /// let abs_difference_1 = (f.1 - x.cos()).abs();
901 /// assert!(abs_difference_0 < 1e-10);
902 /// assert!(abs_difference_0 < 1e-10);
904 #[stable(feature = "rust1", since = "1.0.0")]
906 pub fn sin_cos(self) -> (f64, f64) {
907 (self.sin(), self.cos())
910 /// Returns `e^(self) - 1` in a way that is accurate even if the
911 /// number is close to zero.
917 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
919 /// assert!(abs_difference < 1e-10);
921 #[stable(feature = "rust1", since = "1.0.0")]
923 pub fn exp_m1(self) -> f64 {
924 unsafe { cmath::expm1(self) }
927 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
928 /// the operations were performed separately.
933 /// let x = f64::consts::E - 1.0;
935 /// // ln(1 + (e - 1)) == ln(e) == 1
936 /// let abs_difference = (x.ln_1p() - 1.0).abs();
938 /// assert!(abs_difference < 1e-10);
940 #[stable(feature = "rust1", since = "1.0.0")]
942 pub fn ln_1p(self) -> f64 {
943 unsafe { cmath::log1p(self) }
946 /// Hyperbolic sine function.
951 /// let e = f64::consts::E;
954 /// let f = x.sinh();
955 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
956 /// let g = (e*e - 1.0)/(2.0*e);
957 /// let abs_difference = (f - g).abs();
959 /// assert!(abs_difference < 1e-10);
961 #[stable(feature = "rust1", since = "1.0.0")]
963 pub fn sinh(self) -> f64 {
964 unsafe { cmath::sinh(self) }
967 /// Hyperbolic cosine function.
972 /// let e = f64::consts::E;
974 /// let f = x.cosh();
975 /// // Solving cosh() at 1 gives this result
976 /// let g = (e*e + 1.0)/(2.0*e);
977 /// let abs_difference = (f - g).abs();
980 /// assert!(abs_difference < 1.0e-10);
982 #[stable(feature = "rust1", since = "1.0.0")]
984 pub fn cosh(self) -> f64 {
985 unsafe { cmath::cosh(self) }
988 /// Hyperbolic tangent function.
993 /// let e = f64::consts::E;
996 /// let f = x.tanh();
997 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
998 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
999 /// let abs_difference = (f - g).abs();
1001 /// assert!(abs_difference < 1.0e-10);
1003 #[stable(feature = "rust1", since = "1.0.0")]
1005 pub fn tanh(self) -> f64 {
1006 unsafe { cmath::tanh(self) }
1009 /// Inverse hyperbolic sine function.
1012 /// let x = 1.0_f64;
1013 /// let f = x.sinh().asinh();
1015 /// let abs_difference = (f - x).abs();
1017 /// assert!(abs_difference < 1.0e-10);
1019 #[stable(feature = "rust1", since = "1.0.0")]
1021 pub fn asinh(self) -> f64 {
1023 NEG_INFINITY => NEG_INFINITY,
1024 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1028 /// Inverse hyperbolic cosine function.
1031 /// let x = 1.0_f64;
1032 /// let f = x.cosh().acosh();
1034 /// let abs_difference = (f - x).abs();
1036 /// assert!(abs_difference < 1.0e-10);
1038 #[stable(feature = "rust1", since = "1.0.0")]
1040 pub fn acosh(self) -> f64 {
1042 x if x < 1.0 => NAN,
1043 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1047 /// Inverse hyperbolic tangent function.
1052 /// let e = f64::consts::E;
1053 /// let f = e.tanh().atanh();
1055 /// let abs_difference = (f - e).abs();
1057 /// assert!(abs_difference < 1.0e-10);
1059 #[stable(feature = "rust1", since = "1.0.0")]
1061 pub fn atanh(self) -> f64 {
1062 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1071 use num::FpCategory as Fp;
1075 test_num(10f64, 2f64);
1080 assert_eq!(NAN.min(2.0), 2.0);
1081 assert_eq!(2.0f64.min(NAN), 2.0);
1086 assert_eq!(NAN.max(2.0), 2.0);
1087 assert_eq!(2.0f64.max(NAN), 2.0);
1093 assert!(nan.is_nan());
1094 assert!(!nan.is_infinite());
1095 assert!(!nan.is_finite());
1096 assert!(!nan.is_normal());
1097 assert!(!nan.is_sign_positive());
1098 assert!(!nan.is_sign_negative());
1099 assert_eq!(Fp::Nan, nan.classify());
1103 fn test_infinity() {
1104 let inf: f64 = INFINITY;
1105 assert!(inf.is_infinite());
1106 assert!(!inf.is_finite());
1107 assert!(inf.is_sign_positive());
1108 assert!(!inf.is_sign_negative());
1109 assert!(!inf.is_nan());
1110 assert!(!inf.is_normal());
1111 assert_eq!(Fp::Infinite, inf.classify());
1115 fn test_neg_infinity() {
1116 let neg_inf: f64 = NEG_INFINITY;
1117 assert!(neg_inf.is_infinite());
1118 assert!(!neg_inf.is_finite());
1119 assert!(!neg_inf.is_sign_positive());
1120 assert!(neg_inf.is_sign_negative());
1121 assert!(!neg_inf.is_nan());
1122 assert!(!neg_inf.is_normal());
1123 assert_eq!(Fp::Infinite, neg_inf.classify());
1128 let zero: f64 = 0.0f64;
1129 assert_eq!(0.0, zero);
1130 assert!(!zero.is_infinite());
1131 assert!(zero.is_finite());
1132 assert!(zero.is_sign_positive());
1133 assert!(!zero.is_sign_negative());
1134 assert!(!zero.is_nan());
1135 assert!(!zero.is_normal());
1136 assert_eq!(Fp::Zero, zero.classify());
1140 fn test_neg_zero() {
1141 let neg_zero: f64 = -0.0;
1142 assert_eq!(0.0, neg_zero);
1143 assert!(!neg_zero.is_infinite());
1144 assert!(neg_zero.is_finite());
1145 assert!(!neg_zero.is_sign_positive());
1146 assert!(neg_zero.is_sign_negative());
1147 assert!(!neg_zero.is_nan());
1148 assert!(!neg_zero.is_normal());
1149 assert_eq!(Fp::Zero, neg_zero.classify());
1154 let one: f64 = 1.0f64;
1155 assert_eq!(1.0, one);
1156 assert!(!one.is_infinite());
1157 assert!(one.is_finite());
1158 assert!(one.is_sign_positive());
1159 assert!(!one.is_sign_negative());
1160 assert!(!one.is_nan());
1161 assert!(one.is_normal());
1162 assert_eq!(Fp::Normal, one.classify());
1168 let inf: f64 = INFINITY;
1169 let neg_inf: f64 = NEG_INFINITY;
1170 assert!(nan.is_nan());
1171 assert!(!0.0f64.is_nan());
1172 assert!(!5.3f64.is_nan());
1173 assert!(!(-10.732f64).is_nan());
1174 assert!(!inf.is_nan());
1175 assert!(!neg_inf.is_nan());
1179 fn test_is_infinite() {
1181 let inf: f64 = INFINITY;
1182 let neg_inf: f64 = NEG_INFINITY;
1183 assert!(!nan.is_infinite());
1184 assert!(inf.is_infinite());
1185 assert!(neg_inf.is_infinite());
1186 assert!(!0.0f64.is_infinite());
1187 assert!(!42.8f64.is_infinite());
1188 assert!(!(-109.2f64).is_infinite());
1192 fn test_is_finite() {
1194 let inf: f64 = INFINITY;
1195 let neg_inf: f64 = NEG_INFINITY;
1196 assert!(!nan.is_finite());
1197 assert!(!inf.is_finite());
1198 assert!(!neg_inf.is_finite());
1199 assert!(0.0f64.is_finite());
1200 assert!(42.8f64.is_finite());
1201 assert!((-109.2f64).is_finite());
1205 fn test_is_normal() {
1207 let inf: f64 = INFINITY;
1208 let neg_inf: f64 = NEG_INFINITY;
1209 let zero: f64 = 0.0f64;
1210 let neg_zero: f64 = -0.0;
1211 assert!(!nan.is_normal());
1212 assert!(!inf.is_normal());
1213 assert!(!neg_inf.is_normal());
1214 assert!(!zero.is_normal());
1215 assert!(!neg_zero.is_normal());
1216 assert!(1f64.is_normal());
1217 assert!(1e-307f64.is_normal());
1218 assert!(!1e-308f64.is_normal());
1222 fn test_classify() {
1224 let inf: f64 = INFINITY;
1225 let neg_inf: f64 = NEG_INFINITY;
1226 let zero: f64 = 0.0f64;
1227 let neg_zero: f64 = -0.0;
1228 assert_eq!(nan.classify(), Fp::Nan);
1229 assert_eq!(inf.classify(), Fp::Infinite);
1230 assert_eq!(neg_inf.classify(), Fp::Infinite);
1231 assert_eq!(zero.classify(), Fp::Zero);
1232 assert_eq!(neg_zero.classify(), Fp::Zero);
1233 assert_eq!(1e-307f64.classify(), Fp::Normal);
1234 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1238 fn test_integer_decode() {
1239 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
1240 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
1241 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
1242 assert_eq!(0f64.integer_decode(), (0, -1075, 1));
1243 assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
1244 assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
1245 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1246 assert_eq!(NAN.integer_decode(), (6755399441055744, 972, 1));
1251 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1252 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1253 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1254 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1255 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1256 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1257 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1258 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1259 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1260 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1265 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1266 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1267 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1268 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1269 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1270 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1271 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1272 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1273 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1274 assert_approx_eq!((-1.7f64).ceil(), -1.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);
1283 assert_approx_eq!(0.0f64.round(), 0.0f64);
1284 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1285 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1286 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1287 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1288 assert_approx_eq!((-1.7f64).round(), -2.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);
1297 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1298 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1299 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1300 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1301 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1302 assert_approx_eq!((-1.7f64).trunc(), -1.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);
1311 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1312 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1313 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1314 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1315 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1316 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1321 assert_eq!(INFINITY.abs(), INFINITY);
1322 assert_eq!(1f64.abs(), 1f64);
1323 assert_eq!(0f64.abs(), 0f64);
1324 assert_eq!((-0f64).abs(), 0f64);
1325 assert_eq!((-1f64).abs(), 1f64);
1326 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1327 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1328 assert!(NAN.abs().is_nan());
1333 assert_eq!(INFINITY.signum(), 1f64);
1334 assert_eq!(1f64.signum(), 1f64);
1335 assert_eq!(0f64.signum(), 1f64);
1336 assert_eq!((-0f64).signum(), -1f64);
1337 assert_eq!((-1f64).signum(), -1f64);
1338 assert_eq!(NEG_INFINITY.signum(), -1f64);
1339 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1340 assert!(NAN.signum().is_nan());
1344 fn test_is_sign_positive() {
1345 assert!(INFINITY.is_sign_positive());
1346 assert!(1f64.is_sign_positive());
1347 assert!(0f64.is_sign_positive());
1348 assert!(!(-0f64).is_sign_positive());
1349 assert!(!(-1f64).is_sign_positive());
1350 assert!(!NEG_INFINITY.is_sign_positive());
1351 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1352 assert!(!NAN.is_sign_positive());
1356 fn test_is_sign_negative() {
1357 assert!(!INFINITY.is_sign_negative());
1358 assert!(!1f64.is_sign_negative());
1359 assert!(!0f64.is_sign_negative());
1360 assert!((-0f64).is_sign_negative());
1361 assert!((-1f64).is_sign_negative());
1362 assert!(NEG_INFINITY.is_sign_negative());
1363 assert!((1f64/NEG_INFINITY).is_sign_negative());
1364 assert!(!NAN.is_sign_negative());
1370 let inf: f64 = INFINITY;
1371 let neg_inf: f64 = NEG_INFINITY;
1372 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1373 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1374 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1375 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1376 assert!(nan.mul_add(7.8, 9.0).is_nan());
1377 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1378 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1379 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1380 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1386 let inf: f64 = INFINITY;
1387 let neg_inf: f64 = NEG_INFINITY;
1388 assert_eq!(1.0f64.recip(), 1.0);
1389 assert_eq!(2.0f64.recip(), 0.5);
1390 assert_eq!((-0.4f64).recip(), -2.5);
1391 assert_eq!(0.0f64.recip(), inf);
1392 assert!(nan.recip().is_nan());
1393 assert_eq!(inf.recip(), 0.0);
1394 assert_eq!(neg_inf.recip(), 0.0);
1400 let inf: f64 = INFINITY;
1401 let neg_inf: f64 = NEG_INFINITY;
1402 assert_eq!(1.0f64.powi(1), 1.0);
1403 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1404 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1405 assert_eq!(8.3f64.powi(0), 1.0);
1406 assert!(nan.powi(2).is_nan());
1407 assert_eq!(inf.powi(3), inf);
1408 assert_eq!(neg_inf.powi(2), inf);
1414 let inf: f64 = INFINITY;
1415 let neg_inf: f64 = NEG_INFINITY;
1416 assert_eq!(1.0f64.powf(1.0), 1.0);
1417 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1418 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1419 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1420 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1421 assert_eq!(8.3f64.powf(0.0), 1.0);
1422 assert!(nan.powf(2.0).is_nan());
1423 assert_eq!(inf.powf(2.0), inf);
1424 assert_eq!(neg_inf.powf(3.0), neg_inf);
1428 fn test_sqrt_domain() {
1429 assert!(NAN.sqrt().is_nan());
1430 assert!(NEG_INFINITY.sqrt().is_nan());
1431 assert!((-1.0f64).sqrt().is_nan());
1432 assert_eq!((-0.0f64).sqrt(), -0.0);
1433 assert_eq!(0.0f64.sqrt(), 0.0);
1434 assert_eq!(1.0f64.sqrt(), 1.0);
1435 assert_eq!(INFINITY.sqrt(), INFINITY);
1440 assert_eq!(1.0, 0.0f64.exp());
1441 assert_approx_eq!(2.718282, 1.0f64.exp());
1442 assert_approx_eq!(148.413159, 5.0f64.exp());
1444 let inf: f64 = INFINITY;
1445 let neg_inf: f64 = NEG_INFINITY;
1447 assert_eq!(inf, inf.exp());
1448 assert_eq!(0.0, neg_inf.exp());
1449 assert!(nan.exp().is_nan());
1454 assert_eq!(32.0, 5.0f64.exp2());
1455 assert_eq!(1.0, 0.0f64.exp2());
1457 let inf: f64 = INFINITY;
1458 let neg_inf: f64 = NEG_INFINITY;
1460 assert_eq!(inf, inf.exp2());
1461 assert_eq!(0.0, neg_inf.exp2());
1462 assert!(nan.exp2().is_nan());
1468 let inf: f64 = INFINITY;
1469 let neg_inf: f64 = NEG_INFINITY;
1470 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1471 assert!(nan.ln().is_nan());
1472 assert_eq!(inf.ln(), inf);
1473 assert!(neg_inf.ln().is_nan());
1474 assert!((-2.3f64).ln().is_nan());
1475 assert_eq!((-0.0f64).ln(), neg_inf);
1476 assert_eq!(0.0f64.ln(), neg_inf);
1477 assert_approx_eq!(4.0f64.ln(), 1.386294);
1483 let inf: f64 = INFINITY;
1484 let neg_inf: f64 = NEG_INFINITY;
1485 assert_eq!(10.0f64.log(10.0), 1.0);
1486 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1487 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1488 assert!(1.0f64.log(1.0).is_nan());
1489 assert!(1.0f64.log(-13.9).is_nan());
1490 assert!(nan.log(2.3).is_nan());
1491 assert_eq!(inf.log(10.0), inf);
1492 assert!(neg_inf.log(8.8).is_nan());
1493 assert!((-2.3f64).log(0.1).is_nan());
1494 assert_eq!((-0.0f64).log(2.0), neg_inf);
1495 assert_eq!(0.0f64.log(7.0), neg_inf);
1501 let inf: f64 = INFINITY;
1502 let neg_inf: f64 = NEG_INFINITY;
1503 assert_approx_eq!(10.0f64.log2(), 3.321928);
1504 assert_approx_eq!(2.3f64.log2(), 1.201634);
1505 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1506 assert!(nan.log2().is_nan());
1507 assert_eq!(inf.log2(), inf);
1508 assert!(neg_inf.log2().is_nan());
1509 assert!((-2.3f64).log2().is_nan());
1510 assert_eq!((-0.0f64).log2(), neg_inf);
1511 assert_eq!(0.0f64.log2(), neg_inf);
1517 let inf: f64 = INFINITY;
1518 let neg_inf: f64 = NEG_INFINITY;
1519 assert_eq!(10.0f64.log10(), 1.0);
1520 assert_approx_eq!(2.3f64.log10(), 0.361728);
1521 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1522 assert_eq!(1.0f64.log10(), 0.0);
1523 assert!(nan.log10().is_nan());
1524 assert_eq!(inf.log10(), inf);
1525 assert!(neg_inf.log10().is_nan());
1526 assert!((-2.3f64).log10().is_nan());
1527 assert_eq!((-0.0f64).log10(), neg_inf);
1528 assert_eq!(0.0f64.log10(), neg_inf);
1532 fn test_to_degrees() {
1533 let pi: f64 = consts::PI;
1535 let inf: f64 = INFINITY;
1536 let neg_inf: f64 = NEG_INFINITY;
1537 assert_eq!(0.0f64.to_degrees(), 0.0);
1538 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1539 assert_eq!(pi.to_degrees(), 180.0);
1540 assert!(nan.to_degrees().is_nan());
1541 assert_eq!(inf.to_degrees(), inf);
1542 assert_eq!(neg_inf.to_degrees(), neg_inf);
1546 fn test_to_radians() {
1547 let pi: f64 = consts::PI;
1549 let inf: f64 = INFINITY;
1550 let neg_inf: f64 = NEG_INFINITY;
1551 assert_eq!(0.0f64.to_radians(), 0.0);
1552 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1553 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1554 assert_eq!(180.0f64.to_radians(), pi);
1555 assert!(nan.to_radians().is_nan());
1556 assert_eq!(inf.to_radians(), inf);
1557 assert_eq!(neg_inf.to_radians(), neg_inf);
1562 let f1 = 2.0f64.powi(-123);
1563 let f2 = 2.0f64.powi(-111);
1564 let f3 = 1.75 * 2.0f64.powi(-12);
1565 assert_eq!(f64::ldexp(1f64, -123), f1);
1566 assert_eq!(f64::ldexp(1f64, -111), f2);
1567 assert_eq!(f64::ldexp(1.75f64, -12), f3);
1569 assert_eq!(f64::ldexp(0f64, -123), 0f64);
1570 assert_eq!(f64::ldexp(-0f64, -123), -0f64);
1572 let inf: f64 = INFINITY;
1573 let neg_inf: f64 = NEG_INFINITY;
1575 assert_eq!(f64::ldexp(inf, -123), inf);
1576 assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
1577 assert!(f64::ldexp(nan, -123).is_nan());
1582 let f1 = 2.0f64.powi(-123);
1583 let f2 = 2.0f64.powi(-111);
1584 let f3 = 1.75 * 2.0f64.powi(-123);
1585 let (x1, exp1) = f1.frexp();
1586 let (x2, exp2) = f2.frexp();
1587 let (x3, exp3) = f3.frexp();
1588 assert_eq!((x1, exp1), (0.5f64, -122));
1589 assert_eq!((x2, exp2), (0.5f64, -110));
1590 assert_eq!((x3, exp3), (0.875f64, -122));
1591 assert_eq!(f64::ldexp(x1, exp1), f1);
1592 assert_eq!(f64::ldexp(x2, exp2), f2);
1593 assert_eq!(f64::ldexp(x3, exp3), f3);
1595 assert_eq!(0f64.frexp(), (0f64, 0));
1596 assert_eq!((-0f64).frexp(), (-0f64, 0));
1599 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1600 fn test_frexp_nowin() {
1601 let inf: f64 = INFINITY;
1602 let neg_inf: f64 = NEG_INFINITY;
1604 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1605 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1606 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1611 assert_eq!((-1f64).abs_sub(1f64), 0f64);
1612 assert_eq!(1f64.abs_sub(1f64), 0f64);
1613 assert_eq!(1f64.abs_sub(0f64), 1f64);
1614 assert_eq!(1f64.abs_sub(-1f64), 2f64);
1615 assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
1616 assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
1617 assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
1618 assert_eq!(0f64.abs_sub(INFINITY), 0f64);
1622 fn test_abs_sub_nowin() {
1623 assert!(NAN.abs_sub(-1f64).is_nan());
1624 assert!(1f64.abs_sub(NAN).is_nan());
1629 assert_eq!(0.0f64.asinh(), 0.0f64);
1630 assert_eq!((-0.0f64).asinh(), -0.0f64);
1632 let inf: f64 = INFINITY;
1633 let neg_inf: f64 = NEG_INFINITY;
1635 assert_eq!(inf.asinh(), inf);
1636 assert_eq!(neg_inf.asinh(), neg_inf);
1637 assert!(nan.asinh().is_nan());
1638 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1639 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1644 assert_eq!(1.0f64.acosh(), 0.0f64);
1645 assert!(0.999f64.acosh().is_nan());
1647 let inf: f64 = INFINITY;
1648 let neg_inf: f64 = NEG_INFINITY;
1650 assert_eq!(inf.acosh(), inf);
1651 assert!(neg_inf.acosh().is_nan());
1652 assert!(nan.acosh().is_nan());
1653 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1654 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1659 assert_eq!(0.0f64.atanh(), 0.0f64);
1660 assert_eq!((-0.0f64).atanh(), -0.0f64);
1662 let inf: f64 = INFINITY;
1663 let neg_inf: f64 = NEG_INFINITY;
1665 assert_eq!(1.0f64.atanh(), inf);
1666 assert_eq!((-1.0f64).atanh(), neg_inf);
1667 assert!(2f64.atanh().atanh().is_nan());
1668 assert!((-2f64).atanh().atanh().is_nan());
1669 assert!(inf.atanh().is_nan());
1670 assert!(neg_inf.atanh().is_nan());
1671 assert!(nan.atanh().is_nan());
1672 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1673 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1677 fn test_real_consts() {
1679 let pi: f64 = consts::PI;
1680 let frac_pi_2: f64 = consts::FRAC_PI_2;
1681 let frac_pi_3: f64 = consts::FRAC_PI_3;
1682 let frac_pi_4: f64 = consts::FRAC_PI_4;
1683 let frac_pi_6: f64 = consts::FRAC_PI_6;
1684 let frac_pi_8: f64 = consts::FRAC_PI_8;
1685 let frac_1_pi: f64 = consts::FRAC_1_PI;
1686 let frac_2_pi: f64 = consts::FRAC_2_PI;
1687 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1688 let sqrt2: f64 = consts::SQRT_2;
1689 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1690 let e: f64 = consts::E;
1691 let log2_e: f64 = consts::LOG2_E;
1692 let log10_e: f64 = consts::LOG10_E;
1693 let ln_2: f64 = consts::LN_2;
1694 let ln_10: f64 = consts::LN_10;
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());