1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Operations and constants for 32-bits floats (`f32` type)
13 #![allow(missing_doc)]
19 use from_str::FromStr;
21 use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
22 use num::{Zero, One, Bounded, strconv};
28 use libc::{c_float, c_int};
32 pub fn acosf(n: c_float) -> c_float;
33 pub fn asinf(n: c_float) -> c_float;
34 pub fn atanf(n: c_float) -> c_float;
35 pub fn atan2f(a: c_float, b: c_float) -> c_float;
36 pub fn cbrtf(n: c_float) -> c_float;
37 pub fn coshf(n: c_float) -> c_float;
38 pub fn erff(n: c_float) -> c_float;
39 pub fn erfcf(n: c_float) -> c_float;
40 pub fn expm1f(n: c_float) -> c_float;
41 pub fn fdimf(a: c_float, b: c_float) -> c_float;
42 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
43 pub fn fmaxf(a: c_float, b: c_float) -> c_float;
44 pub fn fminf(a: c_float, b: c_float) -> c_float;
45 pub fn fmodf(a: c_float, b: c_float) -> c_float;
46 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
47 pub fn hypotf(x: c_float, y: c_float) -> c_float;
48 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
49 pub fn logbf(n: c_float) -> c_float;
50 pub fn log1pf(n: c_float) -> c_float;
51 pub fn ilogbf(n: c_float) -> c_int;
52 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
53 pub fn sinhf(n: c_float) -> c_float;
54 pub fn tanf(n: c_float) -> c_float;
55 pub fn tanhf(n: c_float) -> c_float;
56 pub fn tgammaf(n: c_float) -> c_float;
59 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
62 #[link_name="__lgammaf_r"]
63 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
67 pub static RADIX: uint = 2u;
69 pub static MANTISSA_DIGITS: uint = 24u;
70 pub static DIGITS: uint = 6u;
72 pub static EPSILON: f32 = 1.19209290e-07_f32;
74 /// Smallest finite f32 value
75 pub static MIN_VALUE: f32 = -3.40282347e+38_f32;
76 /// Smallest positive, normalized f32 value
77 pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
78 /// Largest finite f32 value
79 pub static MAX_VALUE: f32 = 3.40282347e+38_f32;
81 pub static MIN_EXP: int = -125;
82 pub static MAX_EXP: int = 128;
84 pub static MIN_10_EXP: int = -37;
85 pub static MAX_10_EXP: int = 38;
87 pub static NAN: f32 = 0.0_f32/0.0_f32;
88 pub static INFINITY: f32 = 1.0_f32/0.0_f32;
89 pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
91 /// Various useful constants.
93 // FIXME: replace with mathematical constants from cmath.
95 // FIXME(#5527): These constants should be deprecated once associated
96 // constants are implemented in favour of referencing the respective members
99 /// Archimedes' constant
100 pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
103 pub static PI_2: f32 = 6.28318530717958647692528676655900576_f32;
106 pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
109 pub static FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
112 pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
115 pub static FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
118 pub static FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
121 pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
124 pub static FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
127 pub static FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
130 pub static SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
133 pub static FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
136 pub static E: f32 = 2.71828182845904523536028747135266250_f32;
139 pub static LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
142 pub static LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
145 pub static LN_2: f32 = 0.693147180559945309417232121458176568_f32;
148 pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32;
156 fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
162 fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
164 fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
166 fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
168 fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
171 impl Default for f32 {
173 fn default() -> f32 { 0.0 }
178 fn zero() -> f32 { 0.0 }
180 /// Returns true if the number is equal to either `0.0` or `-0.0`
182 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
187 fn one() -> f32 { 1.0 }
191 impl Add<f32,f32> for f32 {
193 fn add(&self, other: &f32) -> f32 { *self + *other }
197 impl Sub<f32,f32> for f32 {
199 fn sub(&self, other: &f32) -> f32 { *self - *other }
203 impl Mul<f32,f32> for f32 {
205 fn mul(&self, other: &f32) -> f32 { *self * *other }
209 impl Div<f32,f32> for f32 {
211 fn div(&self, other: &f32) -> f32 { *self / *other }
215 impl Rem<f32,f32> for f32 {
217 fn rem(&self, other: &f32) -> f32 {
218 unsafe { cmath::fmodf(*self, *other) }
223 impl Neg<f32> for f32 {
225 fn neg(&self) -> f32 { -*self }
228 impl Signed for f32 {
229 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
231 fn abs(&self) -> f32 {
232 unsafe { intrinsics::fabsf32(*self) }
235 /// The positive difference of two numbers. Returns `0.0` if the number is
236 /// less than or equal to `other`, otherwise the difference between`self`
237 /// and `other` is returned.
239 fn abs_sub(&self, other: &f32) -> f32 {
240 unsafe { cmath::fdimf(*self, *other) }
245 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
246 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
247 /// - `NAN` if the number is NaN
249 fn signum(&self) -> f32 {
250 if self.is_nan() { NAN } else {
251 unsafe { intrinsics::copysignf32(1.0, *self) }
255 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
257 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
259 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
261 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
264 impl Bounded for f32 {
265 // NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
267 fn min_value() -> f32 { -MAX_VALUE }
270 fn max_value() -> f32 { MAX_VALUE }
273 impl Primitive for f32 {}
277 fn nan() -> f32 { NAN }
280 fn infinity() -> f32 { INFINITY }
283 fn neg_infinity() -> f32 { NEG_INFINITY }
286 fn neg_zero() -> f32 { -0.0 }
288 /// Returns `true` if the number is NaN
290 fn is_nan(self) -> bool { self != self }
292 /// Returns `true` if the number is infinite
294 fn is_infinite(self) -> bool {
295 self == Float::infinity() || self == Float::neg_infinity()
298 /// Returns `true` if the number is neither infinite or NaN
300 fn is_finite(self) -> bool {
301 !(self.is_nan() || self.is_infinite())
304 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
306 fn is_normal(self) -> bool {
307 self.classify() == FPNormal
310 /// Returns the floating point category of the number. If only one property
311 /// is going to be tested, it is generally faster to use the specific
312 /// predicate instead.
313 fn classify(self) -> FPCategory {
314 static EXP_MASK: u32 = 0x7f800000;
315 static MAN_MASK: u32 = 0x007fffff;
317 let bits: u32 = unsafe { cast::transmute(self) };
318 match (bits & MAN_MASK, bits & EXP_MASK) {
320 (_, 0) => FPSubnormal,
321 (0, EXP_MASK) => FPInfinite,
322 (_, EXP_MASK) => FPNaN,
328 fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS }
331 fn digits(_: Option<f32>) -> uint { DIGITS }
334 fn epsilon() -> f32 { EPSILON }
337 fn min_exp(_: Option<f32>) -> int { MIN_EXP }
340 fn max_exp(_: Option<f32>) -> int { MAX_EXP }
343 fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
346 fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
349 fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
351 /// Constructs a floating point number by multiplying `x` by 2 raised to the
354 fn ldexp(x: f32, exp: int) -> f32 {
355 unsafe { cmath::ldexpf(x, exp as c_int) }
358 /// Breaks the number into a normalized fraction and a base-2 exponent,
361 /// - `self = x * pow(2, exp)`
362 /// - `0.5 <= abs(x) < 1.0`
364 fn frexp(self) -> (f32, int) {
367 let x = cmath::frexpf(self, &mut exp);
372 /// Returns the mantissa, exponent and sign as integers.
373 fn integer_decode(self) -> (u64, i16, i8) {
374 let bits: u32 = unsafe { cast::transmute(self) };
375 let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
376 let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
377 let mantissa = if exponent == 0 {
378 (bits & 0x7fffff) << 1
380 (bits & 0x7fffff) | 0x800000
382 // Exponent bias + mantissa shift
383 exponent -= 127 + 23;
384 (mantissa as u64, exponent, sign)
387 /// Returns the next representable floating-point value in the direction of
390 fn next_after(self, other: f32) -> f32 {
391 unsafe { cmath::nextafterf(self, other) }
394 /// Round half-way cases toward `NEG_INFINITY`
396 fn floor(self) -> f32 {
397 unsafe { intrinsics::floorf32(self) }
400 /// Round half-way cases toward `INFINITY`
402 fn ceil(self) -> f32 {
403 unsafe { intrinsics::ceilf32(self) }
406 /// Round half-way cases away from `0.0`
408 fn round(self) -> f32 {
409 unsafe { intrinsics::roundf32(self) }
412 /// The integer part of the number (rounds towards `0.0`)
414 fn trunc(self) -> f32 {
415 unsafe { intrinsics::truncf32(self) }
418 /// The fractional part of the number, satisfying:
422 /// assert!(x == x.trunc() + x.fract())
425 fn fract(self) -> f32 { self - self.trunc() }
428 fn max(self, other: f32) -> f32 {
429 unsafe { cmath::fmaxf(self, other) }
433 fn min(self, other: f32) -> f32 {
434 unsafe { cmath::fminf(self, other) }
437 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
438 /// error. This produces a more accurate result with better performance than
439 /// a separate multiplication operation followed by an add.
441 fn mul_add(self, a: f32, b: f32) -> f32 {
442 unsafe { intrinsics::fmaf32(self, a, b) }
445 /// The reciprocal (multiplicative inverse) of the number
447 fn recip(self) -> f32 { 1.0 / self }
449 fn powi(self, n: i32) -> f32 {
450 unsafe { intrinsics::powif32(self, n) }
454 fn powf(self, n: f32) -> f32 {
455 unsafe { intrinsics::powf32(self, n) }
460 fn sqrt2() -> f32 { consts::SQRT2 }
464 fn frac_1_sqrt2() -> f32 { consts::FRAC_1_SQRT2 }
467 fn sqrt(self) -> f32 {
468 unsafe { intrinsics::sqrtf32(self) }
472 fn rsqrt(self) -> f32 { self.sqrt().recip() }
475 fn cbrt(self) -> f32 {
476 unsafe { cmath::cbrtf(self) }
480 fn hypot(self, other: f32) -> f32 {
481 unsafe { cmath::hypotf(self, other) }
484 /// Archimedes' constant
486 fn pi() -> f32 { consts::PI }
490 fn two_pi() -> f32 { consts::PI_2 }
494 fn frac_pi_2() -> f32 { consts::FRAC_PI_2 }
498 fn frac_pi_3() -> f32 { consts::FRAC_PI_3 }
502 fn frac_pi_4() -> f32 { consts::FRAC_PI_4 }
506 fn frac_pi_6() -> f32 { consts::FRAC_PI_6 }
510 fn frac_pi_8() -> f32 { consts::FRAC_PI_8 }
514 fn frac_1_pi() -> f32 { consts::FRAC_1_PI }
518 fn frac_2_pi() -> f32 { consts::FRAC_2_PI }
522 fn frac_2_sqrtpi() -> f32 { consts::FRAC_2_SQRTPI }
525 fn sin(self) -> f32 {
526 unsafe { intrinsics::sinf32(self) }
530 fn cos(self) -> f32 {
531 unsafe { intrinsics::cosf32(self) }
535 fn tan(self) -> f32 {
536 unsafe { cmath::tanf(self) }
540 fn asin(self) -> f32 {
541 unsafe { cmath::asinf(self) }
545 fn acos(self) -> f32 {
546 unsafe { cmath::acosf(self) }
550 fn atan(self) -> f32 {
551 unsafe { cmath::atanf(self) }
555 fn atan2(self, other: f32) -> f32 {
556 unsafe { cmath::atan2f(self, other) }
559 /// Simultaneously computes the sine and cosine of the number
561 fn sin_cos(self) -> (f32, f32) {
562 (self.sin(), self.cos())
567 fn e() -> f32 { consts::E }
571 fn log2_e() -> f32 { consts::LOG2_E }
575 fn log10_e() -> f32 { consts::LOG10_E }
579 fn ln_2() -> f32 { consts::LN_2 }
583 fn ln_10() -> f32 { consts::LN_10 }
585 /// Returns the exponential of the number
587 fn exp(self) -> f32 {
588 unsafe { intrinsics::expf32(self) }
591 /// Returns 2 raised to the power of the number
593 fn exp2(self) -> f32 {
594 unsafe { intrinsics::exp2f32(self) }
597 /// Returns the exponential of the number, minus `1`, in a way that is
598 /// accurate even if the number is close to zero
600 fn exp_m1(self) -> f32 {
601 unsafe { cmath::expm1f(self) }
604 /// Returns the natural logarithm of the number
607 unsafe { intrinsics::logf32(self) }
610 /// Returns the logarithm of the number with respect to an arbitrary base
612 fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
614 /// Returns the base 2 logarithm of the number
616 fn log2(self) -> f32 {
617 unsafe { intrinsics::log2f32(self) }
620 /// Returns the base 10 logarithm of the number
622 fn log10(self) -> f32 {
623 unsafe { intrinsics::log10f32(self) }
626 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
627 /// accurately than if the operations were performed separately
629 fn ln_1p(self) -> f32 {
630 unsafe { cmath::log1pf(self) }
634 fn sinh(self) -> f32 {
635 unsafe { cmath::sinhf(self) }
639 fn cosh(self) -> f32 {
640 unsafe { cmath::coshf(self) }
644 fn tanh(self) -> f32 {
645 unsafe { cmath::tanhf(self) }
648 /// Inverse hyperbolic sine
652 /// - on success, the inverse hyperbolic sine of `self` will be returned
653 /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
654 /// - `NAN` if `self` is `NAN`
656 fn asinh(self) -> f32 {
658 NEG_INFINITY => NEG_INFINITY,
659 x => (x + ((x * x) + 1.0).sqrt()).ln(),
663 /// Inverse hyperbolic cosine
667 /// - on success, the inverse hyperbolic cosine of `self` will be returned
668 /// - `INFINITY` if `self` is `INFINITY`
669 /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
671 fn acosh(self) -> f32 {
673 x if x < 1.0 => Float::nan(),
674 x => (x + ((x * x) - 1.0).sqrt()).ln(),
678 /// Inverse hyperbolic tangent
682 /// - on success, the inverse hyperbolic tangent of `self` will be returned
683 /// - `self` if `self` is `0.0` or `-0.0`
684 /// - `INFINITY` if `self` is `1.0`
685 /// - `NEG_INFINITY` if `self` is `-1.0`
686 /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
687 /// (including `INFINITY` and `NEG_INFINITY`)
689 fn atanh(self) -> f32 {
690 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
693 /// Converts to degrees, assuming the number is in radians
695 fn to_degrees(self) -> f32 { self * (180.0f32 / Float::pi()) }
697 /// Converts to radians, assuming the number is in degrees
699 fn to_radians(self) -> f32 {
700 let value: f32 = Float::pi();
701 self * (value / 180.0f32)
706 // Section: String Conversions
709 /// Converts a float to a string
713 /// * num - The float value
715 pub fn to_str(num: f32) -> ~str {
716 let (r, _) = strconv::float_to_str_common(
717 num, 10u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
721 /// Converts a float to a string in hexadecimal format
725 /// * num - The float value
727 pub fn to_str_hex(num: f32) -> ~str {
728 let (r, _) = strconv::float_to_str_common(
729 num, 16u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
733 /// Converts a float to a string in a given radix, and a flag indicating
734 /// whether it's a special value
738 /// * num - The float value
739 /// * radix - The base to use
741 pub fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
742 strconv::float_to_str_common(num, rdx, true,
743 strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false)
746 /// Converts a float to a string with exactly the number of
747 /// provided significant digits
751 /// * num - The float value
752 /// * digits - The number of significant digits
754 pub fn to_str_exact(num: f32, dig: uint) -> ~str {
755 let (r, _) = strconv::float_to_str_common(
756 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpNone, false);
760 /// Converts a float to a string with a maximum number of
761 /// significant digits
765 /// * num - The float value
766 /// * digits - The number of significant digits
768 pub fn to_str_digits(num: f32, dig: uint) -> ~str {
769 let (r, _) = strconv::float_to_str_common(
770 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpNone, false);
774 /// Converts a float to a string using the exponential notation with exactly the number of
775 /// provided digits after the decimal point in the significand
779 /// * num - The float value
780 /// * digits - The number of digits after the decimal point
781 /// * upper - Use `E` instead of `e` for the exponent sign
783 pub fn to_str_exp_exact(num: f32, dig: uint, upper: bool) -> ~str {
784 let (r, _) = strconv::float_to_str_common(
785 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
789 /// Converts a float to a string using the exponential notation with the maximum number of
790 /// digits after the decimal point in the significand
794 /// * num - The float value
795 /// * digits - The number of digits after the decimal point
796 /// * upper - Use `E` instead of `e` for the exponent sign
798 pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> ~str {
799 let (r, _) = strconv::float_to_str_common(
800 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
804 impl num::ToStrRadix for f32 {
805 /// Converts a float to a string in a given radix
809 /// * num - The float value
810 /// * radix - The base to use
814 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
815 /// possible misinterpretation of the result at higher bases. If those values
816 /// are expected, use `to_str_radix_special()` instead.
818 fn to_str_radix(&self, rdx: uint) -> ~str {
819 let (r, special) = strconv::float_to_str_common(
820 *self, rdx, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
821 if special { fail!("number has a special value, \
822 try to_str_radix_special() if those are expected") }
827 /// Convert a string in base 16 to a float.
828 /// Accepts an optional binary exponent.
830 /// This function accepts strings such as
833 /// * '+a4.fe', equivalent to 'a4.fe'
835 /// * '2b.aP128', or equivalently, '2b.ap128'
837 /// * '.' (understood as 0)
839 /// * '.c', or, equivalently, '0.c'
840 /// * '+inf', 'inf', '-inf', 'NaN'
842 /// Leading and trailing whitespace represent an error.
850 /// `None` if the string did not represent a valid number. Otherwise,
851 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
853 pub fn from_str_hex(num: &str) -> Option<f32> {
854 strconv::from_str_common(num, 16u, true, true, true,
855 strconv::ExpBin, false, false)
858 impl FromStr for f32 {
859 /// Convert a string in base 10 to a float.
860 /// Accepts an optional decimal exponent.
862 /// This function accepts strings such as
865 /// * '+3.14', equivalent to '3.14'
867 /// * '2.5E10', or equivalently, '2.5e10'
869 /// * '.' (understood as 0)
871 /// * '.5', or, equivalently, '0.5'
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`.
885 fn from_str(val: &str) -> Option<f32> {
886 strconv::from_str_common(val, 10u, true, true, true,
887 strconv::ExpDec, false, false)
891 impl num::FromStrRadix for f32 {
892 /// Convert a string in a 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`.
910 fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
911 strconv::from_str_common(val, rdx, true, true, false,
912 strconv::ExpNone, false, false)
924 assert_eq!(NAN.min(2.0), 2.0);
925 assert_eq!(2.0f32.min(NAN), 2.0);
930 assert_eq!(NAN.max(2.0), 2.0);
931 assert_eq!(2.0f32.max(NAN), 2.0);
936 num::test_num(10f32, 2f32);
941 assert_approx_eq!(1.0f32.floor(), 1.0f32);
942 assert_approx_eq!(1.3f32.floor(), 1.0f32);
943 assert_approx_eq!(1.5f32.floor(), 1.0f32);
944 assert_approx_eq!(1.7f32.floor(), 1.0f32);
945 assert_approx_eq!(0.0f32.floor(), 0.0f32);
946 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
947 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
948 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
949 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
950 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
955 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
956 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
957 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
958 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
959 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
960 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
961 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
962 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
963 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
964 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
969 assert_approx_eq!(1.0f32.round(), 1.0f32);
970 assert_approx_eq!(1.3f32.round(), 1.0f32);
971 assert_approx_eq!(1.5f32.round(), 2.0f32);
972 assert_approx_eq!(1.7f32.round(), 2.0f32);
973 assert_approx_eq!(0.0f32.round(), 0.0f32);
974 assert_approx_eq!((-0.0f32).round(), -0.0f32);
975 assert_approx_eq!((-1.0f32).round(), -1.0f32);
976 assert_approx_eq!((-1.3f32).round(), -1.0f32);
977 assert_approx_eq!((-1.5f32).round(), -2.0f32);
978 assert_approx_eq!((-1.7f32).round(), -2.0f32);
983 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
984 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
985 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
986 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
987 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
988 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
989 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
990 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
991 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
992 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
997 assert_approx_eq!(1.0f32.fract(), 0.0f32);
998 assert_approx_eq!(1.3f32.fract(), 0.3f32);
999 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1000 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1001 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1002 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1003 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1004 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1005 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1006 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1011 assert_eq!(0.0f32.asinh(), 0.0f32);
1012 assert_eq!((-0.0f32).asinh(), -0.0f32);
1014 let inf: f32 = Float::infinity();
1015 let neg_inf: f32 = Float::neg_infinity();
1016 let nan: f32 = Float::nan();
1017 assert_eq!(inf.asinh(), inf);
1018 assert_eq!(neg_inf.asinh(), neg_inf);
1019 assert!(nan.asinh().is_nan());
1020 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1021 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1026 assert_eq!(1.0f32.acosh(), 0.0f32);
1027 assert!(0.999f32.acosh().is_nan());
1029 let inf: f32 = Float::infinity();
1030 let neg_inf: f32 = Float::neg_infinity();
1031 let nan: f32 = Float::nan();
1032 assert_eq!(inf.acosh(), inf);
1033 assert!(neg_inf.acosh().is_nan());
1034 assert!(nan.acosh().is_nan());
1035 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1036 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1041 assert_eq!(0.0f32.atanh(), 0.0f32);
1042 assert_eq!((-0.0f32).atanh(), -0.0f32);
1044 let inf32: f32 = Float::infinity();
1045 let neg_inf32: f32 = Float::neg_infinity();
1046 assert_eq!(1.0f32.atanh(), inf32);
1047 assert_eq!((-1.0f32).atanh(), neg_inf32);
1049 assert!(2f64.atanh().atanh().is_nan());
1050 assert!((-2f64).atanh().atanh().is_nan());
1052 let inf64: f32 = Float::infinity();
1053 let neg_inf64: f32 = Float::neg_infinity();
1054 let nan32: f32 = Float::nan();
1055 assert!(inf64.atanh().is_nan());
1056 assert!(neg_inf64.atanh().is_nan());
1057 assert!(nan32.atanh().is_nan());
1059 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1060 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1064 fn test_real_consts() {
1065 let pi: f32 = Float::pi();
1066 let two_pi: f32 = Float::two_pi();
1067 let frac_pi_2: f32 = Float::frac_pi_2();
1068 let frac_pi_3: f32 = Float::frac_pi_3();
1069 let frac_pi_4: f32 = Float::frac_pi_4();
1070 let frac_pi_6: f32 = Float::frac_pi_6();
1071 let frac_pi_8: f32 = Float::frac_pi_8();
1072 let frac_1_pi: f32 = Float::frac_1_pi();
1073 let frac_2_pi: f32 = Float::frac_2_pi();
1074 let frac_2_sqrtpi: f32 = Float::frac_2_sqrtpi();
1075 let sqrt2: f32 = Float::sqrt2();
1076 let frac_1_sqrt2: f32 = Float::frac_1_sqrt2();
1077 let e: f32 = Float::e();
1078 let log2_e: f32 = Float::log2_e();
1079 let log10_e: f32 = Float::log10_e();
1080 let ln_2: f32 = Float::ln_2();
1081 let ln_10: f32 = Float::ln_10();
1083 assert_approx_eq!(two_pi, 2f32 * pi);
1084 assert_approx_eq!(frac_pi_2, pi / 2f32);
1085 assert_approx_eq!(frac_pi_3, pi / 3f32);
1086 assert_approx_eq!(frac_pi_4, pi / 4f32);
1087 assert_approx_eq!(frac_pi_6, pi / 6f32);
1088 assert_approx_eq!(frac_pi_8, pi / 8f32);
1089 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1090 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1091 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1092 assert_approx_eq!(sqrt2, 2f32.sqrt());
1093 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1094 assert_approx_eq!(log2_e, e.log2());
1095 assert_approx_eq!(log10_e, e.log10());
1096 assert_approx_eq!(ln_2, 2f32.ln());
1097 assert_approx_eq!(ln_10, 10f32.ln());
1102 assert_eq!(INFINITY.abs(), INFINITY);
1103 assert_eq!(1f32.abs(), 1f32);
1104 assert_eq!(0f32.abs(), 0f32);
1105 assert_eq!((-0f32).abs(), 0f32);
1106 assert_eq!((-1f32).abs(), 1f32);
1107 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1108 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1109 assert!(NAN.abs().is_nan());
1114 assert_eq!((-1f32).abs_sub(&1f32), 0f32);
1115 assert_eq!(1f32.abs_sub(&1f32), 0f32);
1116 assert_eq!(1f32.abs_sub(&0f32), 1f32);
1117 assert_eq!(1f32.abs_sub(&-1f32), 2f32);
1118 assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
1119 assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
1120 assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
1121 assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
1125 fn test_abs_sub_nowin() {
1126 assert!(NAN.abs_sub(&-1f32).is_nan());
1127 assert!(1f32.abs_sub(&NAN).is_nan());
1132 assert_eq!(INFINITY.signum(), 1f32);
1133 assert_eq!(1f32.signum(), 1f32);
1134 assert_eq!(0f32.signum(), 1f32);
1135 assert_eq!((-0f32).signum(), -1f32);
1136 assert_eq!((-1f32).signum(), -1f32);
1137 assert_eq!(NEG_INFINITY.signum(), -1f32);
1138 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1139 assert!(NAN.signum().is_nan());
1143 fn test_is_positive() {
1144 assert!(INFINITY.is_positive());
1145 assert!(1f32.is_positive());
1146 assert!(0f32.is_positive());
1147 assert!(!(-0f32).is_positive());
1148 assert!(!(-1f32).is_positive());
1149 assert!(!NEG_INFINITY.is_positive());
1150 assert!(!(1f32/NEG_INFINITY).is_positive());
1151 assert!(!NAN.is_positive());
1155 fn test_is_negative() {
1156 assert!(!INFINITY.is_negative());
1157 assert!(!1f32.is_negative());
1158 assert!(!0f32.is_negative());
1159 assert!((-0f32).is_negative());
1160 assert!((-1f32).is_negative());
1161 assert!(NEG_INFINITY.is_negative());
1162 assert!((1f32/NEG_INFINITY).is_negative());
1163 assert!(!NAN.is_negative());
1167 fn test_is_normal() {
1168 let nan: f32 = Float::nan();
1169 let inf: f32 = Float::infinity();
1170 let neg_inf: f32 = Float::neg_infinity();
1171 let zero: f32 = Zero::zero();
1172 let neg_zero: f32 = Float::neg_zero();
1173 assert!(!nan.is_normal());
1174 assert!(!inf.is_normal());
1175 assert!(!neg_inf.is_normal());
1176 assert!(!zero.is_normal());
1177 assert!(!neg_zero.is_normal());
1178 assert!(1f32.is_normal());
1179 assert!(1e-37f32.is_normal());
1180 assert!(!1e-38f32.is_normal());
1184 fn test_classify() {
1185 let nan: f32 = Float::nan();
1186 let inf: f32 = Float::infinity();
1187 let neg_inf: f32 = Float::neg_infinity();
1188 let zero: f32 = Zero::zero();
1189 let neg_zero: f32 = Float::neg_zero();
1190 assert_eq!(nan.classify(), FPNaN);
1191 assert_eq!(inf.classify(), FPInfinite);
1192 assert_eq!(neg_inf.classify(), FPInfinite);
1193 assert_eq!(zero.classify(), FPZero);
1194 assert_eq!(neg_zero.classify(), FPZero);
1195 assert_eq!(1f32.classify(), FPNormal);
1196 assert_eq!(1e-37f32.classify(), FPNormal);
1197 assert_eq!(1e-38f32.classify(), FPSubnormal);
1202 // We have to use from_str until base-2 exponents
1203 // are supported in floating-point literals
1204 let f1: f32 = from_str_hex("1p-123").unwrap();
1205 let f2: f32 = from_str_hex("1p-111").unwrap();
1206 assert_eq!(Float::ldexp(1f32, -123), f1);
1207 assert_eq!(Float::ldexp(1f32, -111), f2);
1209 assert_eq!(Float::ldexp(0f32, -123), 0f32);
1210 assert_eq!(Float::ldexp(-0f32, -123), -0f32);
1212 let inf: f32 = Float::infinity();
1213 let neg_inf: f32 = Float::neg_infinity();
1214 let nan: f32 = Float::nan();
1215 assert_eq!(Float::ldexp(inf, -123), inf);
1216 assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
1217 assert!(Float::ldexp(nan, -123).is_nan());
1222 // We have to use from_str until base-2 exponents
1223 // are supported in floating-point literals
1224 let f1: f32 = from_str_hex("1p-123").unwrap();
1225 let f2: f32 = from_str_hex("1p-111").unwrap();
1226 let (x1, exp1) = f1.frexp();
1227 let (x2, exp2) = f2.frexp();
1228 assert_eq!((x1, exp1), (0.5f32, -122));
1229 assert_eq!((x2, exp2), (0.5f32, -110));
1230 assert_eq!(Float::ldexp(x1, exp1), f1);
1231 assert_eq!(Float::ldexp(x2, exp2), f2);
1233 assert_eq!(0f32.frexp(), (0f32, 0));
1234 assert_eq!((-0f32).frexp(), (-0f32, 0));
1237 #[test] #[ignore(cfg(windows))] // FIXME #8755
1238 fn test_frexp_nowin() {
1239 let inf: f32 = Float::infinity();
1240 let neg_inf: f32 = Float::neg_infinity();
1241 let nan: f32 = Float::nan();
1242 assert_eq!(match inf.frexp() { (x, _) => x }, inf)
1243 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
1244 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1248 fn test_integer_decode() {
1249 assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8));
1250 assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8));
1251 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608u64, 77i16, 1i8));
1252 assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8));
1253 assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8));
1254 assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8));
1255 assert_eq!(NEG_INFINITY.integer_decode(), (8388608u64, 105i16, -1i8));
1256 assert_eq!(NAN.integer_decode(), (12582912u64, 105i16, 1i8));