1 // Copyright 2012-2015 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 #![stable(feature = "rust1", since = "1.0.0")]
14 #![allow(missing_docs)]
15 #![allow(unsigned_negation)]
16 #![doc(primitive = "f32")]
22 use num::{Float, FpCategory};
24 use num::strconv::ExponentFormat::{ExpNone, ExpDec};
25 use num::strconv::SignificantDigits::{DigAll, DigMax, DigExact};
26 use num::strconv::SignFormat::SignNeg;
30 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
31 pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
32 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
33 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
34 pub use core::f32::consts;
38 use libc::{c_float, c_int};
42 pub fn acosf(n: c_float) -> c_float;
43 pub fn asinf(n: c_float) -> c_float;
44 pub fn atanf(n: c_float) -> c_float;
45 pub fn atan2f(a: c_float, b: c_float) -> c_float;
46 pub fn cbrtf(n: c_float) -> c_float;
47 pub fn coshf(n: c_float) -> c_float;
48 pub fn erff(n: c_float) -> c_float;
49 pub fn erfcf(n: c_float) -> c_float;
50 pub fn expm1f(n: c_float) -> c_float;
51 pub fn fdimf(a: c_float, b: c_float) -> c_float;
52 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
53 pub fn fmaxf(a: c_float, b: c_float) -> c_float;
54 pub fn fminf(a: c_float, b: c_float) -> c_float;
55 pub fn fmodf(a: c_float, b: c_float) -> c_float;
56 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
57 pub fn hypotf(x: c_float, y: c_float) -> c_float;
58 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
59 pub fn logbf(n: c_float) -> c_float;
60 pub fn log1pf(n: c_float) -> c_float;
61 pub fn ilogbf(n: c_float) -> c_int;
62 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
63 pub fn sinhf(n: c_float) -> c_float;
64 pub fn tanf(n: c_float) -> c_float;
65 pub fn tanhf(n: c_float) -> c_float;
66 pub fn tgammaf(n: c_float) -> c_float;
69 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
72 #[link_name="__lgammaf_r"]
73 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
77 #[stable(feature = "rust1", since = "1.0.0")]
81 fn nan() -> f32 { num::Float::nan() }
83 fn infinity() -> f32 { num::Float::infinity() }
85 fn neg_infinity() -> f32 { num::Float::neg_infinity() }
87 fn zero() -> f32 { num::Float::zero() }
89 fn neg_zero() -> f32 { num::Float::neg_zero() }
91 fn one() -> f32 { num::Float::one() }
95 fn mantissa_digits(unused_self: Option<f32>) -> usize {
96 num::Float::mantissa_digits(unused_self)
100 fn digits(unused_self: Option<f32>) -> usize { num::Float::digits(unused_self) }
103 fn epsilon() -> f32 { num::Float::epsilon() }
106 fn min_exp(unused_self: Option<f32>) -> isize { num::Float::min_exp(unused_self) }
109 fn max_exp(unused_self: Option<f32>) -> isize { num::Float::max_exp(unused_self) }
112 fn min_10_exp(unused_self: Option<f32>) -> isize { num::Float::min_10_exp(unused_self) }
115 fn max_10_exp(unused_self: Option<f32>) -> isize { num::Float::max_10_exp(unused_self) }
118 fn min_value() -> f32 { num::Float::min_value() }
121 fn min_pos_value(unused_self: Option<f32>) -> f32 { num::Float::min_pos_value(unused_self) }
124 fn max_value() -> f32 { num::Float::max_value() }
127 fn is_nan(self) -> bool { num::Float::is_nan(self) }
129 fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
131 fn is_finite(self) -> bool { num::Float::is_finite(self) }
133 fn is_normal(self) -> bool { num::Float::is_normal(self) }
135 fn classify(self) -> FpCategory { num::Float::classify(self) }
138 fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
141 fn floor(self) -> f32 { num::Float::floor(self) }
143 fn ceil(self) -> f32 { num::Float::ceil(self) }
145 fn round(self) -> f32 { num::Float::round(self) }
147 fn trunc(self) -> f32 { num::Float::trunc(self) }
149 fn fract(self) -> f32 { num::Float::fract(self) }
152 fn abs(self) -> f32 { num::Float::abs(self) }
154 fn signum(self) -> f32 { num::Float::signum(self) }
156 fn is_positive(self) -> bool { num::Float::is_positive(self) }
158 fn is_negative(self) -> bool { num::Float::is_negative(self) }
161 fn mul_add(self, a: f32, b: f32) -> f32 { num::Float::mul_add(self, a, b) }
163 fn recip(self) -> f32 { num::Float::recip(self) }
166 fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
168 fn powf(self, n: f32) -> f32 { num::Float::powf(self, n) }
171 fn sqrt(self) -> f32 { num::Float::sqrt(self) }
173 fn rsqrt(self) -> f32 { num::Float::rsqrt(self) }
176 fn exp(self) -> f32 { num::Float::exp(self) }
178 fn exp2(self) -> f32 { num::Float::exp2(self) }
180 fn ln(self) -> f32 { num::Float::ln(self) }
182 fn log(self, base: f32) -> f32 { num::Float::log(self, base) }
184 fn log2(self) -> f32 { num::Float::log2(self) }
186 fn log10(self) -> f32 { num::Float::log10(self) }
188 fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
190 fn to_radians(self) -> f32 { num::Float::to_radians(self) }
192 /// Constructs a floating point number by multiplying `x` by 2 raised to the
195 fn ldexp(self, exp: isize) -> f32 {
196 unsafe { cmath::ldexpf(self, exp as c_int) }
199 /// Breaks the number into a normalized fraction and a base-2 exponent,
202 /// - `self = x * pow(2, exp)`
203 /// - `0.5 <= abs(x) < 1.0`
205 fn frexp(self) -> (f32, isize) {
208 let x = cmath::frexpf(self, &mut exp);
213 /// Returns the next representable floating-point value in the direction of
216 fn next_after(self, other: f32) -> f32 {
217 unsafe { cmath::nextafterf(self, other) }
221 fn max(self, other: f32) -> f32 {
222 unsafe { cmath::fmaxf(self, other) }
226 fn min(self, other: f32) -> f32 {
227 unsafe { cmath::fminf(self, other) }
231 fn abs_sub(self, other: f32) -> f32 {
232 unsafe { cmath::fdimf(self, other) }
236 fn cbrt(self) -> f32 {
237 unsafe { cmath::cbrtf(self) }
241 fn hypot(self, other: f32) -> f32 {
242 unsafe { cmath::hypotf(self, other) }
246 fn sin(self) -> f32 {
247 unsafe { intrinsics::sinf32(self) }
251 fn cos(self) -> f32 {
252 unsafe { intrinsics::cosf32(self) }
256 fn tan(self) -> f32 {
257 unsafe { cmath::tanf(self) }
261 fn asin(self) -> f32 {
262 unsafe { cmath::asinf(self) }
266 fn acos(self) -> f32 {
267 unsafe { cmath::acosf(self) }
271 fn atan(self) -> f32 {
272 unsafe { cmath::atanf(self) }
276 fn atan2(self, other: f32) -> f32 {
277 unsafe { cmath::atan2f(self, other) }
280 /// Simultaneously computes the sine and cosine of the number
282 fn sin_cos(self) -> (f32, f32) {
283 (self.sin(), self.cos())
286 /// Returns the exponential of the number, minus `1`, in a way that is
287 /// accurate even if the number is close to zero
289 fn exp_m1(self) -> f32 {
290 unsafe { cmath::expm1f(self) }
293 /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
294 /// accurately than if the operations were performed separately
296 fn ln_1p(self) -> f32 {
297 unsafe { cmath::log1pf(self) }
301 fn sinh(self) -> f32 {
302 unsafe { cmath::sinhf(self) }
306 fn cosh(self) -> f32 {
307 unsafe { cmath::coshf(self) }
311 fn tanh(self) -> f32 {
312 unsafe { cmath::tanhf(self) }
315 /// Inverse hyperbolic sine
319 /// - on success, the inverse hyperbolic sine of `self` will be returned
320 /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
321 /// - `NAN` if `self` is `NAN`
323 fn asinh(self) -> f32 {
325 NEG_INFINITY => NEG_INFINITY,
326 x => (x + ((x * x) + 1.0).sqrt()).ln(),
330 /// Inverse hyperbolic cosine
334 /// - on success, the inverse hyperbolic cosine of `self` will be returned
335 /// - `INFINITY` if `self` is `INFINITY`
336 /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
338 fn acosh(self) -> f32 {
340 x if x < 1.0 => Float::nan(),
341 x => (x + ((x * x) - 1.0).sqrt()).ln(),
345 /// Inverse hyperbolic tangent
349 /// - on success, the inverse hyperbolic tangent of `self` will be returned
350 /// - `self` if `self` is `0.0` or `-0.0`
351 /// - `INFINITY` if `self` is `1.0`
352 /// - `NEG_INFINITY` if `self` is `-1.0`
353 /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
354 /// (including `INFINITY` and `NEG_INFINITY`)
356 fn atanh(self) -> f32 {
357 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
363 #[stable(feature = "rust1", since = "1.0.0")]
365 /// Returns `true` if this value is `NaN` and false otherwise.
370 /// let nan = f32::NAN;
373 /// assert!(nan.is_nan());
374 /// assert!(!f.is_nan());
376 #[stable(feature = "rust1", since = "1.0.0")]
378 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
380 /// Returns `true` if this value is positive infinity or negative infinity and
387 /// let inf = f32::INFINITY;
388 /// let neg_inf = f32::NEG_INFINITY;
389 /// let nan = f32::NAN;
391 /// assert!(!f.is_infinite());
392 /// assert!(!nan.is_infinite());
394 /// assert!(inf.is_infinite());
395 /// assert!(neg_inf.is_infinite());
397 #[stable(feature = "rust1", since = "1.0.0")]
399 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
401 /// Returns `true` if this number is neither infinite nor `NaN`.
407 /// let inf = f32::INFINITY;
408 /// let neg_inf = f32::NEG_INFINITY;
409 /// let nan = f32::NAN;
411 /// assert!(f.is_finite());
413 /// assert!(!nan.is_finite());
414 /// assert!(!inf.is_finite());
415 /// assert!(!neg_inf.is_finite());
417 #[stable(feature = "rust1", since = "1.0.0")]
419 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
421 /// Returns `true` if the number is neither zero, infinite,
422 /// [subnormal][subnormal], or `NaN`.
425 /// # #![feature(std_misc)]
428 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
429 /// let max = f32::MAX;
430 /// let lower_than_min = 1.0e-40_f32;
431 /// let zero = 0.0_f32;
433 /// assert!(min.is_normal());
434 /// assert!(max.is_normal());
436 /// assert!(!zero.is_normal());
437 /// assert!(!f32::NAN.is_normal());
438 /// assert!(!f32::INFINITY.is_normal());
439 /// // Values between `0` and `min` are Subnormal.
440 /// assert!(!lower_than_min.is_normal());
442 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
443 #[stable(feature = "rust1", since = "1.0.0")]
445 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
447 /// Returns the floating point category of the number. If only one property
448 /// is going to be tested, it is generally faster to use the specific
449 /// predicate instead.
452 /// use std::num::FpCategory;
455 /// let num = 12.4_f32;
456 /// let inf = f32::INFINITY;
458 /// assert_eq!(num.classify(), FpCategory::Normal);
459 /// assert_eq!(inf.classify(), FpCategory::Infinite);
461 #[stable(feature = "rust1", since = "1.0.0")]
463 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
465 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
466 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
467 /// The floating point encoding is documented in the [Reference][floating-point].
470 /// # #![feature(std_misc)]
473 /// let num = 2.0f32;
475 /// // (8388608, -22, 1)
476 /// let (mantissa, exponent, sign) = num.integer_decode();
477 /// let sign_f = sign as f32;
478 /// let mantissa_f = mantissa as f32;
479 /// let exponent_f = num.powf(exponent as f32);
481 /// // 1 * 8388608 * 2^(-22) == 2
482 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
484 /// assert!(abs_difference <= f32::EPSILON);
486 /// [floating-point]: ../../../../../reference.html#machine-types
487 #[unstable(feature = "std_misc", reason = "signature is undecided")]
489 pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
491 /// Returns the largest integer less than or equal to a number.
494 /// let f = 3.99_f32;
497 /// assert_eq!(f.floor(), 3.0);
498 /// assert_eq!(g.floor(), 3.0);
500 #[stable(feature = "rust1", since = "1.0.0")]
502 pub fn floor(self) -> f32 { num::Float::floor(self) }
504 /// Returns the smallest integer greater than or equal to a number.
507 /// let f = 3.01_f32;
510 /// assert_eq!(f.ceil(), 4.0);
511 /// assert_eq!(g.ceil(), 4.0);
513 #[stable(feature = "rust1", since = "1.0.0")]
515 pub fn ceil(self) -> f32 { num::Float::ceil(self) }
517 /// Returns the nearest integer to a number. Round half-way cases away from
522 /// let g = -3.3_f32;
524 /// assert_eq!(f.round(), 3.0);
525 /// assert_eq!(g.round(), -3.0);
527 #[stable(feature = "rust1", since = "1.0.0")]
529 pub fn round(self) -> f32 { num::Float::round(self) }
531 /// Return the integer part of a number.
535 /// let g = -3.7_f32;
537 /// assert_eq!(f.trunc(), 3.0);
538 /// assert_eq!(g.trunc(), -3.0);
540 #[stable(feature = "rust1", since = "1.0.0")]
542 pub fn trunc(self) -> f32 { num::Float::trunc(self) }
544 /// Returns the fractional part of a number.
550 /// let y = -3.5_f32;
551 /// let abs_difference_x = (x.fract() - 0.5).abs();
552 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
554 /// assert!(abs_difference_x <= f32::EPSILON);
555 /// assert!(abs_difference_y <= f32::EPSILON);
557 #[stable(feature = "rust1", since = "1.0.0")]
559 pub fn fract(self) -> f32 { num::Float::fract(self) }
561 /// Computes the absolute value of `self`. Returns `NAN` if the
568 /// let y = -3.5_f32;
570 /// let abs_difference_x = (x.abs() - x).abs();
571 /// let abs_difference_y = (y.abs() - (-y)).abs();
573 /// assert!(abs_difference_x <= f32::EPSILON);
574 /// assert!(abs_difference_y <= f32::EPSILON);
576 /// assert!(f32::NAN.abs().is_nan());
578 #[stable(feature = "rust1", since = "1.0.0")]
580 pub fn abs(self) -> f32 { num::Float::abs(self) }
582 /// Returns a number that represents the sign of `self`.
584 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
585 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
586 /// - `NAN` if the number is `NAN`
593 /// assert_eq!(f.signum(), 1.0);
594 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
596 /// assert!(f32::NAN.signum().is_nan());
598 #[stable(feature = "rust1", since = "1.0.0")]
600 pub fn signum(self) -> f32 { num::Float::signum(self) }
602 /// Returns `true` if `self`'s sign bit is positive, including
603 /// `+0.0` and `INFINITY`.
608 /// let nan = f32::NAN;
610 /// let g = -7.0_f32;
612 /// assert!(f.is_sign_positive());
613 /// assert!(!g.is_sign_positive());
614 /// // Requires both tests to determine if is `NaN`
615 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
617 #[stable(feature = "rust1", since = "1.0.0")]
619 pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) }
621 #[stable(feature = "rust1", since = "1.0.0")]
622 #[deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
624 pub fn is_positive(self) -> bool { num::Float::is_positive(self) }
626 /// Returns `true` if `self`'s sign is negative, including `-0.0`
627 /// and `NEG_INFINITY`.
632 /// let nan = f32::NAN;
636 /// assert!(!f.is_sign_negative());
637 /// assert!(g.is_sign_negative());
638 /// // Requires both tests to determine if is `NaN`.
639 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
641 #[stable(feature = "rust1", since = "1.0.0")]
643 pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) }
645 #[stable(feature = "rust1", since = "1.0.0")]
646 #[deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
648 pub fn is_negative(self) -> bool { num::Float::is_negative(self) }
650 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
651 /// error. This produces a more accurate result with better performance than
652 /// a separate multiplication operation followed by an add.
657 /// let m = 10.0_f32;
659 /// let b = 60.0_f32;
662 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
664 /// assert!(abs_difference <= f32::EPSILON);
666 #[stable(feature = "rust1", since = "1.0.0")]
668 pub fn mul_add(self, a: f32, b: f32) -> f32 { num::Float::mul_add(self, a, b) }
670 /// Take the reciprocal (inverse) of a number, `1/x`.
676 /// let abs_difference = (x.recip() - (1.0/x)).abs();
678 /// assert!(abs_difference <= f32::EPSILON);
680 #[stable(feature = "rust1", since = "1.0.0")]
682 pub fn recip(self) -> f32 { num::Float::recip(self) }
684 /// Raise a number to an integer power.
686 /// Using this function is generally faster than using `powf`
692 /// let abs_difference = (x.powi(2) - x*x).abs();
694 /// assert!(abs_difference <= f32::EPSILON);
696 #[stable(feature = "rust1", since = "1.0.0")]
698 pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
700 /// Raise a number to a floating point power.
706 /// let abs_difference = (x.powf(2.0) - x*x).abs();
708 /// assert!(abs_difference <= f32::EPSILON);
710 #[stable(feature = "rust1", since = "1.0.0")]
712 pub fn powf(self, n: f32) -> f32 { num::Float::powf(self, n) }
714 /// Take the square root of a number.
716 /// Returns NaN if `self` is a negative number.
721 /// let positive = 4.0_f32;
722 /// let negative = -4.0_f32;
724 /// let abs_difference = (positive.sqrt() - 2.0).abs();
726 /// assert!(abs_difference <= f32::EPSILON);
727 /// assert!(negative.sqrt().is_nan());
729 #[stable(feature = "rust1", since = "1.0.0")]
731 pub fn sqrt(self) -> f32 { num::Float::sqrt(self) }
733 /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
736 /// # #![feature(std_misc)]
741 /// let abs_difference = (f.rsqrt() - 0.5).abs();
743 /// assert!(abs_difference <= f32::EPSILON);
745 #[unstable(feature = "std_misc",
746 reason = "unsure about its place in the world")]
747 #[deprecated(since = "1.0.0", reason = "use self.sqrt().recip() instead")]
749 pub fn rsqrt(self) -> f32 { num::Float::rsqrt(self) }
751 /// Returns `e^(self)`, (the exponential function).
756 /// let one = 1.0f32;
758 /// let e = one.exp();
760 /// // ln(e) - 1 == 0
761 /// let abs_difference = (e.ln() - 1.0).abs();
763 /// assert!(abs_difference <= f32::EPSILON);
765 #[stable(feature = "rust1", since = "1.0.0")]
767 pub fn exp(self) -> f32 { num::Float::exp(self) }
769 /// Returns `2^(self)`.
777 /// let abs_difference = (f.exp2() - 4.0).abs();
779 /// assert!(abs_difference <= f32::EPSILON);
781 #[stable(feature = "rust1", since = "1.0.0")]
783 pub fn exp2(self) -> f32 { num::Float::exp2(self) }
785 /// Returns the natural logarithm of the number.
790 /// let one = 1.0f32;
792 /// let e = one.exp();
794 /// // ln(e) - 1 == 0
795 /// let abs_difference = (e.ln() - 1.0).abs();
797 /// assert!(abs_difference <= f32::EPSILON);
799 #[stable(feature = "rust1", since = "1.0.0")]
801 pub fn ln(self) -> f32 { num::Float::ln(self) }
803 /// Returns the logarithm of the number with respect to an arbitrary base.
808 /// let ten = 10.0f32;
809 /// let two = 2.0f32;
811 /// // log10(10) - 1 == 0
812 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
814 /// // log2(2) - 1 == 0
815 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
817 /// assert!(abs_difference_10 <= f32::EPSILON);
818 /// assert!(abs_difference_2 <= f32::EPSILON);
820 #[stable(feature = "rust1", since = "1.0.0")]
822 pub fn log(self, base: f32) -> f32 { num::Float::log(self, base) }
824 /// Returns the base 2 logarithm of the number.
829 /// let two = 2.0f32;
831 /// // log2(2) - 1 == 0
832 /// let abs_difference = (two.log2() - 1.0).abs();
834 /// assert!(abs_difference <= f32::EPSILON);
836 #[stable(feature = "rust1", since = "1.0.0")]
838 pub fn log2(self) -> f32 { num::Float::log2(self) }
840 /// Returns the base 10 logarithm of the number.
845 /// let ten = 10.0f32;
847 /// // log10(10) - 1 == 0
848 /// let abs_difference = (ten.log10() - 1.0).abs();
850 /// assert!(abs_difference <= f32::EPSILON);
852 #[stable(feature = "rust1", since = "1.0.0")]
854 pub fn log10(self) -> f32 { num::Float::log10(self) }
856 /// Convert radians to degrees.
859 /// # #![feature(std_misc, core)]
860 /// use std::f32::{self, consts};
862 /// let angle = consts::PI;
864 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
866 /// assert!(abs_difference <= f32::EPSILON);
868 #[unstable(feature = "std_misc", reason = "desirability is unclear")]
870 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
872 /// Convert degrees to radians.
875 /// # #![feature(std_misc)]
876 /// use std::f32::{self, consts};
878 /// let angle = 180.0f32;
880 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
882 /// assert!(abs_difference <= f32::EPSILON);
884 #[unstable(feature = "std_misc", reason = "desirability is unclear")]
886 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
888 /// Constructs a floating point number of `x*2^exp`.
891 /// # #![feature(std_misc)]
893 /// // 3*2^2 - 12 == 0
894 /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
896 /// assert!(abs_difference <= f32::EPSILON);
898 #[unstable(feature = "std_misc",
899 reason = "pending integer conventions")]
901 pub fn ldexp(x: f32, exp: isize) -> f32 {
902 unsafe { cmath::ldexpf(x, exp as c_int) }
905 /// Breaks the number into a normalized fraction and a base-2 exponent,
908 /// * `self = x * 2^exp`
909 /// * `0.5 <= abs(x) < 1.0`
912 /// # #![feature(std_misc)]
917 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
918 /// let f = x.frexp();
919 /// let abs_difference_0 = (f.0 - 0.5).abs();
920 /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
922 /// assert!(abs_difference_0 <= f32::EPSILON);
923 /// assert!(abs_difference_1 <= f32::EPSILON);
925 #[unstable(feature = "std_misc",
926 reason = "pending integer conventions")]
928 pub fn frexp(self) -> (f32, isize) {
931 let x = cmath::frexpf(self, &mut exp);
936 /// Returns the next representable floating-point value in the direction of
940 /// # #![feature(std_misc)]
945 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
947 /// assert!(abs_diff <= f32::EPSILON);
949 #[unstable(feature = "std_misc",
950 reason = "unsure about its place in the world")]
952 pub fn next_after(self, other: f32) -> f32 {
953 unsafe { cmath::nextafterf(self, other) }
956 /// Returns the maximum of the two numbers.
962 /// assert_eq!(x.max(y), y);
964 #[stable(feature = "rust1", since = "1.0.0")]
966 pub fn max(self, other: f32) -> f32 {
967 unsafe { cmath::fmaxf(self, other) }
970 /// Returns the minimum of the two numbers.
976 /// assert_eq!(x.min(y), x);
978 #[stable(feature = "rust1", since = "1.0.0")]
980 pub fn min(self, other: f32) -> f32 {
981 unsafe { cmath::fminf(self, other) }
984 /// The positive difference of two numbers.
986 /// * If `self <= other`: `0:0`
987 /// * Else: `self - other`
990 /// # #![feature(std_misc)]
996 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
997 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
999 /// assert!(abs_difference_x <= f32::EPSILON);
1000 /// assert!(abs_difference_y <= f32::EPSILON);
1002 #[stable(feature = "rust1", since = "1.0.0")]
1004 pub fn abs_sub(self, other: f32) -> f32 {
1005 unsafe { cmath::fdimf(self, other) }
1008 /// Take the cubic root of a number.
1011 /// # #![feature(std_misc)]
1016 /// // x^(1/3) - 2 == 0
1017 /// let abs_difference = (x.cbrt() - 2.0).abs();
1019 /// assert!(abs_difference <= f32::EPSILON);
1021 #[stable(feature = "rust1", since = "1.0.0")]
1023 pub fn cbrt(self) -> f32 {
1024 unsafe { cmath::cbrtf(self) }
1027 /// Calculate the length of the hypotenuse of a right-angle triangle given
1028 /// legs of length `x` and `y`.
1036 /// // sqrt(x^2 + y^2)
1037 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
1039 /// assert!(abs_difference <= f32::EPSILON);
1041 #[stable(feature = "rust1", since = "1.0.0")]
1043 pub fn hypot(self, other: f32) -> f32 {
1044 unsafe { cmath::hypotf(self, other) }
1047 /// Computes the sine of a number (in radians).
1052 /// let x = f32::consts::PI/2.0;
1054 /// let abs_difference = (x.sin() - 1.0).abs();
1056 /// assert!(abs_difference <= f32::EPSILON);
1058 #[stable(feature = "rust1", since = "1.0.0")]
1060 pub fn sin(self) -> f32 {
1061 unsafe { intrinsics::sinf32(self) }
1064 /// Computes the cosine of a number (in radians).
1069 /// let x = 2.0*f32::consts::PI;
1071 /// let abs_difference = (x.cos() - 1.0).abs();
1073 /// assert!(abs_difference <= f32::EPSILON);
1075 #[stable(feature = "rust1", since = "1.0.0")]
1077 pub fn cos(self) -> f32 {
1078 unsafe { intrinsics::cosf32(self) }
1081 /// Computes the tangent of a number (in radians).
1086 /// let x = f64::consts::PI/4.0;
1087 /// let abs_difference = (x.tan() - 1.0).abs();
1089 /// assert!(abs_difference < 1e-10);
1091 #[stable(feature = "rust1", since = "1.0.0")]
1093 pub fn tan(self) -> f32 {
1094 unsafe { cmath::tanf(self) }
1097 /// Computes the arcsine of a number. Return value is in radians in
1098 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
1104 /// let f = f32::consts::PI / 2.0;
1106 /// // asin(sin(pi/2))
1107 /// let abs_difference = f.sin().asin().abs_sub(f32::consts::PI / 2.0);
1109 /// assert!(abs_difference <= f32::EPSILON);
1111 #[stable(feature = "rust1", since = "1.0.0")]
1113 pub fn asin(self) -> f32 {
1114 unsafe { cmath::asinf(self) }
1117 /// Computes the arccosine of a number. Return value is in radians in
1118 /// the range [0, pi] or NaN if the number is outside the range
1124 /// let f = f32::consts::PI / 4.0;
1126 /// // acos(cos(pi/4))
1127 /// let abs_difference = f.cos().acos().abs_sub(f32::consts::PI / 4.0);
1129 /// assert!(abs_difference <= f32::EPSILON);
1131 #[stable(feature = "rust1", since = "1.0.0")]
1133 pub fn acos(self) -> f32 {
1134 unsafe { cmath::acosf(self) }
1137 /// Computes the arctangent of a number. Return value is in radians in the
1138 /// range [-pi/2, pi/2];
1146 /// let abs_difference = f.tan().atan().abs_sub(1.0);
1148 /// assert!(abs_difference <= f32::EPSILON);
1150 #[stable(feature = "rust1", since = "1.0.0")]
1152 pub fn atan(self) -> f32 {
1153 unsafe { cmath::atanf(self) }
1156 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
1158 /// * `x = 0`, `y = 0`: `0`
1159 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
1160 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
1161 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
1166 /// let pi = f32::consts::PI;
1167 /// // All angles from horizontal right (+x)
1168 /// // 45 deg counter-clockwise
1169 /// let x1 = 3.0f32;
1170 /// let y1 = -3.0f32;
1172 /// // 135 deg clockwise
1173 /// let x2 = -3.0f32;
1174 /// let y2 = 3.0f32;
1176 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
1177 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
1179 /// assert!(abs_difference_1 <= f32::EPSILON);
1180 /// assert!(abs_difference_2 <= f32::EPSILON);
1182 #[stable(feature = "rust1", since = "1.0.0")]
1184 pub fn atan2(self, other: f32) -> f32 {
1185 unsafe { cmath::atan2f(self, other) }
1188 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
1189 /// `(sin(x), cos(x))`.
1194 /// let x = f32::consts::PI/4.0;
1195 /// let f = x.sin_cos();
1197 /// let abs_difference_0 = (f.0 - x.sin()).abs();
1198 /// let abs_difference_1 = (f.1 - x.cos()).abs();
1200 /// assert!(abs_difference_0 <= f32::EPSILON);
1201 /// assert!(abs_difference_0 <= f32::EPSILON);
1203 #[stable(feature = "rust1", since = "1.0.0")]
1205 pub fn sin_cos(self) -> (f32, f32) {
1206 (self.sin(), self.cos())
1209 /// Returns `e^(self) - 1` in a way that is accurate even if the
1210 /// number is close to zero.
1217 /// // e^(ln(7)) - 1
1218 /// let abs_difference = x.ln().exp_m1().abs_sub(6.0);
1220 /// assert!(abs_difference < 1e-10);
1222 #[stable(feature = "rust1", since = "1.0.0")]
1224 pub fn exp_m1(self) -> f32 {
1225 unsafe { cmath::expm1f(self) }
1228 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
1229 /// the operations were performed separately.
1234 /// let x = f32::consts::E - 1.0;
1236 /// // ln(1 + (e - 1)) == ln(e) == 1
1237 /// let abs_difference = (x.ln_1p() - 1.0).abs();
1239 /// assert!(abs_difference <= f32::EPSILON);
1241 #[stable(feature = "rust1", since = "1.0.0")]
1243 pub fn ln_1p(self) -> f32 {
1244 unsafe { cmath::log1pf(self) }
1247 /// Hyperbolic sine function.
1252 /// let e = f32::consts::E;
1255 /// let f = x.sinh();
1256 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
1257 /// let g = (e*e - 1.0)/(2.0*e);
1258 /// let abs_difference = (f - g).abs();
1260 /// assert!(abs_difference <= f32::EPSILON);
1262 #[stable(feature = "rust1", since = "1.0.0")]
1264 pub fn sinh(self) -> f32 {
1265 unsafe { cmath::sinhf(self) }
1268 /// Hyperbolic cosine function.
1273 /// let e = f32::consts::E;
1275 /// let f = x.cosh();
1276 /// // Solving cosh() at 1 gives this result
1277 /// let g = (e*e + 1.0)/(2.0*e);
1278 /// let abs_difference = f.abs_sub(g);
1281 /// assert!(abs_difference <= f32::EPSILON);
1283 #[stable(feature = "rust1", since = "1.0.0")]
1285 pub fn cosh(self) -> f32 {
1286 unsafe { cmath::coshf(self) }
1289 /// Hyperbolic tangent function.
1294 /// let e = f32::consts::E;
1297 /// let f = x.tanh();
1298 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1299 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1300 /// let abs_difference = (f - g).abs();
1302 /// assert!(abs_difference <= f32::EPSILON);
1304 #[stable(feature = "rust1", since = "1.0.0")]
1306 pub fn tanh(self) -> f32 {
1307 unsafe { cmath::tanhf(self) }
1310 /// Inverse hyperbolic sine function.
1316 /// let f = x.sinh().asinh();
1318 /// let abs_difference = (f - x).abs();
1320 /// assert!(abs_difference <= f32::EPSILON);
1322 #[stable(feature = "rust1", since = "1.0.0")]
1324 pub fn asinh(self) -> f32 {
1326 NEG_INFINITY => NEG_INFINITY,
1327 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1331 /// Inverse hyperbolic cosine function.
1337 /// let f = x.cosh().acosh();
1339 /// let abs_difference = (f - x).abs();
1341 /// assert!(abs_difference <= f32::EPSILON);
1343 #[stable(feature = "rust1", since = "1.0.0")]
1345 pub fn acosh(self) -> f32 {
1347 x if x < 1.0 => Float::nan(),
1348 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1352 /// Inverse hyperbolic tangent function.
1357 /// let e = f32::consts::E;
1358 /// let f = e.tanh().atanh();
1360 /// let abs_difference = f.abs_sub(e);
1362 /// assert!(abs_difference <= f32::EPSILON);
1364 #[stable(feature = "rust1", since = "1.0.0")]
1366 pub fn atanh(self) -> f32 {
1367 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1372 // Section: String Conversions
1375 /// Converts a float to a string
1379 /// * num - The float value
1381 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1382 #[deprecated(since = "1.0.0", reason = "use the ToString trait instead")]
1383 pub fn to_string(num: f32) -> String {
1384 let (r, _) = strconv::float_to_str_common(
1385 num, 10, true, SignNeg, DigAll, ExpNone, false);
1389 /// Converts a float to a string in hexadecimal format
1393 /// * num - The float value
1395 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1396 #[deprecated(since = "1.0.0", reason = "use format! instead")]
1397 pub fn to_str_hex(num: f32) -> String {
1398 let (r, _) = strconv::float_to_str_common(
1399 num, 16, true, SignNeg, DigAll, ExpNone, false);
1403 /// Converts a float to a string in a given radix, and a flag indicating
1404 /// whether it's a special value
1408 /// * num - The float value
1409 /// * radix - The base to use
1411 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1412 #[deprecated(since = "1.0.0", reason = "use format! instead")]
1413 pub fn to_str_radix_special(num: f32, rdx: u32) -> (String, bool) {
1414 strconv::float_to_str_common(num, rdx, true, SignNeg, DigAll, ExpNone, false)
1417 /// Converts a float to a string with exactly the number of
1418 /// provided significant digits
1422 /// * num - The float value
1423 /// * digits - The number of significant digits
1425 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1426 pub fn to_str_exact(num: f32, dig: usize) -> String {
1427 let (r, _) = strconv::float_to_str_common(
1428 num, 10, true, SignNeg, DigExact(dig), ExpNone, false);
1432 /// Converts a float to a string with a maximum number of
1433 /// significant digits
1437 /// * num - The float value
1438 /// * digits - The number of significant digits
1440 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1441 pub fn to_str_digits(num: f32, dig: usize) -> String {
1442 let (r, _) = strconv::float_to_str_common(
1443 num, 10, true, SignNeg, DigMax(dig), ExpNone, false);
1447 /// Converts a float to a string using the exponential notation with exactly the number of
1448 /// provided digits after the decimal point in the significand
1452 /// * num - The float value
1453 /// * digits - The number of digits after the decimal point
1454 /// * upper - Use `E` instead of `e` for the exponent sign
1456 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1457 pub fn to_str_exp_exact(num: f32, dig: usize, upper: bool) -> String {
1458 let (r, _) = strconv::float_to_str_common(
1459 num, 10, true, SignNeg, DigExact(dig), ExpDec, upper);
1463 /// Converts a float to a string using the exponential notation with the maximum number of
1464 /// digits after the decimal point in the significand
1468 /// * num - The float value
1469 /// * digits - The number of digits after the decimal point
1470 /// * upper - Use `E` instead of `e` for the exponent sign
1472 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1473 pub fn to_str_exp_digits(num: f32, dig: usize, upper: bool) -> String {
1474 let (r, _) = strconv::float_to_str_common(
1475 num, 10, true, SignNeg, DigMax(dig), ExpDec, upper);
1483 use num::FpCategory as Fp;
1487 test_num(10f32, 2f32);
1492 assert_eq!(NAN.min(2.0), 2.0);
1493 assert_eq!(2.0f32.min(NAN), 2.0);
1498 assert_eq!(NAN.max(2.0), 2.0);
1499 assert_eq!(2.0f32.max(NAN), 2.0);
1504 let nan: f32 = Float::nan();
1505 assert!(nan.is_nan());
1506 assert!(!nan.is_infinite());
1507 assert!(!nan.is_finite());
1508 assert!(!nan.is_normal());
1509 assert!(!nan.is_sign_positive());
1510 assert!(!nan.is_sign_negative());
1511 assert_eq!(Fp::Nan, nan.classify());
1515 fn test_infinity() {
1516 let inf: f32 = Float::infinity();
1517 assert!(inf.is_infinite());
1518 assert!(!inf.is_finite());
1519 assert!(inf.is_sign_positive());
1520 assert!(!inf.is_sign_negative());
1521 assert!(!inf.is_nan());
1522 assert!(!inf.is_normal());
1523 assert_eq!(Fp::Infinite, inf.classify());
1527 fn test_neg_infinity() {
1528 let neg_inf: f32 = Float::neg_infinity();
1529 assert!(neg_inf.is_infinite());
1530 assert!(!neg_inf.is_finite());
1531 assert!(!neg_inf.is_sign_positive());
1532 assert!(neg_inf.is_sign_negative());
1533 assert!(!neg_inf.is_nan());
1534 assert!(!neg_inf.is_normal());
1535 assert_eq!(Fp::Infinite, neg_inf.classify());
1540 let zero: f32 = Float::zero();
1541 assert_eq!(0.0, zero);
1542 assert!(!zero.is_infinite());
1543 assert!(zero.is_finite());
1544 assert!(zero.is_sign_positive());
1545 assert!(!zero.is_sign_negative());
1546 assert!(!zero.is_nan());
1547 assert!(!zero.is_normal());
1548 assert_eq!(Fp::Zero, zero.classify());
1552 fn test_neg_zero() {
1553 let neg_zero: f32 = Float::neg_zero();
1554 assert_eq!(0.0, neg_zero);
1555 assert!(!neg_zero.is_infinite());
1556 assert!(neg_zero.is_finite());
1557 assert!(!neg_zero.is_sign_positive());
1558 assert!(neg_zero.is_sign_negative());
1559 assert!(!neg_zero.is_nan());
1560 assert!(!neg_zero.is_normal());
1561 assert_eq!(Fp::Zero, neg_zero.classify());
1566 let one: f32 = Float::one();
1567 assert_eq!(1.0, one);
1568 assert!(!one.is_infinite());
1569 assert!(one.is_finite());
1570 assert!(one.is_sign_positive());
1571 assert!(!one.is_sign_negative());
1572 assert!(!one.is_nan());
1573 assert!(one.is_normal());
1574 assert_eq!(Fp::Normal, one.classify());
1579 let nan: f32 = Float::nan();
1580 let inf: f32 = Float::infinity();
1581 let neg_inf: f32 = Float::neg_infinity();
1582 assert!(nan.is_nan());
1583 assert!(!0.0f32.is_nan());
1584 assert!(!5.3f32.is_nan());
1585 assert!(!(-10.732f32).is_nan());
1586 assert!(!inf.is_nan());
1587 assert!(!neg_inf.is_nan());
1591 fn test_is_infinite() {
1592 let nan: f32 = Float::nan();
1593 let inf: f32 = Float::infinity();
1594 let neg_inf: f32 = Float::neg_infinity();
1595 assert!(!nan.is_infinite());
1596 assert!(inf.is_infinite());
1597 assert!(neg_inf.is_infinite());
1598 assert!(!0.0f32.is_infinite());
1599 assert!(!42.8f32.is_infinite());
1600 assert!(!(-109.2f32).is_infinite());
1604 fn test_is_finite() {
1605 let nan: f32 = Float::nan();
1606 let inf: f32 = Float::infinity();
1607 let neg_inf: f32 = Float::neg_infinity();
1608 assert!(!nan.is_finite());
1609 assert!(!inf.is_finite());
1610 assert!(!neg_inf.is_finite());
1611 assert!(0.0f32.is_finite());
1612 assert!(42.8f32.is_finite());
1613 assert!((-109.2f32).is_finite());
1617 fn test_is_normal() {
1618 let nan: f32 = Float::nan();
1619 let inf: f32 = Float::infinity();
1620 let neg_inf: f32 = Float::neg_infinity();
1621 let zero: f32 = Float::zero();
1622 let neg_zero: f32 = Float::neg_zero();
1623 assert!(!nan.is_normal());
1624 assert!(!inf.is_normal());
1625 assert!(!neg_inf.is_normal());
1626 assert!(!zero.is_normal());
1627 assert!(!neg_zero.is_normal());
1628 assert!(1f32.is_normal());
1629 assert!(1e-37f32.is_normal());
1630 assert!(!1e-38f32.is_normal());
1634 fn test_classify() {
1635 let nan: f32 = Float::nan();
1636 let inf: f32 = Float::infinity();
1637 let neg_inf: f32 = Float::neg_infinity();
1638 let zero: f32 = Float::zero();
1639 let neg_zero: f32 = Float::neg_zero();
1640 assert_eq!(nan.classify(), Fp::Nan);
1641 assert_eq!(inf.classify(), Fp::Infinite);
1642 assert_eq!(neg_inf.classify(), Fp::Infinite);
1643 assert_eq!(zero.classify(), Fp::Zero);
1644 assert_eq!(neg_zero.classify(), Fp::Zero);
1645 assert_eq!(1f32.classify(), Fp::Normal);
1646 assert_eq!(1e-37f32.classify(), Fp::Normal);
1647 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1651 fn test_integer_decode() {
1652 assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
1653 assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
1654 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
1655 assert_eq!(0f32.integer_decode(), (0, -150, 1));
1656 assert_eq!((-0f32).integer_decode(), (0, -150, -1));
1657 assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
1658 assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
1659 assert_eq!(NAN.integer_decode(), (12582912, 105, 1));
1664 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1665 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1666 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1667 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1668 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1669 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1670 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1671 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1672 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1673 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1678 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1679 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1680 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1681 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1682 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1683 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1684 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1685 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1686 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1687 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1692 assert_approx_eq!(1.0f32.round(), 1.0f32);
1693 assert_approx_eq!(1.3f32.round(), 1.0f32);
1694 assert_approx_eq!(1.5f32.round(), 2.0f32);
1695 assert_approx_eq!(1.7f32.round(), 2.0f32);
1696 assert_approx_eq!(0.0f32.round(), 0.0f32);
1697 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1698 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1699 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1700 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1701 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1706 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1707 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1708 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1709 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1710 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1711 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1712 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1713 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1714 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1715 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1720 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1721 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1722 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1723 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1724 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1725 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1726 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1727 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1728 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1729 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1734 assert_eq!(INFINITY.abs(), INFINITY);
1735 assert_eq!(1f32.abs(), 1f32);
1736 assert_eq!(0f32.abs(), 0f32);
1737 assert_eq!((-0f32).abs(), 0f32);
1738 assert_eq!((-1f32).abs(), 1f32);
1739 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1740 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1741 assert!(NAN.abs().is_nan());
1746 assert_eq!(INFINITY.signum(), 1f32);
1747 assert_eq!(1f32.signum(), 1f32);
1748 assert_eq!(0f32.signum(), 1f32);
1749 assert_eq!((-0f32).signum(), -1f32);
1750 assert_eq!((-1f32).signum(), -1f32);
1751 assert_eq!(NEG_INFINITY.signum(), -1f32);
1752 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1753 assert!(NAN.signum().is_nan());
1757 fn test_is_sign_positive() {
1758 assert!(INFINITY.is_sign_positive());
1759 assert!(1f32.is_sign_positive());
1760 assert!(0f32.is_sign_positive());
1761 assert!(!(-0f32).is_sign_positive());
1762 assert!(!(-1f32).is_sign_positive());
1763 assert!(!NEG_INFINITY.is_sign_positive());
1764 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1765 assert!(!NAN.is_sign_positive());
1769 fn test_is_sign_negative() {
1770 assert!(!INFINITY.is_sign_negative());
1771 assert!(!1f32.is_sign_negative());
1772 assert!(!0f32.is_sign_negative());
1773 assert!((-0f32).is_sign_negative());
1774 assert!((-1f32).is_sign_negative());
1775 assert!(NEG_INFINITY.is_sign_negative());
1776 assert!((1f32/NEG_INFINITY).is_sign_negative());
1777 assert!(!NAN.is_sign_negative());
1782 let nan: f32 = Float::nan();
1783 let inf: f32 = Float::infinity();
1784 let neg_inf: f32 = Float::neg_infinity();
1785 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1786 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1787 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1788 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1789 assert!(nan.mul_add(7.8, 9.0).is_nan());
1790 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1791 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1792 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1793 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1798 let nan: f32 = Float::nan();
1799 let inf: f32 = Float::infinity();
1800 let neg_inf: f32 = Float::neg_infinity();
1801 assert_eq!(1.0f32.recip(), 1.0);
1802 assert_eq!(2.0f32.recip(), 0.5);
1803 assert_eq!((-0.4f32).recip(), -2.5);
1804 assert_eq!(0.0f32.recip(), inf);
1805 assert!(nan.recip().is_nan());
1806 assert_eq!(inf.recip(), 0.0);
1807 assert_eq!(neg_inf.recip(), 0.0);
1812 let nan: f32 = Float::nan();
1813 let inf: f32 = Float::infinity();
1814 let neg_inf: f32 = Float::neg_infinity();
1815 assert_eq!(1.0f32.powi(1), 1.0);
1816 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1817 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1818 assert_eq!(8.3f32.powi(0), 1.0);
1819 assert!(nan.powi(2).is_nan());
1820 assert_eq!(inf.powi(3), inf);
1821 assert_eq!(neg_inf.powi(2), inf);
1826 let nan: f32 = Float::nan();
1827 let inf: f32 = Float::infinity();
1828 let neg_inf: f32 = Float::neg_infinity();
1829 assert_eq!(1.0f32.powf(1.0), 1.0);
1830 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1831 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1832 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1833 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1834 assert_eq!(8.3f32.powf(0.0), 1.0);
1835 assert!(nan.powf(2.0).is_nan());
1836 assert_eq!(inf.powf(2.0), inf);
1837 assert_eq!(neg_inf.powf(3.0), neg_inf);
1841 fn test_sqrt_domain() {
1842 assert!(NAN.sqrt().is_nan());
1843 assert!(NEG_INFINITY.sqrt().is_nan());
1844 assert!((-1.0f32).sqrt().is_nan());
1845 assert_eq!((-0.0f32).sqrt(), -0.0);
1846 assert_eq!(0.0f32.sqrt(), 0.0);
1847 assert_eq!(1.0f32.sqrt(), 1.0);
1848 assert_eq!(INFINITY.sqrt(), INFINITY);
1853 let nan: f32 = Float::nan();
1854 let inf: f32 = Float::infinity();
1855 let neg_inf: f32 = Float::neg_infinity();
1856 assert!(nan.rsqrt().is_nan());
1857 assert_eq!(inf.rsqrt(), 0.0);
1858 assert!(neg_inf.rsqrt().is_nan());
1859 assert!((-1.0f32).rsqrt().is_nan());
1860 assert_eq!((-0.0f32).rsqrt(), neg_inf);
1861 assert_eq!(0.0f32.rsqrt(), inf);
1862 assert_eq!(1.0f32.rsqrt(), 1.0);
1863 assert_eq!(4.0f32.rsqrt(), 0.5);
1868 assert_eq!(1.0, 0.0f32.exp());
1869 assert_approx_eq!(2.718282, 1.0f32.exp());
1870 assert_approx_eq!(148.413162, 5.0f32.exp());
1872 let inf: f32 = Float::infinity();
1873 let neg_inf: f32 = Float::neg_infinity();
1874 let nan: f32 = Float::nan();
1875 assert_eq!(inf, inf.exp());
1876 assert_eq!(0.0, neg_inf.exp());
1877 assert!(nan.exp().is_nan());
1882 assert_eq!(32.0, 5.0f32.exp2());
1883 assert_eq!(1.0, 0.0f32.exp2());
1885 let inf: f32 = Float::infinity();
1886 let neg_inf: f32 = Float::neg_infinity();
1887 let nan: f32 = Float::nan();
1888 assert_eq!(inf, inf.exp2());
1889 assert_eq!(0.0, neg_inf.exp2());
1890 assert!(nan.exp2().is_nan());
1895 let nan: f32 = Float::nan();
1896 let inf: f32 = Float::infinity();
1897 let neg_inf: f32 = Float::neg_infinity();
1898 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1899 assert!(nan.ln().is_nan());
1900 assert_eq!(inf.ln(), inf);
1901 assert!(neg_inf.ln().is_nan());
1902 assert!((-2.3f32).ln().is_nan());
1903 assert_eq!((-0.0f32).ln(), neg_inf);
1904 assert_eq!(0.0f32.ln(), neg_inf);
1905 assert_approx_eq!(4.0f32.ln(), 1.386294);
1910 let nan: f32 = Float::nan();
1911 let inf: f32 = Float::infinity();
1912 let neg_inf: f32 = Float::neg_infinity();
1913 assert_eq!(10.0f32.log(10.0), 1.0);
1914 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1915 assert_eq!(1.0f32.exp().log(1.0.exp()), 1.0);
1916 assert!(1.0f32.log(1.0).is_nan());
1917 assert!(1.0f32.log(-13.9).is_nan());
1918 assert!(nan.log(2.3).is_nan());
1919 assert_eq!(inf.log(10.0), inf);
1920 assert!(neg_inf.log(8.8).is_nan());
1921 assert!((-2.3f32).log(0.1).is_nan());
1922 assert_eq!((-0.0f32).log(2.0), neg_inf);
1923 assert_eq!(0.0f32.log(7.0), neg_inf);
1928 let nan: f32 = Float::nan();
1929 let inf: f32 = Float::infinity();
1930 let neg_inf: f32 = Float::neg_infinity();
1931 assert_approx_eq!(10.0f32.log2(), 3.321928);
1932 assert_approx_eq!(2.3f32.log2(), 1.201634);
1933 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1934 assert!(nan.log2().is_nan());
1935 assert_eq!(inf.log2(), inf);
1936 assert!(neg_inf.log2().is_nan());
1937 assert!((-2.3f32).log2().is_nan());
1938 assert_eq!((-0.0f32).log2(), neg_inf);
1939 assert_eq!(0.0f32.log2(), neg_inf);
1944 let nan: f32 = Float::nan();
1945 let inf: f32 = Float::infinity();
1946 let neg_inf: f32 = Float::neg_infinity();
1947 assert_eq!(10.0f32.log10(), 1.0);
1948 assert_approx_eq!(2.3f32.log10(), 0.361728);
1949 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1950 assert_eq!(1.0f32.log10(), 0.0);
1951 assert!(nan.log10().is_nan());
1952 assert_eq!(inf.log10(), inf);
1953 assert!(neg_inf.log10().is_nan());
1954 assert!((-2.3f32).log10().is_nan());
1955 assert_eq!((-0.0f32).log10(), neg_inf);
1956 assert_eq!(0.0f32.log10(), neg_inf);
1960 fn test_to_degrees() {
1961 let pi: f32 = consts::PI;
1962 let nan: f32 = Float::nan();
1963 let inf: f32 = Float::infinity();
1964 let neg_inf: f32 = Float::neg_infinity();
1965 assert_eq!(0.0f32.to_degrees(), 0.0);
1966 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1967 assert_eq!(pi.to_degrees(), 180.0);
1968 assert!(nan.to_degrees().is_nan());
1969 assert_eq!(inf.to_degrees(), inf);
1970 assert_eq!(neg_inf.to_degrees(), neg_inf);
1974 fn test_to_radians() {
1975 let pi: f32 = consts::PI;
1976 let nan: f32 = Float::nan();
1977 let inf: f32 = Float::infinity();
1978 let neg_inf: f32 = Float::neg_infinity();
1979 assert_eq!(0.0f32.to_radians(), 0.0);
1980 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1981 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1982 assert_eq!(180.0f32.to_radians(), pi);
1983 assert!(nan.to_radians().is_nan());
1984 assert_eq!(inf.to_radians(), inf);
1985 assert_eq!(neg_inf.to_radians(), neg_inf);
1990 // We have to use from_str until base-2 exponents
1991 // are supported in floating-point literals
1992 let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
1993 let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
1994 let f3: f32 = FromStrRadix::from_str_radix("1.Cp-12", 16).unwrap();
1995 assert_eq!(1f32.ldexp(-123), f1);
1996 assert_eq!(1f32.ldexp(-111), f2);
1997 assert_eq!(Float::ldexp(1.75f32, -12), f3);
1999 assert_eq!(Float::ldexp(0f32, -123), 0f32);
2000 assert_eq!(Float::ldexp(-0f32, -123), -0f32);
2002 let inf: f32 = Float::infinity();
2003 let neg_inf: f32 = Float::neg_infinity();
2004 let nan: f32 = Float::nan();
2005 assert_eq!(Float::ldexp(inf, -123), inf);
2006 assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
2007 assert!(Float::ldexp(nan, -123).is_nan());
2012 // We have to use from_str until base-2 exponents
2013 // are supported in floating-point literals
2014 let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
2015 let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
2016 let f3: f32 = FromStrRadix::from_str_radix("1.Cp-123", 16).unwrap();
2017 let (x1, exp1) = f1.frexp();
2018 let (x2, exp2) = f2.frexp();
2019 let (x3, exp3) = f3.frexp();
2020 assert_eq!((x1, exp1), (0.5f32, -122));
2021 assert_eq!((x2, exp2), (0.5f32, -110));
2022 assert_eq!((x3, exp3), (0.875f32, -122));
2023 assert_eq!(Float::ldexp(x1, exp1), f1);
2024 assert_eq!(Float::ldexp(x2, exp2), f2);
2025 assert_eq!(Float::ldexp(x3, exp3), f3);
2027 assert_eq!(0f32.frexp(), (0f32, 0));
2028 assert_eq!((-0f32).frexp(), (-0f32, 0));
2031 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
2032 fn test_frexp_nowin() {
2033 let inf: f32 = Float::infinity();
2034 let neg_inf: f32 = Float::neg_infinity();
2035 let nan: f32 = Float::nan();
2036 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
2037 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
2038 assert!(match nan.frexp() { (x, _) => x.is_nan() })
2043 assert_eq!((-1f32).abs_sub(1f32), 0f32);
2044 assert_eq!(1f32.abs_sub(1f32), 0f32);
2045 assert_eq!(1f32.abs_sub(0f32), 1f32);
2046 assert_eq!(1f32.abs_sub(-1f32), 2f32);
2047 assert_eq!(NEG_INFINITY.abs_sub(0f32), 0f32);
2048 assert_eq!(INFINITY.abs_sub(1f32), INFINITY);
2049 assert_eq!(0f32.abs_sub(NEG_INFINITY), INFINITY);
2050 assert_eq!(0f32.abs_sub(INFINITY), 0f32);
2054 fn test_abs_sub_nowin() {
2055 assert!(NAN.abs_sub(-1f32).is_nan());
2056 assert!(1f32.abs_sub(NAN).is_nan());
2061 assert_eq!(0.0f32.asinh(), 0.0f32);
2062 assert_eq!((-0.0f32).asinh(), -0.0f32);
2064 let inf: f32 = Float::infinity();
2065 let neg_inf: f32 = Float::neg_infinity();
2066 let nan: f32 = Float::nan();
2067 assert_eq!(inf.asinh(), inf);
2068 assert_eq!(neg_inf.asinh(), neg_inf);
2069 assert!(nan.asinh().is_nan());
2070 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
2071 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
2076 assert_eq!(1.0f32.acosh(), 0.0f32);
2077 assert!(0.999f32.acosh().is_nan());
2079 let inf: f32 = Float::infinity();
2080 let neg_inf: f32 = Float::neg_infinity();
2081 let nan: f32 = Float::nan();
2082 assert_eq!(inf.acosh(), inf);
2083 assert!(neg_inf.acosh().is_nan());
2084 assert!(nan.acosh().is_nan());
2085 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
2086 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
2091 assert_eq!(0.0f32.atanh(), 0.0f32);
2092 assert_eq!((-0.0f32).atanh(), -0.0f32);
2094 let inf32: f32 = Float::infinity();
2095 let neg_inf32: f32 = Float::neg_infinity();
2096 assert_eq!(1.0f32.atanh(), inf32);
2097 assert_eq!((-1.0f32).atanh(), neg_inf32);
2099 assert!(2f64.atanh().atanh().is_nan());
2100 assert!((-2f64).atanh().atanh().is_nan());
2102 let inf64: f32 = Float::infinity();
2103 let neg_inf64: f32 = Float::neg_infinity();
2104 let nan32: f32 = Float::nan();
2105 assert!(inf64.atanh().is_nan());
2106 assert!(neg_inf64.atanh().is_nan());
2107 assert!(nan32.atanh().is_nan());
2109 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
2110 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
2114 fn test_real_consts() {
2117 let pi: f32 = consts::PI;
2118 let two_pi: f32 = consts::PI_2;
2119 let frac_pi_2: f32 = consts::FRAC_PI_2;
2120 let frac_pi_3: f32 = consts::FRAC_PI_3;
2121 let frac_pi_4: f32 = consts::FRAC_PI_4;
2122 let frac_pi_6: f32 = consts::FRAC_PI_6;
2123 let frac_pi_8: f32 = consts::FRAC_PI_8;
2124 let frac_1_pi: f32 = consts::FRAC_1_PI;
2125 let frac_2_pi: f32 = consts::FRAC_2_PI;
2126 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRTPI;
2127 let sqrt2: f32 = consts::SQRT2;
2128 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT2;
2129 let e: f32 = consts::E;
2130 let log2_e: f32 = consts::LOG2_E;
2131 let log10_e: f32 = consts::LOG10_E;
2132 let ln_2: f32 = consts::LN_2;
2133 let ln_10: f32 = consts::LN_10;
2135 assert_approx_eq!(two_pi, 2f32 * pi);
2136 assert_approx_eq!(frac_pi_2, pi / 2f32);
2137 assert_approx_eq!(frac_pi_3, pi / 3f32);
2138 assert_approx_eq!(frac_pi_4, pi / 4f32);
2139 assert_approx_eq!(frac_pi_6, pi / 6f32);
2140 assert_approx_eq!(frac_pi_8, pi / 8f32);
2141 assert_approx_eq!(frac_1_pi, 1f32 / pi);
2142 assert_approx_eq!(frac_2_pi, 2f32 / pi);
2143 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
2144 assert_approx_eq!(sqrt2, 2f32.sqrt());
2145 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
2146 assert_approx_eq!(log2_e, e.log2());
2147 assert_approx_eq!(log10_e, e.log10());
2148 assert_approx_eq!(ln_2, 2f32.ln());
2149 assert_approx_eq!(ln_10, 10f32.ln());