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)]
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use core::f32::consts;
41 use libc::{c_float, c_int};
44 pub fn cbrtf(n: c_float) -> c_float;
45 pub fn erff(n: c_float) -> c_float;
46 pub fn erfcf(n: c_float) -> c_float;
47 pub fn expm1f(n: c_float) -> c_float;
48 pub fn fdimf(a: c_float, b: c_float) -> c_float;
49 pub fn fmodf(a: c_float, b: c_float) -> c_float;
50 pub fn ilogbf(n: c_float) -> c_int;
51 pub fn logbf(n: c_float) -> c_float;
52 pub fn log1pf(n: c_float) -> c_float;
53 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
54 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
55 pub fn tgammaf(n: c_float) -> c_float;
57 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
58 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
59 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
60 pub fn hypotf(x: c_float, y: c_float) -> c_float;
63 // See the comments in the `floor` function for why MSVC is special
65 #[cfg(not(target_env = "msvc"))]
67 pub fn acosf(n: c_float) -> c_float;
68 pub fn asinf(n: c_float) -> c_float;
69 pub fn atan2f(a: c_float, b: c_float) -> c_float;
70 pub fn atanf(n: c_float) -> c_float;
71 pub fn coshf(n: c_float) -> c_float;
72 pub fn sinhf(n: c_float) -> c_float;
73 pub fn tanf(n: c_float) -> c_float;
74 pub fn tanhf(n: c_float) -> c_float;
77 #[cfg(target_env = "msvc")]
78 pub use self::shims::*;
79 #[cfg(target_env = "msvc")]
84 pub unsafe fn acosf(n: c_float) -> c_float {
85 f64::acos(n as f64) as c_float
89 pub unsafe fn asinf(n: c_float) -> c_float {
90 f64::asin(n as f64) as c_float
94 pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
95 f64::atan2(n as f64, b as f64) as c_float
99 pub unsafe fn atanf(n: c_float) -> c_float {
100 f64::atan(n as f64) as c_float
104 pub unsafe fn coshf(n: c_float) -> c_float {
105 f64::cosh(n as f64) as c_float
109 pub unsafe fn sinhf(n: c_float) -> c_float {
110 f64::sinh(n as f64) as c_float
114 pub unsafe fn tanf(n: c_float) -> c_float {
115 f64::tan(n as f64) as c_float
119 pub unsafe fn tanhf(n: c_float) -> c_float {
120 f64::tanh(n as f64) as c_float
128 /// Returns `true` if this value is `NaN` and false otherwise.
133 /// let nan = f32::NAN;
136 /// assert!(nan.is_nan());
137 /// assert!(!f.is_nan());
139 #[stable(feature = "rust1", since = "1.0.0")]
141 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
143 /// Returns `true` if this value is positive infinity or negative infinity and
150 /// let inf = f32::INFINITY;
151 /// let neg_inf = f32::NEG_INFINITY;
152 /// let nan = f32::NAN;
154 /// assert!(!f.is_infinite());
155 /// assert!(!nan.is_infinite());
157 /// assert!(inf.is_infinite());
158 /// assert!(neg_inf.is_infinite());
160 #[stable(feature = "rust1", since = "1.0.0")]
162 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
164 /// Returns `true` if this number is neither infinite nor `NaN`.
170 /// let inf = f32::INFINITY;
171 /// let neg_inf = f32::NEG_INFINITY;
172 /// let nan = f32::NAN;
174 /// assert!(f.is_finite());
176 /// assert!(!nan.is_finite());
177 /// assert!(!inf.is_finite());
178 /// assert!(!neg_inf.is_finite());
180 #[stable(feature = "rust1", since = "1.0.0")]
182 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
184 /// Returns `true` if the number is neither zero, infinite,
185 /// [subnormal][subnormal], or `NaN`.
190 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
191 /// let max = f32::MAX;
192 /// let lower_than_min = 1.0e-40_f32;
193 /// let zero = 0.0_f32;
195 /// assert!(min.is_normal());
196 /// assert!(max.is_normal());
198 /// assert!(!zero.is_normal());
199 /// assert!(!f32::NAN.is_normal());
200 /// assert!(!f32::INFINITY.is_normal());
201 /// // Values between `0` and `min` are Subnormal.
202 /// assert!(!lower_than_min.is_normal());
204 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
205 #[stable(feature = "rust1", since = "1.0.0")]
207 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
209 /// Returns the floating point category of the number. If only one property
210 /// is going to be tested, it is generally faster to use the specific
211 /// predicate instead.
214 /// use std::num::FpCategory;
217 /// let num = 12.4_f32;
218 /// let inf = f32::INFINITY;
220 /// assert_eq!(num.classify(), FpCategory::Normal);
221 /// assert_eq!(inf.classify(), FpCategory::Infinite);
223 #[stable(feature = "rust1", since = "1.0.0")]
225 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
227 /// Returns the largest integer less than or equal to a number.
230 /// let f = 3.99_f32;
233 /// assert_eq!(f.floor(), 3.0);
234 /// assert_eq!(g.floor(), 3.0);
236 #[stable(feature = "rust1", since = "1.0.0")]
238 pub fn floor(self) -> f32 {
239 // On MSVC LLVM will lower many math intrinsics to a call to the
240 // corresponding function. On MSVC, however, many of these functions
241 // aren't actually available as symbols to call, but rather they are all
242 // `static inline` functions in header files. This means that from a C
243 // perspective it's "compatible", but not so much from an ABI
244 // perspective (which we're worried about).
246 // The inline header functions always just cast to a f64 and do their
247 // operation, so we do that here as well, but only for MSVC targets.
249 // Note that there are many MSVC-specific float operations which
250 // redirect to this comment, so `floorf` is just one case of a missing
251 // function on MSVC, but there are many others elsewhere.
252 #[cfg(target_env = "msvc")]
253 return (self as f64).floor() as f32;
254 #[cfg(not(target_env = "msvc"))]
255 return unsafe { intrinsics::floorf32(self) };
258 /// Returns the smallest integer greater than or equal to a number.
261 /// let f = 3.01_f32;
264 /// assert_eq!(f.ceil(), 4.0);
265 /// assert_eq!(g.ceil(), 4.0);
267 #[stable(feature = "rust1", since = "1.0.0")]
269 pub fn ceil(self) -> f32 {
270 // see notes above in `floor`
271 #[cfg(target_env = "msvc")]
272 return (self as f64).ceil() as f32;
273 #[cfg(not(target_env = "msvc"))]
274 return unsafe { intrinsics::ceilf32(self) };
277 /// Returns the nearest integer to a number. Round half-way cases away from
282 /// let g = -3.3_f32;
284 /// assert_eq!(f.round(), 3.0);
285 /// assert_eq!(g.round(), -3.0);
287 #[stable(feature = "rust1", since = "1.0.0")]
289 pub fn round(self) -> f32 {
290 unsafe { intrinsics::roundf32(self) }
293 /// Returns the integer part of a number.
297 /// let g = -3.7_f32;
299 /// assert_eq!(f.trunc(), 3.0);
300 /// assert_eq!(g.trunc(), -3.0);
302 #[stable(feature = "rust1", since = "1.0.0")]
304 pub fn trunc(self) -> f32 {
305 unsafe { intrinsics::truncf32(self) }
308 /// Returns the fractional part of a number.
314 /// let y = -3.5_f32;
315 /// let abs_difference_x = (x.fract() - 0.5).abs();
316 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
318 /// assert!(abs_difference_x <= f32::EPSILON);
319 /// assert!(abs_difference_y <= f32::EPSILON);
321 #[stable(feature = "rust1", since = "1.0.0")]
323 pub fn fract(self) -> f32 { self - self.trunc() }
325 /// Computes the absolute value of `self`. Returns `NAN` if the
332 /// let y = -3.5_f32;
334 /// let abs_difference_x = (x.abs() - x).abs();
335 /// let abs_difference_y = (y.abs() - (-y)).abs();
337 /// assert!(abs_difference_x <= f32::EPSILON);
338 /// assert!(abs_difference_y <= f32::EPSILON);
340 /// assert!(f32::NAN.abs().is_nan());
342 #[stable(feature = "rust1", since = "1.0.0")]
344 pub fn abs(self) -> f32 { num::Float::abs(self) }
346 /// Returns a number that represents the sign of `self`.
348 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
349 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
350 /// - `NAN` if the number is `NAN`
357 /// assert_eq!(f.signum(), 1.0);
358 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
360 /// assert!(f32::NAN.signum().is_nan());
362 #[stable(feature = "rust1", since = "1.0.0")]
364 pub fn signum(self) -> f32 { num::Float::signum(self) }
366 /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
367 /// positive sign bit and positive infinity.
371 /// let g = -7.0_f32;
373 /// assert!(f.is_sign_positive());
374 /// assert!(!g.is_sign_positive());
376 #[stable(feature = "rust1", since = "1.0.0")]
378 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
380 /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
381 /// negative sign bit and negative infinity.
387 /// assert!(!f.is_sign_negative());
388 /// assert!(g.is_sign_negative());
390 #[stable(feature = "rust1", since = "1.0.0")]
392 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
394 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
395 /// error. This produces a more accurate result with better performance than
396 /// a separate multiplication operation followed by an add.
401 /// let m = 10.0_f32;
403 /// let b = 60.0_f32;
406 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
408 /// assert!(abs_difference <= f32::EPSILON);
410 #[stable(feature = "rust1", since = "1.0.0")]
412 pub fn mul_add(self, a: f32, b: f32) -> f32 {
413 unsafe { intrinsics::fmaf32(self, a, b) }
416 /// Takes the reciprocal (inverse) of a number, `1/x`.
422 /// let abs_difference = (x.recip() - (1.0/x)).abs();
424 /// assert!(abs_difference <= f32::EPSILON);
426 #[stable(feature = "rust1", since = "1.0.0")]
428 pub fn recip(self) -> f32 { num::Float::recip(self) }
430 /// Raises a number to an integer power.
432 /// Using this function is generally faster than using `powf`
438 /// let abs_difference = (x.powi(2) - x*x).abs();
440 /// assert!(abs_difference <= f32::EPSILON);
442 #[stable(feature = "rust1", since = "1.0.0")]
444 pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
446 /// Raises a number to a floating point power.
452 /// let abs_difference = (x.powf(2.0) - x*x).abs();
454 /// assert!(abs_difference <= f32::EPSILON);
456 #[stable(feature = "rust1", since = "1.0.0")]
458 pub fn powf(self, n: f32) -> f32 {
459 // see notes above in `floor`
460 #[cfg(target_env = "msvc")]
461 return (self as f64).powf(n as f64) as f32;
462 #[cfg(not(target_env = "msvc"))]
463 return unsafe { intrinsics::powf32(self, n) };
466 /// Takes the square root of a number.
468 /// Returns NaN if `self` is a negative number.
473 /// let positive = 4.0_f32;
474 /// let negative = -4.0_f32;
476 /// let abs_difference = (positive.sqrt() - 2.0).abs();
478 /// assert!(abs_difference <= f32::EPSILON);
479 /// assert!(negative.sqrt().is_nan());
481 #[stable(feature = "rust1", since = "1.0.0")]
483 pub fn sqrt(self) -> f32 {
487 unsafe { intrinsics::sqrtf32(self) }
491 /// Returns `e^(self)`, (the exponential function).
496 /// let one = 1.0f32;
498 /// let e = one.exp();
500 /// // ln(e) - 1 == 0
501 /// let abs_difference = (e.ln() - 1.0).abs();
503 /// assert!(abs_difference <= f32::EPSILON);
505 #[stable(feature = "rust1", since = "1.0.0")]
507 pub fn exp(self) -> f32 {
508 // see notes above in `floor`
509 #[cfg(target_env = "msvc")]
510 return (self as f64).exp() as f32;
511 #[cfg(not(target_env = "msvc"))]
512 return unsafe { intrinsics::expf32(self) };
515 /// Returns `2^(self)`.
523 /// let abs_difference = (f.exp2() - 4.0).abs();
525 /// assert!(abs_difference <= f32::EPSILON);
527 #[stable(feature = "rust1", since = "1.0.0")]
529 pub fn exp2(self) -> f32 {
530 unsafe { intrinsics::exp2f32(self) }
533 /// Returns the natural logarithm of the number.
538 /// let one = 1.0f32;
540 /// let e = one.exp();
542 /// // ln(e) - 1 == 0
543 /// let abs_difference = (e.ln() - 1.0).abs();
545 /// assert!(abs_difference <= f32::EPSILON);
547 #[stable(feature = "rust1", since = "1.0.0")]
549 pub fn ln(self) -> f32 {
550 // see notes above in `floor`
551 #[cfg(target_env = "msvc")]
552 return (self as f64).ln() as f32;
553 #[cfg(not(target_env = "msvc"))]
554 return unsafe { intrinsics::logf32(self) };
557 /// Returns the logarithm of the number with respect to an arbitrary base.
562 /// let ten = 10.0f32;
563 /// let two = 2.0f32;
565 /// // log10(10) - 1 == 0
566 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
568 /// // log2(2) - 1 == 0
569 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
571 /// assert!(abs_difference_10 <= f32::EPSILON);
572 /// assert!(abs_difference_2 <= f32::EPSILON);
574 #[stable(feature = "rust1", since = "1.0.0")]
576 pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
578 /// Returns the base 2 logarithm of the number.
583 /// let two = 2.0f32;
585 /// // log2(2) - 1 == 0
586 /// let abs_difference = (two.log2() - 1.0).abs();
588 /// assert!(abs_difference <= f32::EPSILON);
590 #[stable(feature = "rust1", since = "1.0.0")]
592 pub fn log2(self) -> f32 {
593 #[cfg(target_os = "android")]
594 return ::sys::android::log2f32(self);
595 #[cfg(not(target_os = "android"))]
596 return unsafe { intrinsics::log2f32(self) };
599 /// Returns the base 10 logarithm of the number.
604 /// let ten = 10.0f32;
606 /// // log10(10) - 1 == 0
607 /// let abs_difference = (ten.log10() - 1.0).abs();
609 /// assert!(abs_difference <= f32::EPSILON);
611 #[stable(feature = "rust1", since = "1.0.0")]
613 pub fn log10(self) -> f32 {
614 // see notes above in `floor`
615 #[cfg(target_env = "msvc")]
616 return (self as f64).log10() as f32;
617 #[cfg(not(target_env = "msvc"))]
618 return unsafe { intrinsics::log10f32(self) };
621 /// Converts radians to degrees.
624 /// use std::f32::{self, consts};
626 /// let angle = consts::PI;
628 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
630 /// assert!(abs_difference <= f32::EPSILON);
632 #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
634 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
636 /// Converts degrees to radians.
639 /// use std::f32::{self, consts};
641 /// let angle = 180.0f32;
643 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
645 /// assert!(abs_difference <= f32::EPSILON);
647 #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
649 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
651 /// Returns the maximum of the two numbers.
657 /// assert_eq!(x.max(y), y);
660 /// If one of the arguments is NaN, then the other argument is returned.
661 #[stable(feature = "rust1", since = "1.0.0")]
663 pub fn max(self, other: f32) -> f32 {
664 num::Float::max(self, other)
667 /// Returns the minimum of the two numbers.
673 /// assert_eq!(x.min(y), x);
676 /// If one of the arguments is NaN, then the other argument is returned.
677 #[stable(feature = "rust1", since = "1.0.0")]
679 pub fn min(self, other: f32) -> f32 {
680 num::Float::min(self, other)
683 /// The positive difference of two numbers.
685 /// * If `self <= other`: `0:0`
686 /// * Else: `self - other`
694 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
695 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
697 /// assert!(abs_difference_x <= f32::EPSILON);
698 /// assert!(abs_difference_y <= f32::EPSILON);
700 #[stable(feature = "rust1", since = "1.0.0")]
702 #[rustc_deprecated(since = "1.10.0",
703 reason = "you probably meant `(self - other).abs()`: \
704 this operation is `(self - other).max(0.0)` (also \
705 known as `fdimf` in C). If you truly need the positive \
706 difference, consider using that expression or the C function \
707 `fdimf`, depending on how you wish to handle NaN (please consider \
708 filing an issue describing your use-case too).")]
709 pub fn abs_sub(self, other: f32) -> f32 {
710 unsafe { cmath::fdimf(self, other) }
713 /// Takes the cubic root of a number.
720 /// // x^(1/3) - 2 == 0
721 /// let abs_difference = (x.cbrt() - 2.0).abs();
723 /// assert!(abs_difference <= f32::EPSILON);
725 #[stable(feature = "rust1", since = "1.0.0")]
727 pub fn cbrt(self) -> f32 {
728 unsafe { cmath::cbrtf(self) }
731 /// Calculates the length of the hypotenuse of a right-angle triangle given
732 /// legs of length `x` and `y`.
740 /// // sqrt(x^2 + y^2)
741 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
743 /// assert!(abs_difference <= f32::EPSILON);
745 #[stable(feature = "rust1", since = "1.0.0")]
747 pub fn hypot(self, other: f32) -> f32 {
748 unsafe { cmath::hypotf(self, other) }
751 /// Computes the sine of a number (in radians).
756 /// let x = f32::consts::PI/2.0;
758 /// let abs_difference = (x.sin() - 1.0).abs();
760 /// assert!(abs_difference <= f32::EPSILON);
762 #[stable(feature = "rust1", since = "1.0.0")]
764 pub fn sin(self) -> f32 {
765 // see notes in `core::f32::Float::floor`
766 #[cfg(target_env = "msvc")]
767 return (self as f64).sin() as f32;
768 #[cfg(not(target_env = "msvc"))]
769 return unsafe { intrinsics::sinf32(self) };
772 /// Computes the cosine of a number (in radians).
777 /// let x = 2.0*f32::consts::PI;
779 /// let abs_difference = (x.cos() - 1.0).abs();
781 /// assert!(abs_difference <= f32::EPSILON);
783 #[stable(feature = "rust1", since = "1.0.0")]
785 pub fn cos(self) -> f32 {
786 // see notes in `core::f32::Float::floor`
787 #[cfg(target_env = "msvc")]
788 return (self as f64).cos() as f32;
789 #[cfg(not(target_env = "msvc"))]
790 return unsafe { intrinsics::cosf32(self) };
793 /// Computes the tangent of a number (in radians).
798 /// let x = f32::consts::PI / 4.0;
799 /// let abs_difference = (x.tan() - 1.0).abs();
801 /// assert!(abs_difference <= f32::EPSILON);
803 #[stable(feature = "rust1", since = "1.0.0")]
805 pub fn tan(self) -> f32 {
806 unsafe { cmath::tanf(self) }
809 /// Computes the arcsine of a number. Return value is in radians in
810 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
816 /// let f = f32::consts::PI / 2.0;
818 /// // asin(sin(pi/2))
819 /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
821 /// assert!(abs_difference <= f32::EPSILON);
823 #[stable(feature = "rust1", since = "1.0.0")]
825 pub fn asin(self) -> f32 {
826 unsafe { cmath::asinf(self) }
829 /// Computes the arccosine of a number. Return value is in radians in
830 /// the range [0, pi] or NaN if the number is outside the range
836 /// let f = f32::consts::PI / 4.0;
838 /// // acos(cos(pi/4))
839 /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
841 /// assert!(abs_difference <= f32::EPSILON);
843 #[stable(feature = "rust1", since = "1.0.0")]
845 pub fn acos(self) -> f32 {
846 unsafe { cmath::acosf(self) }
849 /// Computes the arctangent of a number. Return value is in radians in the
850 /// range [-pi/2, pi/2];
858 /// let abs_difference = (f.tan().atan() - 1.0).abs();
860 /// assert!(abs_difference <= f32::EPSILON);
862 #[stable(feature = "rust1", since = "1.0.0")]
864 pub fn atan(self) -> f32 {
865 unsafe { cmath::atanf(self) }
868 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
870 /// * `x = 0`, `y = 0`: `0`
871 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
872 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
873 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
878 /// let pi = f32::consts::PI;
879 /// // All angles from horizontal right (+x)
880 /// // 45 deg counter-clockwise
882 /// let y1 = -3.0f32;
884 /// // 135 deg clockwise
885 /// let x2 = -3.0f32;
888 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
889 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
891 /// assert!(abs_difference_1 <= f32::EPSILON);
892 /// assert!(abs_difference_2 <= f32::EPSILON);
894 #[stable(feature = "rust1", since = "1.0.0")]
896 pub fn atan2(self, other: f32) -> f32 {
897 unsafe { cmath::atan2f(self, other) }
900 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
901 /// `(sin(x), cos(x))`.
906 /// let x = f32::consts::PI/4.0;
907 /// let f = x.sin_cos();
909 /// let abs_difference_0 = (f.0 - x.sin()).abs();
910 /// let abs_difference_1 = (f.1 - x.cos()).abs();
912 /// assert!(abs_difference_0 <= f32::EPSILON);
913 /// assert!(abs_difference_1 <= f32::EPSILON);
915 #[stable(feature = "rust1", since = "1.0.0")]
917 pub fn sin_cos(self) -> (f32, f32) {
918 (self.sin(), self.cos())
921 /// Returns `e^(self) - 1` in a way that is accurate even if the
922 /// number is close to zero.
930 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
932 /// assert!(abs_difference <= f32::EPSILON);
934 #[stable(feature = "rust1", since = "1.0.0")]
936 pub fn exp_m1(self) -> f32 {
937 unsafe { cmath::expm1f(self) }
940 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
941 /// the operations were performed separately.
946 /// let x = f32::consts::E - 1.0;
948 /// // ln(1 + (e - 1)) == ln(e) == 1
949 /// let abs_difference = (x.ln_1p() - 1.0).abs();
951 /// assert!(abs_difference <= f32::EPSILON);
953 #[stable(feature = "rust1", since = "1.0.0")]
955 pub fn ln_1p(self) -> f32 {
956 unsafe { cmath::log1pf(self) }
959 /// Hyperbolic sine function.
964 /// let e = f32::consts::E;
967 /// let f = x.sinh();
968 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
969 /// let g = (e*e - 1.0)/(2.0*e);
970 /// let abs_difference = (f - g).abs();
972 /// assert!(abs_difference <= f32::EPSILON);
974 #[stable(feature = "rust1", since = "1.0.0")]
976 pub fn sinh(self) -> f32 {
977 unsafe { cmath::sinhf(self) }
980 /// Hyperbolic cosine function.
985 /// let e = f32::consts::E;
987 /// let f = x.cosh();
988 /// // Solving cosh() at 1 gives this result
989 /// let g = (e*e + 1.0)/(2.0*e);
990 /// let abs_difference = (f - g).abs();
993 /// assert!(abs_difference <= f32::EPSILON);
995 #[stable(feature = "rust1", since = "1.0.0")]
997 pub fn cosh(self) -> f32 {
998 unsafe { cmath::coshf(self) }
1001 /// Hyperbolic tangent function.
1006 /// let e = f32::consts::E;
1009 /// let f = x.tanh();
1010 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1011 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1012 /// let abs_difference = (f - g).abs();
1014 /// assert!(abs_difference <= f32::EPSILON);
1016 #[stable(feature = "rust1", since = "1.0.0")]
1018 pub fn tanh(self) -> f32 {
1019 unsafe { cmath::tanhf(self) }
1022 /// Inverse hyperbolic sine function.
1028 /// let f = x.sinh().asinh();
1030 /// let abs_difference = (f - x).abs();
1032 /// assert!(abs_difference <= f32::EPSILON);
1034 #[stable(feature = "rust1", since = "1.0.0")]
1036 pub fn asinh(self) -> f32 {
1037 if self == NEG_INFINITY {
1040 (self + ((self * self) + 1.0).sqrt()).ln()
1044 /// Inverse hyperbolic cosine function.
1050 /// let f = x.cosh().acosh();
1052 /// let abs_difference = (f - x).abs();
1054 /// assert!(abs_difference <= f32::EPSILON);
1056 #[stable(feature = "rust1", since = "1.0.0")]
1058 pub fn acosh(self) -> f32 {
1060 x if x < 1.0 => ::f32::NAN,
1061 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1065 /// Inverse hyperbolic tangent function.
1070 /// let e = f32::consts::E;
1071 /// let f = e.tanh().atanh();
1073 /// let abs_difference = (f - e).abs();
1075 /// assert!(abs_difference <= 1e-5);
1077 #[stable(feature = "rust1", since = "1.0.0")]
1079 pub fn atanh(self) -> f32 {
1080 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1083 /// Raw transmutation to `u32`.
1085 /// Converts the `f32` into its raw memory representation,
1086 /// similar to the `transmute` function.
1088 /// Note that this function is distinct from casting.
1093 /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
1094 /// assert_eq!((12.5f32).to_bits(), 0x41480000);
1097 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1099 pub fn to_bits(self) -> u32 {
1100 unsafe { ::mem::transmute(self) }
1103 /// Raw transmutation from `u32`.
1105 /// Converts the given `u32` containing the float's raw memory
1106 /// representation into the `f32` type, similar to the
1107 /// `transmute` function.
1109 /// There is only one difference to a bare `transmute`:
1110 /// Due to the implications onto Rust's safety promises being
1111 /// uncertain, if the representation of a signaling NaN "sNaN" float
1112 /// is passed to the function, the implementation is allowed to
1113 /// return a quiet NaN instead.
1115 /// Note that this function is distinct from casting.
1121 /// let v = f32::from_bits(0x41480000);
1122 /// let difference = (v - 12.5).abs();
1123 /// assert!(difference <= 1e-5);
1124 /// // Example for a signaling NaN value:
1125 /// let snan = 0x7F800001;
1126 /// assert_ne!(f32::from_bits(snan).to_bits(), snan);
1128 #[stable(feature = "float_bits_conv", since = "1.20.0")]
1130 pub fn from_bits(mut v: u32) -> Self {
1131 const EXP_MASK: u32 = 0x7F800000;
1132 const FRACT_MASK: u32 = 0x007FFFFF;
1133 if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1134 // While IEEE 754-2008 specifies encodings for quiet NaNs
1135 // and signaling ones, certain MIPS and PA-RISC
1136 // CPUs treat signaling NaNs differently.
1137 // Therefore to be safe, we pass a known quiet NaN
1138 // if v is any kind of NaN.
1139 // The check above only assumes IEEE 754-1985 to be
1141 v = unsafe { ::mem::transmute(NAN) };
1143 unsafe { ::mem::transmute(v) }
1152 use num::FpCategory as Fp;
1156 test_num(10f32, 2f32);
1161 assert_eq!(NAN.min(2.0), 2.0);
1162 assert_eq!(2.0f32.min(NAN), 2.0);
1167 assert_eq!(NAN.max(2.0), 2.0);
1168 assert_eq!(2.0f32.max(NAN), 2.0);
1173 let nan: f32 = f32::NAN;
1174 assert!(nan.is_nan());
1175 assert!(!nan.is_infinite());
1176 assert!(!nan.is_finite());
1177 assert!(!nan.is_normal());
1178 assert!(nan.is_sign_positive());
1179 assert!(!nan.is_sign_negative());
1180 assert_eq!(Fp::Nan, nan.classify());
1184 fn test_infinity() {
1185 let inf: f32 = f32::INFINITY;
1186 assert!(inf.is_infinite());
1187 assert!(!inf.is_finite());
1188 assert!(inf.is_sign_positive());
1189 assert!(!inf.is_sign_negative());
1190 assert!(!inf.is_nan());
1191 assert!(!inf.is_normal());
1192 assert_eq!(Fp::Infinite, inf.classify());
1196 fn test_neg_infinity() {
1197 let neg_inf: f32 = f32::NEG_INFINITY;
1198 assert!(neg_inf.is_infinite());
1199 assert!(!neg_inf.is_finite());
1200 assert!(!neg_inf.is_sign_positive());
1201 assert!(neg_inf.is_sign_negative());
1202 assert!(!neg_inf.is_nan());
1203 assert!(!neg_inf.is_normal());
1204 assert_eq!(Fp::Infinite, neg_inf.classify());
1209 let zero: f32 = 0.0f32;
1210 assert_eq!(0.0, zero);
1211 assert!(!zero.is_infinite());
1212 assert!(zero.is_finite());
1213 assert!(zero.is_sign_positive());
1214 assert!(!zero.is_sign_negative());
1215 assert!(!zero.is_nan());
1216 assert!(!zero.is_normal());
1217 assert_eq!(Fp::Zero, zero.classify());
1221 fn test_neg_zero() {
1222 let neg_zero: f32 = -0.0;
1223 assert_eq!(0.0, neg_zero);
1224 assert!(!neg_zero.is_infinite());
1225 assert!(neg_zero.is_finite());
1226 assert!(!neg_zero.is_sign_positive());
1227 assert!(neg_zero.is_sign_negative());
1228 assert!(!neg_zero.is_nan());
1229 assert!(!neg_zero.is_normal());
1230 assert_eq!(Fp::Zero, neg_zero.classify());
1235 let one: f32 = 1.0f32;
1236 assert_eq!(1.0, one);
1237 assert!(!one.is_infinite());
1238 assert!(one.is_finite());
1239 assert!(one.is_sign_positive());
1240 assert!(!one.is_sign_negative());
1241 assert!(!one.is_nan());
1242 assert!(one.is_normal());
1243 assert_eq!(Fp::Normal, one.classify());
1248 let nan: f32 = f32::NAN;
1249 let inf: f32 = f32::INFINITY;
1250 let neg_inf: f32 = f32::NEG_INFINITY;
1251 assert!(nan.is_nan());
1252 assert!(!0.0f32.is_nan());
1253 assert!(!5.3f32.is_nan());
1254 assert!(!(-10.732f32).is_nan());
1255 assert!(!inf.is_nan());
1256 assert!(!neg_inf.is_nan());
1260 fn test_is_infinite() {
1261 let nan: f32 = f32::NAN;
1262 let inf: f32 = f32::INFINITY;
1263 let neg_inf: f32 = f32::NEG_INFINITY;
1264 assert!(!nan.is_infinite());
1265 assert!(inf.is_infinite());
1266 assert!(neg_inf.is_infinite());
1267 assert!(!0.0f32.is_infinite());
1268 assert!(!42.8f32.is_infinite());
1269 assert!(!(-109.2f32).is_infinite());
1273 fn test_is_finite() {
1274 let nan: f32 = f32::NAN;
1275 let inf: f32 = f32::INFINITY;
1276 let neg_inf: f32 = f32::NEG_INFINITY;
1277 assert!(!nan.is_finite());
1278 assert!(!inf.is_finite());
1279 assert!(!neg_inf.is_finite());
1280 assert!(0.0f32.is_finite());
1281 assert!(42.8f32.is_finite());
1282 assert!((-109.2f32).is_finite());
1286 fn test_is_normal() {
1287 let nan: f32 = f32::NAN;
1288 let inf: f32 = f32::INFINITY;
1289 let neg_inf: f32 = f32::NEG_INFINITY;
1290 let zero: f32 = 0.0f32;
1291 let neg_zero: f32 = -0.0;
1292 assert!(!nan.is_normal());
1293 assert!(!inf.is_normal());
1294 assert!(!neg_inf.is_normal());
1295 assert!(!zero.is_normal());
1296 assert!(!neg_zero.is_normal());
1297 assert!(1f32.is_normal());
1298 assert!(1e-37f32.is_normal());
1299 assert!(!1e-38f32.is_normal());
1303 fn test_classify() {
1304 let nan: f32 = f32::NAN;
1305 let inf: f32 = f32::INFINITY;
1306 let neg_inf: f32 = f32::NEG_INFINITY;
1307 let zero: f32 = 0.0f32;
1308 let neg_zero: f32 = -0.0;
1309 assert_eq!(nan.classify(), Fp::Nan);
1310 assert_eq!(inf.classify(), Fp::Infinite);
1311 assert_eq!(neg_inf.classify(), Fp::Infinite);
1312 assert_eq!(zero.classify(), Fp::Zero);
1313 assert_eq!(neg_zero.classify(), Fp::Zero);
1314 assert_eq!(1f32.classify(), Fp::Normal);
1315 assert_eq!(1e-37f32.classify(), Fp::Normal);
1316 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1321 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1322 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1323 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1324 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1325 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1326 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1327 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1328 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1329 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1330 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1335 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1336 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1337 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1338 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1339 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1340 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1341 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1342 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1343 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1344 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1349 assert_approx_eq!(1.0f32.round(), 1.0f32);
1350 assert_approx_eq!(1.3f32.round(), 1.0f32);
1351 assert_approx_eq!(1.5f32.round(), 2.0f32);
1352 assert_approx_eq!(1.7f32.round(), 2.0f32);
1353 assert_approx_eq!(0.0f32.round(), 0.0f32);
1354 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1355 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1356 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1357 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1358 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1363 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1364 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1365 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1366 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1367 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1368 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1369 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1370 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1371 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1372 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1377 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1378 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1379 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1380 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1381 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1382 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1383 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1384 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1385 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1386 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1391 assert_eq!(INFINITY.abs(), INFINITY);
1392 assert_eq!(1f32.abs(), 1f32);
1393 assert_eq!(0f32.abs(), 0f32);
1394 assert_eq!((-0f32).abs(), 0f32);
1395 assert_eq!((-1f32).abs(), 1f32);
1396 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1397 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1398 assert!(NAN.abs().is_nan());
1403 assert_eq!(INFINITY.signum(), 1f32);
1404 assert_eq!(1f32.signum(), 1f32);
1405 assert_eq!(0f32.signum(), 1f32);
1406 assert_eq!((-0f32).signum(), -1f32);
1407 assert_eq!((-1f32).signum(), -1f32);
1408 assert_eq!(NEG_INFINITY.signum(), -1f32);
1409 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1410 assert!(NAN.signum().is_nan());
1414 fn test_is_sign_positive() {
1415 assert!(INFINITY.is_sign_positive());
1416 assert!(1f32.is_sign_positive());
1417 assert!(0f32.is_sign_positive());
1418 assert!(!(-0f32).is_sign_positive());
1419 assert!(!(-1f32).is_sign_positive());
1420 assert!(!NEG_INFINITY.is_sign_positive());
1421 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1422 assert!(NAN.is_sign_positive());
1423 assert!(!(-NAN).is_sign_positive());
1427 fn test_is_sign_negative() {
1428 assert!(!INFINITY.is_sign_negative());
1429 assert!(!1f32.is_sign_negative());
1430 assert!(!0f32.is_sign_negative());
1431 assert!((-0f32).is_sign_negative());
1432 assert!((-1f32).is_sign_negative());
1433 assert!(NEG_INFINITY.is_sign_negative());
1434 assert!((1f32/NEG_INFINITY).is_sign_negative());
1435 assert!(!NAN.is_sign_negative());
1436 assert!((-NAN).is_sign_negative());
1441 let nan: f32 = f32::NAN;
1442 let inf: f32 = f32::INFINITY;
1443 let neg_inf: f32 = f32::NEG_INFINITY;
1444 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1445 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1446 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1447 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1448 assert!(nan.mul_add(7.8, 9.0).is_nan());
1449 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1450 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1451 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1452 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1457 let nan: f32 = f32::NAN;
1458 let inf: f32 = f32::INFINITY;
1459 let neg_inf: f32 = f32::NEG_INFINITY;
1460 assert_eq!(1.0f32.recip(), 1.0);
1461 assert_eq!(2.0f32.recip(), 0.5);
1462 assert_eq!((-0.4f32).recip(), -2.5);
1463 assert_eq!(0.0f32.recip(), inf);
1464 assert!(nan.recip().is_nan());
1465 assert_eq!(inf.recip(), 0.0);
1466 assert_eq!(neg_inf.recip(), 0.0);
1471 let nan: f32 = f32::NAN;
1472 let inf: f32 = f32::INFINITY;
1473 let neg_inf: f32 = f32::NEG_INFINITY;
1474 assert_eq!(1.0f32.powi(1), 1.0);
1475 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1476 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1477 assert_eq!(8.3f32.powi(0), 1.0);
1478 assert!(nan.powi(2).is_nan());
1479 assert_eq!(inf.powi(3), inf);
1480 assert_eq!(neg_inf.powi(2), inf);
1485 let nan: f32 = f32::NAN;
1486 let inf: f32 = f32::INFINITY;
1487 let neg_inf: f32 = f32::NEG_INFINITY;
1488 assert_eq!(1.0f32.powf(1.0), 1.0);
1489 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1490 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1491 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1492 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1493 assert_eq!(8.3f32.powf(0.0), 1.0);
1494 assert!(nan.powf(2.0).is_nan());
1495 assert_eq!(inf.powf(2.0), inf);
1496 assert_eq!(neg_inf.powf(3.0), neg_inf);
1500 fn test_sqrt_domain() {
1501 assert!(NAN.sqrt().is_nan());
1502 assert!(NEG_INFINITY.sqrt().is_nan());
1503 assert!((-1.0f32).sqrt().is_nan());
1504 assert_eq!((-0.0f32).sqrt(), -0.0);
1505 assert_eq!(0.0f32.sqrt(), 0.0);
1506 assert_eq!(1.0f32.sqrt(), 1.0);
1507 assert_eq!(INFINITY.sqrt(), INFINITY);
1512 assert_eq!(1.0, 0.0f32.exp());
1513 assert_approx_eq!(2.718282, 1.0f32.exp());
1514 assert_approx_eq!(148.413162, 5.0f32.exp());
1516 let inf: f32 = f32::INFINITY;
1517 let neg_inf: f32 = f32::NEG_INFINITY;
1518 let nan: f32 = f32::NAN;
1519 assert_eq!(inf, inf.exp());
1520 assert_eq!(0.0, neg_inf.exp());
1521 assert!(nan.exp().is_nan());
1526 assert_eq!(32.0, 5.0f32.exp2());
1527 assert_eq!(1.0, 0.0f32.exp2());
1529 let inf: f32 = f32::INFINITY;
1530 let neg_inf: f32 = f32::NEG_INFINITY;
1531 let nan: f32 = f32::NAN;
1532 assert_eq!(inf, inf.exp2());
1533 assert_eq!(0.0, neg_inf.exp2());
1534 assert!(nan.exp2().is_nan());
1539 let nan: f32 = f32::NAN;
1540 let inf: f32 = f32::INFINITY;
1541 let neg_inf: f32 = f32::NEG_INFINITY;
1542 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1543 assert!(nan.ln().is_nan());
1544 assert_eq!(inf.ln(), inf);
1545 assert!(neg_inf.ln().is_nan());
1546 assert!((-2.3f32).ln().is_nan());
1547 assert_eq!((-0.0f32).ln(), neg_inf);
1548 assert_eq!(0.0f32.ln(), neg_inf);
1549 assert_approx_eq!(4.0f32.ln(), 1.386294);
1554 let nan: f32 = f32::NAN;
1555 let inf: f32 = f32::INFINITY;
1556 let neg_inf: f32 = f32::NEG_INFINITY;
1557 assert_eq!(10.0f32.log(10.0), 1.0);
1558 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1559 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1560 assert!(1.0f32.log(1.0).is_nan());
1561 assert!(1.0f32.log(-13.9).is_nan());
1562 assert!(nan.log(2.3).is_nan());
1563 assert_eq!(inf.log(10.0), inf);
1564 assert!(neg_inf.log(8.8).is_nan());
1565 assert!((-2.3f32).log(0.1).is_nan());
1566 assert_eq!((-0.0f32).log(2.0), neg_inf);
1567 assert_eq!(0.0f32.log(7.0), neg_inf);
1572 let nan: f32 = f32::NAN;
1573 let inf: f32 = f32::INFINITY;
1574 let neg_inf: f32 = f32::NEG_INFINITY;
1575 assert_approx_eq!(10.0f32.log2(), 3.321928);
1576 assert_approx_eq!(2.3f32.log2(), 1.201634);
1577 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1578 assert!(nan.log2().is_nan());
1579 assert_eq!(inf.log2(), inf);
1580 assert!(neg_inf.log2().is_nan());
1581 assert!((-2.3f32).log2().is_nan());
1582 assert_eq!((-0.0f32).log2(), neg_inf);
1583 assert_eq!(0.0f32.log2(), neg_inf);
1588 let nan: f32 = f32::NAN;
1589 let inf: f32 = f32::INFINITY;
1590 let neg_inf: f32 = f32::NEG_INFINITY;
1591 assert_eq!(10.0f32.log10(), 1.0);
1592 assert_approx_eq!(2.3f32.log10(), 0.361728);
1593 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1594 assert_eq!(1.0f32.log10(), 0.0);
1595 assert!(nan.log10().is_nan());
1596 assert_eq!(inf.log10(), inf);
1597 assert!(neg_inf.log10().is_nan());
1598 assert!((-2.3f32).log10().is_nan());
1599 assert_eq!((-0.0f32).log10(), neg_inf);
1600 assert_eq!(0.0f32.log10(), neg_inf);
1604 fn test_to_degrees() {
1605 let pi: f32 = consts::PI;
1606 let nan: f32 = f32::NAN;
1607 let inf: f32 = f32::INFINITY;
1608 let neg_inf: f32 = f32::NEG_INFINITY;
1609 assert_eq!(0.0f32.to_degrees(), 0.0);
1610 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1611 assert_eq!(pi.to_degrees(), 180.0);
1612 assert!(nan.to_degrees().is_nan());
1613 assert_eq!(inf.to_degrees(), inf);
1614 assert_eq!(neg_inf.to_degrees(), neg_inf);
1618 fn test_to_radians() {
1619 let pi: f32 = consts::PI;
1620 let nan: f32 = f32::NAN;
1621 let inf: f32 = f32::INFINITY;
1622 let neg_inf: f32 = f32::NEG_INFINITY;
1623 assert_eq!(0.0f32.to_radians(), 0.0);
1624 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1625 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1626 assert_eq!(180.0f32.to_radians(), pi);
1627 assert!(nan.to_radians().is_nan());
1628 assert_eq!(inf.to_radians(), inf);
1629 assert_eq!(neg_inf.to_radians(), neg_inf);
1634 assert_eq!(0.0f32.asinh(), 0.0f32);
1635 assert_eq!((-0.0f32).asinh(), -0.0f32);
1637 let inf: f32 = f32::INFINITY;
1638 let neg_inf: f32 = f32::NEG_INFINITY;
1639 let nan: f32 = f32::NAN;
1640 assert_eq!(inf.asinh(), inf);
1641 assert_eq!(neg_inf.asinh(), neg_inf);
1642 assert!(nan.asinh().is_nan());
1643 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1644 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1649 assert_eq!(1.0f32.acosh(), 0.0f32);
1650 assert!(0.999f32.acosh().is_nan());
1652 let inf: f32 = f32::INFINITY;
1653 let neg_inf: f32 = f32::NEG_INFINITY;
1654 let nan: f32 = f32::NAN;
1655 assert_eq!(inf.acosh(), inf);
1656 assert!(neg_inf.acosh().is_nan());
1657 assert!(nan.acosh().is_nan());
1658 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1659 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1664 assert_eq!(0.0f32.atanh(), 0.0f32);
1665 assert_eq!((-0.0f32).atanh(), -0.0f32);
1667 let inf32: f32 = f32::INFINITY;
1668 let neg_inf32: f32 = f32::NEG_INFINITY;
1669 assert_eq!(1.0f32.atanh(), inf32);
1670 assert_eq!((-1.0f32).atanh(), neg_inf32);
1672 assert!(2f64.atanh().atanh().is_nan());
1673 assert!((-2f64).atanh().atanh().is_nan());
1675 let inf64: f32 = f32::INFINITY;
1676 let neg_inf64: f32 = f32::NEG_INFINITY;
1677 let nan32: f32 = f32::NAN;
1678 assert!(inf64.atanh().is_nan());
1679 assert!(neg_inf64.atanh().is_nan());
1680 assert!(nan32.atanh().is_nan());
1682 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1683 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1687 fn test_real_consts() {
1690 let pi: f32 = consts::PI;
1691 let frac_pi_2: f32 = consts::FRAC_PI_2;
1692 let frac_pi_3: f32 = consts::FRAC_PI_3;
1693 let frac_pi_4: f32 = consts::FRAC_PI_4;
1694 let frac_pi_6: f32 = consts::FRAC_PI_6;
1695 let frac_pi_8: f32 = consts::FRAC_PI_8;
1696 let frac_1_pi: f32 = consts::FRAC_1_PI;
1697 let frac_2_pi: f32 = consts::FRAC_2_PI;
1698 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1699 let sqrt2: f32 = consts::SQRT_2;
1700 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1701 let e: f32 = consts::E;
1702 let log2_e: f32 = consts::LOG2_E;
1703 let log10_e: f32 = consts::LOG10_E;
1704 let ln_2: f32 = consts::LN_2;
1705 let ln_10: f32 = consts::LN_10;
1707 assert_approx_eq!(frac_pi_2, pi / 2f32);
1708 assert_approx_eq!(frac_pi_3, pi / 3f32);
1709 assert_approx_eq!(frac_pi_4, pi / 4f32);
1710 assert_approx_eq!(frac_pi_6, pi / 6f32);
1711 assert_approx_eq!(frac_pi_8, pi / 8f32);
1712 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1713 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1714 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1715 assert_approx_eq!(sqrt2, 2f32.sqrt());
1716 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1717 assert_approx_eq!(log2_e, e.log2());
1718 assert_approx_eq!(log10_e, e.log10());
1719 assert_approx_eq!(ln_2, 2f32.ln());
1720 assert_approx_eq!(ln_10, 10f32.ln());
1724 fn test_float_bits_conv() {
1725 assert_eq!((1f32).to_bits(), 0x3f800000);
1726 assert_eq!((12.5f32).to_bits(), 0x41480000);
1727 assert_eq!((1337f32).to_bits(), 0x44a72000);
1728 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1729 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1730 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1731 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1732 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1735 fn test_snan_masking() {
1736 // NOTE: this test assumes that our current platform
1737 // implements IEEE 754-2008 that specifies the difference
1738 // in encoding of quiet and signaling NaNs.
1739 // If you are porting Rust to a platform that does not
1740 // implement IEEE 754-2008 (but e.g. IEEE 754-1985, which
1741 // only says that "Signaling NaNs shall be reserved operands"
1742 // but doesn't specify the actual setup), feel free to
1743 // cfg out this test.
1744 let snan: u32 = 0x7F801337;
1745 const QNAN_MASK: u32 = 0x00400000;
1746 let nan_masked_fl = f32::from_bits(snan);
1747 let nan_masked = nan_masked_fl.to_bits();
1748 // Ensure that signaling NaNs don't stay the same
1749 assert_ne!(nan_masked, snan);
1750 // Ensure that we have a quiet NaN
1751 assert_ne!(nan_masked & QNAN_MASK, 0);
1752 assert!(nan_masked_fl.is_nan());