1 // Copyright 2012 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 `f64`
13 #[allow(missing_doc)];
14 #[allow(non_uppercase_statics)];
17 use num::{Zero, One, strconv};
18 use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
23 pub use cmath::c_double_targ_consts::*;
24 pub use cmp::{min, max};
26 use self::delegated::*;
28 macro_rules! delegate(
33 $arg:ident : $arg_ty:ty
35 ) -> $rv:ty = $bound_name:path
38 // An inner module is required to get the #[inline] attribute on the
41 use cmath::c_double_utils;
42 use libc::{c_double, c_int};
43 use unstable::intrinsics;
47 pub fn $name($( $arg : $arg_ty ),*) -> $rv {
49 $bound_name($( $arg ),*)
59 fn abs(n: f64) -> f64 = intrinsics::fabsf64,
60 fn cos(n: f64) -> f64 = intrinsics::cosf64,
61 fn exp(n: f64) -> f64 = intrinsics::expf64,
62 fn exp2(n: f64) -> f64 = intrinsics::exp2f64,
63 fn floor(x: f64) -> f64 = intrinsics::floorf64,
64 fn ln(n: f64) -> f64 = intrinsics::logf64,
65 fn log10(n: f64) -> f64 = intrinsics::log10f64,
66 fn log2(n: f64) -> f64 = intrinsics::log2f64,
67 fn mul_add(a: f64, b: f64, c: f64) -> f64 = intrinsics::fmaf64,
68 fn pow(n: f64, e: f64) -> f64 = intrinsics::powf64,
69 fn powi(n: f64, e: c_int) -> f64 = intrinsics::powif64,
70 fn sin(n: f64) -> f64 = intrinsics::sinf64,
71 fn sqrt(n: f64) -> f64 = intrinsics::sqrtf64,
73 // LLVM 3.3 required to use intrinsics for these four
74 fn ceil(n: c_double) -> c_double = c_double_utils::ceil,
75 fn trunc(n: c_double) -> c_double = c_double_utils::trunc,
77 fn ceil(n: f64) -> f64 = intrinsics::ceilf64,
78 fn trunc(n: f64) -> f64 = intrinsics::truncf64,
79 fn rint(n: c_double) -> c_double = intrinsics::rintf64,
80 fn nearbyint(n: c_double) -> c_double = intrinsics::nearbyintf64,
84 fn acos(n: c_double) -> c_double = c_double_utils::acos,
85 fn asin(n: c_double) -> c_double = c_double_utils::asin,
86 fn atan(n: c_double) -> c_double = c_double_utils::atan,
87 fn atan2(a: c_double, b: c_double) -> c_double = c_double_utils::atan2,
88 fn cbrt(n: c_double) -> c_double = c_double_utils::cbrt,
89 fn copysign(x: c_double, y: c_double) -> c_double = c_double_utils::copysign,
90 fn cosh(n: c_double) -> c_double = c_double_utils::cosh,
91 fn erf(n: c_double) -> c_double = c_double_utils::erf,
92 fn erfc(n: c_double) -> c_double = c_double_utils::erfc,
93 fn exp_m1(n: c_double) -> c_double = c_double_utils::exp_m1,
94 fn abs_sub(a: c_double, b: c_double) -> c_double = c_double_utils::abs_sub,
95 fn next_after(x: c_double, y: c_double) -> c_double = c_double_utils::next_after,
96 fn frexp(n: c_double, value: &mut c_int) -> c_double = c_double_utils::frexp,
97 fn hypot(x: c_double, y: c_double) -> c_double = c_double_utils::hypot,
98 fn ldexp(x: c_double, n: c_int) -> c_double = c_double_utils::ldexp,
99 fn lgamma(n: c_double, sign: &mut c_int) -> c_double = c_double_utils::lgamma,
100 fn log_radix(n: c_double) -> c_double = c_double_utils::log_radix,
101 fn ln_1p(n: c_double) -> c_double = c_double_utils::ln_1p,
102 fn ilog_radix(n: c_double) -> c_int = c_double_utils::ilog_radix,
103 fn modf(n: c_double, iptr: &mut c_double) -> c_double = c_double_utils::modf,
104 fn round(n: c_double) -> c_double = c_double_utils::round,
105 fn ldexp_radix(n: c_double, i: c_int) -> c_double = c_double_utils::ldexp_radix,
106 fn sinh(n: c_double) -> c_double = c_double_utils::sinh,
107 fn tan(n: c_double) -> c_double = c_double_utils::tan,
108 fn tanh(n: c_double) -> c_double = c_double_utils::tanh,
109 fn tgamma(n: c_double) -> c_double = c_double_utils::tgamma,
110 fn j0(n: c_double) -> c_double = c_double_utils::j0,
111 fn j1(n: c_double) -> c_double = c_double_utils::j1,
112 fn jn(i: c_int, n: c_double) -> c_double = c_double_utils::jn,
113 fn y0(n: c_double) -> c_double = c_double_utils::y0,
114 fn y1(n: c_double) -> c_double = c_double_utils::y1,
115 fn yn(i: c_int, n: c_double) -> c_double = c_double_utils::yn
118 // FIXME (#1433): obtain these in a different way
120 // These are not defined inside consts:: for consistency with
123 pub static radix: uint = 2u;
125 pub static mantissa_digits: uint = 53u;
126 pub static digits: uint = 15u;
128 pub static epsilon: f64 = 2.2204460492503131e-16_f64;
130 pub static min_value: f64 = 2.2250738585072014e-308_f64;
131 pub static max_value: f64 = 1.7976931348623157e+308_f64;
133 pub static min_exp: int = -1021;
134 pub static max_exp: int = 1024;
136 pub static min_10_exp: int = -307;
137 pub static max_10_exp: int = 308;
139 pub static NaN: f64 = 0.0_f64/0.0_f64;
141 pub static infinity: f64 = 1.0_f64/0.0_f64;
143 pub static neg_infinity: f64 = -1.0_f64/0.0_f64;
145 // FIXME (#1999): add is_normal, is_subnormal, and fpclassify
149 // FIXME (requires Issue #1433 to fix): replace with mathematical
150 // constants from cmath.
151 /// Archimedes' constant
152 pub static pi: f64 = 3.14159265358979323846264338327950288_f64;
155 pub static frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
158 pub static frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
161 pub static frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
164 pub static frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
167 pub static frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
170 pub static sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
173 pub static frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
176 pub static e: f64 = 2.71828182845904523536028747135266250_f64;
179 pub static log2_e: f64 = 1.44269504088896340735992468100189214_f64;
182 pub static log10_e: f64 = 0.434294481903251827651128918916605082_f64;
185 pub static ln_2: f64 = 0.693147180559945309417232121458176568_f64;
188 pub static ln_10: f64 = 2.30258509299404568401799145468436421_f64;
196 fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
198 fn ne(&self, other: &f64) -> bool { (*self) != (*other) }
202 impl ApproxEq<f64> for f64 {
204 fn approx_epsilon() -> f64 { 1.0e-6 }
207 fn approx_eq(&self, other: &f64) -> bool {
208 self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<f64, f64>())
212 fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
213 (*self - *other).abs() < *approx_epsilon
220 fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
222 fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
224 fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
226 fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
229 impl Orderable for f64 {
230 /// Returns `NaN` if either of the numbers are `NaN`.
232 fn min(&self, other: &f64) -> f64 {
234 (self.is_NaN()) { *self }
235 (other.is_NaN()) { *other }
236 (*self < *other) { *self }
241 /// Returns `NaN` if either of the numbers are `NaN`.
243 fn max(&self, other: &f64) -> f64 {
245 (self.is_NaN()) { *self }
246 (other.is_NaN()) { *other }
247 (*self > *other) { *self }
252 /// Returns the number constrained within the range `mn <= self <= mx`.
253 /// If any of the numbers are `NaN` then `NaN` is returned.
255 fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
257 (self.is_NaN()) { *self }
258 (!(*self <= *mx)) { *mx }
259 (!(*self >= *mn)) { *mn }
267 fn zero() -> f64 { 0.0 }
269 /// Returns true if the number is equal to either `0.0` or `-0.0`
271 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
276 fn one() -> f64 { 1.0 }
280 impl Add<f64,f64> for f64 {
281 fn add(&self, other: &f64) -> f64 { *self + *other }
284 impl Sub<f64,f64> for f64 {
285 fn sub(&self, other: &f64) -> f64 { *self - *other }
288 impl Mul<f64,f64> for f64 {
289 fn mul(&self, other: &f64) -> f64 { *self * *other }
292 impl Div<f64,f64> for f64 {
293 fn div(&self, other: &f64) -> f64 { *self / *other }
296 impl Rem<f64,f64> for f64 {
298 fn rem(&self, other: &f64) -> f64 { *self % *other }
301 impl Neg<f64> for f64 {
302 fn neg(&self) -> f64 { -*self }
305 impl Signed for f64 {
306 /// Computes the absolute value. Returns `NaN` if the number is `NaN`.
308 fn abs(&self) -> f64 { abs(*self) }
311 /// The positive difference of two numbers. Returns `0.0` if the number is less than or
312 /// equal to `other`, otherwise the difference between`self` and `other` is returned.
315 fn abs_sub(&self, other: &f64) -> f64 { abs_sub(*self, *other) }
320 /// - `1.0` if the number is positive, `+0.0` or `infinity`
321 /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
322 /// - `NaN` if the number is NaN
325 fn signum(&self) -> f64 {
326 if self.is_NaN() { NaN } else { copysign(1.0, *self) }
329 /// Returns `true` if the number is positive, including `+0.0` and `infinity`
331 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
333 /// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
335 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
339 /// Round half-way cases toward `neg_infinity`
341 fn floor(&self) -> f64 { floor(*self) }
343 /// Round half-way cases toward `infinity`
345 fn ceil(&self) -> f64 { ceil(*self) }
347 /// Round half-way cases away from `0.0`
349 fn round(&self) -> f64 { round(*self) }
351 /// The integer part of the number (rounds towards `0.0`)
353 fn trunc(&self) -> f64 { trunc(*self) }
356 /// The fractional part of the number, satisfying:
359 /// assert!(x == trunc(x) + fract(x))
363 fn fract(&self) -> f64 { *self - self.trunc() }
366 impl Fractional for f64 {
367 /// The reciprocal (multiplicative inverse) of the number
369 fn recip(&self) -> f64 { 1.0 / *self }
372 impl Algebraic for f64 {
374 fn pow(&self, n: &f64) -> f64 { pow(*self, *n) }
377 fn sqrt(&self) -> f64 { sqrt(*self) }
380 fn rsqrt(&self) -> f64 { self.sqrt().recip() }
383 fn cbrt(&self) -> f64 { cbrt(*self) }
386 fn hypot(&self, other: &f64) -> f64 { hypot(*self, *other) }
389 impl Trigonometric for f64 {
391 fn sin(&self) -> f64 { sin(*self) }
394 fn cos(&self) -> f64 { cos(*self) }
397 fn tan(&self) -> f64 { tan(*self) }
400 fn asin(&self) -> f64 { asin(*self) }
403 fn acos(&self) -> f64 { acos(*self) }
406 fn atan(&self) -> f64 { atan(*self) }
409 fn atan2(&self, other: &f64) -> f64 { atan2(*self, *other) }
411 /// Simultaneously computes the sine and cosine of the number
413 fn sin_cos(&self) -> (f64, f64) {
414 (self.sin(), self.cos())
418 impl Exponential for f64 {
419 /// Returns the exponential of the number
421 fn exp(&self) -> f64 { exp(*self) }
423 /// Returns 2 raised to the power of the number
425 fn exp2(&self) -> f64 { exp2(*self) }
427 /// Returns the natural logarithm of the number
429 fn ln(&self) -> f64 { ln(*self) }
431 /// Returns the logarithm of the number with respect to an arbitrary base
433 fn log(&self, base: &f64) -> f64 { self.ln() / base.ln() }
435 /// Returns the base 2 logarithm of the number
437 fn log2(&self) -> f64 { log2(*self) }
439 /// Returns the base 10 logarithm of the number
441 fn log10(&self) -> f64 { log10(*self) }
444 impl Hyperbolic for f64 {
446 fn sinh(&self) -> f64 { sinh(*self) }
449 fn cosh(&self) -> f64 { cosh(*self) }
452 fn tanh(&self) -> f64 { tanh(*self) }
455 /// Inverse hyperbolic sine
459 /// - on success, the inverse hyperbolic sine of `self` will be returned
460 /// - `self` if `self` is `0.0`, `-0.0`, `infinity`, or `neg_infinity`
461 /// - `NaN` if `self` is `NaN`
464 fn asinh(&self) -> f64 {
466 neg_infinity => neg_infinity,
467 x => (x + ((x * x) + 1.0).sqrt()).ln(),
472 /// Inverse hyperbolic cosine
476 /// - on success, the inverse hyperbolic cosine of `self` will be returned
477 /// - `infinity` if `self` is `infinity`
478 /// - `NaN` if `self` is `NaN` or `self < 1.0` (including `neg_infinity`)
481 fn acosh(&self) -> f64 {
483 x if x < 1.0 => Float::NaN(),
484 x => (x + ((x * x) - 1.0).sqrt()).ln(),
489 /// Inverse hyperbolic tangent
493 /// - on success, the inverse hyperbolic tangent of `self` will be returned
494 /// - `self` if `self` is `0.0` or `-0.0`
495 /// - `infinity` if `self` is `1.0`
496 /// - `neg_infinity` if `self` is `-1.0`
497 /// - `NaN` if the `self` is `NaN` or outside the domain of `-1.0 <= self <= 1.0`
498 /// (including `infinity` and `neg_infinity`)
501 fn atanh(&self) -> f64 {
502 0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
507 /// Archimedes' constant
509 fn pi() -> f64 { 3.14159265358979323846264338327950288 }
513 fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
517 fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
521 fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
525 fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
529 fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
533 fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
537 fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
541 fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
545 fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
549 fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
553 fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
557 fn e() -> f64 { 2.71828182845904523536028747135266250 }
561 fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
565 fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
569 fn ln_2() -> f64 { 0.693147180559945309417232121458176568 }
573 fn ln_10() -> f64 { 2.30258509299404568401799145468436421 }
575 /// Converts to degrees, assuming the number is in radians
577 fn to_degrees(&self) -> f64 { *self * (180.0 / Real::pi::<f64>()) }
579 /// Converts to radians, assuming the number is in degrees
581 fn to_radians(&self) -> f64 { *self * (Real::pi::<f64>() / 180.0) }
584 impl RealExt for f64 {
586 fn lgamma(&self) -> (int, f64) {
588 let result = lgamma(*self, &mut sign);
589 (sign as int, result)
593 fn tgamma(&self) -> f64 { tgamma(*self) }
596 fn j0(&self) -> f64 { j0(*self) }
599 fn j1(&self) -> f64 { j1(*self) }
602 fn jn(&self, n: int) -> f64 { jn(n as c_int, *self) }
605 fn y0(&self) -> f64 { y0(*self) }
608 fn y1(&self) -> f64 { y1(*self) }
611 fn yn(&self, n: int) -> f64 { yn(n as c_int, *self) }
614 impl Bounded for f64 {
616 fn min_value() -> f64 { 2.2250738585072014e-308 }
619 fn max_value() -> f64 { 1.7976931348623157e+308 }
622 impl Primitive for f64 {
624 fn bits() -> uint { 64 }
627 fn bytes() -> uint { Primitive::bits::<f64>() / 8 }
632 fn NaN() -> f64 { 0.0 / 0.0 }
635 fn infinity() -> f64 { 1.0 / 0.0 }
638 fn neg_infinity() -> f64 { -1.0 / 0.0 }
641 fn neg_zero() -> f64 { -0.0 }
643 /// Returns `true` if the number is NaN
645 fn is_NaN(&self) -> bool { *self != *self }
647 /// Returns `true` if the number is infinite
649 fn is_infinite(&self) -> bool {
650 *self == Float::infinity() || *self == Float::neg_infinity()
653 /// Returns `true` if the number is neither infinite or NaN
655 fn is_finite(&self) -> bool {
656 !(self.is_NaN() || self.is_infinite())
659 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
661 fn is_normal(&self) -> bool {
662 self.classify() == FPNormal
665 /// Returns the floating point category of the number. If only one property is going to
666 /// be tested, it is generally faster to use the specific predicate instead.
667 fn classify(&self) -> FPCategory {
668 static EXP_MASK: u64 = 0x7ff0000000000000;
669 static MAN_MASK: u64 = 0x000fffffffffffff;
672 unsafe { ::cast::transmute::<f64,u64>(*self) } & MAN_MASK,
673 unsafe { ::cast::transmute::<f64,u64>(*self) } & EXP_MASK,
676 (_, 0) => FPSubnormal,
677 (0, EXP_MASK) => FPInfinite,
678 (_, EXP_MASK) => FPNaN,
684 fn mantissa_digits() -> uint { 53 }
687 fn digits() -> uint { 15 }
690 fn epsilon() -> f64 { 2.2204460492503131e-16 }
693 fn min_exp() -> int { -1021 }
696 fn max_exp() -> int { 1024 }
699 fn min_10_exp() -> int { -307 }
702 fn max_10_exp() -> int { 308 }
704 /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
706 fn ldexp(x: f64, exp: int) -> f64 {
707 ldexp(x, exp as c_int)
711 /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
713 /// - `self = x * pow(2, exp)`
714 /// - `0.5 <= abs(x) < 1.0`
717 fn frexp(&self) -> (f64, int) {
719 let x = frexp(*self, &mut exp);
724 /// Returns the exponential of the number, minus `1`, in a way that is accurate
725 /// even if the number is close to zero
728 fn exp_m1(&self) -> f64 { exp_m1(*self) }
731 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
732 /// than if the operations were performed separately
735 fn ln_1p(&self) -> f64 { ln_1p(*self) }
738 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
739 /// produces a more accurate result with better performance than a separate multiplication
740 /// operation followed by an add.
743 fn mul_add(&self, a: f64, b: f64) -> f64 {
747 /// Returns the next representable floating-point value in the direction of `other`
749 fn next_after(&self, other: f64) -> f64 {
750 next_after(*self, other)
755 // Section: String Conversions
759 /// Converts a float to a string
763 /// * num - The float value
766 pub fn to_str(num: f64) -> ~str {
767 let (r, _) = strconv::float_to_str_common(
768 num, 10u, true, strconv::SignNeg, strconv::DigAll);
773 /// Converts a float to a string in hexadecimal format
777 /// * num - The float value
780 pub fn to_str_hex(num: f64) -> ~str {
781 let (r, _) = strconv::float_to_str_common(
782 num, 16u, true, strconv::SignNeg, strconv::DigAll);
787 /// Converts a float to a string in a given radix
791 /// * num - The float value
792 /// * radix - The base to use
796 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
797 /// possible misinterpretation of the result at higher bases. If those values
798 /// are expected, use `to_str_radix_special()` instead.
801 pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
802 let (r, special) = strconv::float_to_str_common(
803 num, rdx, true, strconv::SignNeg, strconv::DigAll);
804 if special { fail!("number has a special value, \
805 try to_str_radix_special() if those are expected") }
810 /// Converts a float to a string in a given radix, and a flag indicating
811 /// whether it's a special value
815 /// * num - The float value
816 /// * radix - The base to use
819 pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
820 strconv::float_to_str_common(num, rdx, true,
821 strconv::SignNeg, strconv::DigAll)
825 /// Converts a float to a string with exactly the number of
826 /// provided significant digits
830 /// * num - The float value
831 /// * digits - The number of significant digits
834 pub fn to_str_exact(num: f64, dig: uint) -> ~str {
835 let (r, _) = strconv::float_to_str_common(
836 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
841 /// Converts a float to a string with a maximum number of
842 /// significant digits
846 /// * num - The float value
847 /// * digits - The number of significant digits
850 pub fn to_str_digits(num: f64, dig: uint) -> ~str {
851 let (r, _) = strconv::float_to_str_common(
852 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
856 impl to_str::ToStr for f64 {
858 fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
861 impl num::ToStrRadix for f64 {
863 fn to_str_radix(&self, rdx: uint) -> ~str {
864 to_str_radix(*self, rdx)
869 /// Convert a string in base 10 to a float.
870 /// Accepts a optional decimal exponent.
872 /// This function accepts strings such as
875 /// * '+3.14', equivalent to '3.14'
877 /// * '2.5E10', or equivalently, '2.5e10'
879 /// * '.' (understood as 0)
881 /// * '.5', or, equivalently, '0.5'
882 /// * '+inf', 'inf', '-inf', 'NaN'
884 /// Leading and trailing whitespace represent an error.
892 /// `none` if the string did not represent a valid number. Otherwise,
893 /// `Some(n)` where `n` is the floating-point number represented by `num`.
896 pub fn from_str(num: &str) -> Option<f64> {
897 strconv::from_str_common(num, 10u, true, true, true,
898 strconv::ExpDec, false, false)
902 /// Convert a string in base 16 to a float.
903 /// Accepts a optional binary exponent.
905 /// This function accepts strings such as
908 /// * '+a4.fe', equivalent to 'a4.fe'
910 /// * '2b.aP128', or equivalently, '2b.ap128'
912 /// * '.' (understood as 0)
914 /// * '.c', or, equivalently, '0.c'
915 /// * '+inf', 'inf', '-inf', 'NaN'
917 /// Leading and trailing whitespace represent an error.
925 /// `none` if the string did not represent a valid number. Otherwise,
926 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
929 pub fn from_str_hex(num: &str) -> Option<f64> {
930 strconv::from_str_common(num, 16u, true, true, true,
931 strconv::ExpBin, false, false)
935 /// Convert a string in an given base to a float.
937 /// Due to possible conflicts, this function does **not** accept
938 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
939 /// does it recognize exponents of any kind.
941 /// Leading and trailing whitespace represent an error.
946 /// * radix - The base to use. Must lie in the range [2 .. 36]
950 /// `none` if the string did not represent a valid number. Otherwise,
951 /// `Some(n)` where `n` is the floating-point number represented by `num`.
954 pub fn from_str_radix(num: &str, rdx: uint) -> Option<f64> {
955 strconv::from_str_common(num, rdx, true, true, false,
956 strconv::ExpNone, false, false)
959 impl FromStr for f64 {
961 fn from_str(val: &str) -> Option<f64> { from_str(val) }
964 impl num::FromStrRadix for f64 {
966 fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
967 from_str_radix(val, rdx)
982 num::test_num(10f64, 2f64);
987 assert_eq!(1f64.min(&2f64), 1f64);
988 assert_eq!(2f64.min(&1f64), 1f64);
989 assert!(1f64.min(&Float::NaN::<f64>()).is_NaN());
990 assert!(Float::NaN::<f64>().min(&1f64).is_NaN());
995 assert_eq!(1f64.max(&2f64), 2f64);
996 assert_eq!(2f64.max(&1f64), 2f64);
997 assert!(1f64.max(&Float::NaN::<f64>()).is_NaN());
998 assert!(Float::NaN::<f64>().max(&1f64).is_NaN());
1003 assert_eq!(1f64.clamp(&2f64, &4f64), 2f64);
1004 assert_eq!(8f64.clamp(&2f64, &4f64), 4f64);
1005 assert_eq!(3f64.clamp(&2f64, &4f64), 3f64);
1006 assert!(3f64.clamp(&Float::NaN::<f64>(), &4f64).is_NaN());
1007 assert!(3f64.clamp(&2f64, &Float::NaN::<f64>()).is_NaN());
1008 assert!(Float::NaN::<f64>().clamp(&2f64, &4f64).is_NaN());
1013 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1014 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1015 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1016 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1017 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1018 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1019 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1020 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1021 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1022 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1027 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1028 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1029 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1030 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1031 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1032 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1033 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1034 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1035 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1036 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1041 assert_approx_eq!(1.0f64.round(), 1.0f64);
1042 assert_approx_eq!(1.3f64.round(), 1.0f64);
1043 assert_approx_eq!(1.5f64.round(), 2.0f64);
1044 assert_approx_eq!(1.7f64.round(), 2.0f64);
1045 assert_approx_eq!(0.0f64.round(), 0.0f64);
1046 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1047 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1048 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1049 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1050 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1055 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1056 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1057 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1058 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1059 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1060 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1061 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1062 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1063 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1064 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1069 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1070 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1071 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1072 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1073 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1074 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1075 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1076 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1077 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1078 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1083 assert_eq!(0.0f64.asinh(), 0.0f64);
1084 assert_eq!((-0.0f64).asinh(), -0.0f64);
1085 assert_eq!(Float::infinity::<f64>().asinh(), Float::infinity::<f64>());
1086 assert_eq!(Float::neg_infinity::<f64>().asinh(), Float::neg_infinity::<f64>());
1087 assert!(Float::NaN::<f64>().asinh().is_NaN());
1088 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1089 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1094 assert_eq!(1.0f64.acosh(), 0.0f64);
1095 assert!(0.999f64.acosh().is_NaN());
1096 assert_eq!(Float::infinity::<f64>().acosh(), Float::infinity::<f64>());
1097 assert!(Float::neg_infinity::<f64>().acosh().is_NaN());
1098 assert!(Float::NaN::<f64>().acosh().is_NaN());
1099 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1100 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1105 assert_eq!(0.0f64.atanh(), 0.0f64);
1106 assert_eq!((-0.0f64).atanh(), -0.0f64);
1107 assert_eq!(1.0f64.atanh(), Float::infinity::<f64>());
1108 assert_eq!((-1.0f64).atanh(), Float::neg_infinity::<f64>());
1109 assert!(2f64.atanh().atanh().is_NaN());
1110 assert!((-2f64).atanh().atanh().is_NaN());
1111 assert!(Float::infinity::<f64>().atanh().is_NaN());
1112 assert!(Float::neg_infinity::<f64>().atanh().is_NaN());
1113 assert!(Float::NaN::<f64>().atanh().is_NaN());
1114 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1115 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1119 fn test_real_consts() {
1120 assert_approx_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());
1121 assert_approx_eq!(Real::frac_pi_2::<f64>(), Real::pi::<f64>() / 2f64);
1122 assert_approx_eq!(Real::frac_pi_3::<f64>(), Real::pi::<f64>() / 3f64);
1123 assert_approx_eq!(Real::frac_pi_4::<f64>(), Real::pi::<f64>() / 4f64);
1124 assert_approx_eq!(Real::frac_pi_6::<f64>(), Real::pi::<f64>() / 6f64);
1125 assert_approx_eq!(Real::frac_pi_8::<f64>(), Real::pi::<f64>() / 8f64);
1126 assert_approx_eq!(Real::frac_1_pi::<f64>(), 1f64 / Real::pi::<f64>());
1127 assert_approx_eq!(Real::frac_2_pi::<f64>(), 2f64 / Real::pi::<f64>());
1128 assert_approx_eq!(Real::frac_2_sqrtpi::<f64>(), 2f64 / Real::pi::<f64>().sqrt());
1129 assert_approx_eq!(Real::sqrt2::<f64>(), 2f64.sqrt());
1130 assert_approx_eq!(Real::frac_1_sqrt2::<f64>(), 1f64 / 2f64.sqrt());
1131 assert_approx_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
1132 assert_approx_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
1133 assert_approx_eq!(Real::ln_2::<f64>(), 2f64.ln());
1134 assert_approx_eq!(Real::ln_10::<f64>(), 10f64.ln());
1139 assert_eq!(infinity.abs(), infinity);
1140 assert_eq!(1f64.abs(), 1f64);
1141 assert_eq!(0f64.abs(), 0f64);
1142 assert_eq!((-0f64).abs(), 0f64);
1143 assert_eq!((-1f64).abs(), 1f64);
1144 assert_eq!(neg_infinity.abs(), infinity);
1145 assert_eq!((1f64/neg_infinity).abs(), 0f64);
1146 assert!(NaN.abs().is_NaN());
1151 assert_eq!((-1f64).abs_sub(&1f64), 0f64);
1152 assert_eq!(1f64.abs_sub(&1f64), 0f64);
1153 assert_eq!(1f64.abs_sub(&0f64), 1f64);
1154 assert_eq!(1f64.abs_sub(&-1f64), 2f64);
1155 assert_eq!(neg_infinity.abs_sub(&0f64), 0f64);
1156 assert_eq!(infinity.abs_sub(&1f64), infinity);
1157 assert_eq!(0f64.abs_sub(&neg_infinity), infinity);
1158 assert_eq!(0f64.abs_sub(&infinity), 0f64);
1159 assert!(NaN.abs_sub(&-1f64).is_NaN());
1160 assert!(1f64.abs_sub(&NaN).is_NaN());
1165 assert_eq!(infinity.signum(), 1f64);
1166 assert_eq!(1f64.signum(), 1f64);
1167 assert_eq!(0f64.signum(), 1f64);
1168 assert_eq!((-0f64).signum(), -1f64);
1169 assert_eq!((-1f64).signum(), -1f64);
1170 assert_eq!(neg_infinity.signum(), -1f64);
1171 assert_eq!((1f64/neg_infinity).signum(), -1f64);
1172 assert!(NaN.signum().is_NaN());
1176 fn test_is_positive() {
1177 assert!(infinity.is_positive());
1178 assert!(1f64.is_positive());
1179 assert!(0f64.is_positive());
1180 assert!(!(-0f64).is_positive());
1181 assert!(!(-1f64).is_positive());
1182 assert!(!neg_infinity.is_positive());
1183 assert!(!(1f64/neg_infinity).is_positive());
1184 assert!(!NaN.is_positive());
1188 fn test_is_negative() {
1189 assert!(!infinity.is_negative());
1190 assert!(!1f64.is_negative());
1191 assert!(!0f64.is_negative());
1192 assert!((-0f64).is_negative());
1193 assert!((-1f64).is_negative());
1194 assert!(neg_infinity.is_negative());
1195 assert!((1f64/neg_infinity).is_negative());
1196 assert!(!NaN.is_negative());
1200 fn test_approx_eq() {
1201 assert!(1.0f64.approx_eq(&1f64));
1202 assert!(0.9999999f64.approx_eq(&1f64));
1203 assert!(1.000001f64.approx_eq_eps(&1f64, &1.0e-5));
1204 assert!(1.0000001f64.approx_eq_eps(&1f64, &1.0e-6));
1205 assert!(!1.0000001f64.approx_eq_eps(&1f64, &1.0e-7));
1209 fn test_primitive() {
1210 assert_eq!(Primitive::bits::<f64>(), sys::size_of::<f64>() * 8);
1211 assert_eq!(Primitive::bytes::<f64>(), sys::size_of::<f64>());
1215 fn test_is_normal() {
1216 assert!(!Float::NaN::<f64>().is_normal());
1217 assert!(!Float::infinity::<f64>().is_normal());
1218 assert!(!Float::neg_infinity::<f64>().is_normal());
1219 assert!(!Zero::zero::<f64>().is_normal());
1220 assert!(!Float::neg_zero::<f64>().is_normal());
1221 assert!(1f64.is_normal());
1222 assert!(1e-307f64.is_normal());
1223 assert!(!1e-308f64.is_normal());
1227 fn test_classify() {
1228 assert_eq!(Float::NaN::<f64>().classify(), FPNaN);
1229 assert_eq!(Float::infinity::<f64>().classify(), FPInfinite);
1230 assert_eq!(Float::neg_infinity::<f64>().classify(), FPInfinite);
1231 assert_eq!(Zero::zero::<f64>().classify(), FPZero);
1232 assert_eq!(Float::neg_zero::<f64>().classify(), FPZero);
1233 assert_eq!(1e-307f64.classify(), FPNormal);
1234 assert_eq!(1e-308f64.classify(), FPSubnormal);
1239 // We have to use from_str until base-2 exponents
1240 // are supported in floating-point literals
1241 let f1: f64 = from_str_hex("1p-123").unwrap();
1242 let f2: f64 = from_str_hex("1p-111").unwrap();
1243 assert_eq!(Float::ldexp(1f64, -123), f1);
1244 assert_eq!(Float::ldexp(1f64, -111), f2);
1246 assert_eq!(Float::ldexp(0f64, -123), 0f64);
1247 assert_eq!(Float::ldexp(-0f64, -123), -0f64);
1248 assert_eq!(Float::ldexp(Float::infinity::<f64>(), -123),
1249 Float::infinity::<f64>());
1250 assert_eq!(Float::ldexp(Float::neg_infinity::<f64>(), -123),
1251 Float::neg_infinity::<f64>());
1252 assert!(Float::ldexp(Float::NaN::<f64>(), -123).is_NaN());
1257 // We have to use from_str until base-2 exponents
1258 // are supported in floating-point literals
1259 let f1: f64 = from_str_hex("1p-123").unwrap();
1260 let f2: f64 = from_str_hex("1p-111").unwrap();
1261 let (x1, exp1) = f1.frexp();
1262 let (x2, exp2) = f2.frexp();
1263 assert_eq!((x1, exp1), (0.5f64, -122));
1264 assert_eq!((x2, exp2), (0.5f64, -110));
1265 assert_eq!(Float::ldexp(x1, exp1), f1);
1266 assert_eq!(Float::ldexp(x2, exp2), f2);
1268 assert_eq!(0f64.frexp(), (0f64, 0));
1269 assert_eq!((-0f64).frexp(), (-0f64, 0));
1270 assert_eq!(match Float::infinity::<f64>().frexp() { (x, _) => x },
1271 Float::infinity::<f64>())
1272 assert_eq!(match Float::neg_infinity::<f64>().frexp() { (x, _) => x },
1273 Float::neg_infinity::<f64>())
1274 assert!(match Float::NaN::<f64>().frexp() { (x, _) => x.is_NaN() })