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")]
23 use num
::{FpCategory, ParseFloatError}
;
25 pub use core
::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON}
;
26 pub use core
::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP}
;
27 pub use core
::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY}
;
28 pub use core
::f32::{MIN, MIN_POSITIVE, MAX}
;
29 pub use core
::f32::consts
;
33 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 fmaxf(a
: c_float
, b
: c_float
) -> c_float
;
47 pub fn fminf(a
: c_float
, b
: c_float
) -> c_float
;
48 pub fn fmodf(a
: c_float
, b
: c_float
) -> c_float
;
49 pub fn nextafterf(x
: c_float
, y
: c_float
) -> c_float
;
50 pub fn logbf(n
: c_float
) -> c_float
;
51 pub fn log1pf(n
: c_float
) -> c_float
;
52 pub fn ilogbf(n
: c_float
) -> c_int
;
53 pub fn modff(n
: c_float
, iptr
: &mut c_float
) -> c_float
;
54 pub fn sinhf(n
: c_float
) -> c_float
;
55 pub fn tanf(n
: c_float
) -> c_float
;
56 pub fn tanhf(n
: c_float
) -> c_float
;
57 pub fn tgammaf(n
: c_float
) -> c_float
;
59 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
60 pub fn lgammaf_r(n
: c_float
, sign
: &mut c_int
) -> c_float
;
61 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
62 pub fn hypotf(x
: c_float
, y
: c_float
) -> c_float
;
64 #[cfg(any(unix, all(windows, not(target_env = "msvc"))))]
65 pub fn frexpf(n
: c_float
, value
: &mut c_int
) -> c_float
;
66 #[cfg(any(unix, all(windows, not(target_env = "msvc"))))]
67 pub fn ldexpf(x
: c_float
, n
: c_int
) -> c_float
;
70 #[cfg(all(windows, target_env = "msvc"))]
71 pub unsafe fn ldexpf(x
: c_float
, n
: c_int
) -> c_float
{
72 f64::ldexp(x
as f64, n
as isize) as c_float
75 #[cfg(all(windows, target_env = "msvc"))]
76 pub unsafe fn frexpf(x
: c_float
, value
: &mut c_int
) -> c_float
{
77 let (a
, b
) = f64::frexp(x
as f64);
85 #[stable(feature = "rust1", since = "1.0.0")]
87 /// Parses a float as with a given radix
88 #[unstable(feature = "float_from_str_radix", reason = "recently moved API")]
89 pub fn from_str_radix(s
: &str, radix
: u32) -> Result
<f32, ParseFloatError
> {
90 num
::Float
::from_str_radix(s
, radix
)
93 /// Returns `true` if this value is `NaN` and false otherwise.
98 /// let nan = f32::NAN;
101 /// assert!(nan.is_nan());
102 /// assert!(!f.is_nan());
104 #[stable(feature = "rust1", since = "1.0.0")]
106 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
108 /// Returns `true` if this value is positive infinity or negative infinity and
115 /// let inf = f32::INFINITY;
116 /// let neg_inf = f32::NEG_INFINITY;
117 /// let nan = f32::NAN;
119 /// assert!(!f.is_infinite());
120 /// assert!(!nan.is_infinite());
122 /// assert!(inf.is_infinite());
123 /// assert!(neg_inf.is_infinite());
125 #[stable(feature = "rust1", since = "1.0.0")]
127 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
129 /// Returns `true` if this number is neither infinite nor `NaN`.
135 /// let inf = f32::INFINITY;
136 /// let neg_inf = f32::NEG_INFINITY;
137 /// let nan = f32::NAN;
139 /// assert!(f.is_finite());
141 /// assert!(!nan.is_finite());
142 /// assert!(!inf.is_finite());
143 /// assert!(!neg_inf.is_finite());
145 #[stable(feature = "rust1", since = "1.0.0")]
147 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
149 /// Returns `true` if the number is neither zero, infinite,
150 /// [subnormal][subnormal], or `NaN`.
155 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
156 /// let max = f32::MAX;
157 /// let lower_than_min = 1.0e-40_f32;
158 /// let zero = 0.0_f32;
160 /// assert!(min.is_normal());
161 /// assert!(max.is_normal());
163 /// assert!(!zero.is_normal());
164 /// assert!(!f32::NAN.is_normal());
165 /// assert!(!f32::INFINITY.is_normal());
166 /// // Values between `0` and `min` are Subnormal.
167 /// assert!(!lower_than_min.is_normal());
169 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
170 #[stable(feature = "rust1", since = "1.0.0")]
172 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
174 /// Returns the floating point category of the number. If only one property
175 /// is going to be tested, it is generally faster to use the specific
176 /// predicate instead.
179 /// use std::num::FpCategory;
182 /// let num = 12.4_f32;
183 /// let inf = f32::INFINITY;
185 /// assert_eq!(num.classify(), FpCategory::Normal);
186 /// assert_eq!(inf.classify(), FpCategory::Infinite);
188 #[stable(feature = "rust1", since = "1.0.0")]
190 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
192 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
193 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
194 /// The floating point encoding is documented in the [Reference][floating-point].
197 /// # #![feature(float_extras)]
200 /// let num = 2.0f32;
202 /// // (8388608, -22, 1)
203 /// let (mantissa, exponent, sign) = num.integer_decode();
204 /// let sign_f = sign as f32;
205 /// let mantissa_f = mantissa as f32;
206 /// let exponent_f = num.powf(exponent as f32);
208 /// // 1 * 8388608 * 2^(-22) == 2
209 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
211 /// assert!(abs_difference <= f32::EPSILON);
213 /// [floating-point]: ../../../../../reference.html#machine-types
214 #[unstable(feature = "float_extras", reason = "signature is undecided")]
216 pub fn integer_decode(self) -> (u64, i16, i8) {
217 num
::Float
::integer_decode(self)
220 /// Returns the largest integer less than or equal to a number.
223 /// let f = 3.99_f32;
226 /// assert_eq!(f.floor(), 3.0);
227 /// assert_eq!(g.floor(), 3.0);
229 #[stable(feature = "rust1", since = "1.0.0")]
231 pub fn floor(self) -> f32 { num::Float::floor(self) }
233 /// Returns the smallest integer greater than or equal to a number.
236 /// let f = 3.01_f32;
239 /// assert_eq!(f.ceil(), 4.0);
240 /// assert_eq!(g.ceil(), 4.0);
242 #[stable(feature = "rust1", since = "1.0.0")]
244 pub fn ceil(self) -> f32 { num::Float::ceil(self) }
246 /// Returns the nearest integer to a number. Round half-way cases away from
251 /// let g = -3.3_f32;
253 /// assert_eq!(f.round(), 3.0);
254 /// assert_eq!(g.round(), -3.0);
256 #[stable(feature = "rust1", since = "1.0.0")]
258 pub fn round(self) -> f32 { num::Float::round(self) }
260 /// Returns the integer part of a number.
264 /// let g = -3.7_f32;
266 /// assert_eq!(f.trunc(), 3.0);
267 /// assert_eq!(g.trunc(), -3.0);
269 #[stable(feature = "rust1", since = "1.0.0")]
271 pub fn trunc(self) -> f32 { num::Float::trunc(self) }
273 /// Returns the fractional part of a number.
279 /// let y = -3.5_f32;
280 /// let abs_difference_x = (x.fract() - 0.5).abs();
281 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
283 /// assert!(abs_difference_x <= f32::EPSILON);
284 /// assert!(abs_difference_y <= f32::EPSILON);
286 #[stable(feature = "rust1", since = "1.0.0")]
288 pub fn fract(self) -> f32 { num::Float::fract(self) }
290 /// Computes the absolute value of `self`. Returns `NAN` if the
297 /// let y = -3.5_f32;
299 /// let abs_difference_x = (x.abs() - x).abs();
300 /// let abs_difference_y = (y.abs() - (-y)).abs();
302 /// assert!(abs_difference_x <= f32::EPSILON);
303 /// assert!(abs_difference_y <= f32::EPSILON);
305 /// assert!(f32::NAN.abs().is_nan());
307 #[stable(feature = "rust1", since = "1.0.0")]
309 pub fn abs(self) -> f32 { num::Float::abs(self) }
311 /// Returns a number that represents the sign of `self`.
313 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
314 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
315 /// - `NAN` if the number is `NAN`
322 /// assert_eq!(f.signum(), 1.0);
323 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
325 /// assert!(f32::NAN.signum().is_nan());
327 #[stable(feature = "rust1", since = "1.0.0")]
329 pub fn signum(self) -> f32 { num::Float::signum(self) }
331 /// Returns `true` if `self`'s sign bit is positive, including
332 /// `+0.0` and `INFINITY`.
337 /// let nan = f32::NAN;
339 /// let g = -7.0_f32;
341 /// assert!(f.is_sign_positive());
342 /// assert!(!g.is_sign_positive());
343 /// // Requires both tests to determine if is `NaN`
344 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
346 #[stable(feature = "rust1", since = "1.0.0")]
348 pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) }
350 /// Returns `true` if `self`'s sign is negative, including `-0.0`
351 /// and `NEG_INFINITY`.
356 /// let nan = f32::NAN;
360 /// assert!(!f.is_sign_negative());
361 /// assert!(g.is_sign_negative());
362 /// // Requires both tests to determine if is `NaN`.
363 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
365 #[stable(feature = "rust1", since = "1.0.0")]
367 pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) }
369 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
370 /// error. This produces a more accurate result with better performance than
371 /// a separate multiplication operation followed by an add.
376 /// let m = 10.0_f32;
378 /// let b = 60.0_f32;
381 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
383 /// assert!(abs_difference <= f32::EPSILON);
385 #[stable(feature = "rust1", since = "1.0.0")]
387 pub fn mul_add(self, a
: f32, b
: f32) -> f32 { num::Float::mul_add(self, a, b) }
389 /// Takes the reciprocal (inverse) of a number, `1/x`.
395 /// let abs_difference = (x.recip() - (1.0/x)).abs();
397 /// assert!(abs_difference <= f32::EPSILON);
399 #[stable(feature = "rust1", since = "1.0.0")]
401 pub fn recip(self) -> f32 { num::Float::recip(self) }
403 /// Raises a number to an integer power.
405 /// Using this function is generally faster than using `powf`
411 /// let abs_difference = (x.powi(2) - x*x).abs();
413 /// assert!(abs_difference <= f32::EPSILON);
415 #[stable(feature = "rust1", since = "1.0.0")]
417 pub fn powi(self, n
: i32) -> f32 { num::Float::powi(self, n) }
419 /// Raises a number to a floating point power.
425 /// let abs_difference = (x.powf(2.0) - x*x).abs();
427 /// assert!(abs_difference <= f32::EPSILON);
429 #[stable(feature = "rust1", since = "1.0.0")]
431 pub fn powf(self, n
: f32) -> f32 { num::Float::powf(self, n) }
433 /// Takes the square root of a number.
435 /// Returns NaN if `self` is a negative number.
440 /// let positive = 4.0_f32;
441 /// let negative = -4.0_f32;
443 /// let abs_difference = (positive.sqrt() - 2.0).abs();
445 /// assert!(abs_difference <= f32::EPSILON);
446 /// assert!(negative.sqrt().is_nan());
448 #[stable(feature = "rust1", since = "1.0.0")]
450 pub fn sqrt(self) -> f32 { num::Float::sqrt(self) }
452 /// Returns `e^(self)`, (the exponential function).
457 /// let one = 1.0f32;
459 /// let e = one.exp();
461 /// // ln(e) - 1 == 0
462 /// let abs_difference = (e.ln() - 1.0).abs();
464 /// assert!(abs_difference <= f32::EPSILON);
466 #[stable(feature = "rust1", since = "1.0.0")]
468 pub fn exp(self) -> f32 { num::Float::exp(self) }
470 /// Returns `2^(self)`.
478 /// let abs_difference = (f.exp2() - 4.0).abs();
480 /// assert!(abs_difference <= f32::EPSILON);
482 #[stable(feature = "rust1", since = "1.0.0")]
484 pub fn exp2(self) -> f32 { num::Float::exp2(self) }
486 /// Returns the natural logarithm of the number.
491 /// let one = 1.0f32;
493 /// let e = one.exp();
495 /// // ln(e) - 1 == 0
496 /// let abs_difference = (e.ln() - 1.0).abs();
498 /// assert!(abs_difference <= f32::EPSILON);
500 #[stable(feature = "rust1", since = "1.0.0")]
502 pub fn ln(self) -> f32 { num::Float::ln(self) }
504 /// Returns the logarithm of the number with respect to an arbitrary base.
509 /// let ten = 10.0f32;
510 /// let two = 2.0f32;
512 /// // log10(10) - 1 == 0
513 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
515 /// // log2(2) - 1 == 0
516 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
518 /// assert!(abs_difference_10 <= f32::EPSILON);
519 /// assert!(abs_difference_2 <= f32::EPSILON);
521 #[stable(feature = "rust1", since = "1.0.0")]
523 pub fn log(self, base
: f32) -> f32 { num::Float::log(self, base) }
525 /// Returns the base 2 logarithm of the number.
530 /// let two = 2.0f32;
532 /// // log2(2) - 1 == 0
533 /// let abs_difference = (two.log2() - 1.0).abs();
535 /// assert!(abs_difference <= f32::EPSILON);
537 #[stable(feature = "rust1", since = "1.0.0")]
539 pub fn log2(self) -> f32 { num::Float::log2(self) }
541 /// Returns the base 10 logarithm of the number.
546 /// let ten = 10.0f32;
548 /// // log10(10) - 1 == 0
549 /// let abs_difference = (ten.log10() - 1.0).abs();
551 /// assert!(abs_difference <= f32::EPSILON);
553 #[stable(feature = "rust1", since = "1.0.0")]
555 pub fn log10(self) -> f32 { num::Float::log10(self) }
557 /// Converts radians to degrees.
560 /// # #![feature(float_extras)]
561 /// use std::f32::{self, consts};
563 /// let angle = consts::PI;
565 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
567 /// assert!(abs_difference <= f32::EPSILON);
569 #[unstable(feature = "float_extras", reason = "desirability is unclear")]
571 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
573 /// Converts degrees to radians.
576 /// # #![feature(float_extras)]
577 /// use std::f32::{self, consts};
579 /// let angle = 180.0f32;
581 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
583 /// assert!(abs_difference <= f32::EPSILON);
585 #[unstable(feature = "float_extras", reason = "desirability is unclear")]
587 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
589 /// Constructs a floating point number of `x*2^exp`.
592 /// # #![feature(float_extras)]
594 /// // 3*2^2 - 12 == 0
595 /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
597 /// assert!(abs_difference <= f32::EPSILON);
599 #[unstable(feature = "float_extras",
600 reason
= "pending integer conventions")]
602 pub fn ldexp(x
: f32, exp
: isize) -> f32 {
603 unsafe { cmath::ldexpf(x, exp as c_int) }
606 /// Breaks the number into a normalized fraction and a base-2 exponent,
609 /// * `self = x * 2^exp`
610 /// * `0.5 <= abs(x) < 1.0`
613 /// # #![feature(float_extras)]
618 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
619 /// let f = x.frexp();
620 /// let abs_difference_0 = (f.0 - 0.5).abs();
621 /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
623 /// assert!(abs_difference_0 <= f32::EPSILON);
624 /// assert!(abs_difference_1 <= f32::EPSILON);
626 #[unstable(feature = "float_extras",
627 reason
= "pending integer conventions")]
629 pub fn frexp(self) -> (f32, isize) {
632 let x
= cmath
::frexpf(self, &mut exp
);
637 /// Returns the next representable floating-point value in the direction of
641 /// # #![feature(float_extras)]
646 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
648 /// assert!(abs_diff <= f32::EPSILON);
650 #[unstable(feature = "float_extras",
651 reason
= "unsure about its place in the world")]
653 pub fn next_after(self, other
: f32) -> f32 {
654 unsafe { cmath::nextafterf(self, other) }
657 /// Returns the maximum of the two numbers.
663 /// assert_eq!(x.max(y), y);
666 /// If one of the arguments is NaN, then the other argument is returned.
667 #[stable(feature = "rust1", since = "1.0.0")]
669 pub fn max(self, other
: f32) -> f32 {
670 unsafe { cmath::fmaxf(self, other) }
673 /// Returns the minimum of the two numbers.
679 /// assert_eq!(x.min(y), x);
682 /// If one of the arguments is NaN, then the other argument is returned.
683 #[stable(feature = "rust1", since = "1.0.0")]
685 pub fn min(self, other
: f32) -> f32 {
686 unsafe { cmath::fminf(self, other) }
689 /// The positive difference of two numbers.
691 /// * If `self <= other`: `0:0`
692 /// * Else: `self - other`
700 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
701 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
703 /// assert!(abs_difference_x <= f32::EPSILON);
704 /// assert!(abs_difference_y <= f32::EPSILON);
706 #[stable(feature = "rust1", since = "1.0.0")]
708 pub fn abs_sub(self, other
: f32) -> f32 {
709 unsafe { cmath::fdimf(self, other) }
712 /// Takes the cubic root of a number.
719 /// // x^(1/3) - 2 == 0
720 /// let abs_difference = (x.cbrt() - 2.0).abs();
722 /// assert!(abs_difference <= f32::EPSILON);
724 #[stable(feature = "rust1", since = "1.0.0")]
726 pub fn cbrt(self) -> f32 {
727 unsafe { cmath::cbrtf(self) }
730 /// Calculates the length of the hypotenuse of a right-angle triangle given
731 /// legs of length `x` and `y`.
739 /// // sqrt(x^2 + y^2)
740 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
742 /// assert!(abs_difference <= f32::EPSILON);
744 #[stable(feature = "rust1", since = "1.0.0")]
746 pub fn hypot(self, other
: f32) -> f32 {
747 unsafe { cmath::hypotf(self, other) }
750 /// Computes the sine of a number (in radians).
755 /// let x = f32::consts::PI/2.0;
757 /// let abs_difference = (x.sin() - 1.0).abs();
759 /// assert!(abs_difference <= f32::EPSILON);
761 #[stable(feature = "rust1", since = "1.0.0")]
763 pub fn sin(self) -> f32 {
764 unsafe { intrinsics::sinf32(self) }
767 /// Computes the cosine of a number (in radians).
772 /// let x = 2.0*f32::consts::PI;
774 /// let abs_difference = (x.cos() - 1.0).abs();
776 /// assert!(abs_difference <= f32::EPSILON);
778 #[stable(feature = "rust1", since = "1.0.0")]
780 pub fn cos(self) -> f32 {
781 unsafe { intrinsics::cosf32(self) }
784 /// Computes the tangent of a number (in radians).
789 /// let x = f64::consts::PI/4.0;
790 /// let abs_difference = (x.tan() - 1.0).abs();
792 /// assert!(abs_difference < 1e-10);
794 #[stable(feature = "rust1", since = "1.0.0")]
796 pub fn tan(self) -> f32 {
797 unsafe { cmath::tanf(self) }
800 /// Computes the arcsine of a number. Return value is in radians in
801 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
807 /// let f = f32::consts::PI / 2.0;
809 /// // asin(sin(pi/2))
810 /// let abs_difference = f.sin().asin().abs_sub(f32::consts::PI / 2.0);
812 /// assert!(abs_difference <= f32::EPSILON);
814 #[stable(feature = "rust1", since = "1.0.0")]
816 pub fn asin(self) -> f32 {
817 unsafe { cmath::asinf(self) }
820 /// Computes the arccosine of a number. Return value is in radians in
821 /// the range [0, pi] or NaN if the number is outside the range
827 /// let f = f32::consts::PI / 4.0;
829 /// // acos(cos(pi/4))
830 /// let abs_difference = f.cos().acos().abs_sub(f32::consts::PI / 4.0);
832 /// assert!(abs_difference <= f32::EPSILON);
834 #[stable(feature = "rust1", since = "1.0.0")]
836 pub fn acos(self) -> f32 {
837 unsafe { cmath::acosf(self) }
840 /// Computes the arctangent of a number. Return value is in radians in the
841 /// range [-pi/2, pi/2];
849 /// let abs_difference = f.tan().atan().abs_sub(1.0);
851 /// assert!(abs_difference <= f32::EPSILON);
853 #[stable(feature = "rust1", since = "1.0.0")]
855 pub fn atan(self) -> f32 {
856 unsafe { cmath::atanf(self) }
859 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
861 /// * `x = 0`, `y = 0`: `0`
862 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
863 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
864 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
869 /// let pi = f32::consts::PI;
870 /// // All angles from horizontal right (+x)
871 /// // 45 deg counter-clockwise
873 /// let y1 = -3.0f32;
875 /// // 135 deg clockwise
876 /// let x2 = -3.0f32;
879 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
880 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
882 /// assert!(abs_difference_1 <= f32::EPSILON);
883 /// assert!(abs_difference_2 <= f32::EPSILON);
885 #[stable(feature = "rust1", since = "1.0.0")]
887 pub fn atan2(self, other
: f32) -> f32 {
888 unsafe { cmath::atan2f(self, other) }
891 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
892 /// `(sin(x), cos(x))`.
897 /// let x = f32::consts::PI/4.0;
898 /// let f = x.sin_cos();
900 /// let abs_difference_0 = (f.0 - x.sin()).abs();
901 /// let abs_difference_1 = (f.1 - x.cos()).abs();
903 /// assert!(abs_difference_0 <= f32::EPSILON);
904 /// assert!(abs_difference_0 <= f32::EPSILON);
906 #[stable(feature = "rust1", since = "1.0.0")]
908 pub fn sin_cos(self) -> (f32, f32) {
909 (self.sin(), self.cos())
912 /// Returns `e^(self) - 1` in a way that is accurate even if the
913 /// number is close to zero.
919 /// let abs_difference = x.ln().exp_m1().abs_sub(6.0);
921 /// assert!(abs_difference < 1e-10);
923 #[stable(feature = "rust1", since = "1.0.0")]
925 pub fn exp_m1(self) -> f32 {
926 unsafe { cmath::expm1f(self) }
929 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
930 /// the operations were performed separately.
935 /// let x = f32::consts::E - 1.0;
937 /// // ln(1 + (e - 1)) == ln(e) == 1
938 /// let abs_difference = (x.ln_1p() - 1.0).abs();
940 /// assert!(abs_difference <= f32::EPSILON);
942 #[stable(feature = "rust1", since = "1.0.0")]
944 pub fn ln_1p(self) -> f32 {
945 unsafe { cmath::log1pf(self) }
948 /// Hyperbolic sine function.
953 /// let e = f32::consts::E;
956 /// let f = x.sinh();
957 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
958 /// let g = (e*e - 1.0)/(2.0*e);
959 /// let abs_difference = (f - g).abs();
961 /// assert!(abs_difference <= f32::EPSILON);
963 #[stable(feature = "rust1", since = "1.0.0")]
965 pub fn sinh(self) -> f32 {
966 unsafe { cmath::sinhf(self) }
969 /// Hyperbolic cosine function.
974 /// let e = f32::consts::E;
976 /// let f = x.cosh();
977 /// // Solving cosh() at 1 gives this result
978 /// let g = (e*e + 1.0)/(2.0*e);
979 /// let abs_difference = f.abs_sub(g);
982 /// assert!(abs_difference <= f32::EPSILON);
984 #[stable(feature = "rust1", since = "1.0.0")]
986 pub fn cosh(self) -> f32 {
987 unsafe { cmath::coshf(self) }
990 /// Hyperbolic tangent function.
995 /// let e = f32::consts::E;
998 /// let f = x.tanh();
999 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1000 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1001 /// let abs_difference = (f - g).abs();
1003 /// assert!(abs_difference <= f32::EPSILON);
1005 #[stable(feature = "rust1", since = "1.0.0")]
1007 pub fn tanh(self) -> f32 {
1008 unsafe { cmath::tanhf(self) }
1011 /// Inverse hyperbolic sine function.
1017 /// let f = x.sinh().asinh();
1019 /// let abs_difference = (f - x).abs();
1021 /// assert!(abs_difference <= f32::EPSILON);
1023 #[stable(feature = "rust1", since = "1.0.0")]
1025 pub fn asinh(self) -> f32 {
1027 NEG_INFINITY
=> NEG_INFINITY
,
1028 x
=> (x
+ ((x
* x
) + 1.0).sqrt()).ln(),
1032 /// Inverse hyperbolic cosine function.
1038 /// let f = x.cosh().acosh();
1040 /// let abs_difference = (f - x).abs();
1042 /// assert!(abs_difference <= f32::EPSILON);
1044 #[stable(feature = "rust1", since = "1.0.0")]
1046 pub fn acosh(self) -> f32 {
1048 x
if x
< 1.0 => ::f32::NAN
,
1049 x
=> (x
+ ((x
* x
) - 1.0).sqrt()).ln(),
1053 /// Inverse hyperbolic tangent function.
1058 /// let e = f32::consts::E;
1059 /// let f = e.tanh().atanh();
1061 /// let abs_difference = f.abs_sub(e);
1063 /// assert!(abs_difference <= f32::EPSILON);
1065 #[stable(feature = "rust1", since = "1.0.0")]
1067 pub fn atanh(self) -> f32 {
1068 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1077 use num
::FpCategory
as Fp
;
1081 test_num(10f32, 2f32);
1086 assert_eq
!(NAN
.min(2.0), 2.0);
1087 assert_eq
!(2.0f32.min(NAN
), 2.0);
1092 assert_eq
!(NAN
.max(2.0), 2.0);
1093 assert_eq
!(2.0f32.max(NAN
), 2.0);
1098 let nan
: f32 = f32::NAN
;
1099 assert
!(nan
.is_nan());
1100 assert
!(!nan
.is_infinite());
1101 assert
!(!nan
.is_finite());
1102 assert
!(!nan
.is_normal());
1103 assert
!(!nan
.is_sign_positive());
1104 assert
!(!nan
.is_sign_negative());
1105 assert_eq
!(Fp
::Nan
, nan
.classify());
1109 fn test_infinity() {
1110 let inf
: f32 = f32::INFINITY
;
1111 assert
!(inf
.is_infinite());
1112 assert
!(!inf
.is_finite());
1113 assert
!(inf
.is_sign_positive());
1114 assert
!(!inf
.is_sign_negative());
1115 assert
!(!inf
.is_nan());
1116 assert
!(!inf
.is_normal());
1117 assert_eq
!(Fp
::Infinite
, inf
.classify());
1121 fn test_neg_infinity() {
1122 let neg_inf
: f32 = f32::NEG_INFINITY
;
1123 assert
!(neg_inf
.is_infinite());
1124 assert
!(!neg_inf
.is_finite());
1125 assert
!(!neg_inf
.is_sign_positive());
1126 assert
!(neg_inf
.is_sign_negative());
1127 assert
!(!neg_inf
.is_nan());
1128 assert
!(!neg_inf
.is_normal());
1129 assert_eq
!(Fp
::Infinite
, neg_inf
.classify());
1134 let zero
: f32 = 0.0f32;
1135 assert_eq
!(0.0, zero
);
1136 assert
!(!zero
.is_infinite());
1137 assert
!(zero
.is_finite());
1138 assert
!(zero
.is_sign_positive());
1139 assert
!(!zero
.is_sign_negative());
1140 assert
!(!zero
.is_nan());
1141 assert
!(!zero
.is_normal());
1142 assert_eq
!(Fp
::Zero
, zero
.classify());
1146 fn test_neg_zero() {
1147 let neg_zero
: f32 = -0.0;
1148 assert_eq
!(0.0, neg_zero
);
1149 assert
!(!neg_zero
.is_infinite());
1150 assert
!(neg_zero
.is_finite());
1151 assert
!(!neg_zero
.is_sign_positive());
1152 assert
!(neg_zero
.is_sign_negative());
1153 assert
!(!neg_zero
.is_nan());
1154 assert
!(!neg_zero
.is_normal());
1155 assert_eq
!(Fp
::Zero
, neg_zero
.classify());
1160 let one
: f32 = 1.0f32;
1161 assert_eq
!(1.0, one
);
1162 assert
!(!one
.is_infinite());
1163 assert
!(one
.is_finite());
1164 assert
!(one
.is_sign_positive());
1165 assert
!(!one
.is_sign_negative());
1166 assert
!(!one
.is_nan());
1167 assert
!(one
.is_normal());
1168 assert_eq
!(Fp
::Normal
, one
.classify());
1173 let nan
: f32 = f32::NAN
;
1174 let inf
: f32 = f32::INFINITY
;
1175 let neg_inf
: f32 = f32::NEG_INFINITY
;
1176 assert
!(nan
.is_nan());
1177 assert
!(!0.0f32.is_nan());
1178 assert
!(!5.3f32.is_nan());
1179 assert
!(!(-10.732f32).is_nan());
1180 assert
!(!inf
.is_nan());
1181 assert
!(!neg_inf
.is_nan());
1185 fn test_is_infinite() {
1186 let nan
: f32 = f32::NAN
;
1187 let inf
: f32 = f32::INFINITY
;
1188 let neg_inf
: f32 = f32::NEG_INFINITY
;
1189 assert
!(!nan
.is_infinite());
1190 assert
!(inf
.is_infinite());
1191 assert
!(neg_inf
.is_infinite());
1192 assert
!(!0.0f32.is_infinite());
1193 assert
!(!42.8f32.is_infinite());
1194 assert
!(!(-109.2f32).is_infinite());
1198 fn test_is_finite() {
1199 let nan
: f32 = f32::NAN
;
1200 let inf
: f32 = f32::INFINITY
;
1201 let neg_inf
: f32 = f32::NEG_INFINITY
;
1202 assert
!(!nan
.is_finite());
1203 assert
!(!inf
.is_finite());
1204 assert
!(!neg_inf
.is_finite());
1205 assert
!(0.0f32.is_finite());
1206 assert
!(42.8f32.is_finite());
1207 assert
!((-109.2f32).is_finite());
1211 fn test_is_normal() {
1212 let nan
: f32 = f32::NAN
;
1213 let inf
: f32 = f32::INFINITY
;
1214 let neg_inf
: f32 = f32::NEG_INFINITY
;
1215 let zero
: f32 = 0.0f32;
1216 let neg_zero
: f32 = -0.0;
1217 assert
!(!nan
.is_normal());
1218 assert
!(!inf
.is_normal());
1219 assert
!(!neg_inf
.is_normal());
1220 assert
!(!zero
.is_normal());
1221 assert
!(!neg_zero
.is_normal());
1222 assert
!(1f32.is_normal());
1223 assert
!(1e
-37f32.is_normal());
1224 assert
!(!1e
-38f32.is_normal());
1228 fn test_classify() {
1229 let nan
: f32 = f32::NAN
;
1230 let inf
: f32 = f32::INFINITY
;
1231 let neg_inf
: f32 = f32::NEG_INFINITY
;
1232 let zero
: f32 = 0.0f32;
1233 let neg_zero
: f32 = -0.0;
1234 assert_eq
!(nan
.classify(), Fp
::Nan
);
1235 assert_eq
!(inf
.classify(), Fp
::Infinite
);
1236 assert_eq
!(neg_inf
.classify(), Fp
::Infinite
);
1237 assert_eq
!(zero
.classify(), Fp
::Zero
);
1238 assert_eq
!(neg_zero
.classify(), Fp
::Zero
);
1239 assert_eq
!(1f32.classify(), Fp
::Normal
);
1240 assert_eq
!(1e
-37f32.classify(), Fp
::Normal
);
1241 assert_eq
!(1e
-38f32.classify(), Fp
::Subnormal
);
1245 fn test_integer_decode() {
1246 assert_eq
!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
1247 assert_eq
!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
1248 assert_eq
!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
1249 assert_eq
!(0f32.integer_decode(), (0, -150, 1));
1250 assert_eq
!((-0f32).integer_decode(), (0, -150, -1));
1251 assert_eq
!(INFINITY
.integer_decode(), (8388608, 105, 1));
1252 assert_eq
!(NEG_INFINITY
.integer_decode(), (8388608, 105, -1));
1253 assert_eq
!(NAN
.integer_decode(), (12582912, 105, 1));
1258 assert_approx_eq
!(1.0f32.floor(), 1.0f32);
1259 assert_approx_eq
!(1.3f32.floor(), 1.0f32);
1260 assert_approx_eq
!(1.5f32.floor(), 1.0f32);
1261 assert_approx_eq
!(1.7f32.floor(), 1.0f32);
1262 assert_approx_eq
!(0.0f32.floor(), 0.0f32);
1263 assert_approx_eq
!((-0.0f32).floor(), -0.0f32);
1264 assert_approx_eq
!((-1.0f32).floor(), -1.0f32);
1265 assert_approx_eq
!((-1.3f32).floor(), -2.0f32);
1266 assert_approx_eq
!((-1.5f32).floor(), -2.0f32);
1267 assert_approx_eq
!((-1.7f32).floor(), -2.0f32);
1272 assert_approx_eq
!(1.0f32.ceil(), 1.0f32);
1273 assert_approx_eq
!(1.3f32.ceil(), 2.0f32);
1274 assert_approx_eq
!(1.5f32.ceil(), 2.0f32);
1275 assert_approx_eq
!(1.7f32.ceil(), 2.0f32);
1276 assert_approx_eq
!(0.0f32.ceil(), 0.0f32);
1277 assert_approx_eq
!((-0.0f32).ceil(), -0.0f32);
1278 assert_approx_eq
!((-1.0f32).ceil(), -1.0f32);
1279 assert_approx_eq
!((-1.3f32).ceil(), -1.0f32);
1280 assert_approx_eq
!((-1.5f32).ceil(), -1.0f32);
1281 assert_approx_eq
!((-1.7f32).ceil(), -1.0f32);
1286 assert_approx_eq
!(1.0f32.round(), 1.0f32);
1287 assert_approx_eq
!(1.3f32.round(), 1.0f32);
1288 assert_approx_eq
!(1.5f32.round(), 2.0f32);
1289 assert_approx_eq
!(1.7f32.round(), 2.0f32);
1290 assert_approx_eq
!(0.0f32.round(), 0.0f32);
1291 assert_approx_eq
!((-0.0f32).round(), -0.0f32);
1292 assert_approx_eq
!((-1.0f32).round(), -1.0f32);
1293 assert_approx_eq
!((-1.3f32).round(), -1.0f32);
1294 assert_approx_eq
!((-1.5f32).round(), -2.0f32);
1295 assert_approx_eq
!((-1.7f32).round(), -2.0f32);
1300 assert_approx_eq
!(1.0f32.trunc(), 1.0f32);
1301 assert_approx_eq
!(1.3f32.trunc(), 1.0f32);
1302 assert_approx_eq
!(1.5f32.trunc(), 1.0f32);
1303 assert_approx_eq
!(1.7f32.trunc(), 1.0f32);
1304 assert_approx_eq
!(0.0f32.trunc(), 0.0f32);
1305 assert_approx_eq
!((-0.0f32).trunc(), -0.0f32);
1306 assert_approx_eq
!((-1.0f32).trunc(), -1.0f32);
1307 assert_approx_eq
!((-1.3f32).trunc(), -1.0f32);
1308 assert_approx_eq
!((-1.5f32).trunc(), -1.0f32);
1309 assert_approx_eq
!((-1.7f32).trunc(), -1.0f32);
1314 assert_approx_eq
!(1.0f32.fract(), 0.0f32);
1315 assert_approx_eq
!(1.3f32.fract(), 0.3f32);
1316 assert_approx_eq
!(1.5f32.fract(), 0.5f32);
1317 assert_approx_eq
!(1.7f32.fract(), 0.7f32);
1318 assert_approx_eq
!(0.0f32.fract(), 0.0f32);
1319 assert_approx_eq
!((-0.0f32).fract(), -0.0f32);
1320 assert_approx_eq
!((-1.0f32).fract(), -0.0f32);
1321 assert_approx_eq
!((-1.3f32).fract(), -0.3f32);
1322 assert_approx_eq
!((-1.5f32).fract(), -0.5f32);
1323 assert_approx_eq
!((-1.7f32).fract(), -0.7f32);
1328 assert_eq
!(INFINITY
.abs(), INFINITY
);
1329 assert_eq
!(1f32.abs(), 1f32);
1330 assert_eq
!(0f32.abs(), 0f32);
1331 assert_eq
!((-0f32).abs(), 0f32);
1332 assert_eq
!((-1f32).abs(), 1f32);
1333 assert_eq
!(NEG_INFINITY
.abs(), INFINITY
);
1334 assert_eq
!((1f32/NEG_INFINITY
).abs(), 0f32);
1335 assert
!(NAN
.abs().is_nan());
1340 assert_eq
!(INFINITY
.signum(), 1f32);
1341 assert_eq
!(1f32.signum(), 1f32);
1342 assert_eq
!(0f32.signum(), 1f32);
1343 assert_eq
!((-0f32).signum(), -1f32);
1344 assert_eq
!((-1f32).signum(), -1f32);
1345 assert_eq
!(NEG_INFINITY
.signum(), -1f32);
1346 assert_eq
!((1f32/NEG_INFINITY
).signum(), -1f32);
1347 assert
!(NAN
.signum().is_nan());
1351 fn test_is_sign_positive() {
1352 assert
!(INFINITY
.is_sign_positive());
1353 assert
!(1f32.is_sign_positive());
1354 assert
!(0f32.is_sign_positive());
1355 assert
!(!(-0f32).is_sign_positive());
1356 assert
!(!(-1f32).is_sign_positive());
1357 assert
!(!NEG_INFINITY
.is_sign_positive());
1358 assert
!(!(1f32/NEG_INFINITY
).is_sign_positive());
1359 assert
!(!NAN
.is_sign_positive());
1363 fn test_is_sign_negative() {
1364 assert
!(!INFINITY
.is_sign_negative());
1365 assert
!(!1f32.is_sign_negative());
1366 assert
!(!0f32.is_sign_negative());
1367 assert
!((-0f32).is_sign_negative());
1368 assert
!((-1f32).is_sign_negative());
1369 assert
!(NEG_INFINITY
.is_sign_negative());
1370 assert
!((1f32/NEG_INFINITY
).is_sign_negative());
1371 assert
!(!NAN
.is_sign_negative());
1376 let nan
: f32 = f32::NAN
;
1377 let inf
: f32 = f32::INFINITY
;
1378 let neg_inf
: f32 = f32::NEG_INFINITY
;
1379 assert_approx_eq
!(12.3f32.mul_add(4.5, 6.7), 62.05);
1380 assert_approx_eq
!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1381 assert_approx_eq
!(0.0f32.mul_add(8.9, 1.2), 1.2);
1382 assert_approx_eq
!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1383 assert
!(nan
.mul_add(7.8, 9.0).is_nan());
1384 assert_eq
!(inf
.mul_add(7.8, 9.0), inf
);
1385 assert_eq
!(neg_inf
.mul_add(7.8, 9.0), neg_inf
);
1386 assert_eq
!(8.9f32.mul_add(inf
, 3.2), inf
);
1387 assert_eq
!((-3.2f32).mul_add(2.4, neg_inf
), neg_inf
);
1392 let nan
: f32 = f32::NAN
;
1393 let inf
: f32 = f32::INFINITY
;
1394 let neg_inf
: f32 = f32::NEG_INFINITY
;
1395 assert_eq
!(1.0f32.recip(), 1.0);
1396 assert_eq
!(2.0f32.recip(), 0.5);
1397 assert_eq
!((-0.4f32).recip(), -2.5);
1398 assert_eq
!(0.0f32.recip(), inf
);
1399 assert
!(nan
.recip().is_nan());
1400 assert_eq
!(inf
.recip(), 0.0);
1401 assert_eq
!(neg_inf
.recip(), 0.0);
1406 let nan
: f32 = f32::NAN
;
1407 let inf
: f32 = f32::INFINITY
;
1408 let neg_inf
: f32 = f32::NEG_INFINITY
;
1409 assert_eq
!(1.0f32.powi(1), 1.0);
1410 assert_approx_eq
!((-3.1f32).powi(2), 9.61);
1411 assert_approx_eq
!(5.9f32.powi(-2), 0.028727);
1412 assert_eq
!(8.3f32.powi(0), 1.0);
1413 assert
!(nan
.powi(2).is_nan());
1414 assert_eq
!(inf
.powi(3), inf
);
1415 assert_eq
!(neg_inf
.powi(2), inf
);
1420 let nan
: f32 = f32::NAN
;
1421 let inf
: f32 = f32::INFINITY
;
1422 let neg_inf
: f32 = f32::NEG_INFINITY
;
1423 assert_eq
!(1.0f32.powf(1.0), 1.0);
1424 assert_approx_eq
!(3.4f32.powf(4.5), 246.408218);
1425 assert_approx_eq
!(2.7f32.powf(-3.2), 0.041652);
1426 assert_approx_eq
!((-3.1f32).powf(2.0), 9.61);
1427 assert_approx_eq
!(5.9f32.powf(-2.0), 0.028727);
1428 assert_eq
!(8.3f32.powf(0.0), 1.0);
1429 assert
!(nan
.powf(2.0).is_nan());
1430 assert_eq
!(inf
.powf(2.0), inf
);
1431 assert_eq
!(neg_inf
.powf(3.0), neg_inf
);
1435 fn test_sqrt_domain() {
1436 assert
!(NAN
.sqrt().is_nan());
1437 assert
!(NEG_INFINITY
.sqrt().is_nan());
1438 assert
!((-1.0f32).sqrt().is_nan());
1439 assert_eq
!((-0.0f32).sqrt(), -0.0);
1440 assert_eq
!(0.0f32.sqrt(), 0.0);
1441 assert_eq
!(1.0f32.sqrt(), 1.0);
1442 assert_eq
!(INFINITY
.sqrt(), INFINITY
);
1447 assert_eq
!(1.0, 0.0f32.exp());
1448 assert_approx_eq
!(2.718282, 1.0f32.exp());
1449 assert_approx_eq
!(148.413162, 5.0f32.exp());
1451 let inf
: f32 = f32::INFINITY
;
1452 let neg_inf
: f32 = f32::NEG_INFINITY
;
1453 let nan
: f32 = f32::NAN
;
1454 assert_eq
!(inf
, inf
.exp());
1455 assert_eq
!(0.0, neg_inf
.exp());
1456 assert
!(nan
.exp().is_nan());
1461 assert_eq
!(32.0, 5.0f32.exp2());
1462 assert_eq
!(1.0, 0.0f32.exp2());
1464 let inf
: f32 = f32::INFINITY
;
1465 let neg_inf
: f32 = f32::NEG_INFINITY
;
1466 let nan
: f32 = f32::NAN
;
1467 assert_eq
!(inf
, inf
.exp2());
1468 assert_eq
!(0.0, neg_inf
.exp2());
1469 assert
!(nan
.exp2().is_nan());
1474 let nan
: f32 = f32::NAN
;
1475 let inf
: f32 = f32::INFINITY
;
1476 let neg_inf
: f32 = f32::NEG_INFINITY
;
1477 assert_approx_eq
!(1.0f32.exp().ln(), 1.0);
1478 assert
!(nan
.ln().is_nan());
1479 assert_eq
!(inf
.ln(), inf
);
1480 assert
!(neg_inf
.ln().is_nan());
1481 assert
!((-2.3f32).ln().is_nan());
1482 assert_eq
!((-0.0f32).ln(), neg_inf
);
1483 assert_eq
!(0.0f32.ln(), neg_inf
);
1484 assert_approx_eq
!(4.0f32.ln(), 1.386294);
1489 let nan
: f32 = f32::NAN
;
1490 let inf
: f32 = f32::INFINITY
;
1491 let neg_inf
: f32 = f32::NEG_INFINITY
;
1492 assert_eq
!(10.0f32.log(10.0), 1.0);
1493 assert_approx_eq
!(2.3f32.log(3.5), 0.664858);
1494 assert_eq
!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1495 assert
!(1.0f32.log(1.0).is_nan());
1496 assert
!(1.0f32.log(-13.9).is_nan());
1497 assert
!(nan
.log(2.3).is_nan());
1498 assert_eq
!(inf
.log(10.0), inf
);
1499 assert
!(neg_inf
.log(8.8).is_nan());
1500 assert
!((-2.3f32).log(0.1).is_nan());
1501 assert_eq
!((-0.0f32).log(2.0), neg_inf
);
1502 assert_eq
!(0.0f32.log(7.0), neg_inf
);
1507 let nan
: f32 = f32::NAN
;
1508 let inf
: f32 = f32::INFINITY
;
1509 let neg_inf
: f32 = f32::NEG_INFINITY
;
1510 assert_approx_eq
!(10.0f32.log2(), 3.321928);
1511 assert_approx_eq
!(2.3f32.log2(), 1.201634);
1512 assert_approx_eq
!(1.0f32.exp().log2(), 1.442695);
1513 assert
!(nan
.log2().is_nan());
1514 assert_eq
!(inf
.log2(), inf
);
1515 assert
!(neg_inf
.log2().is_nan());
1516 assert
!((-2.3f32).log2().is_nan());
1517 assert_eq
!((-0.0f32).log2(), neg_inf
);
1518 assert_eq
!(0.0f32.log2(), neg_inf
);
1523 let nan
: f32 = f32::NAN
;
1524 let inf
: f32 = f32::INFINITY
;
1525 let neg_inf
: f32 = f32::NEG_INFINITY
;
1526 assert_eq
!(10.0f32.log10(), 1.0);
1527 assert_approx_eq
!(2.3f32.log10(), 0.361728);
1528 assert_approx_eq
!(1.0f32.exp().log10(), 0.434294);
1529 assert_eq
!(1.0f32.log10(), 0.0);
1530 assert
!(nan
.log10().is_nan());
1531 assert_eq
!(inf
.log10(), inf
);
1532 assert
!(neg_inf
.log10().is_nan());
1533 assert
!((-2.3f32).log10().is_nan());
1534 assert_eq
!((-0.0f32).log10(), neg_inf
);
1535 assert_eq
!(0.0f32.log10(), neg_inf
);
1539 fn test_to_degrees() {
1540 let pi
: f32 = consts
::PI
;
1541 let nan
: f32 = f32::NAN
;
1542 let inf
: f32 = f32::INFINITY
;
1543 let neg_inf
: f32 = f32::NEG_INFINITY
;
1544 assert_eq
!(0.0f32.to_degrees(), 0.0);
1545 assert_approx_eq
!((-5.8f32).to_degrees(), -332.315521);
1546 assert_eq
!(pi
.to_degrees(), 180.0);
1547 assert
!(nan
.to_degrees().is_nan());
1548 assert_eq
!(inf
.to_degrees(), inf
);
1549 assert_eq
!(neg_inf
.to_degrees(), neg_inf
);
1553 fn test_to_radians() {
1554 let pi
: f32 = consts
::PI
;
1555 let nan
: f32 = f32::NAN
;
1556 let inf
: f32 = f32::INFINITY
;
1557 let neg_inf
: f32 = f32::NEG_INFINITY
;
1558 assert_eq
!(0.0f32.to_radians(), 0.0);
1559 assert_approx_eq
!(154.6f32.to_radians(), 2.698279);
1560 assert_approx_eq
!((-332.31f32).to_radians(), -5.799903);
1561 assert_eq
!(180.0f32.to_radians(), pi
);
1562 assert
!(nan
.to_radians().is_nan());
1563 assert_eq
!(inf
.to_radians(), inf
);
1564 assert_eq
!(neg_inf
.to_radians(), neg_inf
);
1569 // We have to use from_str until base-2 exponents
1570 // are supported in floating-point literals
1571 let f1
: f32 = f32::from_str_radix("1p-123", 16).unwrap();
1572 let f2
: f32 = f32::from_str_radix("1p-111", 16).unwrap();
1573 let f3
: f32 = f32::from_str_radix("1.Cp-12", 16).unwrap();
1574 assert_eq
!(f32::ldexp(1f32, -123), f1
);
1575 assert_eq
!(f32::ldexp(1f32, -111), f2
);
1576 assert_eq
!(f32::ldexp(1.75f32, -12), f3
);
1578 assert_eq
!(f32::ldexp(0f32, -123), 0f32);
1579 assert_eq
!(f32::ldexp(-0f32, -123), -0f32);
1581 let inf
: f32 = f32::INFINITY
;
1582 let neg_inf
: f32 = f32::NEG_INFINITY
;
1583 let nan
: f32 = f32::NAN
;
1584 assert_eq
!(f32::ldexp(inf
, -123), inf
);
1585 assert_eq
!(f32::ldexp(neg_inf
, -123), neg_inf
);
1586 assert
!(f32::ldexp(nan
, -123).is_nan());
1591 // We have to use from_str until base-2 exponents
1592 // are supported in floating-point literals
1593 let f1
: f32 = f32::from_str_radix("1p-123", 16).unwrap();
1594 let f2
: f32 = f32::from_str_radix("1p-111", 16).unwrap();
1595 let f3
: f32 = f32::from_str_radix("1.Cp-123", 16).unwrap();
1596 let (x1
, exp1
) = f1
.frexp();
1597 let (x2
, exp2
) = f2
.frexp();
1598 let (x3
, exp3
) = f3
.frexp();
1599 assert_eq
!((x1
, exp1
), (0.5f32, -122));
1600 assert_eq
!((x2
, exp2
), (0.5f32, -110));
1601 assert_eq
!((x3
, exp3
), (0.875f32, -122));
1602 assert_eq
!(f32::ldexp(x1
, exp1
), f1
);
1603 assert_eq
!(f32::ldexp(x2
, exp2
), f2
);
1604 assert_eq
!(f32::ldexp(x3
, exp3
), f3
);
1606 assert_eq
!(0f32.frexp(), (0f32, 0));
1607 assert_eq
!((-0f32).frexp(), (-0f32, 0));
1610 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1611 fn test_frexp_nowin() {
1612 let inf
: f32 = f32::INFINITY
;
1613 let neg_inf
: f32 = f32::NEG_INFINITY
;
1614 let nan
: f32 = f32::NAN
;
1615 assert_eq
!(match inf
.frexp() { (x, _) => x }
, inf
);
1616 assert_eq
!(match neg_inf
.frexp() { (x, _) => x }
, neg_inf
);
1617 assert
!(match nan
.frexp() { (x, _) => x.is_nan() }
)
1622 assert_eq
!((-1f32).abs_sub(1f32), 0f32);
1623 assert_eq
!(1f32.abs_sub(1f32), 0f32);
1624 assert_eq
!(1f32.abs_sub(0f32), 1f32);
1625 assert_eq
!(1f32.abs_sub(-1f32), 2f32);
1626 assert_eq
!(NEG_INFINITY
.abs_sub(0f32), 0f32);
1627 assert_eq
!(INFINITY
.abs_sub(1f32), INFINITY
);
1628 assert_eq
!(0f32.abs_sub(NEG_INFINITY
), INFINITY
);
1629 assert_eq
!(0f32.abs_sub(INFINITY
), 0f32);
1633 fn test_abs_sub_nowin() {
1634 assert
!(NAN
.abs_sub(-1f32).is_nan());
1635 assert
!(1f32.abs_sub(NAN
).is_nan());
1640 assert_eq
!(0.0f32.asinh(), 0.0f32);
1641 assert_eq
!((-0.0f32).asinh(), -0.0f32);
1643 let inf
: f32 = f32::INFINITY
;
1644 let neg_inf
: f32 = f32::NEG_INFINITY
;
1645 let nan
: f32 = f32::NAN
;
1646 assert_eq
!(inf
.asinh(), inf
);
1647 assert_eq
!(neg_inf
.asinh(), neg_inf
);
1648 assert
!(nan
.asinh().is_nan());
1649 assert_approx_eq
!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1650 assert_approx_eq
!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1655 assert_eq
!(1.0f32.acosh(), 0.0f32);
1656 assert
!(0.999f32.acosh().is_nan());
1658 let inf
: f32 = f32::INFINITY
;
1659 let neg_inf
: f32 = f32::NEG_INFINITY
;
1660 let nan
: f32 = f32::NAN
;
1661 assert_eq
!(inf
.acosh(), inf
);
1662 assert
!(neg_inf
.acosh().is_nan());
1663 assert
!(nan
.acosh().is_nan());
1664 assert_approx_eq
!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1665 assert_approx_eq
!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1670 assert_eq
!(0.0f32.atanh(), 0.0f32);
1671 assert_eq
!((-0.0f32).atanh(), -0.0f32);
1673 let inf32
: f32 = f32::INFINITY
;
1674 let neg_inf32
: f32 = f32::NEG_INFINITY
;
1675 assert_eq
!(1.0f32.atanh(), inf32
);
1676 assert_eq
!((-1.0f32).atanh(), neg_inf32
);
1678 assert
!(2f64.atanh().atanh().is_nan());
1679 assert
!((-2f64).atanh().atanh().is_nan());
1681 let inf64
: f32 = f32::INFINITY
;
1682 let neg_inf64
: f32 = f32::NEG_INFINITY
;
1683 let nan32
: f32 = f32::NAN
;
1684 assert
!(inf64
.atanh().is_nan());
1685 assert
!(neg_inf64
.atanh().is_nan());
1686 assert
!(nan32
.atanh().is_nan());
1688 assert_approx_eq
!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1689 assert_approx_eq
!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1693 fn test_real_consts() {
1696 let pi
: f32 = consts
::PI
;
1697 let two_pi
: f32 = consts
::PI_2
;
1698 let frac_pi_2
: f32 = consts
::FRAC_PI_2
;
1699 let frac_pi_3
: f32 = consts
::FRAC_PI_3
;
1700 let frac_pi_4
: f32 = consts
::FRAC_PI_4
;
1701 let frac_pi_6
: f32 = consts
::FRAC_PI_6
;
1702 let frac_pi_8
: f32 = consts
::FRAC_PI_8
;
1703 let frac_1_pi
: f32 = consts
::FRAC_1_PI
;
1704 let frac_2_pi
: f32 = consts
::FRAC_2_PI
;
1705 let frac_2_sqrtpi
: f32 = consts
::FRAC_2_SQRT_PI
;
1706 let sqrt2
: f32 = consts
::SQRT_2
;
1707 let frac_1_sqrt2
: f32 = consts
::FRAC_1_SQRT_2
;
1708 let e
: f32 = consts
::E
;
1709 let log2_e
: f32 = consts
::LOG2_E
;
1710 let log10_e
: f32 = consts
::LOG10_E
;
1711 let ln_2
: f32 = consts
::LN_2
;
1712 let ln_10
: f32 = consts
::LN_10
;
1714 assert_approx_eq
!(two_pi
, 2f32 * pi
);
1715 assert_approx_eq
!(frac_pi_2
, pi
/ 2f32);
1716 assert_approx_eq
!(frac_pi_3
, pi
/ 3f32);
1717 assert_approx_eq
!(frac_pi_4
, pi
/ 4f32);
1718 assert_approx_eq
!(frac_pi_6
, pi
/ 6f32);
1719 assert_approx_eq
!(frac_pi_8
, pi
/ 8f32);
1720 assert_approx_eq
!(frac_1_pi
, 1f32 / pi
);
1721 assert_approx_eq
!(frac_2_pi
, 2f32 / pi
);
1722 assert_approx_eq
!(frac_2_sqrtpi
, 2f32 / pi
.sqrt());
1723 assert_approx_eq
!(sqrt2
, 2f32.sqrt());
1724 assert_approx_eq
!(frac_1_sqrt2
, 1f32 / 2f32.sqrt());
1725 assert_approx_eq
!(log2_e
, e
.log2());
1726 assert_approx_eq
!(log10_e
, e
.log10());
1727 assert_approx_eq
!(ln_2
, 2f32.ln());
1728 assert_approx_eq
!(ln_10
, 10f32.ln());