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)]
14 #![allow(unsigned_negate)]
18 use from_str::FromStr;
25 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
26 pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
27 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
28 pub use core::f32::consts;
32 use libc::{c_float, c_int};
36 pub fn acosf(n: c_float) -> c_float;
37 pub fn asinf(n: c_float) -> c_float;
38 pub fn atanf(n: c_float) -> c_float;
39 pub fn atan2f(a: c_float, b: c_float) -> c_float;
40 pub fn cbrtf(n: c_float) -> c_float;
41 pub fn coshf(n: c_float) -> c_float;
42 pub fn erff(n: c_float) -> c_float;
43 pub fn erfcf(n: c_float) -> c_float;
44 pub fn expm1f(n: c_float) -> c_float;
45 pub fn fdimf(a: c_float, b: c_float) -> c_float;
46 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
47 pub fn fmaxf(a: c_float, b: c_float) -> c_float;
48 pub fn fminf(a: c_float, b: c_float) -> c_float;
49 pub fn fmodf(a: c_float, b: c_float) -> c_float;
50 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
51 pub fn hypotf(x: c_float, y: c_float) -> c_float;
52 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
53 pub fn logbf(n: c_float) -> c_float;
54 pub fn log1pf(n: c_float) -> c_float;
55 pub fn ilogbf(n: c_float) -> c_int;
56 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
57 pub fn sinhf(n: c_float) -> c_float;
58 pub fn tanf(n: c_float) -> c_float;
59 pub fn tanhf(n: c_float) -> c_float;
60 pub fn tgammaf(n: c_float) -> c_float;
63 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
66 #[link_name="__lgammaf_r"]
67 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
71 impl FloatMath for f32 {
72 /// Constructs a floating point number by multiplying `x` by 2 raised to the
75 fn ldexp(x: f32, exp: int) -> f32 {
76 unsafe { cmath::ldexpf(x, exp as c_int) }
79 /// Breaks the number into a normalized fraction and a base-2 exponent,
82 /// - `self = x * pow(2, exp)`
83 /// - `0.5 <= abs(x) < 1.0`
85 fn frexp(self) -> (f32, int) {
88 let x = cmath::frexpf(self, &mut exp);
93 /// Returns the next representable floating-point value in the direction of
96 fn next_after(self, other: f32) -> f32 {
97 unsafe { cmath::nextafterf(self, other) }
101 fn max(self, other: f32) -> f32 {
102 unsafe { cmath::fmaxf(self, other) }
106 fn min(self, other: f32) -> f32 {
107 unsafe { cmath::fminf(self, other) }
111 fn cbrt(self) -> f32 {
112 unsafe { cmath::cbrtf(self) }
116 fn hypot(self, other: f32) -> f32 {
117 unsafe { cmath::hypotf(self, other) }
121 fn sin(self) -> f32 {
122 unsafe { intrinsics::sinf32(self) }
126 fn cos(self) -> f32 {
127 unsafe { intrinsics::cosf32(self) }
131 fn tan(self) -> f32 {
132 unsafe { cmath::tanf(self) }
136 fn asin(self) -> f32 {
137 unsafe { cmath::asinf(self) }
141 fn acos(self) -> f32 {
142 unsafe { cmath::acosf(self) }
146 fn atan(self) -> f32 {
147 unsafe { cmath::atanf(self) }
151 fn atan2(self, other: f32) -> f32 {
152 unsafe { cmath::atan2f(self, other) }
155 /// Simultaneously computes the sine and cosine of the number
157 fn sin_cos(self) -> (f32, f32) {
158 (self.sin(), self.cos())
161 /// Returns the exponential of the number, minus `1`, in a way that is
162 /// accurate even if the number is close to zero
164 fn exp_m1(self) -> f32 {
165 unsafe { cmath::expm1f(self) }
168 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
169 /// accurately than if the operations were performed separately
171 fn ln_1p(self) -> f32 {
172 unsafe { cmath::log1pf(self) }
176 fn sinh(self) -> f32 {
177 unsafe { cmath::sinhf(self) }
181 fn cosh(self) -> f32 {
182 unsafe { cmath::coshf(self) }
186 fn tanh(self) -> f32 {
187 unsafe { cmath::tanhf(self) }
190 /// Inverse hyperbolic sine
194 /// - on success, the inverse hyperbolic sine of `self` will be returned
195 /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
196 /// - `NAN` if `self` is `NAN`
198 fn asinh(self) -> f32 {
200 NEG_INFINITY => NEG_INFINITY,
201 x => (x + ((x * x) + 1.0).sqrt()).ln(),
205 /// Inverse hyperbolic cosine
209 /// - on success, the inverse hyperbolic cosine of `self` will be returned
210 /// - `INFINITY` if `self` is `INFINITY`
211 /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
213 fn acosh(self) -> f32 {
215 x if x < 1.0 => Float::nan(),
216 x => (x + ((x * x) - 1.0).sqrt()).ln(),
220 /// Inverse hyperbolic tangent
224 /// - on success, the inverse hyperbolic tangent of `self` will be returned
225 /// - `self` if `self` is `0.0` or `-0.0`
226 /// - `INFINITY` if `self` is `1.0`
227 /// - `NEG_INFINITY` if `self` is `-1.0`
228 /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
229 /// (including `INFINITY` and `NEG_INFINITY`)
231 fn atanh(self) -> f32 {
232 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
237 // Section: String Conversions
240 /// Converts a float to a string
244 /// * num - The float value
246 pub fn to_str(num: f32) -> String {
247 let (r, _) = strconv::float_to_str_common(
248 num, 10u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
252 /// Converts a float to a string in hexadecimal format
256 /// * num - The float value
258 pub fn to_str_hex(num: f32) -> String {
259 let (r, _) = strconv::float_to_str_common(
260 num, 16u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
264 /// Converts a float to a string in a given radix, and a flag indicating
265 /// whether it's a special value
269 /// * num - The float value
270 /// * radix - The base to use
272 pub fn to_str_radix_special(num: f32, rdx: uint) -> (String, bool) {
273 strconv::float_to_str_common(num, rdx, true,
274 strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false)
277 /// Converts a float to a string with exactly the number of
278 /// provided significant digits
282 /// * num - The float value
283 /// * digits - The number of significant digits
285 pub fn to_str_exact(num: f32, dig: uint) -> String {
286 let (r, _) = strconv::float_to_str_common(
287 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpNone, false);
291 /// Converts a float to a string with a maximum number of
292 /// significant digits
296 /// * num - The float value
297 /// * digits - The number of significant digits
299 pub fn to_str_digits(num: f32, dig: uint) -> String {
300 let (r, _) = strconv::float_to_str_common(
301 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpNone, false);
305 /// Converts a float to a string using the exponential notation with exactly the number of
306 /// provided digits after the decimal point in the significand
310 /// * num - The float value
311 /// * digits - The number of digits after the decimal point
312 /// * upper - Use `E` instead of `e` for the exponent sign
314 pub fn to_str_exp_exact(num: f32, dig: uint, upper: bool) -> String {
315 let (r, _) = strconv::float_to_str_common(
316 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
320 /// Converts a float to a string using the exponential notation with the maximum number of
321 /// digits after the decimal point in the significand
325 /// * num - The float value
326 /// * digits - The number of digits after the decimal point
327 /// * upper - Use `E` instead of `e` for the exponent sign
329 pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
330 let (r, _) = strconv::float_to_str_common(
331 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
335 impl num::ToStrRadix for f32 {
336 /// Converts a float to a string in a given radix
340 /// * num - The float value
341 /// * radix - The base to use
345 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
346 /// possible misinterpretation of the result at higher bases. If those values
347 /// are expected, use `to_str_radix_special()` instead.
349 fn to_str_radix(&self, rdx: uint) -> String {
350 let (r, special) = strconv::float_to_str_common(
351 *self, rdx, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
352 if special { fail!("number has a special value, \
353 try to_str_radix_special() if those are expected") }
358 /// Convert a string in base 16 to a float.
359 /// Accepts an optional binary exponent.
361 /// This function accepts strings such as
364 /// * '+a4.fe', equivalent to 'a4.fe'
366 /// * '2b.aP128', or equivalently, '2b.ap128'
368 /// * '.' (understood as 0)
370 /// * '.c', or, equivalently, '0.c'
371 /// * '+inf', 'inf', '-inf', 'NaN'
373 /// Leading and trailing whitespace represent an error.
381 /// `None` if the string did not represent a valid number. Otherwise,
382 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
384 pub fn from_str_hex(num: &str) -> Option<f32> {
385 strconv::from_str_common(num, 16u, true, true, true,
386 strconv::ExpBin, false, false)
389 impl FromStr for f32 {
390 /// Convert a string in base 10 to a float.
391 /// Accepts an optional decimal exponent.
393 /// This function accepts strings such as
396 /// * '+3.14', equivalent to '3.14'
398 /// * '2.5E10', or equivalently, '2.5e10'
400 /// * '.' (understood as 0)
402 /// * '.5', or, equivalently, '0.5'
403 /// * '+inf', 'inf', '-inf', 'NaN'
405 /// Leading and trailing whitespace represent an error.
413 /// `None` if the string did not represent a valid number. Otherwise,
414 /// `Some(n)` where `n` is the floating-point number represented by `num`.
416 fn from_str(val: &str) -> Option<f32> {
417 strconv::from_str_common(val, 10u, true, true, true,
418 strconv::ExpDec, false, false)
422 impl num::FromStrRadix for f32 {
423 /// Convert a string in a given base to a float.
425 /// Due to possible conflicts, this function does **not** accept
426 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
427 /// does it recognize exponents of any kind.
429 /// Leading and trailing whitespace represent an error.
434 /// * radix - The base to use. Must lie in the range [2 .. 36]
438 /// `None` if the string did not represent a valid number. Otherwise,
439 /// `Some(n)` where `n` is the floating-point number represented by `num`.
441 fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
442 strconv::from_str_common(val, rdx, true, true, false,
443 strconv::ExpNone, false, false)
455 assert_eq!(NAN.min(2.0), 2.0);
456 assert_eq!(2.0f32.min(NAN), 2.0);
461 assert_eq!(NAN.max(2.0), 2.0);
462 assert_eq!(2.0f32.max(NAN), 2.0);
467 num::test_num(10f32, 2f32);
472 assert_approx_eq!(1.0f32.floor(), 1.0f32);
473 assert_approx_eq!(1.3f32.floor(), 1.0f32);
474 assert_approx_eq!(1.5f32.floor(), 1.0f32);
475 assert_approx_eq!(1.7f32.floor(), 1.0f32);
476 assert_approx_eq!(0.0f32.floor(), 0.0f32);
477 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
478 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
479 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
480 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
481 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
486 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
487 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
488 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
489 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
490 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
491 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
492 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
493 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
494 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
495 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
500 assert_approx_eq!(1.0f32.round(), 1.0f32);
501 assert_approx_eq!(1.3f32.round(), 1.0f32);
502 assert_approx_eq!(1.5f32.round(), 2.0f32);
503 assert_approx_eq!(1.7f32.round(), 2.0f32);
504 assert_approx_eq!(0.0f32.round(), 0.0f32);
505 assert_approx_eq!((-0.0f32).round(), -0.0f32);
506 assert_approx_eq!((-1.0f32).round(), -1.0f32);
507 assert_approx_eq!((-1.3f32).round(), -1.0f32);
508 assert_approx_eq!((-1.5f32).round(), -2.0f32);
509 assert_approx_eq!((-1.7f32).round(), -2.0f32);
514 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
515 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
516 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
517 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
518 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
519 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
520 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
521 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
522 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
523 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
528 assert_approx_eq!(1.0f32.fract(), 0.0f32);
529 assert_approx_eq!(1.3f32.fract(), 0.3f32);
530 assert_approx_eq!(1.5f32.fract(), 0.5f32);
531 assert_approx_eq!(1.7f32.fract(), 0.7f32);
532 assert_approx_eq!(0.0f32.fract(), 0.0f32);
533 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
534 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
535 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
536 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
537 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
542 assert_eq!(0.0f32.asinh(), 0.0f32);
543 assert_eq!((-0.0f32).asinh(), -0.0f32);
545 let inf: f32 = Float::infinity();
546 let neg_inf: f32 = Float::neg_infinity();
547 let nan: f32 = Float::nan();
548 assert_eq!(inf.asinh(), inf);
549 assert_eq!(neg_inf.asinh(), neg_inf);
550 assert!(nan.asinh().is_nan());
551 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
552 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
557 assert_eq!(1.0f32.acosh(), 0.0f32);
558 assert!(0.999f32.acosh().is_nan());
560 let inf: f32 = Float::infinity();
561 let neg_inf: f32 = Float::neg_infinity();
562 let nan: f32 = Float::nan();
563 assert_eq!(inf.acosh(), inf);
564 assert!(neg_inf.acosh().is_nan());
565 assert!(nan.acosh().is_nan());
566 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
567 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
572 assert_eq!(0.0f32.atanh(), 0.0f32);
573 assert_eq!((-0.0f32).atanh(), -0.0f32);
575 let inf32: f32 = Float::infinity();
576 let neg_inf32: f32 = Float::neg_infinity();
577 assert_eq!(1.0f32.atanh(), inf32);
578 assert_eq!((-1.0f32).atanh(), neg_inf32);
580 assert!(2f64.atanh().atanh().is_nan());
581 assert!((-2f64).atanh().atanh().is_nan());
583 let inf64: f32 = Float::infinity();
584 let neg_inf64: f32 = Float::neg_infinity();
585 let nan32: f32 = Float::nan();
586 assert!(inf64.atanh().is_nan());
587 assert!(neg_inf64.atanh().is_nan());
588 assert!(nan32.atanh().is_nan());
590 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
591 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
595 fn test_real_consts() {
596 let pi: f32 = Float::pi();
597 let two_pi: f32 = Float::two_pi();
598 let frac_pi_2: f32 = Float::frac_pi_2();
599 let frac_pi_3: f32 = Float::frac_pi_3();
600 let frac_pi_4: f32 = Float::frac_pi_4();
601 let frac_pi_6: f32 = Float::frac_pi_6();
602 let frac_pi_8: f32 = Float::frac_pi_8();
603 let frac_1_pi: f32 = Float::frac_1_pi();
604 let frac_2_pi: f32 = Float::frac_2_pi();
605 let frac_2_sqrtpi: f32 = Float::frac_2_sqrtpi();
606 let sqrt2: f32 = Float::sqrt2();
607 let frac_1_sqrt2: f32 = Float::frac_1_sqrt2();
608 let e: f32 = Float::e();
609 let log2_e: f32 = Float::log2_e();
610 let log10_e: f32 = Float::log10_e();
611 let ln_2: f32 = Float::ln_2();
612 let ln_10: f32 = Float::ln_10();
614 assert_approx_eq!(two_pi, 2f32 * pi);
615 assert_approx_eq!(frac_pi_2, pi / 2f32);
616 assert_approx_eq!(frac_pi_3, pi / 3f32);
617 assert_approx_eq!(frac_pi_4, pi / 4f32);
618 assert_approx_eq!(frac_pi_6, pi / 6f32);
619 assert_approx_eq!(frac_pi_8, pi / 8f32);
620 assert_approx_eq!(frac_1_pi, 1f32 / pi);
621 assert_approx_eq!(frac_2_pi, 2f32 / pi);
622 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
623 assert_approx_eq!(sqrt2, 2f32.sqrt());
624 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
625 assert_approx_eq!(log2_e, e.log2());
626 assert_approx_eq!(log10_e, e.log10());
627 assert_approx_eq!(ln_2, 2f32.ln());
628 assert_approx_eq!(ln_10, 10f32.ln());
633 assert_eq!(INFINITY.abs(), INFINITY);
634 assert_eq!(1f32.abs(), 1f32);
635 assert_eq!(0f32.abs(), 0f32);
636 assert_eq!((-0f32).abs(), 0f32);
637 assert_eq!((-1f32).abs(), 1f32);
638 assert_eq!(NEG_INFINITY.abs(), INFINITY);
639 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
640 assert!(NAN.abs().is_nan());
645 assert_eq!((-1f32).abs_sub(&1f32), 0f32);
646 assert_eq!(1f32.abs_sub(&1f32), 0f32);
647 assert_eq!(1f32.abs_sub(&0f32), 1f32);
648 assert_eq!(1f32.abs_sub(&-1f32), 2f32);
649 assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
650 assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
651 assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
652 assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
656 fn test_abs_sub_nowin() {
657 assert!(NAN.abs_sub(&-1f32).is_nan());
658 assert!(1f32.abs_sub(&NAN).is_nan());
663 assert_eq!(INFINITY.signum(), 1f32);
664 assert_eq!(1f32.signum(), 1f32);
665 assert_eq!(0f32.signum(), 1f32);
666 assert_eq!((-0f32).signum(), -1f32);
667 assert_eq!((-1f32).signum(), -1f32);
668 assert_eq!(NEG_INFINITY.signum(), -1f32);
669 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
670 assert!(NAN.signum().is_nan());
674 fn test_is_positive() {
675 assert!(INFINITY.is_positive());
676 assert!(1f32.is_positive());
677 assert!(0f32.is_positive());
678 assert!(!(-0f32).is_positive());
679 assert!(!(-1f32).is_positive());
680 assert!(!NEG_INFINITY.is_positive());
681 assert!(!(1f32/NEG_INFINITY).is_positive());
682 assert!(!NAN.is_positive());
686 fn test_is_negative() {
687 assert!(!INFINITY.is_negative());
688 assert!(!1f32.is_negative());
689 assert!(!0f32.is_negative());
690 assert!((-0f32).is_negative());
691 assert!((-1f32).is_negative());
692 assert!(NEG_INFINITY.is_negative());
693 assert!((1f32/NEG_INFINITY).is_negative());
694 assert!(!NAN.is_negative());
698 fn test_is_normal() {
699 let nan: f32 = Float::nan();
700 let inf: f32 = Float::infinity();
701 let neg_inf: f32 = Float::neg_infinity();
702 let zero: f32 = Zero::zero();
703 let neg_zero: f32 = Float::neg_zero();
704 assert!(!nan.is_normal());
705 assert!(!inf.is_normal());
706 assert!(!neg_inf.is_normal());
707 assert!(!zero.is_normal());
708 assert!(!neg_zero.is_normal());
709 assert!(1f32.is_normal());
710 assert!(1e-37f32.is_normal());
711 assert!(!1e-38f32.is_normal());
716 let nan: f32 = Float::nan();
717 let inf: f32 = Float::infinity();
718 let neg_inf: f32 = Float::neg_infinity();
719 let zero: f32 = Zero::zero();
720 let neg_zero: f32 = Float::neg_zero();
721 assert_eq!(nan.classify(), FPNaN);
722 assert_eq!(inf.classify(), FPInfinite);
723 assert_eq!(neg_inf.classify(), FPInfinite);
724 assert_eq!(zero.classify(), FPZero);
725 assert_eq!(neg_zero.classify(), FPZero);
726 assert_eq!(1f32.classify(), FPNormal);
727 assert_eq!(1e-37f32.classify(), FPNormal);
728 assert_eq!(1e-38f32.classify(), FPSubnormal);
733 // We have to use from_str until base-2 exponents
734 // are supported in floating-point literals
735 let f1: f32 = from_str_hex("1p-123").unwrap();
736 let f2: f32 = from_str_hex("1p-111").unwrap();
737 assert_eq!(FloatMath::ldexp(1f32, -123), f1);
738 assert_eq!(FloatMath::ldexp(1f32, -111), f2);
740 assert_eq!(FloatMath::ldexp(0f32, -123), 0f32);
741 assert_eq!(FloatMath::ldexp(-0f32, -123), -0f32);
743 let inf: f32 = Float::infinity();
744 let neg_inf: f32 = Float::neg_infinity();
745 let nan: f32 = Float::nan();
746 assert_eq!(FloatMath::ldexp(inf, -123), inf);
747 assert_eq!(FloatMath::ldexp(neg_inf, -123), neg_inf);
748 assert!(FloatMath::ldexp(nan, -123).is_nan());
753 // We have to use from_str until base-2 exponents
754 // are supported in floating-point literals
755 let f1: f32 = from_str_hex("1p-123").unwrap();
756 let f2: f32 = from_str_hex("1p-111").unwrap();
757 let (x1, exp1) = f1.frexp();
758 let (x2, exp2) = f2.frexp();
759 assert_eq!((x1, exp1), (0.5f32, -122));
760 assert_eq!((x2, exp2), (0.5f32, -110));
761 assert_eq!(FloatMath::ldexp(x1, exp1), f1);
762 assert_eq!(FloatMath::ldexp(x2, exp2), f2);
764 assert_eq!(0f32.frexp(), (0f32, 0));
765 assert_eq!((-0f32).frexp(), (-0f32, 0));
768 #[test] #[ignore(cfg(windows))] // FIXME #8755
769 fn test_frexp_nowin() {
770 let inf: f32 = Float::infinity();
771 let neg_inf: f32 = Float::neg_infinity();
772 let nan: f32 = Float::nan();
773 assert_eq!(match inf.frexp() { (x, _) => x }, inf)
774 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
775 assert!(match nan.frexp() { (x, _) => x.is_nan() })
779 fn test_integer_decode() {
780 assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8));
781 assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8));
782 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608u64, 77i16, 1i8));
783 assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8));
784 assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8));
785 assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8));
786 assert_eq!(NEG_INFINITY.integer_decode(), (8388608u64, 105i16, -1i8));
787 assert_eq!(NAN.integer_decode(), (12582912u64, 105i16, 1i8));