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)
14 #![allow(missing_doc)]
15 #![allow(unsigned_negate)]
16 #![doc(primitive = "f32")]
20 use from_str::FromStr;
27 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
28 pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
29 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
30 pub use core::f32::consts;
34 use libc::{c_float, c_int};
38 pub fn acosf(n: c_float) -> c_float;
39 pub fn asinf(n: c_float) -> c_float;
40 pub fn atanf(n: c_float) -> c_float;
41 pub fn atan2f(a: c_float, b: c_float) -> c_float;
42 pub fn cbrtf(n: c_float) -> c_float;
43 pub fn coshf(n: c_float) -> c_float;
44 pub fn erff(n: c_float) -> c_float;
45 pub fn erfcf(n: c_float) -> c_float;
46 pub fn expm1f(n: c_float) -> c_float;
47 pub fn fdimf(a: c_float, b: c_float) -> c_float;
48 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
49 pub fn fmaxf(a: c_float, b: c_float) -> c_float;
50 pub fn fminf(a: c_float, b: c_float) -> c_float;
51 pub fn fmodf(a: c_float, b: c_float) -> c_float;
52 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
53 pub fn hypotf(x: c_float, y: c_float) -> c_float;
54 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
55 pub fn logbf(n: c_float) -> c_float;
56 pub fn log1pf(n: c_float) -> c_float;
57 pub fn ilogbf(n: c_float) -> c_int;
58 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
59 pub fn sinhf(n: c_float) -> c_float;
60 pub fn tanf(n: c_float) -> c_float;
61 pub fn tanhf(n: c_float) -> c_float;
62 pub fn tgammaf(n: c_float) -> c_float;
65 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
68 #[link_name="__lgammaf_r"]
69 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
73 impl FloatMath for f32 {
74 /// Constructs a floating point number by multiplying `x` by 2 raised to the
77 fn ldexp(x: f32, exp: int) -> f32 {
78 unsafe { cmath::ldexpf(x, exp as c_int) }
81 /// Breaks the number into a normalized fraction and a base-2 exponent,
84 /// - `self = x * pow(2, exp)`
85 /// - `0.5 <= abs(x) < 1.0`
87 fn frexp(self) -> (f32, int) {
90 let x = cmath::frexpf(self, &mut exp);
95 /// Returns the next representable floating-point value in the direction of
98 fn next_after(self, other: f32) -> f32 {
99 unsafe { cmath::nextafterf(self, other) }
103 fn max(self, other: f32) -> f32 {
104 unsafe { cmath::fmaxf(self, other) }
108 fn min(self, other: f32) -> f32 {
109 unsafe { cmath::fminf(self, other) }
113 fn cbrt(self) -> f32 {
114 unsafe { cmath::cbrtf(self) }
118 fn hypot(self, other: f32) -> f32 {
119 unsafe { cmath::hypotf(self, other) }
123 fn sin(self) -> f32 {
124 unsafe { intrinsics::sinf32(self) }
128 fn cos(self) -> f32 {
129 unsafe { intrinsics::cosf32(self) }
133 fn tan(self) -> f32 {
134 unsafe { cmath::tanf(self) }
138 fn asin(self) -> f32 {
139 unsafe { cmath::asinf(self) }
143 fn acos(self) -> f32 {
144 unsafe { cmath::acosf(self) }
148 fn atan(self) -> f32 {
149 unsafe { cmath::atanf(self) }
153 fn atan2(self, other: f32) -> f32 {
154 unsafe { cmath::atan2f(self, other) }
157 /// Simultaneously computes the sine and cosine of the number
159 fn sin_cos(self) -> (f32, f32) {
160 (self.sin(), self.cos())
163 /// Returns the exponential of the number, minus `1`, in a way that is
164 /// accurate even if the number is close to zero
166 fn exp_m1(self) -> f32 {
167 unsafe { cmath::expm1f(self) }
170 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
171 /// accurately than if the operations were performed separately
173 fn ln_1p(self) -> f32 {
174 unsafe { cmath::log1pf(self) }
178 fn sinh(self) -> f32 {
179 unsafe { cmath::sinhf(self) }
183 fn cosh(self) -> f32 {
184 unsafe { cmath::coshf(self) }
188 fn tanh(self) -> f32 {
189 unsafe { cmath::tanhf(self) }
192 /// Inverse hyperbolic sine
196 /// - on success, the inverse hyperbolic sine of `self` will be returned
197 /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
198 /// - `NAN` if `self` is `NAN`
200 fn asinh(self) -> f32 {
202 NEG_INFINITY => NEG_INFINITY,
203 x => (x + ((x * x) + 1.0).sqrt()).ln(),
207 /// Inverse hyperbolic cosine
211 /// - on success, the inverse hyperbolic cosine of `self` will be returned
212 /// - `INFINITY` if `self` is `INFINITY`
213 /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
215 fn acosh(self) -> f32 {
217 x if x < 1.0 => Float::nan(),
218 x => (x + ((x * x) - 1.0).sqrt()).ln(),
222 /// Inverse hyperbolic tangent
226 /// - on success, the inverse hyperbolic tangent of `self` will be returned
227 /// - `self` if `self` is `0.0` or `-0.0`
228 /// - `INFINITY` if `self` is `1.0`
229 /// - `NEG_INFINITY` if `self` is `-1.0`
230 /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
231 /// (including `INFINITY` and `NEG_INFINITY`)
233 fn atanh(self) -> f32 {
234 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
239 // Section: String Conversions
242 /// Converts a float to a string
246 /// * num - The float value
248 pub fn to_str(num: f32) -> String {
249 let (r, _) = strconv::float_to_str_common(
250 num, 10u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
254 /// Converts a float to a string in hexadecimal format
258 /// * num - The float value
260 pub fn to_str_hex(num: f32) -> String {
261 let (r, _) = strconv::float_to_str_common(
262 num, 16u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
266 /// Converts a float to a string in a given radix, and a flag indicating
267 /// whether it's a special value
271 /// * num - The float value
272 /// * radix - The base to use
274 pub fn to_str_radix_special(num: f32, rdx: uint) -> (String, bool) {
275 strconv::float_to_str_common(num, rdx, true,
276 strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false)
279 /// Converts a float to a string with exactly the number of
280 /// provided significant digits
284 /// * num - The float value
285 /// * digits - The number of significant digits
287 pub fn to_str_exact(num: f32, dig: uint) -> String {
288 let (r, _) = strconv::float_to_str_common(
289 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpNone, false);
293 /// Converts a float to a string with a maximum number of
294 /// significant digits
298 /// * num - The float value
299 /// * digits - The number of significant digits
301 pub fn to_str_digits(num: f32, dig: uint) -> String {
302 let (r, _) = strconv::float_to_str_common(
303 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpNone, false);
307 /// Converts a float to a string using the exponential notation with exactly the number of
308 /// provided digits after the decimal point in the significand
312 /// * num - The float value
313 /// * digits - The number of digits after the decimal point
314 /// * upper - Use `E` instead of `e` for the exponent sign
316 pub fn to_str_exp_exact(num: f32, dig: uint, upper: bool) -> String {
317 let (r, _) = strconv::float_to_str_common(
318 num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
322 /// Converts a float to a string using the exponential notation with the maximum number of
323 /// digits after the decimal point in the significand
327 /// * num - The float value
328 /// * digits - The number of digits after the decimal point
329 /// * upper - Use `E` instead of `e` for the exponent sign
331 pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
332 let (r, _) = strconv::float_to_str_common(
333 num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
337 impl num::ToStrRadix for f32 {
338 /// Converts a float to a string in a given radix
342 /// * num - The float value
343 /// * radix - The base to use
347 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
348 /// possible misinterpretation of the result at higher bases. If those values
349 /// are expected, use `to_str_radix_special()` instead.
351 fn to_str_radix(&self, rdx: uint) -> String {
352 let (r, special) = strconv::float_to_str_common(
353 *self, rdx, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
354 if special { fail!("number has a special value, \
355 try to_str_radix_special() if those are expected") }
360 /// Convert a string in base 16 to a float.
361 /// Accepts an optional binary exponent.
363 /// This function accepts strings such as
366 /// * '+a4.fe', equivalent to 'a4.fe'
368 /// * '2b.aP128', or equivalently, '2b.ap128'
370 /// * '.' (understood as 0)
372 /// * '.c', or, equivalently, '0.c'
373 /// * '+inf', 'inf', '-inf', 'NaN'
375 /// Leading and trailing whitespace represent an error.
383 /// `None` if the string did not represent a valid number. Otherwise,
384 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
386 pub fn from_str_hex(num: &str) -> Option<f32> {
387 strconv::from_str_common(num, 16u, true, true, true,
388 strconv::ExpBin, false, false)
391 impl FromStr for f32 {
392 /// Convert a string in base 10 to a float.
393 /// Accepts an optional decimal exponent.
395 /// This function accepts strings such as
398 /// * '+3.14', equivalent to '3.14'
400 /// * '2.5E10', or equivalently, '2.5e10'
402 /// * '.' (understood as 0)
404 /// * '.5', or, equivalently, '0.5'
405 /// * '+inf', 'inf', '-inf', 'NaN'
407 /// Leading and trailing whitespace represent an error.
415 /// `None` if the string did not represent a valid number. Otherwise,
416 /// `Some(n)` where `n` is the floating-point number represented by `num`.
418 fn from_str(val: &str) -> Option<f32> {
419 strconv::from_str_common(val, 10u, true, true, true,
420 strconv::ExpDec, false, false)
424 impl num::FromStrRadix for f32 {
425 /// Convert a string in a given base to a float.
427 /// Due to possible conflicts, this function does **not** accept
428 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
429 /// does it recognize exponents of any kind.
431 /// Leading and trailing whitespace represent an error.
436 /// * radix - The base to use. Must lie in the range [2 .. 36]
440 /// `None` if the string did not represent a valid number. Otherwise,
441 /// `Some(n)` where `n` is the floating-point number represented by `num`.
443 fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
444 strconv::from_str_common(val, rdx, true, true, false,
445 strconv::ExpNone, false, false)
457 assert_eq!(NAN.min(2.0), 2.0);
458 assert_eq!(2.0f32.min(NAN), 2.0);
463 assert_eq!(NAN.max(2.0), 2.0);
464 assert_eq!(2.0f32.max(NAN), 2.0);
469 num::test_num(10f32, 2f32);
474 assert_approx_eq!(1.0f32.floor(), 1.0f32);
475 assert_approx_eq!(1.3f32.floor(), 1.0f32);
476 assert_approx_eq!(1.5f32.floor(), 1.0f32);
477 assert_approx_eq!(1.7f32.floor(), 1.0f32);
478 assert_approx_eq!(0.0f32.floor(), 0.0f32);
479 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
480 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
481 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
482 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
483 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
488 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
489 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
490 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
491 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
492 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
493 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
494 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
495 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
496 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
497 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
502 assert_approx_eq!(1.0f32.round(), 1.0f32);
503 assert_approx_eq!(1.3f32.round(), 1.0f32);
504 assert_approx_eq!(1.5f32.round(), 2.0f32);
505 assert_approx_eq!(1.7f32.round(), 2.0f32);
506 assert_approx_eq!(0.0f32.round(), 0.0f32);
507 assert_approx_eq!((-0.0f32).round(), -0.0f32);
508 assert_approx_eq!((-1.0f32).round(), -1.0f32);
509 assert_approx_eq!((-1.3f32).round(), -1.0f32);
510 assert_approx_eq!((-1.5f32).round(), -2.0f32);
511 assert_approx_eq!((-1.7f32).round(), -2.0f32);
516 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
517 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
518 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
519 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
520 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
521 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
522 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
523 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
524 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
525 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
530 assert_approx_eq!(1.0f32.fract(), 0.0f32);
531 assert_approx_eq!(1.3f32.fract(), 0.3f32);
532 assert_approx_eq!(1.5f32.fract(), 0.5f32);
533 assert_approx_eq!(1.7f32.fract(), 0.7f32);
534 assert_approx_eq!(0.0f32.fract(), 0.0f32);
535 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
536 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
537 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
538 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
539 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
544 assert_eq!(0.0f32.asinh(), 0.0f32);
545 assert_eq!((-0.0f32).asinh(), -0.0f32);
547 let inf: f32 = Float::infinity();
548 let neg_inf: f32 = Float::neg_infinity();
549 let nan: f32 = Float::nan();
550 assert_eq!(inf.asinh(), inf);
551 assert_eq!(neg_inf.asinh(), neg_inf);
552 assert!(nan.asinh().is_nan());
553 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
554 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
559 assert_eq!(1.0f32.acosh(), 0.0f32);
560 assert!(0.999f32.acosh().is_nan());
562 let inf: f32 = Float::infinity();
563 let neg_inf: f32 = Float::neg_infinity();
564 let nan: f32 = Float::nan();
565 assert_eq!(inf.acosh(), inf);
566 assert!(neg_inf.acosh().is_nan());
567 assert!(nan.acosh().is_nan());
568 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
569 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
574 assert_eq!(0.0f32.atanh(), 0.0f32);
575 assert_eq!((-0.0f32).atanh(), -0.0f32);
577 let inf32: f32 = Float::infinity();
578 let neg_inf32: f32 = Float::neg_infinity();
579 assert_eq!(1.0f32.atanh(), inf32);
580 assert_eq!((-1.0f32).atanh(), neg_inf32);
582 assert!(2f64.atanh().atanh().is_nan());
583 assert!((-2f64).atanh().atanh().is_nan());
585 let inf64: f32 = Float::infinity();
586 let neg_inf64: f32 = Float::neg_infinity();
587 let nan32: f32 = Float::nan();
588 assert!(inf64.atanh().is_nan());
589 assert!(neg_inf64.atanh().is_nan());
590 assert!(nan32.atanh().is_nan());
592 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
593 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
597 fn test_real_consts() {
598 let pi: f32 = Float::pi();
599 let two_pi: f32 = Float::two_pi();
600 let frac_pi_2: f32 = Float::frac_pi_2();
601 let frac_pi_3: f32 = Float::frac_pi_3();
602 let frac_pi_4: f32 = Float::frac_pi_4();
603 let frac_pi_6: f32 = Float::frac_pi_6();
604 let frac_pi_8: f32 = Float::frac_pi_8();
605 let frac_1_pi: f32 = Float::frac_1_pi();
606 let frac_2_pi: f32 = Float::frac_2_pi();
607 let frac_2_sqrtpi: f32 = Float::frac_2_sqrtpi();
608 let sqrt2: f32 = Float::sqrt2();
609 let frac_1_sqrt2: f32 = Float::frac_1_sqrt2();
610 let e: f32 = Float::e();
611 let log2_e: f32 = Float::log2_e();
612 let log10_e: f32 = Float::log10_e();
613 let ln_2: f32 = Float::ln_2();
614 let ln_10: f32 = Float::ln_10();
616 assert_approx_eq!(two_pi, 2f32 * pi);
617 assert_approx_eq!(frac_pi_2, pi / 2f32);
618 assert_approx_eq!(frac_pi_3, pi / 3f32);
619 assert_approx_eq!(frac_pi_4, pi / 4f32);
620 assert_approx_eq!(frac_pi_6, pi / 6f32);
621 assert_approx_eq!(frac_pi_8, pi / 8f32);
622 assert_approx_eq!(frac_1_pi, 1f32 / pi);
623 assert_approx_eq!(frac_2_pi, 2f32 / pi);
624 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
625 assert_approx_eq!(sqrt2, 2f32.sqrt());
626 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
627 assert_approx_eq!(log2_e, e.log2());
628 assert_approx_eq!(log10_e, e.log10());
629 assert_approx_eq!(ln_2, 2f32.ln());
630 assert_approx_eq!(ln_10, 10f32.ln());
635 assert_eq!(INFINITY.abs(), INFINITY);
636 assert_eq!(1f32.abs(), 1f32);
637 assert_eq!(0f32.abs(), 0f32);
638 assert_eq!((-0f32).abs(), 0f32);
639 assert_eq!((-1f32).abs(), 1f32);
640 assert_eq!(NEG_INFINITY.abs(), INFINITY);
641 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
642 assert!(NAN.abs().is_nan());
647 assert_eq!((-1f32).abs_sub(&1f32), 0f32);
648 assert_eq!(1f32.abs_sub(&1f32), 0f32);
649 assert_eq!(1f32.abs_sub(&0f32), 1f32);
650 assert_eq!(1f32.abs_sub(&-1f32), 2f32);
651 assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
652 assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
653 assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
654 assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
658 fn test_abs_sub_nowin() {
659 assert!(NAN.abs_sub(&-1f32).is_nan());
660 assert!(1f32.abs_sub(&NAN).is_nan());
665 assert_eq!(INFINITY.signum(), 1f32);
666 assert_eq!(1f32.signum(), 1f32);
667 assert_eq!(0f32.signum(), 1f32);
668 assert_eq!((-0f32).signum(), -1f32);
669 assert_eq!((-1f32).signum(), -1f32);
670 assert_eq!(NEG_INFINITY.signum(), -1f32);
671 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
672 assert!(NAN.signum().is_nan());
676 fn test_is_positive() {
677 assert!(INFINITY.is_positive());
678 assert!(1f32.is_positive());
679 assert!(0f32.is_positive());
680 assert!(!(-0f32).is_positive());
681 assert!(!(-1f32).is_positive());
682 assert!(!NEG_INFINITY.is_positive());
683 assert!(!(1f32/NEG_INFINITY).is_positive());
684 assert!(!NAN.is_positive());
688 fn test_is_negative() {
689 assert!(!INFINITY.is_negative());
690 assert!(!1f32.is_negative());
691 assert!(!0f32.is_negative());
692 assert!((-0f32).is_negative());
693 assert!((-1f32).is_negative());
694 assert!(NEG_INFINITY.is_negative());
695 assert!((1f32/NEG_INFINITY).is_negative());
696 assert!(!NAN.is_negative());
700 fn test_is_normal() {
701 let nan: f32 = Float::nan();
702 let inf: f32 = Float::infinity();
703 let neg_inf: f32 = Float::neg_infinity();
704 let zero: f32 = Zero::zero();
705 let neg_zero: f32 = Float::neg_zero();
706 assert!(!nan.is_normal());
707 assert!(!inf.is_normal());
708 assert!(!neg_inf.is_normal());
709 assert!(!zero.is_normal());
710 assert!(!neg_zero.is_normal());
711 assert!(1f32.is_normal());
712 assert!(1e-37f32.is_normal());
713 assert!(!1e-38f32.is_normal());
718 let nan: f32 = Float::nan();
719 let inf: f32 = Float::infinity();
720 let neg_inf: f32 = Float::neg_infinity();
721 let zero: f32 = Zero::zero();
722 let neg_zero: f32 = Float::neg_zero();
723 assert_eq!(nan.classify(), FPNaN);
724 assert_eq!(inf.classify(), FPInfinite);
725 assert_eq!(neg_inf.classify(), FPInfinite);
726 assert_eq!(zero.classify(), FPZero);
727 assert_eq!(neg_zero.classify(), FPZero);
728 assert_eq!(1f32.classify(), FPNormal);
729 assert_eq!(1e-37f32.classify(), FPNormal);
730 assert_eq!(1e-38f32.classify(), FPSubnormal);
735 // We have to use from_str until base-2 exponents
736 // are supported in floating-point literals
737 let f1: f32 = from_str_hex("1p-123").unwrap();
738 let f2: f32 = from_str_hex("1p-111").unwrap();
739 assert_eq!(FloatMath::ldexp(1f32, -123), f1);
740 assert_eq!(FloatMath::ldexp(1f32, -111), f2);
742 assert_eq!(FloatMath::ldexp(0f32, -123), 0f32);
743 assert_eq!(FloatMath::ldexp(-0f32, -123), -0f32);
745 let inf: f32 = Float::infinity();
746 let neg_inf: f32 = Float::neg_infinity();
747 let nan: f32 = Float::nan();
748 assert_eq!(FloatMath::ldexp(inf, -123), inf);
749 assert_eq!(FloatMath::ldexp(neg_inf, -123), neg_inf);
750 assert!(FloatMath::ldexp(nan, -123).is_nan());
755 // We have to use from_str until base-2 exponents
756 // are supported in floating-point literals
757 let f1: f32 = from_str_hex("1p-123").unwrap();
758 let f2: f32 = from_str_hex("1p-111").unwrap();
759 let (x1, exp1) = f1.frexp();
760 let (x2, exp2) = f2.frexp();
761 assert_eq!((x1, exp1), (0.5f32, -122));
762 assert_eq!((x2, exp2), (0.5f32, -110));
763 assert_eq!(FloatMath::ldexp(x1, exp1), f1);
764 assert_eq!(FloatMath::ldexp(x2, exp2), f2);
766 assert_eq!(0f32.frexp(), (0f32, 0));
767 assert_eq!((-0f32).frexp(), (-0f32, 0));
770 #[test] #[ignore(cfg(windows))] // FIXME #8755
771 fn test_frexp_nowin() {
772 let inf: f32 = Float::infinity();
773 let neg_inf: f32 = Float::neg_infinity();
774 let nan: f32 = Float::nan();
775 assert_eq!(match inf.frexp() { (x, _) => x }, inf)
776 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
777 assert!(match nan.frexp() { (x, _) => x.is_nan() })
781 fn test_integer_decode() {
782 assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8));
783 assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8));
784 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608u64, 77i16, 1i8));
785 assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8));
786 assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8));
787 assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8));
788 assert_eq!(NEG_INFINITY.integer_decode(), (8388608u64, 105i16, -1i8));
789 assert_eq!(NAN.integer_decode(), (12582912u64, 105i16, 1i8));