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 `f32`
12 #[allow(missing_doc)];
13 #[allow(non_uppercase_statics)];
16 use num::{Zero, One, strconv};
17 use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
22 pub use cmath::c_float_targ_consts::*;
24 use self::delegated::*;
26 macro_rules! delegate(
31 $arg:ident : $arg_ty:ty
33 ) -> $rv:ty = $bound_name:path
36 // An inner module is required to get the #[inline] attribute on the
39 use cmath::c_float_utils;
40 use libc::{c_float, c_int};
41 use unstable::intrinsics;
45 pub fn $name($( $arg : $arg_ty ),*) -> $rv {
47 $bound_name($( $arg ),*)
57 fn abs(n: f32) -> f32 = intrinsics::fabsf32,
58 fn cos(n: f32) -> f32 = intrinsics::cosf32,
59 fn exp(n: f32) -> f32 = intrinsics::expf32,
60 fn exp2(n: f32) -> f32 = intrinsics::exp2f32,
61 fn floor(x: f32) -> f32 = intrinsics::floorf32,
62 fn ln(n: f32) -> f32 = intrinsics::logf32,
63 fn log10(n: f32) -> f32 = intrinsics::log10f32,
64 fn log2(n: f32) -> f32 = intrinsics::log2f32,
65 fn mul_add(a: f32, b: f32, c: f32) -> f32 = intrinsics::fmaf32,
66 fn pow(n: f32, e: f32) -> f32 = intrinsics::powf32,
67 fn powi(n: f32, e: c_int) -> f32 = intrinsics::powif32,
68 fn sin(n: f32) -> f32 = intrinsics::sinf32,
69 fn sqrt(n: f32) -> f32 = intrinsics::sqrtf32,
71 // LLVM 3.3 required to use intrinsics for these four
72 fn ceil(n: c_float) -> c_float = c_float_utils::ceil,
73 fn trunc(n: c_float) -> c_float = c_float_utils::trunc,
75 fn ceil(n: f32) -> f32 = intrinsics::ceilf32,
76 fn trunc(n: f32) -> f32 = intrinsics::truncf32,
77 fn rint(n: f32) -> f32 = intrinsics::rintf32,
78 fn nearbyint(n: f32) -> f32 = intrinsics::nearbyintf32,
82 fn acos(n: c_float) -> c_float = c_float_utils::acos,
83 fn asin(n: c_float) -> c_float = c_float_utils::asin,
84 fn atan(n: c_float) -> c_float = c_float_utils::atan,
85 fn atan2(a: c_float, b: c_float) -> c_float = c_float_utils::atan2,
86 fn cbrt(n: c_float) -> c_float = c_float_utils::cbrt,
87 fn copysign(x: c_float, y: c_float) -> c_float = c_float_utils::copysign,
88 fn cosh(n: c_float) -> c_float = c_float_utils::cosh,
89 fn erf(n: c_float) -> c_float = c_float_utils::erf,
90 fn erfc(n: c_float) -> c_float = c_float_utils::erfc,
91 fn exp_m1(n: c_float) -> c_float = c_float_utils::exp_m1,
92 fn abs_sub(a: c_float, b: c_float) -> c_float = c_float_utils::abs_sub,
93 fn next_after(x: c_float, y: c_float) -> c_float = c_float_utils::next_after,
94 fn frexp(n: c_float, value: &mut c_int) -> c_float = c_float_utils::frexp,
95 fn hypot(x: c_float, y: c_float) -> c_float = c_float_utils::hypot,
96 fn ldexp(x: c_float, n: c_int) -> c_float = c_float_utils::ldexp,
97 fn lgamma(n: c_float, sign: &mut c_int) -> c_float = c_float_utils::lgamma,
98 fn log_radix(n: c_float) -> c_float = c_float_utils::log_radix,
99 fn ln_1p(n: c_float) -> c_float = c_float_utils::ln_1p,
100 fn ilog_radix(n: c_float) -> c_int = c_float_utils::ilog_radix,
101 fn modf(n: c_float, iptr: &mut c_float) -> c_float = c_float_utils::modf,
102 fn round(n: c_float) -> c_float = c_float_utils::round,
103 fn ldexp_radix(n: c_float, i: c_int) -> c_float = c_float_utils::ldexp_radix,
104 fn sinh(n: c_float) -> c_float = c_float_utils::sinh,
105 fn tan(n: c_float) -> c_float = c_float_utils::tan,
106 fn tanh(n: c_float) -> c_float = c_float_utils::tanh,
107 fn tgamma(n: c_float) -> c_float = c_float_utils::tgamma
110 // These are not defined inside consts:: for consistency with
113 pub static NaN: f32 = 0.0_f32/0.0_f32;
115 pub static infinity: f32 = 1.0_f32/0.0_f32;
117 pub static neg_infinity: f32 = -1.0_f32/0.0_f32;
119 // FIXME (#1999): replace the predicates below with llvm intrinsics or
120 // calls to the libmath macros in the rust runtime for performance.
122 // FIXME (#1999): add is_normal, is_subnormal, and fpclassify.
126 // FIXME (requires Issue #1433 to fix): replace with mathematical
127 // staticants from cmath.
128 /// Archimedes' constant
129 pub static pi: f32 = 3.14159265358979323846264338327950288_f32;
132 pub static frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32;
135 pub static frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32;
138 pub static frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32;
141 pub static frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32;
144 pub static frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32;
147 pub static sqrt2: f32 = 1.41421356237309504880168872420969808_f32;
150 pub static frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32;
153 pub static e: f32 = 2.71828182845904523536028747135266250_f32;
156 pub static log2_e: f32 = 1.44269504088896340735992468100189214_f32;
159 pub static log10_e: f32 = 0.434294481903251827651128918916605082_f32;
162 pub static ln_2: f32 = 0.693147180559945309417232121458176568_f32;
165 pub static ln_10: f32 = 2.30258509299404568401799145468436421_f32;
173 fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
175 fn ne(&self, other: &f32) -> bool { (*self) != (*other) }
179 impl ApproxEq<f32> for f32 {
181 fn approx_epsilon() -> f32 { 1.0e-6 }
184 fn approx_eq(&self, other: &f32) -> bool {
185 self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<f32, f32>())
189 fn approx_eq_eps(&self, other: &f32, approx_epsilon: &f32) -> bool {
190 (*self - *other).abs() < *approx_epsilon
197 fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
199 fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
201 fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
203 fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
206 impl Orderable for f32 {
207 /// Returns `NaN` if either of the numbers are `NaN`.
209 fn min(&self, other: &f32) -> f32 {
211 (self.is_NaN()) { *self }
212 (other.is_NaN()) { *other }
213 (*self < *other) { *self }
218 /// Returns `NaN` if either of the numbers are `NaN`.
220 fn max(&self, other: &f32) -> f32 {
222 (self.is_NaN()) { *self }
223 (other.is_NaN()) { *other }
224 (*self > *other) { *self }
229 /// Returns the number constrained within the range `mn <= self <= mx`.
230 /// If any of the numbers are `NaN` then `NaN` is returned.
232 fn clamp(&self, mn: &f32, mx: &f32) -> f32 {
234 (self.is_NaN()) { *self }
235 (!(*self <= *mx)) { *mx }
236 (!(*self >= *mn)) { *mn }
244 fn zero() -> f32 { 0.0 }
246 /// Returns true if the number is equal to either `0.0` or `-0.0`
248 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
253 fn one() -> f32 { 1.0 }
257 impl Add<f32,f32> for f32 {
259 fn add(&self, other: &f32) -> f32 { *self + *other }
263 impl Sub<f32,f32> for f32 {
265 fn sub(&self, other: &f32) -> f32 { *self - *other }
269 impl Mul<f32,f32> for f32 {
271 fn mul(&self, other: &f32) -> f32 { *self * *other }
275 impl Div<f32,f32> for f32 {
277 fn div(&self, other: &f32) -> f32 { *self / *other }
281 impl Rem<f32,f32> for f32 {
283 fn rem(&self, other: &f32) -> f32 { *self % *other }
287 impl Neg<f32> for f32 {
289 fn neg(&self) -> f32 { -*self }
292 impl Signed for f32 {
293 /// Computes the absolute value. Returns `NaN` if the number is `NaN`.
295 fn abs(&self) -> f32 { abs(*self) }
298 /// The positive difference of two numbers. Returns `0.0` if the number is less than or
299 /// equal to `other`, otherwise the difference between`self` and `other` is returned.
302 fn abs_sub(&self, other: &f32) -> f32 { abs_sub(*self, *other) }
307 /// - `1.0` if the number is positive, `+0.0` or `infinity`
308 /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
309 /// - `NaN` if the number is NaN
312 fn signum(&self) -> f32 {
313 if self.is_NaN() { NaN } else { copysign(1.0, *self) }
316 /// Returns `true` if the number is positive, including `+0.0` and `infinity`
318 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
320 /// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
322 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
326 /// Round half-way cases toward `neg_infinity`
328 fn floor(&self) -> f32 { floor(*self) }
330 /// Round half-way cases toward `infinity`
332 fn ceil(&self) -> f32 { ceil(*self) }
334 /// Round half-way cases away from `0.0`
336 fn round(&self) -> f32 { round(*self) }
338 /// The integer part of the number (rounds towards `0.0`)
340 fn trunc(&self) -> f32 { trunc(*self) }
343 /// The fractional part of the number, satisfying:
346 /// assert!(x == trunc(x) + fract(x))
350 fn fract(&self) -> f32 { *self - self.trunc() }
353 impl Fractional for f32 {
354 /// The reciprocal (multiplicative inverse) of the number
356 fn recip(&self) -> f32 { 1.0 / *self }
359 impl Algebraic for f32 {
361 fn pow(&self, n: &f32) -> f32 { pow(*self, *n) }
364 fn sqrt(&self) -> f32 { sqrt(*self) }
367 fn rsqrt(&self) -> f32 { self.sqrt().recip() }
370 fn cbrt(&self) -> f32 { cbrt(*self) }
373 fn hypot(&self, other: &f32) -> f32 { hypot(*self, *other) }
376 impl Trigonometric for f32 {
378 fn sin(&self) -> f32 { sin(*self) }
381 fn cos(&self) -> f32 { cos(*self) }
384 fn tan(&self) -> f32 { tan(*self) }
387 fn asin(&self) -> f32 { asin(*self) }
390 fn acos(&self) -> f32 { acos(*self) }
393 fn atan(&self) -> f32 { atan(*self) }
396 fn atan2(&self, other: &f32) -> f32 { atan2(*self, *other) }
398 /// Simultaneously computes the sine and cosine of the number
400 fn sin_cos(&self) -> (f32, f32) {
401 (self.sin(), self.cos())
405 impl Exponential for f32 {
406 /// Returns the exponential of the number
408 fn exp(&self) -> f32 { exp(*self) }
410 /// Returns 2 raised to the power of the number
412 fn exp2(&self) -> f32 { exp2(*self) }
414 /// Returns the natural logarithm of the number
416 fn ln(&self) -> f32 { ln(*self) }
418 /// Returns the logarithm of the number with respect to an arbitrary base
420 fn log(&self, base: &f32) -> f32 { self.ln() / base.ln() }
422 /// Returns the base 2 logarithm of the number
424 fn log2(&self) -> f32 { log2(*self) }
426 /// Returns the base 10 logarithm of the number
428 fn log10(&self) -> f32 { log10(*self) }
431 impl Hyperbolic for f32 {
433 fn sinh(&self) -> f32 { sinh(*self) }
436 fn cosh(&self) -> f32 { cosh(*self) }
439 fn tanh(&self) -> f32 { tanh(*self) }
442 /// Inverse hyperbolic sine
446 /// - on success, the inverse hyperbolic sine of `self` will be returned
447 /// - `self` if `self` is `0.0`, `-0.0`, `infinity`, or `neg_infinity`
448 /// - `NaN` if `self` is `NaN`
451 fn asinh(&self) -> f32 {
453 neg_infinity => neg_infinity,
454 x => (x + ((x * x) + 1.0).sqrt()).ln(),
459 /// Inverse hyperbolic cosine
463 /// - on success, the inverse hyperbolic cosine of `self` will be returned
464 /// - `infinity` if `self` is `infinity`
465 /// - `NaN` if `self` is `NaN` or `self < 1.0` (including `neg_infinity`)
468 fn acosh(&self) -> f32 {
470 x if x < 1.0 => Float::NaN(),
471 x => (x + ((x * x) - 1.0).sqrt()).ln(),
476 /// Inverse hyperbolic tangent
480 /// - on success, the inverse hyperbolic tangent of `self` will be returned
481 /// - `self` if `self` is `0.0` or `-0.0`
482 /// - `infinity` if `self` is `1.0`
483 /// - `neg_infinity` if `self` is `-1.0`
484 /// - `NaN` if the `self` is `NaN` or outside the domain of `-1.0 <= self <= 1.0`
485 /// (including `infinity` and `neg_infinity`)
488 fn atanh(&self) -> f32 {
489 0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
494 /// Archimedes' constant
496 fn pi() -> f32 { 3.14159265358979323846264338327950288 }
500 fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }
504 fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }
508 fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
512 fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
516 fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
520 fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
524 fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
528 fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
532 fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
536 fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
540 fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
544 fn e() -> f32 { 2.71828182845904523536028747135266250 }
548 fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
552 fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
556 fn ln_2() -> f32 { 0.693147180559945309417232121458176568 }
560 fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }
562 /// Converts to degrees, assuming the number is in radians
564 fn to_degrees(&self) -> f32 { *self * (180.0 / Real::pi::<f32>()) }
566 /// Converts to radians, assuming the number is in degrees
568 fn to_radians(&self) -> f32 { *self * (Real::pi::<f32>() / 180.0) }
571 impl Bounded for f32 {
573 fn min_value() -> f32 { 1.17549435e-38 }
576 fn max_value() -> f32 { 3.40282347e+38 }
579 impl Primitive for f32 {
581 fn bits() -> uint { 32 }
584 fn bytes() -> uint { Primitive::bits::<f32>() / 8 }
589 fn NaN() -> f32 { 0.0 / 0.0 }
592 fn infinity() -> f32 { 1.0 / 0.0 }
595 fn neg_infinity() -> f32 { -1.0 / 0.0 }
598 fn neg_zero() -> f32 { -0.0 }
600 /// Returns `true` if the number is NaN
602 fn is_NaN(&self) -> bool { *self != *self }
604 /// Returns `true` if the number is infinite
606 fn is_infinite(&self) -> bool {
607 *self == Float::infinity() || *self == Float::neg_infinity()
610 /// Returns `true` if the number is neither infinite or NaN
612 fn is_finite(&self) -> bool {
613 !(self.is_NaN() || self.is_infinite())
616 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
618 fn is_normal(&self) -> bool {
619 self.classify() == FPNormal
622 /// Returns the floating point category of the number. If only one property is going to
623 /// be tested, it is generally faster to use the specific predicate instead.
624 fn classify(&self) -> FPCategory {
625 static EXP_MASK: u32 = 0x7f800000;
626 static MAN_MASK: u32 = 0x007fffff;
629 unsafe { ::cast::transmute::<f32,u32>(*self) } & MAN_MASK,
630 unsafe { ::cast::transmute::<f32,u32>(*self) } & EXP_MASK,
633 (_, 0) => FPSubnormal,
634 (0, EXP_MASK) => FPInfinite,
635 (_, EXP_MASK) => FPNaN,
641 fn mantissa_digits() -> uint { 24 }
644 fn digits() -> uint { 6 }
647 fn epsilon() -> f32 { 1.19209290e-07 }
650 fn min_exp() -> int { -125 }
653 fn max_exp() -> int { 128 }
656 fn min_10_exp() -> int { -37 }
659 fn max_10_exp() -> int { 38 }
661 /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
663 fn ldexp(x: f32, exp: int) -> f32 {
664 ldexp(x, exp as c_int)
668 /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
670 /// - `self = x * pow(2, exp)`
671 /// - `0.5 <= abs(x) < 1.0`
674 fn frexp(&self) -> (f32, int) {
676 let x = frexp(*self, &mut exp);
681 /// Returns the exponential of the number, minus `1`, in a way that is accurate
682 /// even if the number is close to zero
685 fn exp_m1(&self) -> f32 { exp_m1(*self) }
688 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
689 /// than if the operations were performed separately
692 fn ln_1p(&self) -> f32 { ln_1p(*self) }
695 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
696 /// produces a more accurate result with better performance than a separate multiplication
697 /// operation followed by an add.
700 fn mul_add(&self, a: f32, b: f32) -> f32 {
704 /// Returns the next representable floating-point value in the direction of `other`
706 fn next_after(&self, other: f32) -> f32 {
707 next_after(*self, other)
712 // Section: String Conversions
716 /// Converts a float to a string
720 /// * num - The float value
723 pub fn to_str(num: f32) -> ~str {
724 let (r, _) = strconv::float_to_str_common(
725 num, 10u, true, strconv::SignNeg, strconv::DigAll);
730 /// Converts a float to a string in hexadecimal format
734 /// * num - The float value
737 pub fn to_str_hex(num: f32) -> ~str {
738 let (r, _) = strconv::float_to_str_common(
739 num, 16u, true, strconv::SignNeg, strconv::DigAll);
744 /// Converts a float to a string in a given radix
748 /// * num - The float value
749 /// * radix - The base to use
753 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
754 /// possible misinterpretation of the result at higher bases. If those values
755 /// are expected, use `to_str_radix_special()` instead.
758 pub fn to_str_radix(num: f32, rdx: uint) -> ~str {
759 let (r, special) = strconv::float_to_str_common(
760 num, rdx, true, strconv::SignNeg, strconv::DigAll);
761 if special { fail!("number has a special value, \
762 try to_str_radix_special() if those are expected") }
767 /// Converts a float to a string in a given radix, and a flag indicating
768 /// whether it's a special value
772 /// * num - The float value
773 /// * radix - The base to use
776 pub fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
777 strconv::float_to_str_common(num, rdx, true,
778 strconv::SignNeg, strconv::DigAll)
782 /// Converts a float to a string with exactly the number of
783 /// provided significant digits
787 /// * num - The float value
788 /// * digits - The number of significant digits
791 pub fn to_str_exact(num: f32, dig: uint) -> ~str {
792 let (r, _) = strconv::float_to_str_common(
793 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
798 /// Converts a float to a string with a maximum number of
799 /// significant digits
803 /// * num - The float value
804 /// * digits - The number of significant digits
807 pub fn to_str_digits(num: f32, dig: uint) -> ~str {
808 let (r, _) = strconv::float_to_str_common(
809 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
813 impl to_str::ToStr for f32 {
815 fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
818 impl num::ToStrRadix for f32 {
820 fn to_str_radix(&self, rdx: uint) -> ~str {
821 to_str_radix(*self, rdx)
826 /// Convert a string in base 10 to a float.
827 /// Accepts a optional decimal exponent.
829 /// This function accepts strings such as
832 /// * '+3.14', equivalent to '3.14'
834 /// * '2.5E10', or equivalently, '2.5e10'
836 /// * '.' (understood as 0)
838 /// * '.5', or, equivalently, '0.5'
839 /// * '+inf', 'inf', '-inf', 'NaN'
841 /// Leading and trailing whitespace represent an error.
849 /// `none` if the string did not represent a valid number. Otherwise,
850 /// `Some(n)` where `n` is the floating-point number represented by `num`.
853 pub fn from_str(num: &str) -> Option<f32> {
854 strconv::from_str_common(num, 10u, true, true, true,
855 strconv::ExpDec, false, false)
859 /// Convert a string in base 16 to a float.
860 /// Accepts a optional binary exponent.
862 /// This function accepts strings such as
865 /// * '+a4.fe', equivalent to 'a4.fe'
867 /// * '2b.aP128', or equivalently, '2b.ap128'
869 /// * '.' (understood as 0)
871 /// * '.c', or, equivalently, '0.c'
872 /// * '+inf', 'inf', '-inf', 'NaN'
874 /// Leading and trailing whitespace represent an error.
882 /// `none` if the string did not represent a valid number. Otherwise,
883 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
886 pub fn from_str_hex(num: &str) -> Option<f32> {
887 strconv::from_str_common(num, 16u, true, true, true,
888 strconv::ExpBin, false, false)
892 /// Convert a string in an given base to a float.
894 /// Due to possible conflicts, this function does **not** accept
895 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
896 /// does it recognize exponents of any kind.
898 /// Leading and trailing whitespace represent an error.
903 /// * radix - The base to use. Must lie in the range [2 .. 36]
907 /// `none` if the string did not represent a valid number. Otherwise,
908 /// `Some(n)` where `n` is the floating-point number represented by `num`.
911 pub fn from_str_radix(num: &str, rdx: uint) -> Option<f32> {
912 strconv::from_str_common(num, rdx, true, true, false,
913 strconv::ExpNone, false, false)
916 impl FromStr for f32 {
918 fn from_str(val: &str) -> Option<f32> { from_str(val) }
921 impl num::FromStrRadix for f32 {
923 fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
924 from_str_radix(val, rdx)
939 num::test_num(10f32, 2f32);
944 assert_eq!(1f32.min(&2f32), 1f32);
945 assert_eq!(2f32.min(&1f32), 1f32);
950 assert_eq!(1f32.max(&2f32), 2f32);
951 assert_eq!(2f32.max(&1f32), 2f32);
956 assert_eq!(1f32.clamp(&2f32, &4f32), 2f32);
957 assert_eq!(8f32.clamp(&2f32, &4f32), 4f32);
958 assert_eq!(3f32.clamp(&2f32, &4f32), 3f32);
959 assert!(3f32.clamp(&Float::NaN::<f32>(), &4f32).is_NaN());
960 assert!(3f32.clamp(&2f32, &Float::NaN::<f32>()).is_NaN());
961 assert!(Float::NaN::<f32>().clamp(&2f32, &4f32).is_NaN());
966 assert_approx_eq!(1.0f32.floor(), 1.0f32);
967 assert_approx_eq!(1.3f32.floor(), 1.0f32);
968 assert_approx_eq!(1.5f32.floor(), 1.0f32);
969 assert_approx_eq!(1.7f32.floor(), 1.0f32);
970 assert_approx_eq!(0.0f32.floor(), 0.0f32);
971 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
972 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
973 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
974 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
975 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
980 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
981 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
982 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
983 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
984 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
985 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
986 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
987 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
988 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
989 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
994 assert_approx_eq!(1.0f32.round(), 1.0f32);
995 assert_approx_eq!(1.3f32.round(), 1.0f32);
996 assert_approx_eq!(1.5f32.round(), 2.0f32);
997 assert_approx_eq!(1.7f32.round(), 2.0f32);
998 assert_approx_eq!(0.0f32.round(), 0.0f32);
999 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1000 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1001 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1002 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1003 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1008 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1009 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1010 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1011 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1012 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1013 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1014 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1015 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1016 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1017 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1022 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1023 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1024 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1025 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1026 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1027 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1028 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1029 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1030 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1031 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1036 assert_eq!(0.0f32.asinh(), 0.0f32);
1037 assert_eq!((-0.0f32).asinh(), -0.0f32);
1038 assert_eq!(Float::infinity::<f32>().asinh(), Float::infinity::<f32>());
1039 assert_eq!(Float::neg_infinity::<f32>().asinh(), Float::neg_infinity::<f32>());
1040 assert!(Float::NaN::<f32>().asinh().is_NaN());
1041 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1042 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1047 assert_eq!(1.0f32.acosh(), 0.0f32);
1048 assert!(0.999f32.acosh().is_NaN());
1049 assert_eq!(Float::infinity::<f32>().acosh(), Float::infinity::<f32>());
1050 assert!(Float::neg_infinity::<f32>().acosh().is_NaN());
1051 assert!(Float::NaN::<f32>().acosh().is_NaN());
1052 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1053 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1058 assert_eq!(0.0f32.atanh(), 0.0f32);
1059 assert_eq!((-0.0f32).atanh(), -0.0f32);
1060 assert_eq!(1.0f32.atanh(), Float::infinity::<f32>());
1061 assert_eq!((-1.0f32).atanh(), Float::neg_infinity::<f32>());
1062 assert!(2f64.atanh().atanh().is_NaN());
1063 assert!((-2f64).atanh().atanh().is_NaN());
1064 assert!(Float::infinity::<f64>().atanh().is_NaN());
1065 assert!(Float::neg_infinity::<f64>().atanh().is_NaN());
1066 assert!(Float::NaN::<f32>().atanh().is_NaN());
1067 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1068 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1072 fn test_real_consts() {
1073 assert_approx_eq!(Real::two_pi::<f32>(), 2f32 * Real::pi::<f32>());
1074 assert_approx_eq!(Real::frac_pi_2::<f32>(), Real::pi::<f32>() / 2f32);
1075 assert_approx_eq!(Real::frac_pi_3::<f32>(), Real::pi::<f32>() / 3f32);
1076 assert_approx_eq!(Real::frac_pi_4::<f32>(), Real::pi::<f32>() / 4f32);
1077 assert_approx_eq!(Real::frac_pi_6::<f32>(), Real::pi::<f32>() / 6f32);
1078 assert_approx_eq!(Real::frac_pi_8::<f32>(), Real::pi::<f32>() / 8f32);
1079 assert_approx_eq!(Real::frac_1_pi::<f32>(), 1f32 / Real::pi::<f32>());
1080 assert_approx_eq!(Real::frac_2_pi::<f32>(), 2f32 / Real::pi::<f32>());
1081 assert_approx_eq!(Real::frac_2_sqrtpi::<f32>(), 2f32 / Real::pi::<f32>().sqrt());
1082 assert_approx_eq!(Real::sqrt2::<f32>(), 2f32.sqrt());
1083 assert_approx_eq!(Real::frac_1_sqrt2::<f32>(), 1f32 / 2f32.sqrt());
1084 assert_approx_eq!(Real::log2_e::<f32>(), Real::e::<f32>().log2());
1085 assert_approx_eq!(Real::log10_e::<f32>(), Real::e::<f32>().log10());
1086 assert_approx_eq!(Real::ln_2::<f32>(), 2f32.ln());
1087 assert_approx_eq!(Real::ln_10::<f32>(), 10f32.ln());
1092 assert_eq!(infinity.abs(), infinity);
1093 assert_eq!(1f32.abs(), 1f32);
1094 assert_eq!(0f32.abs(), 0f32);
1095 assert_eq!((-0f32).abs(), 0f32);
1096 assert_eq!((-1f32).abs(), 1f32);
1097 assert_eq!(neg_infinity.abs(), infinity);
1098 assert_eq!((1f32/neg_infinity).abs(), 0f32);
1099 assert!(NaN.abs().is_NaN());
1104 assert_eq!((-1f32).abs_sub(&1f32), 0f32);
1105 assert_eq!(1f32.abs_sub(&1f32), 0f32);
1106 assert_eq!(1f32.abs_sub(&0f32), 1f32);
1107 assert_eq!(1f32.abs_sub(&-1f32), 2f32);
1108 assert_eq!(neg_infinity.abs_sub(&0f32), 0f32);
1109 assert_eq!(infinity.abs_sub(&1f32), infinity);
1110 assert_eq!(0f32.abs_sub(&neg_infinity), infinity);
1111 assert_eq!(0f32.abs_sub(&infinity), 0f32);
1112 assert!(NaN.abs_sub(&-1f32).is_NaN());
1113 assert!(1f32.abs_sub(&NaN).is_NaN());
1118 assert_eq!(infinity.signum(), 1f32);
1119 assert_eq!(1f32.signum(), 1f32);
1120 assert_eq!(0f32.signum(), 1f32);
1121 assert_eq!((-0f32).signum(), -1f32);
1122 assert_eq!((-1f32).signum(), -1f32);
1123 assert_eq!(neg_infinity.signum(), -1f32);
1124 assert_eq!((1f32/neg_infinity).signum(), -1f32);
1125 assert!(NaN.signum().is_NaN());
1129 fn test_is_positive() {
1130 assert!(infinity.is_positive());
1131 assert!(1f32.is_positive());
1132 assert!(0f32.is_positive());
1133 assert!(!(-0f32).is_positive());
1134 assert!(!(-1f32).is_positive());
1135 assert!(!neg_infinity.is_positive());
1136 assert!(!(1f32/neg_infinity).is_positive());
1137 assert!(!NaN.is_positive());
1141 fn test_is_negative() {
1142 assert!(!infinity.is_negative());
1143 assert!(!1f32.is_negative());
1144 assert!(!0f32.is_negative());
1145 assert!((-0f32).is_negative());
1146 assert!((-1f32).is_negative());
1147 assert!(neg_infinity.is_negative());
1148 assert!((1f32/neg_infinity).is_negative());
1149 assert!(!NaN.is_negative());
1153 fn test_approx_eq() {
1154 assert!(1.0f32.approx_eq(&1f32));
1155 assert!(0.9999999f32.approx_eq(&1f32));
1156 assert!(1.000001f32.approx_eq_eps(&1f32, &1.0e-5));
1157 assert!(1.0000001f32.approx_eq_eps(&1f32, &1.0e-6));
1158 assert!(!1.0000001f32.approx_eq_eps(&1f32, &1.0e-7));
1162 fn test_primitive() {
1163 assert_eq!(Primitive::bits::<f32>(), sys::size_of::<f32>() * 8);
1164 assert_eq!(Primitive::bytes::<f32>(), sys::size_of::<f32>());
1168 fn test_is_normal() {
1169 assert!(!Float::NaN::<f32>().is_normal());
1170 assert!(!Float::infinity::<f32>().is_normal());
1171 assert!(!Float::neg_infinity::<f32>().is_normal());
1172 assert!(!Zero::zero::<f32>().is_normal());
1173 assert!(!Float::neg_zero::<f32>().is_normal());
1174 assert!(1f32.is_normal());
1175 assert!(1e-37f32.is_normal());
1176 assert!(!1e-38f32.is_normal());
1180 fn test_classify() {
1181 assert_eq!(Float::NaN::<f32>().classify(), FPNaN);
1182 assert_eq!(Float::infinity::<f32>().classify(), FPInfinite);
1183 assert_eq!(Float::neg_infinity::<f32>().classify(), FPInfinite);
1184 assert_eq!(Zero::zero::<f32>().classify(), FPZero);
1185 assert_eq!(Float::neg_zero::<f32>().classify(), FPZero);
1186 assert_eq!(1f32.classify(), FPNormal);
1187 assert_eq!(1e-37f32.classify(), FPNormal);
1188 assert_eq!(1e-38f32.classify(), FPSubnormal);
1193 // We have to use from_str until base-2 exponents
1194 // are supported in floating-point literals
1195 let f1: f32 = from_str_hex("1p-123").unwrap();
1196 let f2: f32 = from_str_hex("1p-111").unwrap();
1197 assert_eq!(Float::ldexp(1f32, -123), f1);
1198 assert_eq!(Float::ldexp(1f32, -111), f2);
1200 assert_eq!(Float::ldexp(0f32, -123), 0f32);
1201 assert_eq!(Float::ldexp(-0f32, -123), -0f32);
1202 assert_eq!(Float::ldexp(Float::infinity::<f32>(), -123),
1203 Float::infinity::<f32>());
1204 assert_eq!(Float::ldexp(Float::neg_infinity::<f32>(), -123),
1205 Float::neg_infinity::<f32>());
1206 assert!(Float::ldexp(Float::NaN::<f32>(), -123).is_NaN());
1211 // We have to use from_str until base-2 exponents
1212 // are supported in floating-point literals
1213 let f1: f32 = from_str_hex("1p-123").unwrap();
1214 let f2: f32 = from_str_hex("1p-111").unwrap();
1215 let (x1, exp1) = f1.frexp();
1216 let (x2, exp2) = f2.frexp();
1217 assert_eq!((x1, exp1), (0.5f32, -122));
1218 assert_eq!((x2, exp2), (0.5f32, -110));
1219 assert_eq!(Float::ldexp(x1, exp1), f1);
1220 assert_eq!(Float::ldexp(x2, exp2), f2);
1222 assert_eq!(0f32.frexp(), (0f32, 0));
1223 assert_eq!((-0f32).frexp(), (-0f32, 0));
1224 assert_eq!(match Float::infinity::<f32>().frexp() { (x, _) => x },
1225 Float::infinity::<f32>())
1226 assert_eq!(match Float::neg_infinity::<f32>().frexp() { (x, _) => x },
1227 Float::neg_infinity::<f32>())
1228 assert!(match Float::NaN::<f32>().frexp() { (x, _) => x.is_NaN() })