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`
15 use num::{Zero, One, strconv};
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 expm1(n: c_double) -> c_double = c_double_utils::expm1,
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 ln1p(n: c_double) -> c_double = c_double_utils::ln1p,
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;
223 pub fn logarithm(n: f64, b: f64) -> f64 {
224 return log2(n) / log2(b);
232 fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
234 fn ne(&self, other: &f64) -> bool { (*self) != (*other) }
240 fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
242 fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
244 fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
246 fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
249 impl Orderable for f64 {
250 /// Returns `NaN` if either of the numbers are `NaN`.
252 fn min(&self, other: &f64) -> f64 {
253 if self.is_NaN() || other.is_NaN() { Float::NaN() } else { fmin(*self, *other) }
256 /// Returns `NaN` if either of the numbers are `NaN`.
258 fn max(&self, other: &f64) -> f64 {
259 if self.is_NaN() || other.is_NaN() { Float::NaN() } else { fmax(*self, *other) }
262 /// Returns the number constrained within the range `mn <= self <= mx`.
263 /// If any of the numbers are `NaN` then `NaN` is returned.
265 fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
266 if self.is_NaN() { *self }
267 else if !(*self <= *mx) { *mx }
268 else if !(*self >= *mn) { *mn }
275 fn zero() -> f64 { 0.0 }
277 /// Returns true if the number is equal to either `0.0` or `-0.0`
279 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
284 fn one() -> f64 { 1.0 }
288 impl Add<f64,f64> for f64 {
289 fn add(&self, other: &f64) -> f64 { *self + *other }
292 impl Sub<f64,f64> for f64 {
293 fn sub(&self, other: &f64) -> f64 { *self - *other }
296 impl Mul<f64,f64> for f64 {
297 fn mul(&self, other: &f64) -> f64 { *self * *other }
300 impl Div<f64,f64> for f64 {
301 fn div(&self, other: &f64) -> f64 { *self / *other }
303 #[cfg(stage0,notest)]
304 impl Modulo<f64,f64> for f64 {
305 fn modulo(&self, other: &f64) -> f64 { *self % *other }
307 #[cfg(not(stage0),notest)]
308 impl Rem<f64,f64> for f64 {
310 fn rem(&self, other: &f64) -> f64 { *self % *other }
313 impl Neg<f64> for f64 {
314 fn neg(&self) -> f64 { -*self }
317 impl Signed for f64 {
318 /// Computes the absolute value. Returns `NaN` if the number is `NaN`.
320 fn abs(&self) -> f64 { abs(*self) }
325 /// - `1.0` if the number is positive, `+0.0` or `infinity`
326 /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
327 /// - `NaN` if the number is NaN
330 fn signum(&self) -> f64 {
331 if self.is_NaN() { NaN } else { copysign(1.0, *self) }
334 /// Returns `true` if the number is positive, including `+0.0` and `infinity`
336 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
338 /// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
340 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
344 /// Round half-way cases toward `neg_infinity`
346 fn floor(&self) -> f64 { floor(*self) }
348 /// Round half-way cases toward `infinity`
350 fn ceil(&self) -> f64 { ceil(*self) }
352 /// Round half-way cases away from `0.0`
354 fn round(&self) -> f64 { round(*self) }
356 /// The integer part of the number (rounds towards `0.0`)
358 fn trunc(&self) -> f64 { trunc(*self) }
361 /// The fractional part of the number, satisfying:
364 /// assert!(x == trunc(x) + fract(x))
368 fn fract(&self) -> f64 { *self - self.trunc() }
371 impl Fractional for f64 {
372 /// The reciprocal (multiplicative inverse) of the number
374 fn recip(&self) -> f64 { 1.0 / *self }
377 impl Algebraic for f64 {
379 fn pow(&self, n: f64) -> f64 { pow(*self, n) }
382 fn sqrt(&self) -> f64 { sqrt(*self) }
385 fn rsqrt(&self) -> f64 { self.sqrt().recip() }
388 fn cbrt(&self) -> f64 { cbrt(*self) }
391 fn hypot(&self, other: f64) -> f64 { hypot(*self, other) }
394 impl Trigonometric for f64 {
396 fn sin(&self) -> f64 { sin(*self) }
399 fn cos(&self) -> f64 { cos(*self) }
402 fn tan(&self) -> f64 { tan(*self) }
405 fn asin(&self) -> f64 { asin(*self) }
408 fn acos(&self) -> f64 { acos(*self) }
411 fn atan(&self) -> f64 { atan(*self) }
414 fn atan2(&self, other: f64) -> f64 { atan2(*self, other) }
417 impl Exponential for f64 {
419 fn exp(&self) -> f64 { exp(*self) }
422 fn exp2(&self) -> f64 { exp2(*self) }
425 fn expm1(&self) -> f64 { expm1(*self) }
428 fn log(&self) -> f64 { ln(*self) }
431 fn log2(&self) -> f64 { log2(*self) }
434 fn log10(&self) -> f64 { log10(*self) }
437 impl Hyperbolic for f64 {
439 fn sinh(&self) -> f64 { sinh(*self) }
442 fn cosh(&self) -> f64 { cosh(*self) }
445 fn tanh(&self) -> f64 { tanh(*self) }
449 /// Archimedes' constant
451 fn pi() -> f64 { 3.14159265358979323846264338327950288 }
455 fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
459 fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
463 fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
467 fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
471 fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
475 fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
479 fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
483 fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
487 fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
491 fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
495 fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
499 fn e() -> f64 { 2.71828182845904523536028747135266250 }
503 fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
507 fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
511 fn log_2() -> f64 { 0.693147180559945309417232121458176568 }
515 fn log_10() -> f64 { 2.30258509299404568401799145468436421 }
517 /// Converts to degrees, assuming the number is in radians
519 fn to_degrees(&self) -> f64 { *self * (180.0 / Real::pi::<f64>()) }
521 /// Converts to radians, assuming the number is in degrees
523 fn to_radians(&self) -> f64 { *self * (Real::pi::<f64>() / 180.0) }
526 impl RealExt for f64 {
528 fn lgamma(&self) -> (int, f64) {
530 let result = lgamma(*self, &mut sign);
531 (sign as int, result)
535 fn tgamma(&self) -> f64 { tgamma(*self) }
538 fn j0(&self) -> f64 { j0(*self) }
541 fn j1(&self) -> f64 { j1(*self) }
544 fn jn(&self, n: int) -> f64 { jn(n as c_int, *self) }
547 fn y0(&self) -> f64 { y0(*self) }
550 fn y1(&self) -> f64 { y1(*self) }
553 fn yn(&self, n: int) -> f64 { yn(n as c_int, *self) }
556 impl Bounded for f64 {
558 fn min_value() -> f64 { 2.2250738585072014e-308 }
561 fn max_value() -> f64 { 1.7976931348623157e+308 }
564 impl Primitive for f64 {
566 fn bits() -> uint { 64 }
569 fn bytes() -> uint { Primitive::bits::<f64>() / 8 }
574 fn NaN() -> f64 { 0.0 / 0.0 }
577 fn infinity() -> f64 { 1.0 / 0.0 }
580 fn neg_infinity() -> f64 { -1.0 / 0.0 }
583 fn neg_zero() -> f64 { -0.0 }
586 fn is_NaN(&self) -> bool { *self != *self }
588 /// Returns `true` if the number is infinite
590 fn is_infinite(&self) -> bool {
591 *self == Float::infinity() || *self == Float::neg_infinity()
594 /// Returns `true` if the number is finite
596 fn is_finite(&self) -> bool {
597 !(self.is_NaN() || self.is_infinite())
601 fn mantissa_digits() -> uint { 53 }
604 fn digits() -> uint { 15 }
607 fn epsilon() -> f64 { 2.2204460492503131e-16 }
610 fn min_exp() -> int { -1021 }
613 fn max_exp() -> int { 1024 }
616 fn min_10_exp() -> int { -307 }
619 fn max_10_exp() -> int { 308 }
622 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
623 /// produces a more accurate result with better performance than a separate multiplication
624 /// operation followed by an add.
627 fn mul_add(&self, a: f64, b: f64) -> f64 {
631 /// Returns the next representable floating-point value in the direction of `other`
633 fn next_after(&self, other: f64) -> f64 {
634 next_after(*self, other)
639 // Section: String Conversions
643 /// Converts a float to a string
647 /// * num - The float value
650 pub fn to_str(num: f64) -> ~str {
651 let (r, _) = strconv::to_str_common(
652 &num, 10u, true, strconv::SignNeg, strconv::DigAll);
657 /// Converts a float to a string in hexadecimal format
661 /// * num - The float value
664 pub fn to_str_hex(num: f64) -> ~str {
665 let (r, _) = strconv::to_str_common(
666 &num, 16u, true, strconv::SignNeg, strconv::DigAll);
671 /// Converts a float to a string in a given radix
675 /// * num - The float value
676 /// * radix - The base to use
680 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
681 /// possible misinterpretation of the result at higher bases. If those values
682 /// are expected, use `to_str_radix_special()` instead.
685 pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
686 let (r, special) = strconv::to_str_common(
687 &num, rdx, true, strconv::SignNeg, strconv::DigAll);
688 if special { fail!(~"number has a special value, \
689 try to_str_radix_special() if those are expected") }
694 /// Converts a float to a string in a given radix, and a flag indicating
695 /// whether it's a special value
699 /// * num - The float value
700 /// * radix - The base to use
703 pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
704 strconv::to_str_common(&num, rdx, true,
705 strconv::SignNeg, strconv::DigAll)
709 /// Converts a float to a string with exactly the number of
710 /// provided significant digits
714 /// * num - The float value
715 /// * digits - The number of significant digits
718 pub fn to_str_exact(num: f64, dig: uint) -> ~str {
719 let (r, _) = strconv::to_str_common(
720 &num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
725 /// Converts a float to a string with a maximum number of
726 /// significant digits
730 /// * num - The float value
731 /// * digits - The number of significant digits
734 pub fn to_str_digits(num: f64, dig: uint) -> ~str {
735 let (r, _) = strconv::to_str_common(
736 &num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
740 impl to_str::ToStr for f64 {
742 fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
745 impl num::ToStrRadix for f64 {
747 fn to_str_radix(&self, rdx: uint) -> ~str {
748 to_str_radix(*self, rdx)
753 /// Convert a string in base 10 to a float.
754 /// Accepts a optional decimal exponent.
756 /// This function accepts strings such as
759 /// * '+3.14', equivalent to '3.14'
761 /// * '2.5E10', or equivalently, '2.5e10'
763 /// * '.' (understood as 0)
765 /// * '.5', or, equivalently, '0.5'
766 /// * '+inf', 'inf', '-inf', 'NaN'
768 /// Leading and trailing whitespace represent an error.
776 /// `none` if the string did not represent a valid number. Otherwise,
777 /// `Some(n)` where `n` is the floating-point number represented by `num`.
780 pub fn from_str(num: &str) -> Option<f64> {
781 strconv::from_str_common(num, 10u, true, true, true,
782 strconv::ExpDec, false, false)
786 /// Convert a string in base 16 to a float.
787 /// Accepts a optional binary exponent.
789 /// This function accepts strings such as
792 /// * '+a4.fe', equivalent to 'a4.fe'
794 /// * '2b.aP128', or equivalently, '2b.ap128'
796 /// * '.' (understood as 0)
798 /// * '.c', or, equivalently, '0.c'
799 /// * '+inf', 'inf', '-inf', 'NaN'
801 /// Leading and trailing whitespace represent an error.
809 /// `none` if the string did not represent a valid number. Otherwise,
810 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
813 pub fn from_str_hex(num: &str) -> Option<f64> {
814 strconv::from_str_common(num, 16u, true, true, true,
815 strconv::ExpBin, false, false)
819 /// Convert a string in an given base to a float.
821 /// Due to possible conflicts, this function does **not** accept
822 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
823 /// does it recognize exponents of any kind.
825 /// Leading and trailing whitespace represent an error.
830 /// * radix - The base to use. Must lie in the range [2 .. 36]
834 /// `none` if the string did not represent a valid number. Otherwise,
835 /// `Some(n)` where `n` is the floating-point number represented by `num`.
838 pub fn from_str_radix(num: &str, rdx: uint) -> Option<f64> {
839 strconv::from_str_common(num, rdx, true, true, false,
840 strconv::ExpNone, false, false)
843 impl from_str::FromStr for f64 {
845 fn from_str(val: &str) -> Option<f64> { from_str(val) }
848 impl num::FromStrRadix for f64 {
850 fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
851 from_str_radix(val, rdx)
861 macro_rules! assert_fuzzy_eq(
862 ($a:expr, $b:expr) => ({
864 if !((a - b).abs() < 1.0e-6) {
865 fail!(fmt!("The values were not approximately equal. \
866 Found: %? and expected %?", a, b));
873 num::test_num(10f64, 2f64);
878 assert_eq!(1f64.min(&2f64), 1f64);
879 assert_eq!(2f64.min(&1f64), 1f64);
880 assert!(1f64.min(&Float::NaN::<f64>()).is_NaN());
881 assert!(Float::NaN::<f64>().min(&1f64).is_NaN());
886 assert_eq!(1f64.max(&2f64), 2f64);
887 assert_eq!(2f64.max(&1f64), 2f64);
888 assert!(1f64.max(&Float::NaN::<f64>()).is_NaN());
889 assert!(Float::NaN::<f64>().max(&1f64).is_NaN());
894 assert_eq!(1f64.clamp(&2f64, &4f64), 2f64);
895 assert_eq!(8f64.clamp(&2f64, &4f64), 4f64);
896 assert_eq!(3f64.clamp(&2f64, &4f64), 3f64);
897 assert!(3f64.clamp(&Float::NaN::<f64>(), &4f64).is_NaN());
898 assert!(3f64.clamp(&2f64, &Float::NaN::<f64>()).is_NaN());
899 assert!(Float::NaN::<f64>().clamp(&2f64, &4f64).is_NaN());
904 assert_fuzzy_eq!(1.0f64.floor(), 1.0f64);
905 assert_fuzzy_eq!(1.3f64.floor(), 1.0f64);
906 assert_fuzzy_eq!(1.5f64.floor(), 1.0f64);
907 assert_fuzzy_eq!(1.7f64.floor(), 1.0f64);
908 assert_fuzzy_eq!(0.0f64.floor(), 0.0f64);
909 assert_fuzzy_eq!((-0.0f64).floor(), -0.0f64);
910 assert_fuzzy_eq!((-1.0f64).floor(), -1.0f64);
911 assert_fuzzy_eq!((-1.3f64).floor(), -2.0f64);
912 assert_fuzzy_eq!((-1.5f64).floor(), -2.0f64);
913 assert_fuzzy_eq!((-1.7f64).floor(), -2.0f64);
918 assert_fuzzy_eq!(1.0f64.ceil(), 1.0f64);
919 assert_fuzzy_eq!(1.3f64.ceil(), 2.0f64);
920 assert_fuzzy_eq!(1.5f64.ceil(), 2.0f64);
921 assert_fuzzy_eq!(1.7f64.ceil(), 2.0f64);
922 assert_fuzzy_eq!(0.0f64.ceil(), 0.0f64);
923 assert_fuzzy_eq!((-0.0f64).ceil(), -0.0f64);
924 assert_fuzzy_eq!((-1.0f64).ceil(), -1.0f64);
925 assert_fuzzy_eq!((-1.3f64).ceil(), -1.0f64);
926 assert_fuzzy_eq!((-1.5f64).ceil(), -1.0f64);
927 assert_fuzzy_eq!((-1.7f64).ceil(), -1.0f64);
932 assert_fuzzy_eq!(1.0f64.round(), 1.0f64);
933 assert_fuzzy_eq!(1.3f64.round(), 1.0f64);
934 assert_fuzzy_eq!(1.5f64.round(), 2.0f64);
935 assert_fuzzy_eq!(1.7f64.round(), 2.0f64);
936 assert_fuzzy_eq!(0.0f64.round(), 0.0f64);
937 assert_fuzzy_eq!((-0.0f64).round(), -0.0f64);
938 assert_fuzzy_eq!((-1.0f64).round(), -1.0f64);
939 assert_fuzzy_eq!((-1.3f64).round(), -1.0f64);
940 assert_fuzzy_eq!((-1.5f64).round(), -2.0f64);
941 assert_fuzzy_eq!((-1.7f64).round(), -2.0f64);
946 assert_fuzzy_eq!(1.0f64.trunc(), 1.0f64);
947 assert_fuzzy_eq!(1.3f64.trunc(), 1.0f64);
948 assert_fuzzy_eq!(1.5f64.trunc(), 1.0f64);
949 assert_fuzzy_eq!(1.7f64.trunc(), 1.0f64);
950 assert_fuzzy_eq!(0.0f64.trunc(), 0.0f64);
951 assert_fuzzy_eq!((-0.0f64).trunc(), -0.0f64);
952 assert_fuzzy_eq!((-1.0f64).trunc(), -1.0f64);
953 assert_fuzzy_eq!((-1.3f64).trunc(), -1.0f64);
954 assert_fuzzy_eq!((-1.5f64).trunc(), -1.0f64);
955 assert_fuzzy_eq!((-1.7f64).trunc(), -1.0f64);
960 assert_fuzzy_eq!(1.0f64.fract(), 0.0f64);
961 assert_fuzzy_eq!(1.3f64.fract(), 0.3f64);
962 assert_fuzzy_eq!(1.5f64.fract(), 0.5f64);
963 assert_fuzzy_eq!(1.7f64.fract(), 0.7f64);
964 assert_fuzzy_eq!(0.0f64.fract(), 0.0f64);
965 assert_fuzzy_eq!((-0.0f64).fract(), -0.0f64);
966 assert_fuzzy_eq!((-1.0f64).fract(), -0.0f64);
967 assert_fuzzy_eq!((-1.3f64).fract(), -0.3f64);
968 assert_fuzzy_eq!((-1.5f64).fract(), -0.5f64);
969 assert_fuzzy_eq!((-1.7f64).fract(), -0.7f64);
973 fn test_real_consts() {
974 assert_fuzzy_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());
975 assert_fuzzy_eq!(Real::frac_pi_2::<f64>(), Real::pi::<f64>() / 2f64);
976 assert_fuzzy_eq!(Real::frac_pi_3::<f64>(), Real::pi::<f64>() / 3f64);
977 assert_fuzzy_eq!(Real::frac_pi_4::<f64>(), Real::pi::<f64>() / 4f64);
978 assert_fuzzy_eq!(Real::frac_pi_6::<f64>(), Real::pi::<f64>() / 6f64);
979 assert_fuzzy_eq!(Real::frac_pi_8::<f64>(), Real::pi::<f64>() / 8f64);
980 assert_fuzzy_eq!(Real::frac_1_pi::<f64>(), 1f64 / Real::pi::<f64>());
981 assert_fuzzy_eq!(Real::frac_2_pi::<f64>(), 2f64 / Real::pi::<f64>());
982 assert_fuzzy_eq!(Real::frac_2_sqrtpi::<f64>(), 2f64 / Real::pi::<f64>().sqrt());
983 assert_fuzzy_eq!(Real::sqrt2::<f64>(), 2f64.sqrt());
984 assert_fuzzy_eq!(Real::frac_1_sqrt2::<f64>(), 1f64 / 2f64.sqrt());
985 assert_fuzzy_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
986 assert_fuzzy_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
987 assert_fuzzy_eq!(Real::log_2::<f64>(), 2f64.log());
988 assert_fuzzy_eq!(Real::log_10::<f64>(), 10f64.log());
992 pub fn test_signed() {
993 assert_eq!(infinity.abs(), infinity);
994 assert_eq!(1f64.abs(), 1f64);
995 assert_eq!(0f64.abs(), 0f64);
996 assert_eq!((-0f64).abs(), 0f64);
997 assert_eq!((-1f64).abs(), 1f64);
998 assert_eq!(neg_infinity.abs(), infinity);
999 assert_eq!((1f64/neg_infinity).abs(), 0f64);
1000 assert!(NaN.abs().is_NaN());
1002 assert_eq!(infinity.signum(), 1f64);
1003 assert_eq!(1f64.signum(), 1f64);
1004 assert_eq!(0f64.signum(), 1f64);
1005 assert_eq!((-0f64).signum(), -1f64);
1006 assert_eq!((-1f64).signum(), -1f64);
1007 assert_eq!(neg_infinity.signum(), -1f64);
1008 assert_eq!((1f64/neg_infinity).signum(), -1f64);
1009 assert!(NaN.signum().is_NaN());
1011 assert!(infinity.is_positive());
1012 assert!(1f64.is_positive());
1013 assert!(0f64.is_positive());
1014 assert!(!(-0f64).is_positive());
1015 assert!(!(-1f64).is_positive());
1016 assert!(!neg_infinity.is_positive());
1017 assert!(!(1f64/neg_infinity).is_positive());
1018 assert!(!NaN.is_positive());
1020 assert!(!infinity.is_negative());
1021 assert!(!1f64.is_negative());
1022 assert!(!0f64.is_negative());
1023 assert!((-0f64).is_negative());
1024 assert!((-1f64).is_negative());
1025 assert!(neg_infinity.is_negative());
1026 assert!((1f64/neg_infinity).is_negative());
1027 assert!(!NaN.is_negative());
1031 fn test_primitive() {
1032 assert_eq!(Primitive::bits::<f64>(), sys::size_of::<f64>() * 8);
1033 assert_eq!(Primitive::bytes::<f64>(), sys::size_of::<f64>());
1041 // indent-tabs-mode: nil
1042 // c-basic-offset: 4
1043 // buffer-file-coding-system: utf-8-unix