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 (#1433): obtain these in a different way
78 // FIXME(#11621): These constants should be deprecated once CTFE is implemented
79 // in favour of calling their respective functions in `Bounded` and `Float`.
81 pub static RADIX: uint = 2u;
83 pub static MANTISSA_DIGITS: uint = 53u;
84 pub static DIGITS: uint = 15u;
86 pub static EPSILON: f64 = 2.2204460492503131e-16_f64;
88 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 // FIXME (#1999): add is_normal, is_subnormal, and fpclassify
105 /// Various useful constants.
107 // FIXME (requires Issue #1433 to fix): replace with mathematical
108 // constants from cmath.
110 // FIXME(#11621): These constants should be deprecated once CTFE is
111 // implemented in favour of calling their respective functions in `Float`.
113 /// Archimedes' constant
114 pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
117 pub static FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
120 pub static FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
123 pub static FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
126 pub static FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
129 pub static FRAC_2_SQRTPI: f64 = 1.12837916709551257389615890312154517_f64;
132 pub static SQRT2: f64 = 1.41421356237309504880168872420969808_f64;
135 pub static FRAC_1_SQRT2: f64 = 0.707106781186547524400844362104849039_f64;
138 pub static E: f64 = 2.71828182845904523536028747135266250_f64;
141 pub static LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
144 pub static LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
147 pub static LN_2: f64 = 0.693147180559945309417232121458176568_f64;
150 pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64;
158 fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
164 fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
166 fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
168 fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
170 fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
173 impl Default for f64 {
175 fn default() -> f64 { 0.0 }
180 fn zero() -> f64 { 0.0 }
182 /// Returns true if the number is equal to either `0.0` or `-0.0`
184 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
189 fn one() -> f64 { 1.0 }
193 impl Add<f64,f64> for f64 {
195 fn add(&self, other: &f64) -> f64 { *self + *other }
198 impl Sub<f64,f64> for f64 {
200 fn sub(&self, other: &f64) -> f64 { *self - *other }
203 impl Mul<f64,f64> for f64 {
205 fn mul(&self, other: &f64) -> f64 { *self * *other }
208 impl Div<f64,f64> for f64 {
210 fn div(&self, other: &f64) -> f64 { *self / *other }
213 impl Rem<f64,f64> for f64 {
215 fn rem(&self, other: &f64) -> f64 {
216 unsafe { cmath::fmod(*self, *other) }
220 impl Neg<f64> for f64 {
222 fn neg(&self) -> f64 { -*self }
225 impl Signed for f64 {
226 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
228 fn abs(&self) -> f64 {
229 unsafe { intrinsics::fabsf64(*self) }
232 /// The positive difference of two numbers. Returns `0.0` if the number is less than or
233 /// equal to `other`, otherwise the difference between`self` and `other` is returned.
235 fn abs_sub(&self, other: &f64) -> f64 {
236 unsafe { cmath::fdim(*self, *other) }
241 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
242 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
243 /// - `NAN` if the number is NaN
245 fn signum(&self) -> f64 {
246 if self.is_nan() { NAN } else {
247 unsafe { intrinsics::copysignf64(1.0, *self) }
251 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
253 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
255 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
257 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
260 impl Bounded for f64 {
262 fn min_value() -> f64 { 2.2250738585072014e-308 }
265 fn max_value() -> f64 { 1.7976931348623157e+308 }
268 impl Primitive for f64 {}
272 fn nan() -> f64 { 0.0 / 0.0 }
275 fn infinity() -> f64 { 1.0 / 0.0 }
278 fn neg_infinity() -> f64 { -1.0 / 0.0 }
281 fn neg_zero() -> f64 { -0.0 }
283 /// Returns `true` if the number is NaN
285 fn is_nan(self) -> bool { self != self }
287 /// Returns `true` if the number is infinite
289 fn is_infinite(self) -> bool {
290 self == Float::infinity() || self == Float::neg_infinity()
293 /// Returns `true` if the number is neither infinite or NaN
295 fn is_finite(self) -> bool {
296 !(self.is_nan() || self.is_infinite())
299 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
301 fn is_normal(self) -> bool {
302 self.classify() == FPNormal
305 /// Returns the floating point category of the number. If only one property
306 /// is going to be tested, it is generally faster to use the specific
307 /// predicate instead.
308 fn classify(self) -> FPCategory {
309 static EXP_MASK: u64 = 0x7ff0000000000000;
310 static MAN_MASK: u64 = 0x000fffffffffffff;
312 let bits: u64 = unsafe { cast::transmute(self) };
313 match (bits & MAN_MASK, bits & EXP_MASK) {
315 (_, 0) => FPSubnormal,
316 (0, EXP_MASK) => FPInfinite,
317 (_, EXP_MASK) => FPNaN,
323 fn mantissa_digits(_: Option<f64>) -> uint { 53 }
326 fn digits(_: Option<f64>) -> uint { 15 }
329 fn epsilon() -> f64 { 2.2204460492503131e-16 }
332 fn min_exp(_: Option<f64>) -> int { -1021 }
335 fn max_exp(_: Option<f64>) -> int { 1024 }
338 fn min_10_exp(_: Option<f64>) -> int { -307 }
341 fn max_10_exp(_: Option<f64>) -> int { 308 }
343 /// Constructs a floating point number by multiplying `x` by 2 raised to the
346 fn ldexp(x: f64, exp: int) -> f64 {
347 unsafe { cmath::ldexp(x, exp as c_int) }
350 /// Breaks the number into a normalized fraction and a base-2 exponent,
353 /// - `self = x * pow(2, exp)`
354 /// - `0.5 <= abs(x) < 1.0`
356 fn frexp(self) -> (f64, int) {
359 let x = cmath::frexp(self, &mut exp);
364 /// Returns the mantissa, exponent and sign as integers.
365 fn integer_decode(self) -> (u64, i16, i8) {
366 let bits: u64 = unsafe { cast::transmute(self) };
367 let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
368 let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
369 let mantissa = if exponent == 0 {
370 (bits & 0xfffffffffffff) << 1
372 (bits & 0xfffffffffffff) | 0x10000000000000
374 // Exponent bias + mantissa shift
375 exponent -= 1023 + 52;
376 (mantissa, exponent, sign)
379 /// Returns the next representable floating-point value in the direction of
382 fn next_after(self, other: f64) -> f64 {
383 unsafe { cmath::nextafter(self, other) }
386 /// Round half-way cases toward `NEG_INFINITY`
388 fn floor(self) -> f64 {
389 unsafe { intrinsics::floorf64(self) }
392 /// Round half-way cases toward `INFINITY`
394 fn ceil(self) -> f64 {
395 unsafe { intrinsics::ceilf64(self) }
398 /// Round half-way cases away from `0.0`
400 fn round(self) -> f64 {
401 unsafe { intrinsics::roundf64(self) }
404 /// The integer part of the number (rounds towards `0.0`)
406 fn trunc(self) -> f64 {
407 unsafe { intrinsics::truncf64(self) }
410 /// The fractional part of the number, satisfying:
414 /// assert!(x == x.trunc() + x.fract())
417 fn fract(self) -> f64 { self - self.trunc() }
420 fn max(self, other: f64) -> f64 {
421 unsafe { cmath::fmax(self, other) }
425 fn min(self, other: f64) -> f64 {
426 unsafe { cmath::fmin(self, other) }
429 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
430 /// error. This produces a more accurate result with better performance than
431 /// a separate multiplication operation followed by an add.
433 fn mul_add(self, a: f64, b: f64) -> f64 {
434 unsafe { intrinsics::fmaf64(self, a, b) }
437 /// The reciprocal (multiplicative inverse) of the number
439 fn recip(self) -> f64 { 1.0 / self }
442 fn powf(self, n: f64) -> f64 {
443 unsafe { intrinsics::powf64(self, n) }
447 fn powi(self, n: i32) -> f64 {
448 unsafe { intrinsics::powif64(self, n) }
453 fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
457 fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
460 fn sqrt(self) -> f64 {
461 unsafe { intrinsics::sqrtf64(self) }
465 fn rsqrt(self) -> f64 { self.sqrt().recip() }
468 fn cbrt(self) -> f64 {
469 unsafe { cmath::cbrt(self) }
473 fn hypot(self, other: f64) -> f64 {
474 unsafe { cmath::hypot(self, other) }
477 /// Archimedes' constant
479 fn pi() -> f64 { 3.14159265358979323846264338327950288 }
483 fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
487 fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
491 fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
495 fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
499 fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
503 fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
507 fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
511 fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
515 fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
518 fn sin(self) -> f64 {
519 unsafe { intrinsics::sinf64(self) }
523 fn cos(self) -> f64 {
524 unsafe { intrinsics::cosf64(self) }
528 fn tan(self) -> f64 {
529 unsafe { cmath::tan(self) }
533 fn asin(self) -> f64 {
534 unsafe { cmath::asin(self) }
538 fn acos(self) -> f64 {
539 unsafe { cmath::acos(self) }
543 fn atan(self) -> f64 {
544 unsafe { cmath::atan(self) }
548 fn atan2(self, other: f64) -> f64 {
549 unsafe { cmath::atan2(self, other) }
552 /// Simultaneously computes the sine and cosine of the number
554 fn sin_cos(self) -> (f64, f64) {
555 (self.sin(), self.cos())
560 fn e() -> f64 { 2.71828182845904523536028747135266250 }
564 fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
568 fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
572 fn ln_2() -> f64 { 0.693147180559945309417232121458176568 }
576 fn ln_10() -> f64 { 2.30258509299404568401799145468436421 }
578 /// Returns the exponential of the number
580 fn exp(self) -> f64 {
581 unsafe { intrinsics::expf64(self) }
584 /// Returns 2 raised to the power of the number
586 fn exp2(self) -> f64 {
587 unsafe { intrinsics::exp2f64(self) }
590 /// Returns the exponential of the number, minus `1`, in a way that is
591 /// accurate even if the number is close to zero
593 fn exp_m1(self) -> f64 {
594 unsafe { cmath::expm1(self) }
597 /// Returns the natural logarithm of the number
600 unsafe { intrinsics::logf64(self) }
603 /// Returns the logarithm of the number with respect to an arbitrary base
605 fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
607 /// Returns the base 2 logarithm of the number
609 fn log2(self) -> f64 {
610 unsafe { intrinsics::log2f64(self) }
613 /// Returns the base 10 logarithm of the number
615 fn log10(self) -> f64 {
616 unsafe { intrinsics::log10f64(self) }
619 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
620 /// accurately than if the operations were performed separately
622 fn ln_1p(self) -> f64 {
623 unsafe { cmath::log1p(self) }
627 fn sinh(self) -> f64 {
628 unsafe { cmath::sinh(self) }
632 fn cosh(self) -> f64 {
633 unsafe { cmath::cosh(self) }
637 fn tanh(self) -> f64 {
638 unsafe { cmath::tanh(self) }
641 /// Inverse hyperbolic sine
645 /// - on success, the inverse hyperbolic sine of `self` will be returned
646 /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
647 /// - `NAN` if `self` is `NAN`
649 fn asinh(self) -> f64 {
651 NEG_INFINITY => NEG_INFINITY,
652 x => (x + ((x * x) + 1.0).sqrt()).ln(),
656 /// Inverse hyperbolic cosine
660 /// - on success, the inverse hyperbolic cosine of `self` will be returned
661 /// - `INFINITY` if `self` is `INFINITY`
662 /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
664 fn acosh(self) -> f64 {
666 x if x < 1.0 => Float::nan(),
667 x => (x + ((x * x) - 1.0).sqrt()).ln(),
671 /// Inverse hyperbolic tangent
675 /// - on success, the inverse hyperbolic tangent of `self` will be returned
676 /// - `self` if `self` is `0.0` or `-0.0`
677 /// - `INFINITY` if `self` is `1.0`
678 /// - `NEG_INFINITY` if `self` is `-1.0`
679 /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
680 /// (including `INFINITY` and `NEG_INFINITY`)
682 fn atanh(self) -> f64 {
683 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
686 /// Converts to degrees, assuming the number is in radians
688 fn to_degrees(self) -> f64 { self * (180.0f64 / Float::pi()) }
690 /// Converts to radians, assuming the number is in degrees
692 fn to_radians(self) -> f64 {
693 let value: f64 = Float::pi();
694 self * (value / 180.0)
699 // Section: String Conversions
702 /// Converts a float to a string
706 /// * num - The float value
708 pub fn to_str(num: f64) -> ~str {
709 let (r, _) = strconv::float_to_str_common(
710 num, 10u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
714 /// Converts a float to a string in hexadecimal format
718 /// * num - The float value
720 pub fn to_str_hex(num: f64) -> ~str {
721 let (r, _) = strconv::float_to_str_common(
722 num, 16u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
726 /// Converts a float to a string in a given radix, and a flag indicating
727 /// whether it's a special value
731 /// * num - The float value
732 /// * radix - The base to use
734 pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
735 strconv::float_to_str_common(num, rdx, true,
736 strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false)
739 /// Converts a float to a string with exactly the number of
740 /// provided significant digits
744 /// * num - The float value
745 /// * digits - The number of significant digits
747 pub fn to_str_exact(num: f64, dig: uint) -> ~str {
748 let (r, _) = strconv::float_to_str_common(
749 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpNone, false);
753 /// Converts a float to a string with a maximum number of
754 /// significant digits
758 /// * num - The float value
759 /// * digits - The number of significant digits
761 pub fn to_str_digits(num: f64, dig: uint) -> ~str {
762 let (r, _) = strconv::float_to_str_common(
763 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpNone, false);
767 /// Converts a float to a string using the exponential notation with exactly the number of
768 /// provided digits after the decimal point in the significand
772 /// * num - The float value
773 /// * digits - The number of digits after the decimal point
774 /// * upper - Use `E` instead of `e` for the exponent sign
776 pub fn to_str_exp_exact(num: f64, dig: uint, upper: bool) -> ~str {
777 let (r, _) = strconv::float_to_str_common(
778 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
782 /// Converts a float to a string using the exponential notation with the maximum number of
783 /// digits after the decimal point in the significand
787 /// * num - The float value
788 /// * digits - The number of digits after the decimal point
789 /// * upper - Use `E` instead of `e` for the exponent sign
791 pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> ~str {
792 let (r, _) = strconv::float_to_str_common(
793 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
797 impl num::ToStrRadix for f64 {
798 /// Converts a float to a string in a given radix
802 /// * num - The float value
803 /// * radix - The base to use
807 /// Fails if called on a special value like `inf`, `-inf` or `NAN` due to
808 /// possible misinterpretation of the result at higher bases. If those values
809 /// are expected, use `to_str_radix_special()` instead.
811 fn to_str_radix(&self, rdx: uint) -> ~str {
812 let (r, special) = strconv::float_to_str_common(
813 *self, rdx, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
814 if special { fail!("number has a special value, \
815 try to_str_radix_special() if those are expected") }
820 /// Convert a string in base 16 to a float.
821 /// Accepts an optional binary exponent.
823 /// This function accepts strings such as
826 /// * '+a4.fe', equivalent to 'a4.fe'
828 /// * '2b.aP128', or equivalently, '2b.ap128'
830 /// * '.' (understood as 0)
832 /// * '.c', or, equivalently, '0.c'
833 /// * '+inf', 'inf', '-inf', 'NaN'
835 /// Leading and trailing whitespace represent an error.
843 /// `None` if the string did not represent a valid number. Otherwise,
844 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
846 pub fn from_str_hex(num: &str) -> Option<f64> {
847 strconv::from_str_common(num, 16u, true, true, true,
848 strconv::ExpBin, false, false)
851 impl FromStr for f64 {
852 /// Convert a string in base 10 to a float.
853 /// Accepts an optional decimal exponent.
855 /// This function accepts strings such as
858 /// * '+3.14', equivalent to '3.14'
860 /// * '2.5E10', or equivalently, '2.5e10'
862 /// * '.' (understood as 0)
864 /// * '.5', or, equivalently, '0.5'
865 /// * '+inf', 'inf', '-inf', 'NaN'
867 /// Leading and trailing whitespace represent an error.
875 /// `none` if the string did not represent a valid number. Otherwise,
876 /// `Some(n)` where `n` is the floating-point number represented by `num`.
878 fn from_str(val: &str) -> Option<f64> {
879 strconv::from_str_common(val, 10u, true, true, true,
880 strconv::ExpDec, false, false)
884 impl num::FromStrRadix for f64 {
885 /// Convert a string in a given base to a float.
887 /// Due to possible conflicts, this function does **not** accept
888 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
889 /// does it recognize exponents of any kind.
891 /// Leading and trailing whitespace represent an error.
896 /// * radix - The base to use. Must lie in the range [2 .. 36]
900 /// `None` if the string did not represent a valid number. Otherwise,
901 /// `Some(n)` where `n` is the floating-point number represented by `num`.
903 fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
904 strconv::from_str_common(val, rdx, true, true, false,
905 strconv::ExpNone, false, false)
917 assert_eq!(NAN.min(2.0), 2.0);
918 assert_eq!(2.0f64.min(NAN), 2.0);
923 assert_eq!(NAN.max(2.0), 2.0);
924 assert_eq!(2.0f64.max(NAN), 2.0);
929 num::test_num(10f64, 2f64);
934 assert_approx_eq!(1.0f64.floor(), 1.0f64);
935 assert_approx_eq!(1.3f64.floor(), 1.0f64);
936 assert_approx_eq!(1.5f64.floor(), 1.0f64);
937 assert_approx_eq!(1.7f64.floor(), 1.0f64);
938 assert_approx_eq!(0.0f64.floor(), 0.0f64);
939 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
940 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
941 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
942 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
943 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
948 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
949 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
950 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
951 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
952 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
953 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
954 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
955 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
956 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
957 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
962 assert_approx_eq!(1.0f64.round(), 1.0f64);
963 assert_approx_eq!(1.3f64.round(), 1.0f64);
964 assert_approx_eq!(1.5f64.round(), 2.0f64);
965 assert_approx_eq!(1.7f64.round(), 2.0f64);
966 assert_approx_eq!(0.0f64.round(), 0.0f64);
967 assert_approx_eq!((-0.0f64).round(), -0.0f64);
968 assert_approx_eq!((-1.0f64).round(), -1.0f64);
969 assert_approx_eq!((-1.3f64).round(), -1.0f64);
970 assert_approx_eq!((-1.5f64).round(), -2.0f64);
971 assert_approx_eq!((-1.7f64).round(), -2.0f64);
976 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
977 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
978 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
979 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
980 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
981 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
982 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
983 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
984 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
985 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
990 assert_approx_eq!(1.0f64.fract(), 0.0f64);
991 assert_approx_eq!(1.3f64.fract(), 0.3f64);
992 assert_approx_eq!(1.5f64.fract(), 0.5f64);
993 assert_approx_eq!(1.7f64.fract(), 0.7f64);
994 assert_approx_eq!(0.0f64.fract(), 0.0f64);
995 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
996 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
997 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
998 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
999 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1004 assert_eq!(0.0f64.asinh(), 0.0f64);
1005 assert_eq!((-0.0f64).asinh(), -0.0f64);
1007 let inf: f64 = Float::infinity();
1008 let neg_inf: f64 = Float::neg_infinity();
1009 let nan: f64 = Float::nan();
1010 assert_eq!(inf.asinh(), inf);
1011 assert_eq!(neg_inf.asinh(), neg_inf);
1012 assert!(nan.asinh().is_nan());
1013 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1014 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1019 assert_eq!(1.0f64.acosh(), 0.0f64);
1020 assert!(0.999f64.acosh().is_nan());
1022 let inf: f64 = Float::infinity();
1023 let neg_inf: f64 = Float::neg_infinity();
1024 let nan: f64 = Float::nan();
1025 assert_eq!(inf.acosh(), inf);
1026 assert!(neg_inf.acosh().is_nan());
1027 assert!(nan.acosh().is_nan());
1028 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1029 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1034 assert_eq!(0.0f64.atanh(), 0.0f64);
1035 assert_eq!((-0.0f64).atanh(), -0.0f64);
1037 let inf: f64 = Float::infinity();
1038 let neg_inf: f64 = Float::neg_infinity();
1039 let nan: f64 = Float::nan();
1040 assert_eq!(1.0f64.atanh(), inf);
1041 assert_eq!((-1.0f64).atanh(), neg_inf);
1042 assert!(2f64.atanh().atanh().is_nan());
1043 assert!((-2f64).atanh().atanh().is_nan());
1044 assert!(inf.atanh().is_nan());
1045 assert!(neg_inf.atanh().is_nan());
1046 assert!(nan.atanh().is_nan());
1047 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1048 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1052 fn test_real_consts() {
1053 let pi: f64 = Float::pi();
1054 let two_pi: f64 = Float::two_pi();
1055 let frac_pi_2: f64 = Float::frac_pi_2();
1056 let frac_pi_3: f64 = Float::frac_pi_3();
1057 let frac_pi_4: f64 = Float::frac_pi_4();
1058 let frac_pi_6: f64 = Float::frac_pi_6();
1059 let frac_pi_8: f64 = Float::frac_pi_8();
1060 let frac_1_pi: f64 = Float::frac_1_pi();
1061 let frac_2_pi: f64 = Float::frac_2_pi();
1062 let frac_2_sqrtpi: f64 = Float::frac_2_sqrtpi();
1063 let sqrt2: f64 = Float::sqrt2();
1064 let frac_1_sqrt2: f64 = Float::frac_1_sqrt2();
1065 let e: f64 = Float::e();
1066 let log2_e: f64 = Float::log2_e();
1067 let log10_e: f64 = Float::log10_e();
1068 let ln_2: f64 = Float::ln_2();
1069 let ln_10: f64 = Float::ln_10();
1071 assert_approx_eq!(two_pi, 2.0 * pi);
1072 assert_approx_eq!(frac_pi_2, pi / 2f64);
1073 assert_approx_eq!(frac_pi_3, pi / 3f64);
1074 assert_approx_eq!(frac_pi_4, pi / 4f64);
1075 assert_approx_eq!(frac_pi_6, pi / 6f64);
1076 assert_approx_eq!(frac_pi_8, pi / 8f64);
1077 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1078 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1079 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1080 assert_approx_eq!(sqrt2, 2f64.sqrt());
1081 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1082 assert_approx_eq!(log2_e, e.log2());
1083 assert_approx_eq!(log10_e, e.log10());
1084 assert_approx_eq!(ln_2, 2f64.ln());
1085 assert_approx_eq!(ln_10, 10f64.ln());
1090 assert_eq!(INFINITY.abs(), INFINITY);
1091 assert_eq!(1f64.abs(), 1f64);
1092 assert_eq!(0f64.abs(), 0f64);
1093 assert_eq!((-0f64).abs(), 0f64);
1094 assert_eq!((-1f64).abs(), 1f64);
1095 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1096 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1097 assert!(NAN.abs().is_nan());
1102 assert_eq!((-1f64).abs_sub(&1f64), 0f64);
1103 assert_eq!(1f64.abs_sub(&1f64), 0f64);
1104 assert_eq!(1f64.abs_sub(&0f64), 1f64);
1105 assert_eq!(1f64.abs_sub(&-1f64), 2f64);
1106 assert_eq!(NEG_INFINITY.abs_sub(&0f64), 0f64);
1107 assert_eq!(INFINITY.abs_sub(&1f64), INFINITY);
1108 assert_eq!(0f64.abs_sub(&NEG_INFINITY), INFINITY);
1109 assert_eq!(0f64.abs_sub(&INFINITY), 0f64);
1113 fn test_abs_sub_nowin() {
1114 assert!(NAN.abs_sub(&-1f64).is_nan());
1115 assert!(1f64.abs_sub(&NAN).is_nan());
1120 assert_eq!(INFINITY.signum(), 1f64);
1121 assert_eq!(1f64.signum(), 1f64);
1122 assert_eq!(0f64.signum(), 1f64);
1123 assert_eq!((-0f64).signum(), -1f64);
1124 assert_eq!((-1f64).signum(), -1f64);
1125 assert_eq!(NEG_INFINITY.signum(), -1f64);
1126 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1127 assert!(NAN.signum().is_nan());
1131 fn test_is_positive() {
1132 assert!(INFINITY.is_positive());
1133 assert!(1f64.is_positive());
1134 assert!(0f64.is_positive());
1135 assert!(!(-0f64).is_positive());
1136 assert!(!(-1f64).is_positive());
1137 assert!(!NEG_INFINITY.is_positive());
1138 assert!(!(1f64/NEG_INFINITY).is_positive());
1139 assert!(!NAN.is_positive());
1143 fn test_is_negative() {
1144 assert!(!INFINITY.is_negative());
1145 assert!(!1f64.is_negative());
1146 assert!(!0f64.is_negative());
1147 assert!((-0f64).is_negative());
1148 assert!((-1f64).is_negative());
1149 assert!(NEG_INFINITY.is_negative());
1150 assert!((1f64/NEG_INFINITY).is_negative());
1151 assert!(!NAN.is_negative());
1155 fn test_is_normal() {
1156 let nan: f64 = Float::nan();
1157 let inf: f64 = Float::infinity();
1158 let neg_inf: f64 = Float::neg_infinity();
1159 let zero: f64 = Zero::zero();
1160 let neg_zero: f64 = Float::neg_zero();
1161 assert!(!nan.is_normal());
1162 assert!(!inf.is_normal());
1163 assert!(!neg_inf.is_normal());
1164 assert!(!zero.is_normal());
1165 assert!(!neg_zero.is_normal());
1166 assert!(1f64.is_normal());
1167 assert!(1e-307f64.is_normal());
1168 assert!(!1e-308f64.is_normal());
1172 fn test_classify() {
1173 let nan: f64 = Float::nan();
1174 let inf: f64 = Float::infinity();
1175 let neg_inf: f64 = Float::neg_infinity();
1176 let zero: f64 = Zero::zero();
1177 let neg_zero: f64 = Float::neg_zero();
1178 assert_eq!(nan.classify(), FPNaN);
1179 assert_eq!(inf.classify(), FPInfinite);
1180 assert_eq!(neg_inf.classify(), FPInfinite);
1181 assert_eq!(zero.classify(), FPZero);
1182 assert_eq!(neg_zero.classify(), FPZero);
1183 assert_eq!(1e-307f64.classify(), FPNormal);
1184 assert_eq!(1e-308f64.classify(), FPSubnormal);
1189 // We have to use from_str until base-2 exponents
1190 // are supported in floating-point literals
1191 let f1: f64 = from_str_hex("1p-123").unwrap();
1192 let f2: f64 = from_str_hex("1p-111").unwrap();
1193 assert_eq!(Float::ldexp(1f64, -123), f1);
1194 assert_eq!(Float::ldexp(1f64, -111), f2);
1196 assert_eq!(Float::ldexp(0f64, -123), 0f64);
1197 assert_eq!(Float::ldexp(-0f64, -123), -0f64);
1199 let inf: f64 = Float::infinity();
1200 let neg_inf: f64 = Float::neg_infinity();
1201 let nan: f64 = Float::nan();
1202 assert_eq!(Float::ldexp(inf, -123), inf);
1203 assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
1204 assert!(Float::ldexp(nan, -123).is_nan());
1209 // We have to use from_str until base-2 exponents
1210 // are supported in floating-point literals
1211 let f1: f64 = from_str_hex("1p-123").unwrap();
1212 let f2: f64 = from_str_hex("1p-111").unwrap();
1213 let (x1, exp1) = f1.frexp();
1214 let (x2, exp2) = f2.frexp();
1215 assert_eq!((x1, exp1), (0.5f64, -122));
1216 assert_eq!((x2, exp2), (0.5f64, -110));
1217 assert_eq!(Float::ldexp(x1, exp1), f1);
1218 assert_eq!(Float::ldexp(x2, exp2), f2);
1220 assert_eq!(0f64.frexp(), (0f64, 0));
1221 assert_eq!((-0f64).frexp(), (-0f64, 0));
1224 #[test] #[ignore(cfg(windows))] // FIXME #8755
1225 fn test_frexp_nowin() {
1226 let inf: f64 = Float::infinity();
1227 let neg_inf: f64 = Float::neg_infinity();
1228 let nan: f64 = Float::nan();
1229 assert_eq!(match inf.frexp() { (x, _) => x }, inf)
1230 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
1231 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1235 fn test_integer_decode() {
1236 assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906u64, -51i16, 1i8));
1237 assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931u64, -39i16, -1i8));
1238 assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496u64, 48i16, 1i8));
1239 assert_eq!(0f64.integer_decode(), (0u64, -1075i16, 1i8));
1240 assert_eq!((-0f64).integer_decode(), (0u64, -1075i16, -1i8));
1241 assert_eq!(INFINITY.integer_decode(), (4503599627370496u64, 972i16, 1i8));
1242 assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
1243 assert_eq!(NAN.integer_decode(), (6755399441055744u64, 972i16, 1i8));