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 //! This module provides constants which are specific to the implementation
12 //! of the `f32` floating point data type. Mathematically significant
13 //! numbers are provided in the `consts` sub-module.
15 //! *[See also the `f32` primitive type](../primitive.f32.html).*
17 #![stable(feature = "rust1", since = "1.0.0")]
18 #![allow(missing_docs)]
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub use core::f32::consts;
44 /// Returns `true` if this value is `NaN` and false otherwise.
49 /// let nan = f32::NAN;
52 /// assert!(nan.is_nan());
53 /// assert!(!f.is_nan());
55 #[stable(feature = "rust1", since = "1.0.0")]
57 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
59 /// Returns `true` if this value is positive infinity or negative infinity and
66 /// let inf = f32::INFINITY;
67 /// let neg_inf = f32::NEG_INFINITY;
68 /// let nan = f32::NAN;
70 /// assert!(!f.is_infinite());
71 /// assert!(!nan.is_infinite());
73 /// assert!(inf.is_infinite());
74 /// assert!(neg_inf.is_infinite());
76 #[stable(feature = "rust1", since = "1.0.0")]
78 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
80 /// Returns `true` if this number is neither infinite nor `NaN`.
86 /// let inf = f32::INFINITY;
87 /// let neg_inf = f32::NEG_INFINITY;
88 /// let nan = f32::NAN;
90 /// assert!(f.is_finite());
92 /// assert!(!nan.is_finite());
93 /// assert!(!inf.is_finite());
94 /// assert!(!neg_inf.is_finite());
96 #[stable(feature = "rust1", since = "1.0.0")]
98 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
100 /// Returns `true` if the number is neither zero, infinite,
101 /// [subnormal][subnormal], or `NaN`.
106 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
107 /// let max = f32::MAX;
108 /// let lower_than_min = 1.0e-40_f32;
109 /// let zero = 0.0_f32;
111 /// assert!(min.is_normal());
112 /// assert!(max.is_normal());
114 /// assert!(!zero.is_normal());
115 /// assert!(!f32::NAN.is_normal());
116 /// assert!(!f32::INFINITY.is_normal());
117 /// // Values between `0` and `min` are Subnormal.
118 /// assert!(!lower_than_min.is_normal());
120 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
121 #[stable(feature = "rust1", since = "1.0.0")]
123 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
125 /// Returns the floating point category of the number. If only one property
126 /// is going to be tested, it is generally faster to use the specific
127 /// predicate instead.
130 /// use std::num::FpCategory;
133 /// let num = 12.4_f32;
134 /// let inf = f32::INFINITY;
136 /// assert_eq!(num.classify(), FpCategory::Normal);
137 /// assert_eq!(inf.classify(), FpCategory::Infinite);
139 #[stable(feature = "rust1", since = "1.0.0")]
141 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
143 /// Returns the largest integer less than or equal to a number.
146 /// let f = 3.99_f32;
149 /// assert_eq!(f.floor(), 3.0);
150 /// assert_eq!(g.floor(), 3.0);
152 #[stable(feature = "rust1", since = "1.0.0")]
154 pub fn floor(self) -> f32 {
155 // On MSVC LLVM will lower many math intrinsics to a call to the
156 // corresponding function. On MSVC, however, many of these functions
157 // aren't actually available as symbols to call, but rather they are all
158 // `static inline` functions in header files. This means that from a C
159 // perspective it's "compatible", but not so much from an ABI
160 // perspective (which we're worried about).
162 // The inline header functions always just cast to a f64 and do their
163 // operation, so we do that here as well, but only for MSVC targets.
165 // Note that there are many MSVC-specific float operations which
166 // redirect to this comment, so `floorf` is just one case of a missing
167 // function on MSVC, but there are many others elsewhere.
168 #[cfg(target_env = "msvc")]
169 return (self as f64).floor() as f32;
170 #[cfg(not(target_env = "msvc"))]
171 return unsafe { intrinsics::floorf32(self) };
174 /// Returns the smallest integer greater than or equal to a number.
177 /// let f = 3.01_f32;
180 /// assert_eq!(f.ceil(), 4.0);
181 /// assert_eq!(g.ceil(), 4.0);
183 #[stable(feature = "rust1", since = "1.0.0")]
185 pub fn ceil(self) -> f32 {
186 // see notes above in `floor`
187 #[cfg(target_env = "msvc")]
188 return (self as f64).ceil() as f32;
189 #[cfg(not(target_env = "msvc"))]
190 return unsafe { intrinsics::ceilf32(self) };
193 /// Returns the nearest integer to a number. Round half-way cases away from
198 /// let g = -3.3_f32;
200 /// assert_eq!(f.round(), 3.0);
201 /// assert_eq!(g.round(), -3.0);
203 #[stable(feature = "rust1", since = "1.0.0")]
205 pub fn round(self) -> f32 {
206 unsafe { intrinsics::roundf32(self) }
209 /// Returns the integer part of a number.
213 /// let g = -3.7_f32;
215 /// assert_eq!(f.trunc(), 3.0);
216 /// assert_eq!(g.trunc(), -3.0);
218 #[stable(feature = "rust1", since = "1.0.0")]
220 pub fn trunc(self) -> f32 {
221 unsafe { intrinsics::truncf32(self) }
224 /// Returns the fractional part of a number.
230 /// let y = -3.5_f32;
231 /// let abs_difference_x = (x.fract() - 0.5).abs();
232 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
234 /// assert!(abs_difference_x <= f32::EPSILON);
235 /// assert!(abs_difference_y <= f32::EPSILON);
237 #[stable(feature = "rust1", since = "1.0.0")]
239 pub fn fract(self) -> f32 { self - self.trunc() }
241 /// Computes the absolute value of `self`. Returns `NAN` if the
248 /// let y = -3.5_f32;
250 /// let abs_difference_x = (x.abs() - x).abs();
251 /// let abs_difference_y = (y.abs() - (-y)).abs();
253 /// assert!(abs_difference_x <= f32::EPSILON);
254 /// assert!(abs_difference_y <= f32::EPSILON);
256 /// assert!(f32::NAN.abs().is_nan());
258 #[stable(feature = "rust1", since = "1.0.0")]
260 pub fn abs(self) -> f32 { num::Float::abs(self) }
262 /// Returns a number that represents the sign of `self`.
264 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
265 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
266 /// - `NAN` if the number is `NAN`
273 /// assert_eq!(f.signum(), 1.0);
274 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
276 /// assert!(f32::NAN.signum().is_nan());
278 #[stable(feature = "rust1", since = "1.0.0")]
280 pub fn signum(self) -> f32 { num::Float::signum(self) }
282 /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
283 /// positive sign bit and positive infinity.
287 /// let g = -7.0_f32;
289 /// assert!(f.is_sign_positive());
290 /// assert!(!g.is_sign_positive());
292 #[stable(feature = "rust1", since = "1.0.0")]
294 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
296 /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
297 /// negative sign bit and negative infinity.
303 /// assert!(!f.is_sign_negative());
304 /// assert!(g.is_sign_negative());
306 #[stable(feature = "rust1", since = "1.0.0")]
308 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
310 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
311 /// error. This produces a more accurate result with better performance than
312 /// a separate multiplication operation followed by an add.
317 /// let m = 10.0_f32;
319 /// let b = 60.0_f32;
322 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
324 /// assert!(abs_difference <= f32::EPSILON);
326 #[stable(feature = "rust1", since = "1.0.0")]
328 pub fn mul_add(self, a: f32, b: f32) -> f32 {
329 unsafe { intrinsics::fmaf32(self, a, b) }
332 /// Takes the reciprocal (inverse) of a number, `1/x`.
338 /// let abs_difference = (x.recip() - (1.0/x)).abs();
340 /// assert!(abs_difference <= f32::EPSILON);
342 #[stable(feature = "rust1", since = "1.0.0")]
344 pub fn recip(self) -> f32 { num::Float::recip(self) }
346 /// Raises a number to an integer power.
348 /// Using this function is generally faster than using `powf`
354 /// let abs_difference = (x.powi(2) - x*x).abs();
356 /// assert!(abs_difference <= f32::EPSILON);
358 #[stable(feature = "rust1", since = "1.0.0")]
360 pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
362 /// Raises a number to a floating point power.
368 /// let abs_difference = (x.powf(2.0) - x*x).abs();
370 /// assert!(abs_difference <= f32::EPSILON);
372 #[stable(feature = "rust1", since = "1.0.0")]
374 pub fn powf(self, n: f32) -> f32 {
375 // see notes above in `floor`
376 #[cfg(target_env = "msvc")]
377 return (self as f64).powf(n as f64) as f32;
378 #[cfg(not(target_env = "msvc"))]
379 return unsafe { intrinsics::powf32(self, n) };
382 /// Takes the square root of a number.
384 /// Returns NaN if `self` is a negative number.
389 /// let positive = 4.0_f32;
390 /// let negative = -4.0_f32;
392 /// let abs_difference = (positive.sqrt() - 2.0).abs();
394 /// assert!(abs_difference <= f32::EPSILON);
395 /// assert!(negative.sqrt().is_nan());
397 #[stable(feature = "rust1", since = "1.0.0")]
399 pub fn sqrt(self) -> f32 {
403 unsafe { intrinsics::sqrtf32(self) }
407 /// Returns `e^(self)`, (the exponential function).
412 /// let one = 1.0f32;
414 /// let e = one.exp();
416 /// // ln(e) - 1 == 0
417 /// let abs_difference = (e.ln() - 1.0).abs();
419 /// assert!(abs_difference <= f32::EPSILON);
421 #[stable(feature = "rust1", since = "1.0.0")]
423 pub fn exp(self) -> f32 {
424 // see notes above in `floor`
425 #[cfg(target_env = "msvc")]
426 return (self as f64).exp() as f32;
427 #[cfg(not(target_env = "msvc"))]
428 return unsafe { intrinsics::expf32(self) };
431 /// Returns `2^(self)`.
439 /// let abs_difference = (f.exp2() - 4.0).abs();
441 /// assert!(abs_difference <= f32::EPSILON);
443 #[stable(feature = "rust1", since = "1.0.0")]
445 pub fn exp2(self) -> f32 {
446 unsafe { intrinsics::exp2f32(self) }
449 /// Returns the natural logarithm of the number.
454 /// let one = 1.0f32;
456 /// let e = one.exp();
458 /// // ln(e) - 1 == 0
459 /// let abs_difference = (e.ln() - 1.0).abs();
461 /// assert!(abs_difference <= f32::EPSILON);
463 #[stable(feature = "rust1", since = "1.0.0")]
465 pub fn ln(self) -> f32 {
466 // see notes above in `floor`
467 #[cfg(target_env = "msvc")]
468 return (self as f64).ln() as f32;
469 #[cfg(not(target_env = "msvc"))]
470 return unsafe { intrinsics::logf32(self) };
473 /// Returns the logarithm of the number with respect to an arbitrary base.
478 /// let ten = 10.0f32;
479 /// let two = 2.0f32;
481 /// // log10(10) - 1 == 0
482 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
484 /// // log2(2) - 1 == 0
485 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
487 /// assert!(abs_difference_10 <= f32::EPSILON);
488 /// assert!(abs_difference_2 <= f32::EPSILON);
490 #[stable(feature = "rust1", since = "1.0.0")]
492 pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
494 /// Returns the base 2 logarithm of the number.
499 /// let two = 2.0f32;
501 /// // log2(2) - 1 == 0
502 /// let abs_difference = (two.log2() - 1.0).abs();
504 /// assert!(abs_difference <= f32::EPSILON);
506 #[stable(feature = "rust1", since = "1.0.0")]
508 pub fn log2(self) -> f32 {
509 #[cfg(target_os = "android")]
510 return ::sys::android::log2f32(self);
511 #[cfg(not(target_os = "android"))]
512 return unsafe { intrinsics::log2f32(self) };
515 /// Returns the base 10 logarithm of the number.
520 /// let ten = 10.0f32;
522 /// // log10(10) - 1 == 0
523 /// let abs_difference = (ten.log10() - 1.0).abs();
525 /// assert!(abs_difference <= f32::EPSILON);
527 #[stable(feature = "rust1", since = "1.0.0")]
529 pub fn log10(self) -> f32 {
530 // see notes above in `floor`
531 #[cfg(target_env = "msvc")]
532 return (self as f64).log10() as f32;
533 #[cfg(not(target_env = "msvc"))]
534 return unsafe { intrinsics::log10f32(self) };
537 /// Converts radians to degrees.
540 /// use std::f32::{self, consts};
542 /// let angle = consts::PI;
544 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
546 /// assert!(abs_difference <= f32::EPSILON);
548 #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
550 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
552 /// Converts degrees to radians.
555 /// use std::f32::{self, consts};
557 /// let angle = 180.0f32;
559 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
561 /// assert!(abs_difference <= f32::EPSILON);
563 #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
565 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
567 /// Returns the maximum of the two numbers.
573 /// assert_eq!(x.max(y), y);
576 /// If one of the arguments is NaN, then the other argument is returned.
577 #[stable(feature = "rust1", since = "1.0.0")]
579 pub fn max(self, other: f32) -> f32 {
580 num::Float::max(self, other)
583 /// Returns the minimum of the two numbers.
589 /// assert_eq!(x.min(y), x);
592 /// If one of the arguments is NaN, then the other argument is returned.
593 #[stable(feature = "rust1", since = "1.0.0")]
595 pub fn min(self, other: f32) -> f32 {
596 num::Float::min(self, other)
599 /// The positive difference of two numbers.
601 /// * If `self <= other`: `0:0`
602 /// * Else: `self - other`
610 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
611 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
613 /// assert!(abs_difference_x <= f32::EPSILON);
614 /// assert!(abs_difference_y <= f32::EPSILON);
616 #[stable(feature = "rust1", since = "1.0.0")]
618 #[rustc_deprecated(since = "1.10.0",
619 reason = "you probably meant `(self - other).abs()`: \
620 this operation is `(self - other).max(0.0)` (also \
621 known as `fdimf` in C). If you truly need the positive \
622 difference, consider using that expression or the C function \
623 `fdimf`, depending on how you wish to handle NaN (please consider \
624 filing an issue describing your use-case too).")]
625 pub fn abs_sub(self, other: f32) -> f32 {
626 unsafe { cmath::fdimf(self, other) }
629 /// Takes the cubic root of a number.
636 /// // x^(1/3) - 2 == 0
637 /// let abs_difference = (x.cbrt() - 2.0).abs();
639 /// assert!(abs_difference <= f32::EPSILON);
641 #[stable(feature = "rust1", since = "1.0.0")]
643 pub fn cbrt(self) -> f32 {
644 unsafe { cmath::cbrtf(self) }
647 /// Calculates the length of the hypotenuse of a right-angle triangle given
648 /// legs of length `x` and `y`.
656 /// // sqrt(x^2 + y^2)
657 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
659 /// assert!(abs_difference <= f32::EPSILON);
661 #[stable(feature = "rust1", since = "1.0.0")]
663 pub fn hypot(self, other: f32) -> f32 {
664 unsafe { cmath::hypotf(self, other) }
667 /// Computes the sine of a number (in radians).
672 /// let x = f32::consts::PI/2.0;
674 /// let abs_difference = (x.sin() - 1.0).abs();
676 /// assert!(abs_difference <= f32::EPSILON);
678 #[stable(feature = "rust1", since = "1.0.0")]
680 pub fn sin(self) -> f32 {
681 // see notes in `core::f32::Float::floor`
682 #[cfg(target_env = "msvc")]
683 return (self as f64).sin() as f32;
684 #[cfg(not(target_env = "msvc"))]
685 return unsafe { intrinsics::sinf32(self) };
688 /// Computes the cosine of a number (in radians).
693 /// let x = 2.0*f32::consts::PI;
695 /// let abs_difference = (x.cos() - 1.0).abs();
697 /// assert!(abs_difference <= f32::EPSILON);
699 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn cos(self) -> f32 {
702 // see notes in `core::f32::Float::floor`
703 #[cfg(target_env = "msvc")]
704 return (self as f64).cos() as f32;
705 #[cfg(not(target_env = "msvc"))]
706 return unsafe { intrinsics::cosf32(self) };
709 /// Computes the tangent of a number (in radians).
714 /// let x = f32::consts::PI / 4.0;
715 /// let abs_difference = (x.tan() - 1.0).abs();
717 /// assert!(abs_difference <= f32::EPSILON);
719 #[stable(feature = "rust1", since = "1.0.0")]
721 pub fn tan(self) -> f32 {
722 unsafe { cmath::tanf(self) }
725 /// Computes the arcsine of a number. Return value is in radians in
726 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
732 /// let f = f32::consts::PI / 2.0;
734 /// // asin(sin(pi/2))
735 /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
737 /// assert!(abs_difference <= f32::EPSILON);
739 #[stable(feature = "rust1", since = "1.0.0")]
741 pub fn asin(self) -> f32 {
742 unsafe { cmath::asinf(self) }
745 /// Computes the arccosine of a number. Return value is in radians in
746 /// the range [0, pi] or NaN if the number is outside the range
752 /// let f = f32::consts::PI / 4.0;
754 /// // acos(cos(pi/4))
755 /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
757 /// assert!(abs_difference <= f32::EPSILON);
759 #[stable(feature = "rust1", since = "1.0.0")]
761 pub fn acos(self) -> f32 {
762 unsafe { cmath::acosf(self) }
765 /// Computes the arctangent of a number. Return value is in radians in the
766 /// range [-pi/2, pi/2];
774 /// let abs_difference = (f.tan().atan() - 1.0).abs();
776 /// assert!(abs_difference <= f32::EPSILON);
778 #[stable(feature = "rust1", since = "1.0.0")]
780 pub fn atan(self) -> f32 {
781 unsafe { cmath::atanf(self) }
784 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
786 /// * `x = 0`, `y = 0`: `0`
787 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
788 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
789 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
794 /// let pi = f32::consts::PI;
795 /// // All angles from horizontal right (+x)
796 /// // 45 deg counter-clockwise
798 /// let y1 = -3.0f32;
800 /// // 135 deg clockwise
801 /// let x2 = -3.0f32;
804 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
805 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
807 /// assert!(abs_difference_1 <= f32::EPSILON);
808 /// assert!(abs_difference_2 <= f32::EPSILON);
810 #[stable(feature = "rust1", since = "1.0.0")]
812 pub fn atan2(self, other: f32) -> f32 {
813 unsafe { cmath::atan2f(self, other) }
816 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
817 /// `(sin(x), cos(x))`.
822 /// let x = f32::consts::PI/4.0;
823 /// let f = x.sin_cos();
825 /// let abs_difference_0 = (f.0 - x.sin()).abs();
826 /// let abs_difference_1 = (f.1 - x.cos()).abs();
828 /// assert!(abs_difference_0 <= f32::EPSILON);
829 /// assert!(abs_difference_1 <= f32::EPSILON);
831 #[stable(feature = "rust1", since = "1.0.0")]
833 pub fn sin_cos(self) -> (f32, f32) {
834 (self.sin(), self.cos())
837 /// Returns `e^(self) - 1` in a way that is accurate even if the
838 /// number is close to zero.
846 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
848 /// assert!(abs_difference <= f32::EPSILON);
850 #[stable(feature = "rust1", since = "1.0.0")]
852 pub fn exp_m1(self) -> f32 {
853 unsafe { cmath::expm1f(self) }
856 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
857 /// the operations were performed separately.
862 /// let x = f32::consts::E - 1.0;
864 /// // ln(1 + (e - 1)) == ln(e) == 1
865 /// let abs_difference = (x.ln_1p() - 1.0).abs();
867 /// assert!(abs_difference <= f32::EPSILON);
869 #[stable(feature = "rust1", since = "1.0.0")]
871 pub fn ln_1p(self) -> f32 {
872 unsafe { cmath::log1pf(self) }
875 /// Hyperbolic sine function.
880 /// let e = f32::consts::E;
883 /// let f = x.sinh();
884 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
885 /// let g = (e*e - 1.0)/(2.0*e);
886 /// let abs_difference = (f - g).abs();
888 /// assert!(abs_difference <= f32::EPSILON);
890 #[stable(feature = "rust1", since = "1.0.0")]
892 pub fn sinh(self) -> f32 {
893 unsafe { cmath::sinhf(self) }
896 /// Hyperbolic cosine function.
901 /// let e = f32::consts::E;
903 /// let f = x.cosh();
904 /// // Solving cosh() at 1 gives this result
905 /// let g = (e*e + 1.0)/(2.0*e);
906 /// let abs_difference = (f - g).abs();
909 /// assert!(abs_difference <= f32::EPSILON);
911 #[stable(feature = "rust1", since = "1.0.0")]
913 pub fn cosh(self) -> f32 {
914 unsafe { cmath::coshf(self) }
917 /// Hyperbolic tangent function.
922 /// let e = f32::consts::E;
925 /// let f = x.tanh();
926 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
927 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
928 /// let abs_difference = (f - g).abs();
930 /// assert!(abs_difference <= f32::EPSILON);
932 #[stable(feature = "rust1", since = "1.0.0")]
934 pub fn tanh(self) -> f32 {
935 unsafe { cmath::tanhf(self) }
938 /// Inverse hyperbolic sine function.
944 /// let f = x.sinh().asinh();
946 /// let abs_difference = (f - x).abs();
948 /// assert!(abs_difference <= f32::EPSILON);
950 #[stable(feature = "rust1", since = "1.0.0")]
952 pub fn asinh(self) -> f32 {
953 if self == NEG_INFINITY {
956 (self + ((self * self) + 1.0).sqrt()).ln()
960 /// Inverse hyperbolic cosine function.
966 /// let f = x.cosh().acosh();
968 /// let abs_difference = (f - x).abs();
970 /// assert!(abs_difference <= f32::EPSILON);
972 #[stable(feature = "rust1", since = "1.0.0")]
974 pub fn acosh(self) -> f32 {
976 x if x < 1.0 => ::f32::NAN,
977 x => (x + ((x * x) - 1.0).sqrt()).ln(),
981 /// Inverse hyperbolic tangent function.
986 /// let e = f32::consts::E;
987 /// let f = e.tanh().atanh();
989 /// let abs_difference = (f - e).abs();
991 /// assert!(abs_difference <= 1e-5);
993 #[stable(feature = "rust1", since = "1.0.0")]
995 pub fn atanh(self) -> f32 {
996 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
999 /// Raw transmutation to `u32`.
1001 /// Converts the `f32` into its raw memory representation,
1002 /// similar to the `transmute` function.
1004 /// Note that this function is distinct from casting.
1009 /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
1010 /// assert_eq!((12.5f32).to_bits(), 0x41480000);
1013 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1015 pub fn to_bits(self) -> u32 {
1016 unsafe { ::mem::transmute(self) }
1019 /// Raw transmutation from `u32`.
1021 /// Converts the given `u32` containing the float's raw memory
1022 /// representation into the `f32` type, similar to the
1023 /// `transmute` function.
1025 /// There is only one difference to a bare `transmute`:
1026 /// Due to the implications onto Rust's safety promises being
1027 /// uncertain, if the representation of a signaling NaN "sNaN" float
1028 /// is passed to the function, the implementation is allowed to
1029 /// return a quiet NaN instead.
1031 /// Note that this function is distinct from casting.
1037 /// let v = f32::from_bits(0x41480000);
1038 /// let difference = (v - 12.5).abs();
1039 /// assert!(difference <= 1e-5);
1040 /// // Example for a signaling NaN value:
1041 /// let snan = 0x7F800001;
1042 /// assert_ne!(f32::from_bits(snan).to_bits(), snan);
1044 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1046 pub fn from_bits(mut v: u32) -> Self {
1047 const EXP_MASK: u32 = 0x7F800000;
1048 const FRACT_MASK: u32 = 0x007FFFFF;
1049 if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1050 // While IEEE 754-2008 specifies encodings for quiet NaNs
1051 // and signaling ones, certain MIPS and PA-RISC
1052 // CPUs treat signaling NaNs differently.
1053 // Therefore to be safe, we pass a known quiet NaN
1054 // if v is any kind of NaN.
1055 // The check above only assumes IEEE 754-1985 to be
1057 v = unsafe { ::mem::transmute(NAN) };
1059 unsafe { ::mem::transmute(v) }
1068 use num::FpCategory as Fp;
1072 test_num(10f32, 2f32);
1077 assert_eq!(NAN.min(2.0), 2.0);
1078 assert_eq!(2.0f32.min(NAN), 2.0);
1083 assert_eq!(NAN.max(2.0), 2.0);
1084 assert_eq!(2.0f32.max(NAN), 2.0);
1089 let nan: f32 = f32::NAN;
1090 assert!(nan.is_nan());
1091 assert!(!nan.is_infinite());
1092 assert!(!nan.is_finite());
1093 assert!(!nan.is_normal());
1094 assert!(nan.is_sign_positive());
1095 assert!(!nan.is_sign_negative());
1096 assert_eq!(Fp::Nan, nan.classify());
1100 fn test_infinity() {
1101 let inf: f32 = f32::INFINITY;
1102 assert!(inf.is_infinite());
1103 assert!(!inf.is_finite());
1104 assert!(inf.is_sign_positive());
1105 assert!(!inf.is_sign_negative());
1106 assert!(!inf.is_nan());
1107 assert!(!inf.is_normal());
1108 assert_eq!(Fp::Infinite, inf.classify());
1112 fn test_neg_infinity() {
1113 let neg_inf: f32 = f32::NEG_INFINITY;
1114 assert!(neg_inf.is_infinite());
1115 assert!(!neg_inf.is_finite());
1116 assert!(!neg_inf.is_sign_positive());
1117 assert!(neg_inf.is_sign_negative());
1118 assert!(!neg_inf.is_nan());
1119 assert!(!neg_inf.is_normal());
1120 assert_eq!(Fp::Infinite, neg_inf.classify());
1125 let zero: f32 = 0.0f32;
1126 assert_eq!(0.0, zero);
1127 assert!(!zero.is_infinite());
1128 assert!(zero.is_finite());
1129 assert!(zero.is_sign_positive());
1130 assert!(!zero.is_sign_negative());
1131 assert!(!zero.is_nan());
1132 assert!(!zero.is_normal());
1133 assert_eq!(Fp::Zero, zero.classify());
1137 fn test_neg_zero() {
1138 let neg_zero: f32 = -0.0;
1139 assert_eq!(0.0, neg_zero);
1140 assert!(!neg_zero.is_infinite());
1141 assert!(neg_zero.is_finite());
1142 assert!(!neg_zero.is_sign_positive());
1143 assert!(neg_zero.is_sign_negative());
1144 assert!(!neg_zero.is_nan());
1145 assert!(!neg_zero.is_normal());
1146 assert_eq!(Fp::Zero, neg_zero.classify());
1151 let one: f32 = 1.0f32;
1152 assert_eq!(1.0, one);
1153 assert!(!one.is_infinite());
1154 assert!(one.is_finite());
1155 assert!(one.is_sign_positive());
1156 assert!(!one.is_sign_negative());
1157 assert!(!one.is_nan());
1158 assert!(one.is_normal());
1159 assert_eq!(Fp::Normal, one.classify());
1164 let nan: f32 = f32::NAN;
1165 let inf: f32 = f32::INFINITY;
1166 let neg_inf: f32 = f32::NEG_INFINITY;
1167 assert!(nan.is_nan());
1168 assert!(!0.0f32.is_nan());
1169 assert!(!5.3f32.is_nan());
1170 assert!(!(-10.732f32).is_nan());
1171 assert!(!inf.is_nan());
1172 assert!(!neg_inf.is_nan());
1176 fn test_is_infinite() {
1177 let nan: f32 = f32::NAN;
1178 let inf: f32 = f32::INFINITY;
1179 let neg_inf: f32 = f32::NEG_INFINITY;
1180 assert!(!nan.is_infinite());
1181 assert!(inf.is_infinite());
1182 assert!(neg_inf.is_infinite());
1183 assert!(!0.0f32.is_infinite());
1184 assert!(!42.8f32.is_infinite());
1185 assert!(!(-109.2f32).is_infinite());
1189 fn test_is_finite() {
1190 let nan: f32 = f32::NAN;
1191 let inf: f32 = f32::INFINITY;
1192 let neg_inf: f32 = f32::NEG_INFINITY;
1193 assert!(!nan.is_finite());
1194 assert!(!inf.is_finite());
1195 assert!(!neg_inf.is_finite());
1196 assert!(0.0f32.is_finite());
1197 assert!(42.8f32.is_finite());
1198 assert!((-109.2f32).is_finite());
1202 fn test_is_normal() {
1203 let nan: f32 = f32::NAN;
1204 let inf: f32 = f32::INFINITY;
1205 let neg_inf: f32 = f32::NEG_INFINITY;
1206 let zero: f32 = 0.0f32;
1207 let neg_zero: f32 = -0.0;
1208 assert!(!nan.is_normal());
1209 assert!(!inf.is_normal());
1210 assert!(!neg_inf.is_normal());
1211 assert!(!zero.is_normal());
1212 assert!(!neg_zero.is_normal());
1213 assert!(1f32.is_normal());
1214 assert!(1e-37f32.is_normal());
1215 assert!(!1e-38f32.is_normal());
1219 fn test_classify() {
1220 let nan: f32 = f32::NAN;
1221 let inf: f32 = f32::INFINITY;
1222 let neg_inf: f32 = f32::NEG_INFINITY;
1223 let zero: f32 = 0.0f32;
1224 let neg_zero: f32 = -0.0;
1225 assert_eq!(nan.classify(), Fp::Nan);
1226 assert_eq!(inf.classify(), Fp::Infinite);
1227 assert_eq!(neg_inf.classify(), Fp::Infinite);
1228 assert_eq!(zero.classify(), Fp::Zero);
1229 assert_eq!(neg_zero.classify(), Fp::Zero);
1230 assert_eq!(1f32.classify(), Fp::Normal);
1231 assert_eq!(1e-37f32.classify(), Fp::Normal);
1232 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1237 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1238 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1239 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1240 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1241 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1242 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1243 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1244 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1245 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1246 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1251 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1252 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1253 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1254 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1255 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1256 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1257 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1258 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1259 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1260 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1265 assert_approx_eq!(1.0f32.round(), 1.0f32);
1266 assert_approx_eq!(1.3f32.round(), 1.0f32);
1267 assert_approx_eq!(1.5f32.round(), 2.0f32);
1268 assert_approx_eq!(1.7f32.round(), 2.0f32);
1269 assert_approx_eq!(0.0f32.round(), 0.0f32);
1270 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1271 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1272 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1273 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1274 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1279 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1280 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1281 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1282 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1283 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1284 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1285 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1286 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1287 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1288 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1293 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1294 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1295 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1296 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1297 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1298 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1299 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1300 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1301 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1302 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1307 assert_eq!(INFINITY.abs(), INFINITY);
1308 assert_eq!(1f32.abs(), 1f32);
1309 assert_eq!(0f32.abs(), 0f32);
1310 assert_eq!((-0f32).abs(), 0f32);
1311 assert_eq!((-1f32).abs(), 1f32);
1312 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1313 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1314 assert!(NAN.abs().is_nan());
1319 assert_eq!(INFINITY.signum(), 1f32);
1320 assert_eq!(1f32.signum(), 1f32);
1321 assert_eq!(0f32.signum(), 1f32);
1322 assert_eq!((-0f32).signum(), -1f32);
1323 assert_eq!((-1f32).signum(), -1f32);
1324 assert_eq!(NEG_INFINITY.signum(), -1f32);
1325 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1326 assert!(NAN.signum().is_nan());
1330 fn test_is_sign_positive() {
1331 assert!(INFINITY.is_sign_positive());
1332 assert!(1f32.is_sign_positive());
1333 assert!(0f32.is_sign_positive());
1334 assert!(!(-0f32).is_sign_positive());
1335 assert!(!(-1f32).is_sign_positive());
1336 assert!(!NEG_INFINITY.is_sign_positive());
1337 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1338 assert!(NAN.is_sign_positive());
1339 assert!(!(-NAN).is_sign_positive());
1343 fn test_is_sign_negative() {
1344 assert!(!INFINITY.is_sign_negative());
1345 assert!(!1f32.is_sign_negative());
1346 assert!(!0f32.is_sign_negative());
1347 assert!((-0f32).is_sign_negative());
1348 assert!((-1f32).is_sign_negative());
1349 assert!(NEG_INFINITY.is_sign_negative());
1350 assert!((1f32/NEG_INFINITY).is_sign_negative());
1351 assert!(!NAN.is_sign_negative());
1352 assert!((-NAN).is_sign_negative());
1357 let nan: f32 = f32::NAN;
1358 let inf: f32 = f32::INFINITY;
1359 let neg_inf: f32 = f32::NEG_INFINITY;
1360 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1361 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1362 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1363 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1364 assert!(nan.mul_add(7.8, 9.0).is_nan());
1365 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1366 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1367 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1368 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1373 let nan: f32 = f32::NAN;
1374 let inf: f32 = f32::INFINITY;
1375 let neg_inf: f32 = f32::NEG_INFINITY;
1376 assert_eq!(1.0f32.recip(), 1.0);
1377 assert_eq!(2.0f32.recip(), 0.5);
1378 assert_eq!((-0.4f32).recip(), -2.5);
1379 assert_eq!(0.0f32.recip(), inf);
1380 assert!(nan.recip().is_nan());
1381 assert_eq!(inf.recip(), 0.0);
1382 assert_eq!(neg_inf.recip(), 0.0);
1387 let nan: f32 = f32::NAN;
1388 let inf: f32 = f32::INFINITY;
1389 let neg_inf: f32 = f32::NEG_INFINITY;
1390 assert_eq!(1.0f32.powi(1), 1.0);
1391 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1392 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1393 assert_eq!(8.3f32.powi(0), 1.0);
1394 assert!(nan.powi(2).is_nan());
1395 assert_eq!(inf.powi(3), inf);
1396 assert_eq!(neg_inf.powi(2), inf);
1401 let nan: f32 = f32::NAN;
1402 let inf: f32 = f32::INFINITY;
1403 let neg_inf: f32 = f32::NEG_INFINITY;
1404 assert_eq!(1.0f32.powf(1.0), 1.0);
1405 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1406 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1407 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1408 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1409 assert_eq!(8.3f32.powf(0.0), 1.0);
1410 assert!(nan.powf(2.0).is_nan());
1411 assert_eq!(inf.powf(2.0), inf);
1412 assert_eq!(neg_inf.powf(3.0), neg_inf);
1416 fn test_sqrt_domain() {
1417 assert!(NAN.sqrt().is_nan());
1418 assert!(NEG_INFINITY.sqrt().is_nan());
1419 assert!((-1.0f32).sqrt().is_nan());
1420 assert_eq!((-0.0f32).sqrt(), -0.0);
1421 assert_eq!(0.0f32.sqrt(), 0.0);
1422 assert_eq!(1.0f32.sqrt(), 1.0);
1423 assert_eq!(INFINITY.sqrt(), INFINITY);
1428 assert_eq!(1.0, 0.0f32.exp());
1429 assert_approx_eq!(2.718282, 1.0f32.exp());
1430 assert_approx_eq!(148.413162, 5.0f32.exp());
1432 let inf: f32 = f32::INFINITY;
1433 let neg_inf: f32 = f32::NEG_INFINITY;
1434 let nan: f32 = f32::NAN;
1435 assert_eq!(inf, inf.exp());
1436 assert_eq!(0.0, neg_inf.exp());
1437 assert!(nan.exp().is_nan());
1442 assert_eq!(32.0, 5.0f32.exp2());
1443 assert_eq!(1.0, 0.0f32.exp2());
1445 let inf: f32 = f32::INFINITY;
1446 let neg_inf: f32 = f32::NEG_INFINITY;
1447 let nan: f32 = f32::NAN;
1448 assert_eq!(inf, inf.exp2());
1449 assert_eq!(0.0, neg_inf.exp2());
1450 assert!(nan.exp2().is_nan());
1455 let nan: f32 = f32::NAN;
1456 let inf: f32 = f32::INFINITY;
1457 let neg_inf: f32 = f32::NEG_INFINITY;
1458 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1459 assert!(nan.ln().is_nan());
1460 assert_eq!(inf.ln(), inf);
1461 assert!(neg_inf.ln().is_nan());
1462 assert!((-2.3f32).ln().is_nan());
1463 assert_eq!((-0.0f32).ln(), neg_inf);
1464 assert_eq!(0.0f32.ln(), neg_inf);
1465 assert_approx_eq!(4.0f32.ln(), 1.386294);
1470 let nan: f32 = f32::NAN;
1471 let inf: f32 = f32::INFINITY;
1472 let neg_inf: f32 = f32::NEG_INFINITY;
1473 assert_eq!(10.0f32.log(10.0), 1.0);
1474 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1475 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1476 assert!(1.0f32.log(1.0).is_nan());
1477 assert!(1.0f32.log(-13.9).is_nan());
1478 assert!(nan.log(2.3).is_nan());
1479 assert_eq!(inf.log(10.0), inf);
1480 assert!(neg_inf.log(8.8).is_nan());
1481 assert!((-2.3f32).log(0.1).is_nan());
1482 assert_eq!((-0.0f32).log(2.0), neg_inf);
1483 assert_eq!(0.0f32.log(7.0), neg_inf);
1488 let nan: f32 = f32::NAN;
1489 let inf: f32 = f32::INFINITY;
1490 let neg_inf: f32 = f32::NEG_INFINITY;
1491 assert_approx_eq!(10.0f32.log2(), 3.321928);
1492 assert_approx_eq!(2.3f32.log2(), 1.201634);
1493 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1494 assert!(nan.log2().is_nan());
1495 assert_eq!(inf.log2(), inf);
1496 assert!(neg_inf.log2().is_nan());
1497 assert!((-2.3f32).log2().is_nan());
1498 assert_eq!((-0.0f32).log2(), neg_inf);
1499 assert_eq!(0.0f32.log2(), neg_inf);
1504 let nan: f32 = f32::NAN;
1505 let inf: f32 = f32::INFINITY;
1506 let neg_inf: f32 = f32::NEG_INFINITY;
1507 assert_eq!(10.0f32.log10(), 1.0);
1508 assert_approx_eq!(2.3f32.log10(), 0.361728);
1509 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1510 assert_eq!(1.0f32.log10(), 0.0);
1511 assert!(nan.log10().is_nan());
1512 assert_eq!(inf.log10(), inf);
1513 assert!(neg_inf.log10().is_nan());
1514 assert!((-2.3f32).log10().is_nan());
1515 assert_eq!((-0.0f32).log10(), neg_inf);
1516 assert_eq!(0.0f32.log10(), neg_inf);
1520 fn test_to_degrees() {
1521 let pi: f32 = consts::PI;
1522 let nan: f32 = f32::NAN;
1523 let inf: f32 = f32::INFINITY;
1524 let neg_inf: f32 = f32::NEG_INFINITY;
1525 assert_eq!(0.0f32.to_degrees(), 0.0);
1526 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1527 assert_eq!(pi.to_degrees(), 180.0);
1528 assert!(nan.to_degrees().is_nan());
1529 assert_eq!(inf.to_degrees(), inf);
1530 assert_eq!(neg_inf.to_degrees(), neg_inf);
1534 fn test_to_radians() {
1535 let pi: f32 = consts::PI;
1536 let nan: f32 = f32::NAN;
1537 let inf: f32 = f32::INFINITY;
1538 let neg_inf: f32 = f32::NEG_INFINITY;
1539 assert_eq!(0.0f32.to_radians(), 0.0);
1540 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1541 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1542 assert_eq!(180.0f32.to_radians(), pi);
1543 assert!(nan.to_radians().is_nan());
1544 assert_eq!(inf.to_radians(), inf);
1545 assert_eq!(neg_inf.to_radians(), neg_inf);
1550 assert_eq!(0.0f32.asinh(), 0.0f32);
1551 assert_eq!((-0.0f32).asinh(), -0.0f32);
1553 let inf: f32 = f32::INFINITY;
1554 let neg_inf: f32 = f32::NEG_INFINITY;
1555 let nan: f32 = f32::NAN;
1556 assert_eq!(inf.asinh(), inf);
1557 assert_eq!(neg_inf.asinh(), neg_inf);
1558 assert!(nan.asinh().is_nan());
1559 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1560 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1565 assert_eq!(1.0f32.acosh(), 0.0f32);
1566 assert!(0.999f32.acosh().is_nan());
1568 let inf: f32 = f32::INFINITY;
1569 let neg_inf: f32 = f32::NEG_INFINITY;
1570 let nan: f32 = f32::NAN;
1571 assert_eq!(inf.acosh(), inf);
1572 assert!(neg_inf.acosh().is_nan());
1573 assert!(nan.acosh().is_nan());
1574 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1575 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1580 assert_eq!(0.0f32.atanh(), 0.0f32);
1581 assert_eq!((-0.0f32).atanh(), -0.0f32);
1583 let inf32: f32 = f32::INFINITY;
1584 let neg_inf32: f32 = f32::NEG_INFINITY;
1585 assert_eq!(1.0f32.atanh(), inf32);
1586 assert_eq!((-1.0f32).atanh(), neg_inf32);
1588 assert!(2f64.atanh().atanh().is_nan());
1589 assert!((-2f64).atanh().atanh().is_nan());
1591 let inf64: f32 = f32::INFINITY;
1592 let neg_inf64: f32 = f32::NEG_INFINITY;
1593 let nan32: f32 = f32::NAN;
1594 assert!(inf64.atanh().is_nan());
1595 assert!(neg_inf64.atanh().is_nan());
1596 assert!(nan32.atanh().is_nan());
1598 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1599 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1603 fn test_real_consts() {
1606 let pi: f32 = consts::PI;
1607 let frac_pi_2: f32 = consts::FRAC_PI_2;
1608 let frac_pi_3: f32 = consts::FRAC_PI_3;
1609 let frac_pi_4: f32 = consts::FRAC_PI_4;
1610 let frac_pi_6: f32 = consts::FRAC_PI_6;
1611 let frac_pi_8: f32 = consts::FRAC_PI_8;
1612 let frac_1_pi: f32 = consts::FRAC_1_PI;
1613 let frac_2_pi: f32 = consts::FRAC_2_PI;
1614 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1615 let sqrt2: f32 = consts::SQRT_2;
1616 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1617 let e: f32 = consts::E;
1618 let log2_e: f32 = consts::LOG2_E;
1619 let log10_e: f32 = consts::LOG10_E;
1620 let ln_2: f32 = consts::LN_2;
1621 let ln_10: f32 = consts::LN_10;
1623 assert_approx_eq!(frac_pi_2, pi / 2f32);
1624 assert_approx_eq!(frac_pi_3, pi / 3f32);
1625 assert_approx_eq!(frac_pi_4, pi / 4f32);
1626 assert_approx_eq!(frac_pi_6, pi / 6f32);
1627 assert_approx_eq!(frac_pi_8, pi / 8f32);
1628 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1629 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1630 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1631 assert_approx_eq!(sqrt2, 2f32.sqrt());
1632 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1633 assert_approx_eq!(log2_e, e.log2());
1634 assert_approx_eq!(log10_e, e.log10());
1635 assert_approx_eq!(ln_2, 2f32.ln());
1636 assert_approx_eq!(ln_10, 10f32.ln());
1640 fn test_float_bits_conv() {
1641 assert_eq!((1f32).to_bits(), 0x3f800000);
1642 assert_eq!((12.5f32).to_bits(), 0x41480000);
1643 assert_eq!((1337f32).to_bits(), 0x44a72000);
1644 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1645 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1646 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1647 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1648 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1651 fn test_snan_masking() {
1652 // NOTE: this test assumes that our current platform
1653 // implements IEEE 754-2008 that specifies the difference
1654 // in encoding of quiet and signaling NaNs.
1655 // If you are porting Rust to a platform that does not
1656 // implement IEEE 754-2008 (but e.g. IEEE 754-1985, which
1657 // only says that "Signaling NaNs shall be reserved operands"
1658 // but doesn't specify the actual setup), feel free to
1659 // cfg out this test.
1660 let snan: u32 = 0x7F801337;
1661 const QNAN_MASK: u32 = 0x00400000;
1662 let nan_masked_fl = f32::from_bits(snan);
1663 let nan_masked = nan_masked_fl.to_bits();
1664 // Ensure that signaling NaNs don't stay the same
1665 assert_ne!(nan_masked, snan);
1666 // Ensure that we have a quiet NaN
1667 assert_ne!(nan_masked & QNAN_MASK, 0);
1668 assert!(nan_masked_fl.is_nan());