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`
14 use num::{Zero, One, strconv};
15 use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
18 pub use cmath::c_double_targ_consts::*;
19 pub use cmp::{min, max};
21 // An inner module is required to get the #[inline(always)] attribute on the
23 pub use self::delegated::*;
25 macro_rules! delegate(
30 $arg:ident : $arg_ty:ty
32 ) -> $rv:ty = $bound_name:path
36 use cmath::c_double_utils;
37 use libc::{c_double, c_int};
38 use unstable::intrinsics;
42 pub fn $name($( $arg : $arg_ty ),*) -> $rv {
44 $bound_name($( $arg ),*)
54 fn abs(n: f64) -> f64 = intrinsics::fabsf64,
55 fn cos(n: f64) -> f64 = intrinsics::cosf64,
56 fn exp(n: f64) -> f64 = intrinsics::expf64,
57 fn exp2(n: f64) -> f64 = intrinsics::exp2f64,
58 fn floor(x: f64) -> f64 = intrinsics::floorf64,
59 fn ln(n: f64) -> f64 = intrinsics::logf64,
60 fn log10(n: f64) -> f64 = intrinsics::log10f64,
61 fn log2(n: f64) -> f64 = intrinsics::log2f64,
62 fn mul_add(a: f64, b: f64, c: f64) -> f64 = intrinsics::fmaf64,
63 fn pow(n: f64, e: f64) -> f64 = intrinsics::powf64,
64 fn powi(n: f64, e: c_int) -> f64 = intrinsics::powif64,
65 fn sin(n: f64) -> f64 = intrinsics::sinf64,
66 fn sqrt(n: f64) -> f64 = intrinsics::sqrtf64,
68 // LLVM 3.3 required to use intrinsics for these four
69 fn ceil(n: c_double) -> c_double = c_double_utils::ceil,
70 fn trunc(n: c_double) -> c_double = c_double_utils::trunc,
72 fn ceil(n: f64) -> f64 = intrinsics::ceilf64,
73 fn trunc(n: f64) -> f64 = intrinsics::truncf64,
74 fn rint(n: c_double) -> c_double = intrinsics::rintf64,
75 fn nearbyint(n: c_double) -> c_double = intrinsics::nearbyintf64,
79 fn acos(n: c_double) -> c_double = c_double_utils::acos,
80 fn asin(n: c_double) -> c_double = c_double_utils::asin,
81 fn atan(n: c_double) -> c_double = c_double_utils::atan,
82 fn atan2(a: c_double, b: c_double) -> c_double = c_double_utils::atan2,
83 fn cbrt(n: c_double) -> c_double = c_double_utils::cbrt,
84 fn copysign(x: c_double, y: c_double) -> c_double = c_double_utils::copysign,
85 fn cosh(n: c_double) -> c_double = c_double_utils::cosh,
86 fn erf(n: c_double) -> c_double = c_double_utils::erf,
87 fn erfc(n: c_double) -> c_double = c_double_utils::erfc,
88 fn exp_m1(n: c_double) -> c_double = c_double_utils::exp_m1,
89 fn abs_sub(a: c_double, b: c_double) -> c_double = c_double_utils::abs_sub,
90 fn fmax(a: c_double, b: c_double) -> c_double = c_double_utils::fmax,
91 fn fmin(a: c_double, b: c_double) -> c_double = c_double_utils::fmin,
92 fn next_after(x: c_double, y: c_double) -> c_double = c_double_utils::next_after,
93 fn frexp(n: c_double, value: &mut c_int) -> c_double = c_double_utils::frexp,
94 fn hypot(x: c_double, y: c_double) -> c_double = c_double_utils::hypot,
95 fn ldexp(x: c_double, n: c_int) -> c_double = c_double_utils::ldexp,
96 fn lgamma(n: c_double, sign: &mut c_int) -> c_double = c_double_utils::lgamma,
97 fn log_radix(n: c_double) -> c_double = c_double_utils::log_radix,
98 fn ln_1p(n: c_double) -> c_double = c_double_utils::ln_1p,
99 fn ilog_radix(n: c_double) -> c_int = c_double_utils::ilog_radix,
100 fn modf(n: c_double, iptr: &mut c_double) -> c_double = c_double_utils::modf,
101 fn round(n: c_double) -> c_double = c_double_utils::round,
102 fn ldexp_radix(n: c_double, i: c_int) -> c_double = c_double_utils::ldexp_radix,
103 fn sinh(n: c_double) -> c_double = c_double_utils::sinh,
104 fn tan(n: c_double) -> c_double = c_double_utils::tan,
105 fn tanh(n: c_double) -> c_double = c_double_utils::tanh,
106 fn tgamma(n: c_double) -> c_double = c_double_utils::tgamma,
107 fn j0(n: c_double) -> c_double = c_double_utils::j0,
108 fn j1(n: c_double) -> c_double = c_double_utils::j1,
109 fn jn(i: c_int, n: c_double) -> c_double = c_double_utils::jn,
110 fn y0(n: c_double) -> c_double = c_double_utils::y0,
111 fn y1(n: c_double) -> c_double = c_double_utils::y1,
112 fn yn(i: c_int, n: c_double) -> c_double = c_double_utils::yn
115 // FIXME (#1433): obtain these in a different way
117 // These are not defined inside consts:: for consistency with
120 pub static radix: uint = 2u;
122 pub static mantissa_digits: uint = 53u;
123 pub static digits: uint = 15u;
125 pub static epsilon: f64 = 2.2204460492503131e-16_f64;
127 pub static min_value: f64 = 2.2250738585072014e-308_f64;
128 pub static max_value: f64 = 1.7976931348623157e+308_f64;
130 pub static min_exp: int = -1021;
131 pub static max_exp: int = 1024;
133 pub static min_10_exp: int = -307;
134 pub static max_10_exp: int = 308;
136 pub static NaN: f64 = 0.0_f64/0.0_f64;
138 pub static infinity: f64 = 1.0_f64/0.0_f64;
140 pub static neg_infinity: f64 = -1.0_f64/0.0_f64;
143 pub fn add(x: f64, y: f64) -> f64 { return x + y; }
146 pub fn sub(x: f64, y: f64) -> f64 { return x - y; }
149 pub fn mul(x: f64, y: f64) -> f64 { return x * y; }
152 pub fn div(x: f64, y: f64) -> f64 { return x / y; }
155 pub fn rem(x: f64, y: f64) -> f64 { return x % y; }
158 pub fn lt(x: f64, y: f64) -> bool { return x < y; }
161 pub fn le(x: f64, y: f64) -> bool { return x <= y; }
164 pub fn eq(x: f64, y: f64) -> bool { return x == y; }
167 pub fn ne(x: f64, y: f64) -> bool { return x != y; }
170 pub fn ge(x: f64, y: f64) -> bool { return x >= y; }
173 pub fn gt(x: f64, y: f64) -> bool { return x > y; }
176 // FIXME (#1999): add is_normal, is_subnormal, and fpclassify
180 // FIXME (requires Issue #1433 to fix): replace with mathematical
181 // constants from cmath.
182 /// Archimedes' constant
183 pub static pi: f64 = 3.14159265358979323846264338327950288_f64;
186 pub static frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
189 pub static frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
192 pub static frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
195 pub static frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
198 pub static frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
201 pub static sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
204 pub static frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
207 pub static e: f64 = 2.71828182845904523536028747135266250_f64;
210 pub static log2_e: f64 = 1.44269504088896340735992468100189214_f64;
213 pub static log10_e: f64 = 0.434294481903251827651128918916605082_f64;
216 pub static ln_2: f64 = 0.693147180559945309417232121458176568_f64;
219 pub static ln_10: f64 = 2.30258509299404568401799145468436421_f64;
227 fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
229 fn ne(&self, other: &f64) -> bool { (*self) != (*other) }
233 impl ApproxEq<f64> for f64 {
235 fn approx_epsilon() -> f64 { 1.0e-6 }
238 fn approx_eq(&self, other: &f64) -> bool {
239 self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<f64, f64>())
243 fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
244 (*self - *other).abs() < *approx_epsilon
251 fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
253 fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
255 fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
257 fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
260 impl Orderable for f64 {
261 /// Returns `NaN` if either of the numbers are `NaN`.
263 fn min(&self, other: &f64) -> f64 {
264 if self.is_NaN() || other.is_NaN() { Float::NaN() } else { fmin(*self, *other) }
267 /// Returns `NaN` if either of the numbers are `NaN`.
269 fn max(&self, other: &f64) -> f64 {
270 if self.is_NaN() || other.is_NaN() { Float::NaN() } else { fmax(*self, *other) }
273 /// Returns the number constrained within the range `mn <= self <= mx`.
274 /// If any of the numbers are `NaN` then `NaN` is returned.
276 fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
277 if self.is_NaN() { *self }
278 else if !(*self <= *mx) { *mx }
279 else if !(*self >= *mn) { *mn }
286 fn zero() -> f64 { 0.0 }
288 /// Returns true if the number is equal to either `0.0` or `-0.0`
290 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
295 fn one() -> f64 { 1.0 }
299 impl Add<f64,f64> for f64 {
300 fn add(&self, other: &f64) -> f64 { *self + *other }
303 impl Sub<f64,f64> for f64 {
304 fn sub(&self, other: &f64) -> f64 { *self - *other }
307 impl Mul<f64,f64> for f64 {
308 fn mul(&self, other: &f64) -> f64 { *self * *other }
311 impl Div<f64,f64> for f64 {
312 fn div(&self, other: &f64) -> f64 { *self / *other }
315 impl Rem<f64,f64> for f64 {
317 fn rem(&self, other: &f64) -> f64 { *self % *other }
320 impl Neg<f64> for f64 {
321 fn neg(&self) -> f64 { -*self }
324 impl Signed for f64 {
325 /// Computes the absolute value. Returns `NaN` if the number is `NaN`.
327 fn abs(&self) -> f64 { abs(*self) }
330 /// The positive difference of two numbers. Returns `0.0` if the number is less than or
331 /// equal to `other`, otherwise the difference between`self` and `other` is returned.
334 fn abs_sub(&self, other: &f64) -> f64 { abs_sub(*self, *other) }
339 /// - `1.0` if the number is positive, `+0.0` or `infinity`
340 /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
341 /// - `NaN` if the number is NaN
344 fn signum(&self) -> f64 {
345 if self.is_NaN() { NaN } else { copysign(1.0, *self) }
348 /// Returns `true` if the number is positive, including `+0.0` and `infinity`
350 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
352 /// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
354 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
358 /// Round half-way cases toward `neg_infinity`
360 fn floor(&self) -> f64 { floor(*self) }
362 /// Round half-way cases toward `infinity`
364 fn ceil(&self) -> f64 { ceil(*self) }
366 /// Round half-way cases away from `0.0`
368 fn round(&self) -> f64 { round(*self) }
370 /// The integer part of the number (rounds towards `0.0`)
372 fn trunc(&self) -> f64 { trunc(*self) }
375 /// The fractional part of the number, satisfying:
378 /// assert!(x == trunc(x) + fract(x))
382 fn fract(&self) -> f64 { *self - self.trunc() }
385 impl Fractional for f64 {
386 /// The reciprocal (multiplicative inverse) of the number
388 fn recip(&self) -> f64 { 1.0 / *self }
391 impl Algebraic for f64 {
393 fn pow(&self, n: f64) -> f64 { pow(*self, n) }
396 fn sqrt(&self) -> f64 { sqrt(*self) }
399 fn rsqrt(&self) -> f64 { self.sqrt().recip() }
402 fn cbrt(&self) -> f64 { cbrt(*self) }
405 fn hypot(&self, other: f64) -> f64 { hypot(*self, other) }
408 impl Trigonometric for f64 {
410 fn sin(&self) -> f64 { sin(*self) }
413 fn cos(&self) -> f64 { cos(*self) }
416 fn tan(&self) -> f64 { tan(*self) }
419 fn asin(&self) -> f64 { asin(*self) }
422 fn acos(&self) -> f64 { acos(*self) }
425 fn atan(&self) -> f64 { atan(*self) }
428 fn atan2(&self, other: f64) -> f64 { atan2(*self, other) }
430 /// Simultaneously computes the sine and cosine of the number
432 fn sin_cos(&self) -> (f64, f64) {
433 (self.sin(), self.cos())
437 impl Exponential for f64 {
438 /// Returns the exponential of the number
440 fn exp(&self) -> f64 { exp(*self) }
442 /// Returns 2 raised to the power of the number
444 fn exp2(&self) -> f64 { exp2(*self) }
446 /// Returns the natural logarithm of the number
448 fn ln(&self) -> f64 { ln(*self) }
450 /// Returns the logarithm of the number with respect to an arbitrary base
452 fn log(&self, base: f64) -> f64 { self.ln() / base.ln() }
454 /// Returns the base 2 logarithm of the number
456 fn log2(&self) -> f64 { log2(*self) }
458 /// Returns the base 10 logarithm of the number
460 fn log10(&self) -> f64 { log10(*self) }
463 impl Hyperbolic for f64 {
465 fn sinh(&self) -> f64 { sinh(*self) }
468 fn cosh(&self) -> f64 { cosh(*self) }
471 fn tanh(&self) -> f64 { tanh(*self) }
474 /// Inverse hyperbolic sine
478 /// - on success, the inverse hyperbolic sine of `self` will be returned
479 /// - `self` if `self` is `0.0`, `-0.0`, `infinity`, or `neg_infinity`
480 /// - `NaN` if `self` is `NaN`
483 fn asinh(&self) -> f64 {
485 neg_infinity => neg_infinity,
486 x => (x + ((x * x) + 1.0).sqrt()).ln(),
491 /// Inverse hyperbolic cosine
495 /// - on success, the inverse hyperbolic cosine of `self` will be returned
496 /// - `infinity` if `self` is `infinity`
497 /// - `NaN` if `self` is `NaN` or `self < 1.0` (including `neg_infinity`)
500 fn acosh(&self) -> f64 {
502 x if x < 1.0 => Float::NaN(),
503 x => (x + ((x * x) - 1.0).sqrt()).ln(),
508 /// Inverse hyperbolic tangent
512 /// - on success, the inverse hyperbolic tangent of `self` will be returned
513 /// - `self` if `self` is `0.0` or `-0.0`
514 /// - `infinity` if `self` is `1.0`
515 /// - `neg_infinity` if `self` is `-1.0`
516 /// - `NaN` if the `self` is `NaN` or outside the domain of `-1.0 <= self <= 1.0`
517 /// (including `infinity` and `neg_infinity`)
520 fn atanh(&self) -> f64 {
521 0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
526 /// Archimedes' constant
528 fn pi() -> f64 { 3.14159265358979323846264338327950288 }
532 fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
536 fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
540 fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
544 fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
548 fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
552 fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
556 fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
560 fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
564 fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
568 fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
572 fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
576 fn e() -> f64 { 2.71828182845904523536028747135266250 }
580 fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
584 fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
588 fn ln_2() -> f64 { 0.693147180559945309417232121458176568 }
592 fn ln_10() -> f64 { 2.30258509299404568401799145468436421 }
594 /// Converts to degrees, assuming the number is in radians
596 fn to_degrees(&self) -> f64 { *self * (180.0 / Real::pi::<f64>()) }
598 /// Converts to radians, assuming the number is in degrees
600 fn to_radians(&self) -> f64 { *self * (Real::pi::<f64>() / 180.0) }
603 impl RealExt for f64 {
605 fn lgamma(&self) -> (int, f64) {
607 let result = lgamma(*self, &mut sign);
608 (sign as int, result)
612 fn tgamma(&self) -> f64 { tgamma(*self) }
615 fn j0(&self) -> f64 { j0(*self) }
618 fn j1(&self) -> f64 { j1(*self) }
621 fn jn(&self, n: int) -> f64 { jn(n as c_int, *self) }
624 fn y0(&self) -> f64 { y0(*self) }
627 fn y1(&self) -> f64 { y1(*self) }
630 fn yn(&self, n: int) -> f64 { yn(n as c_int, *self) }
633 impl Bounded for f64 {
635 fn min_value() -> f64 { 2.2250738585072014e-308 }
638 fn max_value() -> f64 { 1.7976931348623157e+308 }
641 impl Primitive for f64 {
643 fn bits() -> uint { 64 }
646 fn bytes() -> uint { Primitive::bits::<f64>() / 8 }
651 fn NaN() -> f64 { 0.0 / 0.0 }
654 fn infinity() -> f64 { 1.0 / 0.0 }
657 fn neg_infinity() -> f64 { -1.0 / 0.0 }
660 fn neg_zero() -> f64 { -0.0 }
662 /// Returns `true` if the number is NaN
664 fn is_NaN(&self) -> bool { *self != *self }
666 /// Returns `true` if the number is infinite
668 fn is_infinite(&self) -> bool {
669 *self == Float::infinity() || *self == Float::neg_infinity()
672 /// Returns `true` if the number is neither infinite or NaN
674 fn is_finite(&self) -> bool {
675 !(self.is_NaN() || self.is_infinite())
678 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
680 fn is_normal(&self) -> bool {
681 self.classify() == FPNormal
684 /// Returns the floating point category of the number. If only one property is going to
685 /// be tested, it is generally faster to use the specific predicate instead.
686 fn classify(&self) -> FPCategory {
687 static EXP_MASK: u64 = 0x7ff0000000000000;
688 static MAN_MASK: u64 = 0x000fffffffffffff;
691 unsafe { ::cast::transmute::<f64,u64>(*self) } & MAN_MASK,
692 unsafe { ::cast::transmute::<f64,u64>(*self) } & EXP_MASK,
695 (_, 0) => FPSubnormal,
696 (0, EXP_MASK) => FPInfinite,
697 (_, EXP_MASK) => FPNaN,
703 fn mantissa_digits() -> uint { 53 }
706 fn digits() -> uint { 15 }
709 fn epsilon() -> f64 { 2.2204460492503131e-16 }
712 fn min_exp() -> int { -1021 }
715 fn max_exp() -> int { 1024 }
718 fn min_10_exp() -> int { -307 }
721 fn max_10_exp() -> int { 308 }
723 /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
725 fn ldexp(x: f64, exp: int) -> f64 {
726 ldexp(x, exp as c_int)
730 /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
732 /// - `self = x * pow(2, exp)`
733 /// - `0.5 <= abs(x) < 1.0`
736 fn frexp(&self) -> (f64, int) {
738 let x = frexp(*self, &mut exp);
743 /// Returns the exponential of the number, minus `1`, in a way that is accurate
744 /// even if the number is close to zero
747 fn exp_m1(&self) -> f64 { exp_m1(*self) }
750 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
751 /// than if the operations were performed separately
754 fn ln_1p(&self) -> f64 { ln_1p(*self) }
757 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
758 /// produces a more accurate result with better performance than a separate multiplication
759 /// operation followed by an add.
762 fn mul_add(&self, a: f64, b: f64) -> f64 {
766 /// Returns the next representable floating-point value in the direction of `other`
768 fn next_after(&self, other: f64) -> f64 {
769 next_after(*self, other)
774 // Section: String Conversions
778 /// Converts a float to a string
782 /// * num - The float value
785 pub fn to_str(num: f64) -> ~str {
786 let (r, _) = strconv::to_str_common(
787 &num, 10u, true, strconv::SignNeg, strconv::DigAll);
792 /// Converts a float to a string in hexadecimal format
796 /// * num - The float value
799 pub fn to_str_hex(num: f64) -> ~str {
800 let (r, _) = strconv::to_str_common(
801 &num, 16u, true, strconv::SignNeg, strconv::DigAll);
806 /// Converts a float to a string in a given radix
810 /// * num - The float value
811 /// * radix - The base to use
815 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
816 /// possible misinterpretation of the result at higher bases. If those values
817 /// are expected, use `to_str_radix_special()` instead.
820 pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
821 let (r, special) = strconv::to_str_common(
822 &num, rdx, true, strconv::SignNeg, strconv::DigAll);
823 if special { fail!("number has a special value, \
824 try to_str_radix_special() if those are expected") }
829 /// Converts a float to a string in a given radix, and a flag indicating
830 /// whether it's a special value
834 /// * num - The float value
835 /// * radix - The base to use
838 pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
839 strconv::to_str_common(&num, rdx, true,
840 strconv::SignNeg, strconv::DigAll)
844 /// Converts a float to a string with exactly the number of
845 /// provided significant digits
849 /// * num - The float value
850 /// * digits - The number of significant digits
853 pub fn to_str_exact(num: f64, dig: uint) -> ~str {
854 let (r, _) = strconv::to_str_common(
855 &num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
860 /// Converts a float to a string with a maximum number of
861 /// significant digits
865 /// * num - The float value
866 /// * digits - The number of significant digits
869 pub fn to_str_digits(num: f64, dig: uint) -> ~str {
870 let (r, _) = strconv::to_str_common(
871 &num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
875 impl to_str::ToStr for f64 {
877 fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
880 impl num::ToStrRadix for f64 {
882 fn to_str_radix(&self, rdx: uint) -> ~str {
883 to_str_radix(*self, rdx)
888 /// Convert a string in base 10 to a float.
889 /// Accepts a optional decimal exponent.
891 /// This function accepts strings such as
894 /// * '+3.14', equivalent to '3.14'
896 /// * '2.5E10', or equivalently, '2.5e10'
898 /// * '.' (understood as 0)
900 /// * '.5', or, equivalently, '0.5'
901 /// * '+inf', 'inf', '-inf', 'NaN'
903 /// Leading and trailing whitespace represent an error.
911 /// `none` if the string did not represent a valid number. Otherwise,
912 /// `Some(n)` where `n` is the floating-point number represented by `num`.
915 pub fn from_str(num: &str) -> Option<f64> {
916 strconv::from_str_common(num, 10u, true, true, true,
917 strconv::ExpDec, false, false)
921 /// Convert a string in base 16 to a float.
922 /// Accepts a optional binary exponent.
924 /// This function accepts strings such as
927 /// * '+a4.fe', equivalent to 'a4.fe'
929 /// * '2b.aP128', or equivalently, '2b.ap128'
931 /// * '.' (understood as 0)
933 /// * '.c', or, equivalently, '0.c'
934 /// * '+inf', 'inf', '-inf', 'NaN'
936 /// Leading and trailing whitespace represent an error.
944 /// `none` if the string did not represent a valid number. Otherwise,
945 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
948 pub fn from_str_hex(num: &str) -> Option<f64> {
949 strconv::from_str_common(num, 16u, true, true, true,
950 strconv::ExpBin, false, false)
954 /// Convert a string in an given base to a float.
956 /// Due to possible conflicts, this function does **not** accept
957 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
958 /// does it recognize exponents of any kind.
960 /// Leading and trailing whitespace represent an error.
965 /// * radix - The base to use. Must lie in the range [2 .. 36]
969 /// `none` if the string did not represent a valid number. Otherwise,
970 /// `Some(n)` where `n` is the floating-point number represented by `num`.
973 pub fn from_str_radix(num: &str, rdx: uint) -> Option<f64> {
974 strconv::from_str_common(num, rdx, true, true, false,
975 strconv::ExpNone, false, false)
978 impl FromStr for f64 {
980 fn from_str(val: &str) -> Option<f64> { from_str(val) }
983 impl num::FromStrRadix for f64 {
985 fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
986 from_str_radix(val, rdx)
999 num::test_num(10f64, 2f64);
1004 assert_eq!(1f64.min(&2f64), 1f64);
1005 assert_eq!(2f64.min(&1f64), 1f64);
1006 assert!(1f64.min(&Float::NaN::<f64>()).is_NaN());
1007 assert!(Float::NaN::<f64>().min(&1f64).is_NaN());
1012 assert_eq!(1f64.max(&2f64), 2f64);
1013 assert_eq!(2f64.max(&1f64), 2f64);
1014 assert!(1f64.max(&Float::NaN::<f64>()).is_NaN());
1015 assert!(Float::NaN::<f64>().max(&1f64).is_NaN());
1020 assert_eq!(1f64.clamp(&2f64, &4f64), 2f64);
1021 assert_eq!(8f64.clamp(&2f64, &4f64), 4f64);
1022 assert_eq!(3f64.clamp(&2f64, &4f64), 3f64);
1023 assert!(3f64.clamp(&Float::NaN::<f64>(), &4f64).is_NaN());
1024 assert!(3f64.clamp(&2f64, &Float::NaN::<f64>()).is_NaN());
1025 assert!(Float::NaN::<f64>().clamp(&2f64, &4f64).is_NaN());
1030 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1031 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1032 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1033 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1034 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1035 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1036 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1037 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1038 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1039 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1044 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1045 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1046 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1047 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1048 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1049 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1050 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1051 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1052 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1053 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1058 assert_approx_eq!(1.0f64.round(), 1.0f64);
1059 assert_approx_eq!(1.3f64.round(), 1.0f64);
1060 assert_approx_eq!(1.5f64.round(), 2.0f64);
1061 assert_approx_eq!(1.7f64.round(), 2.0f64);
1062 assert_approx_eq!(0.0f64.round(), 0.0f64);
1063 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1064 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1065 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1066 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1067 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1072 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1073 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1074 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1075 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1076 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1077 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1078 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1079 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1080 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1081 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1086 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1087 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1088 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1089 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1090 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1091 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1092 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1093 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1094 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1095 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1100 assert_eq!(0.0f64.asinh(), 0.0f64);
1101 assert_eq!((-0.0f64).asinh(), -0.0f64);
1102 assert_eq!(Float::infinity::<f64>().asinh(), Float::infinity::<f64>());
1103 assert_eq!(Float::neg_infinity::<f64>().asinh(), Float::neg_infinity::<f64>());
1104 assert!(Float::NaN::<f64>().asinh().is_NaN());
1105 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1106 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1111 assert_eq!(1.0f64.acosh(), 0.0f64);
1112 assert!(0.999f64.acosh().is_NaN());
1113 assert_eq!(Float::infinity::<f64>().acosh(), Float::infinity::<f64>());
1114 assert!(Float::neg_infinity::<f64>().acosh().is_NaN());
1115 assert!(Float::NaN::<f64>().acosh().is_NaN());
1116 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1117 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1122 assert_eq!(0.0f64.atanh(), 0.0f64);
1123 assert_eq!((-0.0f64).atanh(), -0.0f64);
1124 assert_eq!(1.0f64.atanh(), Float::infinity::<f64>());
1125 assert_eq!((-1.0f64).atanh(), Float::neg_infinity::<f64>());
1126 assert!(2f64.atanh().atanh().is_NaN());
1127 assert!((-2f64).atanh().atanh().is_NaN());
1128 assert!(Float::infinity::<f64>().atanh().is_NaN());
1129 assert!(Float::neg_infinity::<f64>().atanh().is_NaN());
1130 assert!(Float::NaN::<f64>().atanh().is_NaN());
1131 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1132 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1136 fn test_real_consts() {
1137 assert_approx_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());
1138 assert_approx_eq!(Real::frac_pi_2::<f64>(), Real::pi::<f64>() / 2f64);
1139 assert_approx_eq!(Real::frac_pi_3::<f64>(), Real::pi::<f64>() / 3f64);
1140 assert_approx_eq!(Real::frac_pi_4::<f64>(), Real::pi::<f64>() / 4f64);
1141 assert_approx_eq!(Real::frac_pi_6::<f64>(), Real::pi::<f64>() / 6f64);
1142 assert_approx_eq!(Real::frac_pi_8::<f64>(), Real::pi::<f64>() / 8f64);
1143 assert_approx_eq!(Real::frac_1_pi::<f64>(), 1f64 / Real::pi::<f64>());
1144 assert_approx_eq!(Real::frac_2_pi::<f64>(), 2f64 / Real::pi::<f64>());
1145 assert_approx_eq!(Real::frac_2_sqrtpi::<f64>(), 2f64 / Real::pi::<f64>().sqrt());
1146 assert_approx_eq!(Real::sqrt2::<f64>(), 2f64.sqrt());
1147 assert_approx_eq!(Real::frac_1_sqrt2::<f64>(), 1f64 / 2f64.sqrt());
1148 assert_approx_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
1149 assert_approx_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
1150 assert_approx_eq!(Real::ln_2::<f64>(), 2f64.ln());
1151 assert_approx_eq!(Real::ln_10::<f64>(), 10f64.ln());
1156 assert_eq!(infinity.abs(), infinity);
1157 assert_eq!(1f64.abs(), 1f64);
1158 assert_eq!(0f64.abs(), 0f64);
1159 assert_eq!((-0f64).abs(), 0f64);
1160 assert_eq!((-1f64).abs(), 1f64);
1161 assert_eq!(neg_infinity.abs(), infinity);
1162 assert_eq!((1f64/neg_infinity).abs(), 0f64);
1163 assert!(NaN.abs().is_NaN());
1168 assert_eq!((-1f64).abs_sub(&1f64), 0f64);
1169 assert_eq!(1f64.abs_sub(&1f64), 0f64);
1170 assert_eq!(1f64.abs_sub(&0f64), 1f64);
1171 assert_eq!(1f64.abs_sub(&-1f64), 2f64);
1172 assert_eq!(neg_infinity.abs_sub(&0f64), 0f64);
1173 assert_eq!(infinity.abs_sub(&1f64), infinity);
1174 assert_eq!(0f64.abs_sub(&neg_infinity), infinity);
1175 assert_eq!(0f64.abs_sub(&infinity), 0f64);
1176 assert!(NaN.abs_sub(&-1f64).is_NaN());
1177 assert!(1f64.abs_sub(&NaN).is_NaN());
1182 assert_eq!(infinity.signum(), 1f64);
1183 assert_eq!(1f64.signum(), 1f64);
1184 assert_eq!(0f64.signum(), 1f64);
1185 assert_eq!((-0f64).signum(), -1f64);
1186 assert_eq!((-1f64).signum(), -1f64);
1187 assert_eq!(neg_infinity.signum(), -1f64);
1188 assert_eq!((1f64/neg_infinity).signum(), -1f64);
1189 assert!(NaN.signum().is_NaN());
1193 fn test_is_positive() {
1194 assert!(infinity.is_positive());
1195 assert!(1f64.is_positive());
1196 assert!(0f64.is_positive());
1197 assert!(!(-0f64).is_positive());
1198 assert!(!(-1f64).is_positive());
1199 assert!(!neg_infinity.is_positive());
1200 assert!(!(1f64/neg_infinity).is_positive());
1201 assert!(!NaN.is_positive());
1205 fn test_is_negative() {
1206 assert!(!infinity.is_negative());
1207 assert!(!1f64.is_negative());
1208 assert!(!0f64.is_negative());
1209 assert!((-0f64).is_negative());
1210 assert!((-1f64).is_negative());
1211 assert!(neg_infinity.is_negative());
1212 assert!((1f64/neg_infinity).is_negative());
1213 assert!(!NaN.is_negative());
1217 fn test_approx_eq() {
1218 assert!(1.0f64.approx_eq(&1f64));
1219 assert!(0.9999999f64.approx_eq(&1f64));
1220 assert!(1.000001f64.approx_eq_eps(&1f64, &1.0e-5));
1221 assert!(1.0000001f64.approx_eq_eps(&1f64, &1.0e-6));
1222 assert!(!1.0000001f64.approx_eq_eps(&1f64, &1.0e-7));
1226 fn test_primitive() {
1227 assert_eq!(Primitive::bits::<f64>(), sys::size_of::<f64>() * 8);
1228 assert_eq!(Primitive::bytes::<f64>(), sys::size_of::<f64>());
1232 fn test_is_normal() {
1233 assert!(!Float::NaN::<f64>().is_normal());
1234 assert!(!Float::infinity::<f64>().is_normal());
1235 assert!(!Float::neg_infinity::<f64>().is_normal());
1236 assert!(!Zero::zero::<f64>().is_normal());
1237 assert!(!Float::neg_zero::<f64>().is_normal());
1238 assert!(1f64.is_normal());
1239 assert!(1e-307f64.is_normal());
1240 assert!(!1e-308f64.is_normal());
1244 fn test_classify() {
1245 assert_eq!(Float::NaN::<f64>().classify(), FPNaN);
1246 assert_eq!(Float::infinity::<f64>().classify(), FPInfinite);
1247 assert_eq!(Float::neg_infinity::<f64>().classify(), FPInfinite);
1248 assert_eq!(Zero::zero::<f64>().classify(), FPZero);
1249 assert_eq!(Float::neg_zero::<f64>().classify(), FPZero);
1250 assert_eq!(1e-307f64.classify(), FPNormal);
1251 assert_eq!(1e-308f64.classify(), FPSubnormal);
1256 // We have to use from_str until base-2 exponents
1257 // are supported in floating-point literals
1258 let f1: f64 = from_str_hex("1p-123").unwrap();
1259 let f2: f64 = from_str_hex("1p-111").unwrap();
1260 assert_eq!(Float::ldexp(1f64, -123), f1);
1261 assert_eq!(Float::ldexp(1f64, -111), f2);
1263 assert_eq!(Float::ldexp(0f64, -123), 0f64);
1264 assert_eq!(Float::ldexp(-0f64, -123), -0f64);
1265 assert_eq!(Float::ldexp(Float::infinity::<f64>(), -123),
1266 Float::infinity::<f64>());
1267 assert_eq!(Float::ldexp(Float::neg_infinity::<f64>(), -123),
1268 Float::neg_infinity::<f64>());
1269 assert!(Float::ldexp(Float::NaN::<f64>(), -123).is_NaN());
1274 // We have to use from_str until base-2 exponents
1275 // are supported in floating-point literals
1276 let f1: f64 = from_str_hex("1p-123").unwrap();
1277 let f2: f64 = from_str_hex("1p-111").unwrap();
1278 let (x1, exp1) = f1.frexp();
1279 let (x2, exp2) = f2.frexp();
1280 assert_eq!((x1, exp1), (0.5f64, -122));
1281 assert_eq!((x2, exp2), (0.5f64, -110));
1282 assert_eq!(Float::ldexp(x1, exp1), f1);
1283 assert_eq!(Float::ldexp(x2, exp2), f2);
1285 assert_eq!(0f64.frexp(), (0f64, 0));
1286 assert_eq!((-0f64).frexp(), (-0f64, 0));
1287 assert_eq!(match Float::infinity::<f64>().frexp() { (x, _) => x },
1288 Float::infinity::<f64>())
1289 assert_eq!(match Float::neg_infinity::<f64>().frexp() { (x, _) => x },
1290 Float::neg_infinity::<f64>())
1291 assert!(match Float::NaN::<f64>().frexp() { (x, _) => x.is_NaN() })