1 // Copyright 2012-2014 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 //! Operations and constants for 64-bits floats (`f64` type)
13 #![allow(missing_doc)]
19 use from_str::FromStr;
21 use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
22 use num::{Zero, One, Bounded, strconv};
28 use libc::{c_double, c_int};
32 pub fn acos(n: c_double) -> c_double;
33 pub fn asin(n: c_double) -> c_double;
34 pub fn atan(n: c_double) -> c_double;
35 pub fn atan2(a: c_double, b: c_double) -> c_double;
36 pub fn cbrt(n: c_double) -> c_double;
37 pub fn cosh(n: c_double) -> c_double;
38 pub fn erf(n: c_double) -> c_double;
39 pub fn erfc(n: c_double) -> c_double;
40 pub fn expm1(n: c_double) -> c_double;
41 pub fn fdim(a: c_double, b: c_double) -> c_double;
42 pub fn fmax(a: c_double, b: c_double) -> c_double;
43 pub fn fmin(a: c_double, b: c_double) -> c_double;
44 pub fn fmod(a: c_double, b: c_double) -> c_double;
45 pub fn nextafter(x: c_double, y: c_double) -> c_double;
46 pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
47 pub fn hypot(x: c_double, y: c_double) -> c_double;
48 pub fn ldexp(x: c_double, n: c_int) -> c_double;
49 pub fn logb(n: c_double) -> c_double;
50 pub fn log1p(n: c_double) -> c_double;
51 pub fn ilogb(n: c_double) -> c_int;
52 pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
53 pub fn sinh(n: c_double) -> c_double;
54 pub fn tan(n: c_double) -> c_double;
55 pub fn tanh(n: c_double) -> c_double;
56 pub fn tgamma(n: c_double) -> c_double;
58 // These are commonly only available for doubles
60 pub fn j0(n: c_double) -> c_double;
61 pub fn j1(n: c_double) -> c_double;
62 pub fn jn(i: c_int, n: c_double) -> c_double;
64 pub fn y0(n: c_double) -> c_double;
65 pub fn y1(n: c_double) -> c_double;
66 pub fn yn(i: c_int, n: c_double) -> c_double;
69 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
71 #[link_name="__lgamma_r"]
72 pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
76 // FIXME(#11621): These constants should be deprecated once CTFE is implemented
77 // in favour of calling their respective functions in `Bounded` and `Float`.
79 pub static RADIX: uint = 2u;
81 pub static MANTISSA_DIGITS: uint = 53u;
82 pub static DIGITS: uint = 15u;
84 pub static EPSILON: f64 = 2.2204460492503131e-16_f64;
86 /// Minimum normalized f64 value
87 pub static MIN_VALUE: f64 = 2.2250738585072014e-308_f64;
89 pub static MAX_VALUE: f64 = 1.7976931348623157e+308_f64;
91 pub static MIN_EXP: int = -1021;
92 pub static MAX_EXP: int = 1024;
94 pub static MIN_10_EXP: int = -307;
95 pub static MAX_10_EXP: int = 308;
97 pub static NAN: f64 = 0.0_f64/0.0_f64;
99 pub static INFINITY: f64 = 1.0_f64/0.0_f64;
101 pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
103 /// Various useful constants.
105 // FIXME: replace with mathematical constants from cmath.
107 // FIXME(#11621): These constants should be deprecated once CTFE is
108 // implemented in favour of calling their respective functions in `Float`.
110 /// Archimedes' constant
111 pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
114 pub static PI_2: f64 = 6.28318530717958647692528676655900576_f64;
117 pub static FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
120 pub static FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
123 pub static FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
126 pub static FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
129 pub static FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
132 pub static FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
135 pub static FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
138 pub static FRAC_2_SQRTPI: f64 = 1.12837916709551257389615890312154517_f64;
141 pub static SQRT2: f64 = 1.41421356237309504880168872420969808_f64;
144 pub static FRAC_1_SQRT2: f64 = 0.707106781186547524400844362104849039_f64;
147 pub static E: f64 = 2.71828182845904523536028747135266250_f64;
150 pub static LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
153 pub static LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
156 pub static LN_2: f64 = 0.693147180559945309417232121458176568_f64;
159 pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64;
167 fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
173 fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
175 fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
177 fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
179 fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
182 impl Default for f64 {
184 fn default() -> f64 { 0.0 }
189 fn zero() -> f64 { 0.0 }
191 /// Returns true if the number is equal to either `0.0` or `-0.0`
193 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
198 fn one() -> f64 { 1.0 }
202 impl Add<f64,f64> for f64 {
204 fn add(&self, other: &f64) -> f64 { *self + *other }
207 impl Sub<f64,f64> for f64 {
209 fn sub(&self, other: &f64) -> f64 { *self - *other }
212 impl Mul<f64,f64> for f64 {
214 fn mul(&self, other: &f64) -> f64 { *self * *other }
217 impl Div<f64,f64> for f64 {
219 fn div(&self, other: &f64) -> f64 { *self / *other }
222 impl Rem<f64,f64> for f64 {
224 fn rem(&self, other: &f64) -> f64 {
225 unsafe { cmath::fmod(*self, *other) }
229 impl Neg<f64> for f64 {
231 fn neg(&self) -> f64 { -*self }
234 impl Signed for f64 {
235 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
237 fn abs(&self) -> f64 {
238 unsafe { intrinsics::fabsf64(*self) }
241 /// The positive difference of two numbers. Returns `0.0` if the number is less than or
242 /// equal to `other`, otherwise the difference between`self` and `other` is returned.
244 fn abs_sub(&self, other: &f64) -> f64 {
245 unsafe { cmath::fdim(*self, *other) }
250 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
251 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
252 /// - `NAN` if the number is NaN
254 fn signum(&self) -> f64 {
255 if self.is_nan() { NAN } else {
256 unsafe { intrinsics::copysignf64(1.0, *self) }
260 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
262 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
264 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
266 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
269 impl Bounded for f64 {
270 // NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
272 fn min_value() -> f64 { -MAX_VALUE }
275 fn max_value() -> f64 { MAX_VALUE }
278 impl Primitive for f64 {}
282 fn nan() -> f64 { NAN }
285 fn infinity() -> f64 { INFINITY }
288 fn neg_infinity() -> f64 { NEG_INFINITY }
291 fn neg_zero() -> f64 { -0.0 }
293 /// Returns `true` if the number is NaN
295 fn is_nan(self) -> bool { self != self }
297 /// Returns `true` if the number is infinite
299 fn is_infinite(self) -> bool {
300 self == Float::infinity() || self == Float::neg_infinity()
303 /// Returns `true` if the number is neither infinite or NaN
305 fn is_finite(self) -> bool {
306 !(self.is_nan() || self.is_infinite())
309 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
311 fn is_normal(self) -> bool {
312 self.classify() == FPNormal
315 /// Returns the floating point category of the number. If only one property
316 /// is going to be tested, it is generally faster to use the specific
317 /// predicate instead.
318 fn classify(self) -> FPCategory {
319 static EXP_MASK: u64 = 0x7ff0000000000000;
320 static MAN_MASK: u64 = 0x000fffffffffffff;
322 let bits: u64 = unsafe { cast::transmute(self) };
323 match (bits & MAN_MASK, bits & EXP_MASK) {
325 (_, 0) => FPSubnormal,
326 (0, EXP_MASK) => FPInfinite,
327 (_, EXP_MASK) => FPNaN,
333 fn mantissa_digits(_: Option<f64>) -> uint { 53 }
336 fn digits(_: Option<f64>) -> uint { 15 }
339 fn epsilon() -> f64 { 2.2204460492503131e-16 }
342 fn min_exp(_: Option<f64>) -> int { -1021 }
345 fn max_exp(_: Option<f64>) -> int { 1024 }
348 fn min_10_exp(_: Option<f64>) -> int { -307 }
351 fn max_10_exp(_: Option<f64>) -> int { 308 }
353 /// Constructs a floating point number by multiplying `x` by 2 raised to the
356 fn ldexp(x: f64, exp: int) -> f64 {
357 unsafe { cmath::ldexp(x, exp as c_int) }
360 /// Breaks the number into a normalized fraction and a base-2 exponent,
363 /// - `self = x * pow(2, exp)`
364 /// - `0.5 <= abs(x) < 1.0`
366 fn frexp(self) -> (f64, int) {
369 let x = cmath::frexp(self, &mut exp);
374 /// Returns the mantissa, exponent and sign as integers.
375 fn integer_decode(self) -> (u64, i16, i8) {
376 let bits: u64 = unsafe { cast::transmute(self) };
377 let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
378 let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
379 let mantissa = if exponent == 0 {
380 (bits & 0xfffffffffffff) << 1
382 (bits & 0xfffffffffffff) | 0x10000000000000
384 // Exponent bias + mantissa shift
385 exponent -= 1023 + 52;
386 (mantissa, exponent, sign)
389 /// Returns the next representable floating-point value in the direction of
392 fn next_after(self, other: f64) -> f64 {
393 unsafe { cmath::nextafter(self, other) }
396 /// Round half-way cases toward `NEG_INFINITY`
398 fn floor(self) -> f64 {
399 unsafe { intrinsics::floorf64(self) }
402 /// Round half-way cases toward `INFINITY`
404 fn ceil(self) -> f64 {
405 unsafe { intrinsics::ceilf64(self) }
408 /// Round half-way cases away from `0.0`
410 fn round(self) -> f64 {
411 unsafe { intrinsics::roundf64(self) }
414 /// The integer part of the number (rounds towards `0.0`)
416 fn trunc(self) -> f64 {
417 unsafe { intrinsics::truncf64(self) }
420 /// The fractional part of the number, satisfying:
424 /// assert!(x == x.trunc() + x.fract())
427 fn fract(self) -> f64 { self - self.trunc() }
430 fn max(self, other: f64) -> f64 {
431 unsafe { cmath::fmax(self, other) }
435 fn min(self, other: f64) -> f64 {
436 unsafe { cmath::fmin(self, other) }
439 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
440 /// error. This produces a more accurate result with better performance than
441 /// a separate multiplication operation followed by an add.
443 fn mul_add(self, a: f64, b: f64) -> f64 {
444 unsafe { intrinsics::fmaf64(self, a, b) }
447 /// The reciprocal (multiplicative inverse) of the number
449 fn recip(self) -> f64 { 1.0 / self }
452 fn powf(self, n: f64) -> f64 {
453 unsafe { intrinsics::powf64(self, n) }
457 fn powi(self, n: i32) -> f64 {
458 unsafe { intrinsics::powif64(self, n) }
463 fn sqrt2() -> f64 { consts::SQRT2 }
467 fn frac_1_sqrt2() -> f64 { consts::FRAC_1_SQRT2 }
470 fn sqrt(self) -> f64 {
471 unsafe { intrinsics::sqrtf64(self) }
475 fn rsqrt(self) -> f64 { self.sqrt().recip() }
478 fn cbrt(self) -> f64 {
479 unsafe { cmath::cbrt(self) }
483 fn hypot(self, other: f64) -> f64 {
484 unsafe { cmath::hypot(self, other) }
487 /// Archimedes' constant
489 fn pi() -> f64 { consts::PI }
493 fn two_pi() -> f64 { consts::PI_2 }
497 fn frac_pi_2() -> f64 { consts::FRAC_PI_2 }
501 fn frac_pi_3() -> f64 { consts::FRAC_PI_3 }
505 fn frac_pi_4() -> f64 { consts::FRAC_PI_4 }
509 fn frac_pi_6() -> f64 { consts::FRAC_PI_6 }
513 fn frac_pi_8() -> f64 { consts::FRAC_PI_8 }
517 fn frac_1_pi() -> f64 { consts::FRAC_1_PI }
521 fn frac_2_pi() -> f64 { consts::FRAC_2_PI }
525 fn frac_2_sqrtpi() -> f64 { consts::FRAC_2_SQRTPI }
528 fn sin(self) -> f64 {
529 unsafe { intrinsics::sinf64(self) }
533 fn cos(self) -> f64 {
534 unsafe { intrinsics::cosf64(self) }
538 fn tan(self) -> f64 {
539 unsafe { cmath::tan(self) }
543 fn asin(self) -> f64 {
544 unsafe { cmath::asin(self) }
548 fn acos(self) -> f64 {
549 unsafe { cmath::acos(self) }
553 fn atan(self) -> f64 {
554 unsafe { cmath::atan(self) }
558 fn atan2(self, other: f64) -> f64 {
559 unsafe { cmath::atan2(self, other) }
562 /// Simultaneously computes the sine and cosine of the number
564 fn sin_cos(self) -> (f64, f64) {
565 (self.sin(), self.cos())
570 fn e() -> f64 { consts::E }
574 fn log2_e() -> f64 { consts::LOG2_E }
578 fn log10_e() -> f64 { consts::LOG10_E }
582 fn ln_2() -> f64 { consts::LN_2 }
586 fn ln_10() -> f64 { consts::LN_10 }
588 /// Returns the exponential of the number
590 fn exp(self) -> f64 {
591 unsafe { intrinsics::expf64(self) }
594 /// Returns 2 raised to the power of the number
596 fn exp2(self) -> f64 {
597 unsafe { intrinsics::exp2f64(self) }
600 /// Returns the exponential of the number, minus `1`, in a way that is
601 /// accurate even if the number is close to zero
603 fn exp_m1(self) -> f64 {
604 unsafe { cmath::expm1(self) }
607 /// Returns the natural logarithm of the number
610 unsafe { intrinsics::logf64(self) }
613 /// Returns the logarithm of the number with respect to an arbitrary base
615 fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
617 /// Returns the base 2 logarithm of the number
619 fn log2(self) -> f64 {
620 unsafe { intrinsics::log2f64(self) }
623 /// Returns the base 10 logarithm of the number
625 fn log10(self) -> f64 {
626 unsafe { intrinsics::log10f64(self) }
629 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
630 /// accurately than if the operations were performed separately
632 fn ln_1p(self) -> f64 {
633 unsafe { cmath::log1p(self) }
637 fn sinh(self) -> f64 {
638 unsafe { cmath::sinh(self) }
642 fn cosh(self) -> f64 {
643 unsafe { cmath::cosh(self) }
647 fn tanh(self) -> f64 {
648 unsafe { cmath::tanh(self) }
651 /// Inverse hyperbolic sine
655 /// - on success, the inverse hyperbolic sine of `self` will be returned
656 /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
657 /// - `NAN` if `self` is `NAN`
659 fn asinh(self) -> f64 {
661 NEG_INFINITY => NEG_INFINITY,
662 x => (x + ((x * x) + 1.0).sqrt()).ln(),
666 /// Inverse hyperbolic cosine
670 /// - on success, the inverse hyperbolic cosine of `self` will be returned
671 /// - `INFINITY` if `self` is `INFINITY`
672 /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
674 fn acosh(self) -> f64 {
676 x if x < 1.0 => Float::nan(),
677 x => (x + ((x * x) - 1.0).sqrt()).ln(),
681 /// Inverse hyperbolic tangent
685 /// - on success, the inverse hyperbolic tangent of `self` will be returned
686 /// - `self` if `self` is `0.0` or `-0.0`
687 /// - `INFINITY` if `self` is `1.0`
688 /// - `NEG_INFINITY` if `self` is `-1.0`
689 /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
690 /// (including `INFINITY` and `NEG_INFINITY`)
692 fn atanh(self) -> f64 {
693 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
696 /// Converts to degrees, assuming the number is in radians
698 fn to_degrees(self) -> f64 { self * (180.0f64 / Float::pi()) }
700 /// Converts to radians, assuming the number is in degrees
702 fn to_radians(self) -> f64 {
703 let value: f64 = Float::pi();
704 self * (value / 180.0)
709 // Section: String Conversions
712 /// Converts a float to a string
716 /// * num - The float value
718 pub fn to_str(num: f64) -> ~str {
719 let (r, _) = strconv::float_to_str_common(
720 num, 10u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
724 /// Converts a float to a string in hexadecimal format
728 /// * num - The float value
730 pub fn to_str_hex(num: f64) -> ~str {
731 let (r, _) = strconv::float_to_str_common(
732 num, 16u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
736 /// Converts a float to a string in a given radix, and a flag indicating
737 /// whether it's a special value
741 /// * num - The float value
742 /// * radix - The base to use
744 pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
745 strconv::float_to_str_common(num, rdx, true,
746 strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false)
749 /// Converts a float to a string with exactly the number of
750 /// provided significant digits
754 /// * num - The float value
755 /// * digits - The number of significant digits
757 pub fn to_str_exact(num: f64, dig: uint) -> ~str {
758 let (r, _) = strconv::float_to_str_common(
759 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpNone, false);
763 /// Converts a float to a string with a maximum number of
764 /// significant digits
768 /// * num - The float value
769 /// * digits - The number of significant digits
771 pub fn to_str_digits(num: f64, dig: uint) -> ~str {
772 let (r, _) = strconv::float_to_str_common(
773 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpNone, false);
777 /// Converts a float to a string using the exponential notation with exactly the number of
778 /// provided digits after the decimal point in the significand
782 /// * num - The float value
783 /// * digits - The number of digits after the decimal point
784 /// * upper - Use `E` instead of `e` for the exponent sign
786 pub fn to_str_exp_exact(num: f64, dig: uint, upper: bool) -> ~str {
787 let (r, _) = strconv::float_to_str_common(
788 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
792 /// Converts a float to a string using the exponential notation with the maximum number of
793 /// digits after the decimal point in the significand
797 /// * num - The float value
798 /// * digits - The number of digits after the decimal point
799 /// * upper - Use `E` instead of `e` for the exponent sign
801 pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> ~str {
802 let (r, _) = strconv::float_to_str_common(
803 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
807 impl num::ToStrRadix for f64 {
808 /// Converts a float to a string in a given radix
812 /// * num - The float value
813 /// * radix - The base to use
817 /// Fails if called on a special value like `inf`, `-inf` or `NAN` due to
818 /// possible misinterpretation of the result at higher bases. If those values
819 /// are expected, use `to_str_radix_special()` instead.
821 fn to_str_radix(&self, rdx: uint) -> ~str {
822 let (r, special) = strconv::float_to_str_common(
823 *self, rdx, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
824 if special { fail!("number has a special value, \
825 try to_str_radix_special() if those are expected") }
830 /// Convert a string in base 16 to a float.
831 /// Accepts an optional binary exponent.
833 /// This function accepts strings such as
836 /// * '+a4.fe', equivalent to 'a4.fe'
838 /// * '2b.aP128', or equivalently, '2b.ap128'
840 /// * '.' (understood as 0)
842 /// * '.c', or, equivalently, '0.c'
843 /// * '+inf', 'inf', '-inf', 'NaN'
845 /// Leading and trailing whitespace represent an error.
853 /// `None` if the string did not represent a valid number. Otherwise,
854 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
856 pub fn from_str_hex(num: &str) -> Option<f64> {
857 strconv::from_str_common(num, 16u, true, true, true,
858 strconv::ExpBin, false, false)
861 impl FromStr for f64 {
862 /// Convert a string in base 10 to a float.
863 /// Accepts an optional decimal exponent.
865 /// This function accepts strings such as
868 /// * '+3.14', equivalent to '3.14'
870 /// * '2.5E10', or equivalently, '2.5e10'
872 /// * '.' (understood as 0)
874 /// * '.5', or, equivalently, '0.5'
875 /// * '+inf', 'inf', '-inf', 'NaN'
877 /// Leading and trailing whitespace represent an error.
885 /// `none` if the string did not represent a valid number. Otherwise,
886 /// `Some(n)` where `n` is the floating-point number represented by `num`.
888 fn from_str(val: &str) -> Option<f64> {
889 strconv::from_str_common(val, 10u, true, true, true,
890 strconv::ExpDec, false, false)
894 impl num::FromStrRadix for f64 {
895 /// Convert a string in a given base to a float.
897 /// Due to possible conflicts, this function does **not** accept
898 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
899 /// does it recognize exponents of any kind.
901 /// Leading and trailing whitespace represent an error.
906 /// * radix - The base to use. Must lie in the range [2 .. 36]
910 /// `None` if the string did not represent a valid number. Otherwise,
911 /// `Some(n)` where `n` is the floating-point number represented by `num`.
913 fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
914 strconv::from_str_common(val, rdx, true, true, false,
915 strconv::ExpNone, false, false)
927 assert_eq!(NAN.min(2.0), 2.0);
928 assert_eq!(2.0f64.min(NAN), 2.0);
933 assert_eq!(NAN.max(2.0), 2.0);
934 assert_eq!(2.0f64.max(NAN), 2.0);
939 num::test_num(10f64, 2f64);
944 assert_approx_eq!(1.0f64.floor(), 1.0f64);
945 assert_approx_eq!(1.3f64.floor(), 1.0f64);
946 assert_approx_eq!(1.5f64.floor(), 1.0f64);
947 assert_approx_eq!(1.7f64.floor(), 1.0f64);
948 assert_approx_eq!(0.0f64.floor(), 0.0f64);
949 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
950 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
951 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
952 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
953 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
958 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
959 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
960 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
961 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
962 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
963 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
964 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
965 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
966 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
967 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
972 assert_approx_eq!(1.0f64.round(), 1.0f64);
973 assert_approx_eq!(1.3f64.round(), 1.0f64);
974 assert_approx_eq!(1.5f64.round(), 2.0f64);
975 assert_approx_eq!(1.7f64.round(), 2.0f64);
976 assert_approx_eq!(0.0f64.round(), 0.0f64);
977 assert_approx_eq!((-0.0f64).round(), -0.0f64);
978 assert_approx_eq!((-1.0f64).round(), -1.0f64);
979 assert_approx_eq!((-1.3f64).round(), -1.0f64);
980 assert_approx_eq!((-1.5f64).round(), -2.0f64);
981 assert_approx_eq!((-1.7f64).round(), -2.0f64);
986 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
987 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
988 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
989 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
990 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
991 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
992 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
993 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
994 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
995 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1000 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1001 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1002 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1003 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1004 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1005 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1006 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1007 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1008 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1009 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1014 assert_eq!(0.0f64.asinh(), 0.0f64);
1015 assert_eq!((-0.0f64).asinh(), -0.0f64);
1017 let inf: f64 = Float::infinity();
1018 let neg_inf: f64 = Float::neg_infinity();
1019 let nan: f64 = Float::nan();
1020 assert_eq!(inf.asinh(), inf);
1021 assert_eq!(neg_inf.asinh(), neg_inf);
1022 assert!(nan.asinh().is_nan());
1023 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1024 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1029 assert_eq!(1.0f64.acosh(), 0.0f64);
1030 assert!(0.999f64.acosh().is_nan());
1032 let inf: f64 = Float::infinity();
1033 let neg_inf: f64 = Float::neg_infinity();
1034 let nan: f64 = Float::nan();
1035 assert_eq!(inf.acosh(), inf);
1036 assert!(neg_inf.acosh().is_nan());
1037 assert!(nan.acosh().is_nan());
1038 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1039 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1044 assert_eq!(0.0f64.atanh(), 0.0f64);
1045 assert_eq!((-0.0f64).atanh(), -0.0f64);
1047 let inf: f64 = Float::infinity();
1048 let neg_inf: f64 = Float::neg_infinity();
1049 let nan: f64 = Float::nan();
1050 assert_eq!(1.0f64.atanh(), inf);
1051 assert_eq!((-1.0f64).atanh(), neg_inf);
1052 assert!(2f64.atanh().atanh().is_nan());
1053 assert!((-2f64).atanh().atanh().is_nan());
1054 assert!(inf.atanh().is_nan());
1055 assert!(neg_inf.atanh().is_nan());
1056 assert!(nan.atanh().is_nan());
1057 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1058 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1062 fn test_real_consts() {
1063 let pi: f64 = Float::pi();
1064 let two_pi: f64 = Float::two_pi();
1065 let frac_pi_2: f64 = Float::frac_pi_2();
1066 let frac_pi_3: f64 = Float::frac_pi_3();
1067 let frac_pi_4: f64 = Float::frac_pi_4();
1068 let frac_pi_6: f64 = Float::frac_pi_6();
1069 let frac_pi_8: f64 = Float::frac_pi_8();
1070 let frac_1_pi: f64 = Float::frac_1_pi();
1071 let frac_2_pi: f64 = Float::frac_2_pi();
1072 let frac_2_sqrtpi: f64 = Float::frac_2_sqrtpi();
1073 let sqrt2: f64 = Float::sqrt2();
1074 let frac_1_sqrt2: f64 = Float::frac_1_sqrt2();
1075 let e: f64 = Float::e();
1076 let log2_e: f64 = Float::log2_e();
1077 let log10_e: f64 = Float::log10_e();
1078 let ln_2: f64 = Float::ln_2();
1079 let ln_10: f64 = Float::ln_10();
1081 assert_approx_eq!(two_pi, 2.0 * pi);
1082 assert_approx_eq!(frac_pi_2, pi / 2f64);
1083 assert_approx_eq!(frac_pi_3, pi / 3f64);
1084 assert_approx_eq!(frac_pi_4, pi / 4f64);
1085 assert_approx_eq!(frac_pi_6, pi / 6f64);
1086 assert_approx_eq!(frac_pi_8, pi / 8f64);
1087 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1088 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1089 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1090 assert_approx_eq!(sqrt2, 2f64.sqrt());
1091 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1092 assert_approx_eq!(log2_e, e.log2());
1093 assert_approx_eq!(log10_e, e.log10());
1094 assert_approx_eq!(ln_2, 2f64.ln());
1095 assert_approx_eq!(ln_10, 10f64.ln());
1100 assert_eq!(INFINITY.abs(), INFINITY);
1101 assert_eq!(1f64.abs(), 1f64);
1102 assert_eq!(0f64.abs(), 0f64);
1103 assert_eq!((-0f64).abs(), 0f64);
1104 assert_eq!((-1f64).abs(), 1f64);
1105 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1106 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1107 assert!(NAN.abs().is_nan());
1112 assert_eq!((-1f64).abs_sub(&1f64), 0f64);
1113 assert_eq!(1f64.abs_sub(&1f64), 0f64);
1114 assert_eq!(1f64.abs_sub(&0f64), 1f64);
1115 assert_eq!(1f64.abs_sub(&-1f64), 2f64);
1116 assert_eq!(NEG_INFINITY.abs_sub(&0f64), 0f64);
1117 assert_eq!(INFINITY.abs_sub(&1f64), INFINITY);
1118 assert_eq!(0f64.abs_sub(&NEG_INFINITY), INFINITY);
1119 assert_eq!(0f64.abs_sub(&INFINITY), 0f64);
1123 fn test_abs_sub_nowin() {
1124 assert!(NAN.abs_sub(&-1f64).is_nan());
1125 assert!(1f64.abs_sub(&NAN).is_nan());
1130 assert_eq!(INFINITY.signum(), 1f64);
1131 assert_eq!(1f64.signum(), 1f64);
1132 assert_eq!(0f64.signum(), 1f64);
1133 assert_eq!((-0f64).signum(), -1f64);
1134 assert_eq!((-1f64).signum(), -1f64);
1135 assert_eq!(NEG_INFINITY.signum(), -1f64);
1136 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1137 assert!(NAN.signum().is_nan());
1141 fn test_is_positive() {
1142 assert!(INFINITY.is_positive());
1143 assert!(1f64.is_positive());
1144 assert!(0f64.is_positive());
1145 assert!(!(-0f64).is_positive());
1146 assert!(!(-1f64).is_positive());
1147 assert!(!NEG_INFINITY.is_positive());
1148 assert!(!(1f64/NEG_INFINITY).is_positive());
1149 assert!(!NAN.is_positive());
1153 fn test_is_negative() {
1154 assert!(!INFINITY.is_negative());
1155 assert!(!1f64.is_negative());
1156 assert!(!0f64.is_negative());
1157 assert!((-0f64).is_negative());
1158 assert!((-1f64).is_negative());
1159 assert!(NEG_INFINITY.is_negative());
1160 assert!((1f64/NEG_INFINITY).is_negative());
1161 assert!(!NAN.is_negative());
1165 fn test_is_normal() {
1166 let nan: f64 = Float::nan();
1167 let inf: f64 = Float::infinity();
1168 let neg_inf: f64 = Float::neg_infinity();
1169 let zero: f64 = Zero::zero();
1170 let neg_zero: f64 = Float::neg_zero();
1171 assert!(!nan.is_normal());
1172 assert!(!inf.is_normal());
1173 assert!(!neg_inf.is_normal());
1174 assert!(!zero.is_normal());
1175 assert!(!neg_zero.is_normal());
1176 assert!(1f64.is_normal());
1177 assert!(1e-307f64.is_normal());
1178 assert!(!1e-308f64.is_normal());
1182 fn test_classify() {
1183 let nan: f64 = Float::nan();
1184 let inf: f64 = Float::infinity();
1185 let neg_inf: f64 = Float::neg_infinity();
1186 let zero: f64 = Zero::zero();
1187 let neg_zero: f64 = Float::neg_zero();
1188 assert_eq!(nan.classify(), FPNaN);
1189 assert_eq!(inf.classify(), FPInfinite);
1190 assert_eq!(neg_inf.classify(), FPInfinite);
1191 assert_eq!(zero.classify(), FPZero);
1192 assert_eq!(neg_zero.classify(), FPZero);
1193 assert_eq!(1e-307f64.classify(), FPNormal);
1194 assert_eq!(1e-308f64.classify(), FPSubnormal);
1199 // We have to use from_str until base-2 exponents
1200 // are supported in floating-point literals
1201 let f1: f64 = from_str_hex("1p-123").unwrap();
1202 let f2: f64 = from_str_hex("1p-111").unwrap();
1203 assert_eq!(Float::ldexp(1f64, -123), f1);
1204 assert_eq!(Float::ldexp(1f64, -111), f2);
1206 assert_eq!(Float::ldexp(0f64, -123), 0f64);
1207 assert_eq!(Float::ldexp(-0f64, -123), -0f64);
1209 let inf: f64 = Float::infinity();
1210 let neg_inf: f64 = Float::neg_infinity();
1211 let nan: f64 = Float::nan();
1212 assert_eq!(Float::ldexp(inf, -123), inf);
1213 assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
1214 assert!(Float::ldexp(nan, -123).is_nan());
1219 // We have to use from_str until base-2 exponents
1220 // are supported in floating-point literals
1221 let f1: f64 = from_str_hex("1p-123").unwrap();
1222 let f2: f64 = from_str_hex("1p-111").unwrap();
1223 let (x1, exp1) = f1.frexp();
1224 let (x2, exp2) = f2.frexp();
1225 assert_eq!((x1, exp1), (0.5f64, -122));
1226 assert_eq!((x2, exp2), (0.5f64, -110));
1227 assert_eq!(Float::ldexp(x1, exp1), f1);
1228 assert_eq!(Float::ldexp(x2, exp2), f2);
1230 assert_eq!(0f64.frexp(), (0f64, 0));
1231 assert_eq!((-0f64).frexp(), (-0f64, 0));
1234 #[test] #[ignore(cfg(windows))] // FIXME #8755
1235 fn test_frexp_nowin() {
1236 let inf: f64 = Float::infinity();
1237 let neg_inf: f64 = Float::neg_infinity();
1238 let nan: f64 = Float::nan();
1239 assert_eq!(match inf.frexp() { (x, _) => x }, inf)
1240 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
1241 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1245 fn test_integer_decode() {
1246 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906u64, -51i16, 1i8));
1247 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931u64, -39i16, -1i8));
1248 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496u64, 48i16, 1i8));
1249 assert_eq!(0f64.integer_decode(), (0u64, -1075i16, 1i8));
1250 assert_eq!((-0f64).integer_decode(), (0u64, -1075i16, -1i8));
1251 assert_eq!(INFINITY.integer_decode(), (4503599627370496u64, 972i16, 1i8));
1252 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1253 assert_eq!(NAN.integer_decode(), (6755399441055744u64, 972i16, 1i8));