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;
43 use libc::{c_float, c_int};
46 pub fn cbrtf(n: c_float) -> c_float;
47 pub fn erff(n: c_float) -> c_float;
48 pub fn erfcf(n: c_float) -> c_float;
49 pub fn expm1f(n: c_float) -> c_float;
50 pub fn fdimf(a: c_float, b: c_float) -> c_float;
51 pub fn fmaxf(a: c_float, b: c_float) -> c_float;
52 pub fn fminf(a: c_float, b: c_float) -> c_float;
53 pub fn fmodf(a: c_float, b: c_float) -> c_float;
54 pub fn ilogbf(n: c_float) -> c_int;
55 pub fn logbf(n: c_float) -> c_float;
56 pub fn log1pf(n: c_float) -> c_float;
57 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
58 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
59 pub fn tgammaf(n: c_float) -> c_float;
61 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
62 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
63 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
64 pub fn hypotf(x: c_float, y: c_float) -> c_float;
67 // See the comments in the `floor` function for why MSVC is special
69 #[cfg(not(target_env = "msvc"))]
71 pub fn acosf(n: c_float) -> c_float;
72 pub fn asinf(n: c_float) -> c_float;
73 pub fn atan2f(a: c_float, b: c_float) -> c_float;
74 pub fn atanf(n: c_float) -> c_float;
75 pub fn coshf(n: c_float) -> c_float;
76 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
77 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
78 pub fn sinhf(n: c_float) -> c_float;
79 pub fn tanf(n: c_float) -> c_float;
80 pub fn tanhf(n: c_float) -> c_float;
83 #[cfg(target_env = "msvc")]
84 pub use self::shims::*;
85 #[cfg(target_env = "msvc")]
87 use libc::{c_float, c_int};
90 pub unsafe fn acosf(n: c_float) -> c_float {
91 f64::acos(n as f64) as c_float
95 pub unsafe fn asinf(n: c_float) -> c_float {
96 f64::asin(n as f64) as c_float
100 pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
101 f64::atan2(n as f64, b as f64) as c_float
105 pub unsafe fn atanf(n: c_float) -> c_float {
106 f64::atan(n as f64) as c_float
110 pub unsafe fn coshf(n: c_float) -> c_float {
111 f64::cosh(n as f64) as c_float
116 pub unsafe fn frexpf(x: c_float, value: &mut c_int) -> c_float {
117 let (a, b) = f64::frexp(x as f64);
124 pub unsafe fn ldexpf(x: c_float, n: c_int) -> c_float {
125 f64::ldexp(x as f64, n as isize) as c_float
129 pub unsafe fn sinhf(n: c_float) -> c_float {
130 f64::sinh(n as f64) as c_float
134 pub unsafe fn tanf(n: c_float) -> c_float {
135 f64::tan(n as f64) as c_float
139 pub unsafe fn tanhf(n: c_float) -> c_float {
140 f64::tanh(n as f64) as c_float
148 /// Returns `true` if this value is `NaN` and false otherwise.
153 /// let nan = f32::NAN;
156 /// assert!(nan.is_nan());
157 /// assert!(!f.is_nan());
159 #[stable(feature = "rust1", since = "1.0.0")]
161 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
163 /// Returns `true` if this value is positive infinity or negative infinity and
170 /// let inf = f32::INFINITY;
171 /// let neg_inf = f32::NEG_INFINITY;
172 /// let nan = f32::NAN;
174 /// assert!(!f.is_infinite());
175 /// assert!(!nan.is_infinite());
177 /// assert!(inf.is_infinite());
178 /// assert!(neg_inf.is_infinite());
180 #[stable(feature = "rust1", since = "1.0.0")]
182 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
184 /// Returns `true` if this number is neither infinite nor `NaN`.
190 /// let inf = f32::INFINITY;
191 /// let neg_inf = f32::NEG_INFINITY;
192 /// let nan = f32::NAN;
194 /// assert!(f.is_finite());
196 /// assert!(!nan.is_finite());
197 /// assert!(!inf.is_finite());
198 /// assert!(!neg_inf.is_finite());
200 #[stable(feature = "rust1", since = "1.0.0")]
202 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
204 /// Returns `true` if the number is neither zero, infinite,
205 /// [subnormal][subnormal], or `NaN`.
210 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
211 /// let max = f32::MAX;
212 /// let lower_than_min = 1.0e-40_f32;
213 /// let zero = 0.0_f32;
215 /// assert!(min.is_normal());
216 /// assert!(max.is_normal());
218 /// assert!(!zero.is_normal());
219 /// assert!(!f32::NAN.is_normal());
220 /// assert!(!f32::INFINITY.is_normal());
221 /// // Values between `0` and `min` are Subnormal.
222 /// assert!(!lower_than_min.is_normal());
224 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
225 #[stable(feature = "rust1", since = "1.0.0")]
227 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
229 /// Returns the floating point category of the number. If only one property
230 /// is going to be tested, it is generally faster to use the specific
231 /// predicate instead.
234 /// use std::num::FpCategory;
237 /// let num = 12.4_f32;
238 /// let inf = f32::INFINITY;
240 /// assert_eq!(num.classify(), FpCategory::Normal);
241 /// assert_eq!(inf.classify(), FpCategory::Infinite);
243 #[stable(feature = "rust1", since = "1.0.0")]
245 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
247 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
248 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
249 /// The floating point encoding is documented in the [Reference][floating-point].
252 /// #![feature(float_extras)]
256 /// let num = 2.0f32;
258 /// // (8388608, -22, 1)
259 /// let (mantissa, exponent, sign) = num.integer_decode();
260 /// let sign_f = sign as f32;
261 /// let mantissa_f = mantissa as f32;
262 /// let exponent_f = num.powf(exponent as f32);
264 /// // 1 * 8388608 * 2^(-22) == 2
265 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
267 /// assert!(abs_difference <= f32::EPSILON);
269 /// [floating-point]: ../reference/types.html#machine-types
270 #[unstable(feature = "float_extras", reason = "signature is undecided",
272 #[rustc_deprecated(since = "1.11.0",
273 reason = "never really came to fruition and easily \
274 implementable outside the standard library")]
277 pub fn integer_decode(self) -> (u64, i16, i8) {
278 num::Float::integer_decode(self)
281 /// Returns the largest integer less than or equal to a number.
284 /// let f = 3.99_f32;
287 /// assert_eq!(f.floor(), 3.0);
288 /// assert_eq!(g.floor(), 3.0);
290 #[stable(feature = "rust1", since = "1.0.0")]
292 pub fn floor(self) -> f32 {
293 // On MSVC LLVM will lower many math intrinsics to a call to the
294 // corresponding function. On MSVC, however, many of these functions
295 // aren't actually available as symbols to call, but rather they are all
296 // `static inline` functions in header files. This means that from a C
297 // perspective it's "compatible", but not so much from an ABI
298 // perspective (which we're worried about).
300 // The inline header functions always just cast to a f64 and do their
301 // operation, so we do that here as well, but only for MSVC targets.
303 // Note that there are many MSVC-specific float operations which
304 // redirect to this comment, so `floorf` is just one case of a missing
305 // function on MSVC, but there are many others elsewhere.
306 #[cfg(target_env = "msvc")]
307 return (self as f64).floor() as f32;
308 #[cfg(not(target_env = "msvc"))]
309 return unsafe { intrinsics::floorf32(self) };
312 /// Returns the smallest integer greater than or equal to a number.
315 /// let f = 3.01_f32;
318 /// assert_eq!(f.ceil(), 4.0);
319 /// assert_eq!(g.ceil(), 4.0);
321 #[stable(feature = "rust1", since = "1.0.0")]
323 pub fn ceil(self) -> f32 {
324 // see notes above in `floor`
325 #[cfg(target_env = "msvc")]
326 return (self as f64).ceil() as f32;
327 #[cfg(not(target_env = "msvc"))]
328 return unsafe { intrinsics::ceilf32(self) };
331 /// Returns the nearest integer to a number. Round half-way cases away from
336 /// let g = -3.3_f32;
338 /// assert_eq!(f.round(), 3.0);
339 /// assert_eq!(g.round(), -3.0);
341 #[stable(feature = "rust1", since = "1.0.0")]
343 pub fn round(self) -> f32 {
344 unsafe { intrinsics::roundf32(self) }
347 /// Returns the integer part of a number.
351 /// let g = -3.7_f32;
353 /// assert_eq!(f.trunc(), 3.0);
354 /// assert_eq!(g.trunc(), -3.0);
356 #[stable(feature = "rust1", since = "1.0.0")]
358 pub fn trunc(self) -> f32 {
359 unsafe { intrinsics::truncf32(self) }
362 /// Returns the fractional part of a number.
368 /// let y = -3.5_f32;
369 /// let abs_difference_x = (x.fract() - 0.5).abs();
370 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
372 /// assert!(abs_difference_x <= f32::EPSILON);
373 /// assert!(abs_difference_y <= f32::EPSILON);
375 #[stable(feature = "rust1", since = "1.0.0")]
377 pub fn fract(self) -> f32 { self - self.trunc() }
379 /// Computes the absolute value of `self`. Returns `NAN` if the
386 /// let y = -3.5_f32;
388 /// let abs_difference_x = (x.abs() - x).abs();
389 /// let abs_difference_y = (y.abs() - (-y)).abs();
391 /// assert!(abs_difference_x <= f32::EPSILON);
392 /// assert!(abs_difference_y <= f32::EPSILON);
394 /// assert!(f32::NAN.abs().is_nan());
396 #[stable(feature = "rust1", since = "1.0.0")]
398 pub fn abs(self) -> f32 { num::Float::abs(self) }
400 /// Returns a number that represents the sign of `self`.
402 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
403 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
404 /// - `NAN` if the number is `NAN`
411 /// assert_eq!(f.signum(), 1.0);
412 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
414 /// assert!(f32::NAN.signum().is_nan());
416 #[stable(feature = "rust1", since = "1.0.0")]
418 pub fn signum(self) -> f32 { num::Float::signum(self) }
420 /// Returns `true` if `self`'s sign bit is positive, including
421 /// `+0.0` and `INFINITY`.
426 /// let nan = f32::NAN;
428 /// let g = -7.0_f32;
430 /// assert!(f.is_sign_positive());
431 /// assert!(!g.is_sign_positive());
432 /// // Requires both tests to determine if is `NaN`
433 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
435 #[stable(feature = "rust1", since = "1.0.0")]
437 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
439 /// Returns `true` if `self`'s sign is negative, including `-0.0`
440 /// and `NEG_INFINITY`.
445 /// let nan = f32::NAN;
449 /// assert!(!f.is_sign_negative());
450 /// assert!(g.is_sign_negative());
451 /// // Requires both tests to determine if is `NaN`.
452 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
454 #[stable(feature = "rust1", since = "1.0.0")]
456 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
458 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
459 /// error. This produces a more accurate result with better performance than
460 /// a separate multiplication operation followed by an add.
465 /// let m = 10.0_f32;
467 /// let b = 60.0_f32;
470 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
472 /// assert!(abs_difference <= f32::EPSILON);
474 #[stable(feature = "rust1", since = "1.0.0")]
476 pub fn mul_add(self, a: f32, b: f32) -> f32 {
477 unsafe { intrinsics::fmaf32(self, a, b) }
480 /// Takes the reciprocal (inverse) of a number, `1/x`.
486 /// let abs_difference = (x.recip() - (1.0/x)).abs();
488 /// assert!(abs_difference <= f32::EPSILON);
490 #[stable(feature = "rust1", since = "1.0.0")]
492 pub fn recip(self) -> f32 { num::Float::recip(self) }
494 /// Raises a number to an integer power.
496 /// Using this function is generally faster than using `powf`
502 /// let abs_difference = (x.powi(2) - x*x).abs();
504 /// assert!(abs_difference <= f32::EPSILON);
506 #[stable(feature = "rust1", since = "1.0.0")]
508 pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
510 /// Raises a number to a floating point power.
516 /// let abs_difference = (x.powf(2.0) - x*x).abs();
518 /// assert!(abs_difference <= f32::EPSILON);
520 #[stable(feature = "rust1", since = "1.0.0")]
522 pub fn powf(self, n: f32) -> f32 {
523 // see notes above in `floor`
524 #[cfg(target_env = "msvc")]
525 return (self as f64).powf(n as f64) as f32;
526 #[cfg(not(target_env = "msvc"))]
527 return unsafe { intrinsics::powf32(self, n) };
530 /// Takes the square root of a number.
532 /// Returns NaN if `self` is a negative number.
537 /// let positive = 4.0_f32;
538 /// let negative = -4.0_f32;
540 /// let abs_difference = (positive.sqrt() - 2.0).abs();
542 /// assert!(abs_difference <= f32::EPSILON);
543 /// assert!(negative.sqrt().is_nan());
545 #[stable(feature = "rust1", since = "1.0.0")]
547 pub fn sqrt(self) -> f32 {
551 unsafe { intrinsics::sqrtf32(self) }
555 /// Returns `e^(self)`, (the exponential function).
560 /// let one = 1.0f32;
562 /// let e = one.exp();
564 /// // ln(e) - 1 == 0
565 /// let abs_difference = (e.ln() - 1.0).abs();
567 /// assert!(abs_difference <= f32::EPSILON);
569 #[stable(feature = "rust1", since = "1.0.0")]
571 pub fn exp(self) -> f32 {
572 // see notes above in `floor`
573 #[cfg(target_env = "msvc")]
574 return (self as f64).exp() as f32;
575 #[cfg(not(target_env = "msvc"))]
576 return unsafe { intrinsics::expf32(self) };
579 /// Returns `2^(self)`.
587 /// let abs_difference = (f.exp2() - 4.0).abs();
589 /// assert!(abs_difference <= f32::EPSILON);
591 #[stable(feature = "rust1", since = "1.0.0")]
593 pub fn exp2(self) -> f32 {
594 unsafe { intrinsics::exp2f32(self) }
597 /// Returns the natural logarithm of the number.
602 /// let one = 1.0f32;
604 /// let e = one.exp();
606 /// // ln(e) - 1 == 0
607 /// let abs_difference = (e.ln() - 1.0).abs();
609 /// assert!(abs_difference <= f32::EPSILON);
611 #[stable(feature = "rust1", since = "1.0.0")]
613 pub fn ln(self) -> f32 {
614 // see notes above in `floor`
615 #[cfg(target_env = "msvc")]
616 return (self as f64).ln() as f32;
617 #[cfg(not(target_env = "msvc"))]
618 return unsafe { intrinsics::logf32(self) };
621 /// Returns the logarithm of the number with respect to an arbitrary base.
626 /// let ten = 10.0f32;
627 /// let two = 2.0f32;
629 /// // log10(10) - 1 == 0
630 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
632 /// // log2(2) - 1 == 0
633 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
635 /// assert!(abs_difference_10 <= f32::EPSILON);
636 /// assert!(abs_difference_2 <= f32::EPSILON);
638 #[stable(feature = "rust1", since = "1.0.0")]
640 pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
642 /// Returns the base 2 logarithm of the number.
647 /// let two = 2.0f32;
649 /// // log2(2) - 1 == 0
650 /// let abs_difference = (two.log2() - 1.0).abs();
652 /// assert!(abs_difference <= f32::EPSILON);
654 #[stable(feature = "rust1", since = "1.0.0")]
656 pub fn log2(self) -> f32 {
657 #[cfg(target_os = "android")]
658 return ::sys::android::log2f32(self);
659 #[cfg(not(target_os = "android"))]
660 return unsafe { intrinsics::log2f32(self) };
663 /// Returns the base 10 logarithm of the number.
668 /// let ten = 10.0f32;
670 /// // log10(10) - 1 == 0
671 /// let abs_difference = (ten.log10() - 1.0).abs();
673 /// assert!(abs_difference <= f32::EPSILON);
675 #[stable(feature = "rust1", since = "1.0.0")]
677 pub fn log10(self) -> f32 {
678 // see notes above in `floor`
679 #[cfg(target_env = "msvc")]
680 return (self as f64).log10() as f32;
681 #[cfg(not(target_env = "msvc"))]
682 return unsafe { intrinsics::log10f32(self) };
685 /// Converts radians to degrees.
688 /// use std::f32::{self, consts};
690 /// let angle = consts::PI;
692 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
694 /// assert!(abs_difference <= f32::EPSILON);
696 #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
698 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
700 /// Converts degrees to radians.
703 /// use std::f32::{self, consts};
705 /// let angle = 180.0f32;
707 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
709 /// assert!(abs_difference <= f32::EPSILON);
711 #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
713 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
715 /// Constructs a floating point number of `x*2^exp`.
718 /// #![feature(float_extras)]
721 /// // 3*2^2 - 12 == 0
722 /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
724 /// assert!(abs_difference <= f32::EPSILON);
726 #[unstable(feature = "float_extras",
727 reason = "pending integer conventions",
729 #[rustc_deprecated(since = "1.11.0",
730 reason = "never really came to fruition and easily \
731 implementable outside the standard library")]
733 pub fn ldexp(x: f32, exp: isize) -> f32 {
734 unsafe { cmath::ldexpf(x, exp as c_int) }
737 /// Breaks the number into a normalized fraction and a base-2 exponent,
740 /// * `self = x * 2^exp`
741 /// * `0.5 <= abs(x) < 1.0`
744 /// #![feature(float_extras)]
750 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
751 /// let f = x.frexp();
752 /// let abs_difference_0 = (f.0 - 0.5).abs();
753 /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
755 /// assert!(abs_difference_0 <= f32::EPSILON);
756 /// assert!(abs_difference_1 <= f32::EPSILON);
758 #[unstable(feature = "float_extras",
759 reason = "pending integer conventions",
761 #[rustc_deprecated(since = "1.11.0",
762 reason = "never really came to fruition and easily \
763 implementable outside the standard library")]
765 pub fn frexp(self) -> (f32, isize) {
768 let x = cmath::frexpf(self, &mut exp);
773 /// Returns the next representable floating-point value in the direction of
777 /// #![feature(float_extras)]
783 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
785 /// assert!(abs_diff <= f32::EPSILON);
787 #[unstable(feature = "float_extras",
788 reason = "unsure about its place in the world",
790 #[rustc_deprecated(since = "1.11.0",
791 reason = "never really came to fruition and easily \
792 implementable outside the standard library")]
794 pub fn next_after(self, other: f32) -> f32 {
795 unsafe { cmath::nextafterf(self, other) }
798 /// Returns the maximum of the two numbers.
804 /// assert_eq!(x.max(y), y);
807 /// If one of the arguments is NaN, then the other argument is returned.
808 #[stable(feature = "rust1", since = "1.0.0")]
810 pub fn max(self, other: f32) -> f32 {
811 unsafe { cmath::fmaxf(self, other) }
814 /// Returns the minimum of the two numbers.
820 /// assert_eq!(x.min(y), x);
823 /// If one of the arguments is NaN, then the other argument is returned.
824 #[stable(feature = "rust1", since = "1.0.0")]
826 pub fn min(self, other: f32) -> f32 {
827 unsafe { cmath::fminf(self, other) }
830 /// The positive difference of two numbers.
832 /// * If `self <= other`: `0:0`
833 /// * Else: `self - other`
841 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
842 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
844 /// assert!(abs_difference_x <= f32::EPSILON);
845 /// assert!(abs_difference_y <= f32::EPSILON);
847 #[stable(feature = "rust1", since = "1.0.0")]
849 #[rustc_deprecated(since = "1.10.0",
850 reason = "you probably meant `(self - other).abs()`: \
851 this operation is `(self - other).max(0.0)` (also \
852 known as `fdimf` in C). If you truly need the positive \
853 difference, consider using that expression or the C function \
854 `fdimf`, depending on how you wish to handle NaN (please consider \
855 filing an issue describing your use-case too).")]
856 pub fn abs_sub(self, other: f32) -> f32 {
857 unsafe { cmath::fdimf(self, other) }
860 /// Takes the cubic root of a number.
867 /// // x^(1/3) - 2 == 0
868 /// let abs_difference = (x.cbrt() - 2.0).abs();
870 /// assert!(abs_difference <= f32::EPSILON);
872 #[stable(feature = "rust1", since = "1.0.0")]
874 pub fn cbrt(self) -> f32 {
875 unsafe { cmath::cbrtf(self) }
878 /// Calculates the length of the hypotenuse of a right-angle triangle given
879 /// legs of length `x` and `y`.
887 /// // sqrt(x^2 + y^2)
888 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
890 /// assert!(abs_difference <= f32::EPSILON);
892 #[stable(feature = "rust1", since = "1.0.0")]
894 pub fn hypot(self, other: f32) -> f32 {
895 unsafe { cmath::hypotf(self, other) }
898 /// Computes the sine of a number (in radians).
903 /// let x = f32::consts::PI/2.0;
905 /// let abs_difference = (x.sin() - 1.0).abs();
907 /// assert!(abs_difference <= f32::EPSILON);
909 #[stable(feature = "rust1", since = "1.0.0")]
911 pub fn sin(self) -> f32 {
912 // see notes in `core::f32::Float::floor`
913 #[cfg(target_env = "msvc")]
914 return (self as f64).sin() as f32;
915 #[cfg(not(target_env = "msvc"))]
916 return unsafe { intrinsics::sinf32(self) };
919 /// Computes the cosine of a number (in radians).
924 /// let x = 2.0*f32::consts::PI;
926 /// let abs_difference = (x.cos() - 1.0).abs();
928 /// assert!(abs_difference <= f32::EPSILON);
930 #[stable(feature = "rust1", since = "1.0.0")]
932 pub fn cos(self) -> f32 {
933 // see notes in `core::f32::Float::floor`
934 #[cfg(target_env = "msvc")]
935 return (self as f64).cos() as f32;
936 #[cfg(not(target_env = "msvc"))]
937 return unsafe { intrinsics::cosf32(self) };
940 /// Computes the tangent of a number (in radians).
945 /// let x = f32::consts::PI / 4.0;
946 /// let abs_difference = (x.tan() - 1.0).abs();
948 /// assert!(abs_difference <= f32::EPSILON);
950 #[stable(feature = "rust1", since = "1.0.0")]
952 pub fn tan(self) -> f32 {
953 unsafe { cmath::tanf(self) }
956 /// Computes the arcsine of a number. Return value is in radians in
957 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
963 /// let f = f32::consts::PI / 2.0;
965 /// // asin(sin(pi/2))
966 /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
968 /// assert!(abs_difference <= f32::EPSILON);
970 #[stable(feature = "rust1", since = "1.0.0")]
972 pub fn asin(self) -> f32 {
973 unsafe { cmath::asinf(self) }
976 /// Computes the arccosine of a number. Return value is in radians in
977 /// the range [0, pi] or NaN if the number is outside the range
983 /// let f = f32::consts::PI / 4.0;
985 /// // acos(cos(pi/4))
986 /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
988 /// assert!(abs_difference <= f32::EPSILON);
990 #[stable(feature = "rust1", since = "1.0.0")]
992 pub fn acos(self) -> f32 {
993 unsafe { cmath::acosf(self) }
996 /// Computes the arctangent of a number. Return value is in radians in the
997 /// range [-pi/2, pi/2];
1005 /// let abs_difference = (f.tan().atan() - 1.0).abs();
1007 /// assert!(abs_difference <= f32::EPSILON);
1009 #[stable(feature = "rust1", since = "1.0.0")]
1011 pub fn atan(self) -> f32 {
1012 unsafe { cmath::atanf(self) }
1015 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
1017 /// * `x = 0`, `y = 0`: `0`
1018 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
1019 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
1020 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
1025 /// let pi = f32::consts::PI;
1026 /// // All angles from horizontal right (+x)
1027 /// // 45 deg counter-clockwise
1028 /// let x1 = 3.0f32;
1029 /// let y1 = -3.0f32;
1031 /// // 135 deg clockwise
1032 /// let x2 = -3.0f32;
1033 /// let y2 = 3.0f32;
1035 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
1036 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
1038 /// assert!(abs_difference_1 <= f32::EPSILON);
1039 /// assert!(abs_difference_2 <= f32::EPSILON);
1041 #[stable(feature = "rust1", since = "1.0.0")]
1043 pub fn atan2(self, other: f32) -> f32 {
1044 unsafe { cmath::atan2f(self, other) }
1047 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
1048 /// `(sin(x), cos(x))`.
1053 /// let x = f32::consts::PI/4.0;
1054 /// let f = x.sin_cos();
1056 /// let abs_difference_0 = (f.0 - x.sin()).abs();
1057 /// let abs_difference_1 = (f.1 - x.cos()).abs();
1059 /// assert!(abs_difference_0 <= f32::EPSILON);
1060 /// assert!(abs_difference_1 <= f32::EPSILON);
1062 #[stable(feature = "rust1", since = "1.0.0")]
1064 pub fn sin_cos(self) -> (f32, f32) {
1065 (self.sin(), self.cos())
1068 /// Returns `e^(self) - 1` in a way that is accurate even if the
1069 /// number is close to zero.
1076 /// // e^(ln(6)) - 1
1077 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
1079 /// assert!(abs_difference <= f32::EPSILON);
1081 #[stable(feature = "rust1", since = "1.0.0")]
1083 pub fn exp_m1(self) -> f32 {
1084 unsafe { cmath::expm1f(self) }
1087 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
1088 /// the operations were performed separately.
1093 /// let x = f32::consts::E - 1.0;
1095 /// // ln(1 + (e - 1)) == ln(e) == 1
1096 /// let abs_difference = (x.ln_1p() - 1.0).abs();
1098 /// assert!(abs_difference <= f32::EPSILON);
1100 #[stable(feature = "rust1", since = "1.0.0")]
1102 pub fn ln_1p(self) -> f32 {
1103 unsafe { cmath::log1pf(self) }
1106 /// Hyperbolic sine function.
1111 /// let e = f32::consts::E;
1114 /// let f = x.sinh();
1115 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
1116 /// let g = (e*e - 1.0)/(2.0*e);
1117 /// let abs_difference = (f - g).abs();
1119 /// assert!(abs_difference <= f32::EPSILON);
1121 #[stable(feature = "rust1", since = "1.0.0")]
1123 pub fn sinh(self) -> f32 {
1124 unsafe { cmath::sinhf(self) }
1127 /// Hyperbolic cosine function.
1132 /// let e = f32::consts::E;
1134 /// let f = x.cosh();
1135 /// // Solving cosh() at 1 gives this result
1136 /// let g = (e*e + 1.0)/(2.0*e);
1137 /// let abs_difference = (f - g).abs();
1140 /// assert!(abs_difference <= f32::EPSILON);
1142 #[stable(feature = "rust1", since = "1.0.0")]
1144 pub fn cosh(self) -> f32 {
1145 unsafe { cmath::coshf(self) }
1148 /// Hyperbolic tangent function.
1153 /// let e = f32::consts::E;
1156 /// let f = x.tanh();
1157 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1158 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1159 /// let abs_difference = (f - g).abs();
1161 /// assert!(abs_difference <= f32::EPSILON);
1163 #[stable(feature = "rust1", since = "1.0.0")]
1165 pub fn tanh(self) -> f32 {
1166 unsafe { cmath::tanhf(self) }
1169 /// Inverse hyperbolic sine function.
1175 /// let f = x.sinh().asinh();
1177 /// let abs_difference = (f - x).abs();
1179 /// assert!(abs_difference <= f32::EPSILON);
1181 #[stable(feature = "rust1", since = "1.0.0")]
1183 pub fn asinh(self) -> f32 {
1184 if self == NEG_INFINITY {
1187 (self + ((self * self) + 1.0).sqrt()).ln()
1191 /// Inverse hyperbolic cosine function.
1197 /// let f = x.cosh().acosh();
1199 /// let abs_difference = (f - x).abs();
1201 /// assert!(abs_difference <= f32::EPSILON);
1203 #[stable(feature = "rust1", since = "1.0.0")]
1205 pub fn acosh(self) -> f32 {
1207 x if x < 1.0 => ::f32::NAN,
1208 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1212 /// Inverse hyperbolic tangent function.
1217 /// let e = f32::consts::E;
1218 /// let f = e.tanh().atanh();
1220 /// let abs_difference = (f - e).abs();
1222 /// assert!(abs_difference <= 1e-5);
1224 #[stable(feature = "rust1", since = "1.0.0")]
1226 pub fn atanh(self) -> f32 {
1227 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1230 /// Raw transmutation to `u32`.
1232 /// Converts the `f32` into its raw memory representation,
1233 /// similar to the `transmute` function.
1235 /// Note that this function is distinct from casting.
1240 /// #![feature(float_bits_conv)]
1241 /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
1242 /// assert_eq!((12.5f32).to_bits(), 0x41480000);
1245 #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "40470")]
1247 pub fn to_bits(self) -> u32 {
1248 unsafe { ::mem::transmute(self) }
1251 /// Raw transmutation from `u32`.
1253 /// Converts the given `u32` containing the float's raw memory
1254 /// representation into the `f32` type, similar to the
1255 /// `transmute` function.
1257 /// There is only one difference to a bare `transmute`:
1258 /// Due to the implications onto Rust's safety promises being
1259 /// uncertain, if the representation of a signaling NaN "sNaN" float
1260 /// is passed to the function, the implementation is allowed to
1261 /// return a quiet NaN instead.
1263 /// Note that this function is distinct from casting.
1268 /// #![feature(float_bits_conv)]
1270 /// let v = f32::from_bits(0x41480000);
1271 /// let difference = (v - 12.5).abs();
1272 /// assert!(difference <= 1e-5);
1273 /// // Example for a signaling NaN value:
1274 /// let snan = 0x7F800001;
1275 /// assert_ne!(f32::from_bits(snan).to_bits(), snan);
1277 #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "40470")]
1279 pub fn from_bits(mut v: u32) -> Self {
1280 const EXP_MASK: u32 = 0x7F800000;
1281 const QNAN_MASK: u32 = 0x00400000;
1282 const FRACT_MASK: u32 = 0x007FFFFF;
1283 if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1284 // If we have a NaN value, we
1285 // convert signaling NaN values to quiet NaN
1286 // by setting the the highest bit of the fraction
1289 unsafe { ::mem::transmute(v) }
1298 use num::FpCategory as Fp;
1302 test_num(10f32, 2f32);
1307 assert_eq!(NAN.min(2.0), 2.0);
1308 assert_eq!(2.0f32.min(NAN), 2.0);
1313 assert_eq!(NAN.max(2.0), 2.0);
1314 assert_eq!(2.0f32.max(NAN), 2.0);
1319 let nan: f32 = f32::NAN;
1320 assert!(nan.is_nan());
1321 assert!(!nan.is_infinite());
1322 assert!(!nan.is_finite());
1323 assert!(!nan.is_normal());
1324 assert!(!nan.is_sign_positive());
1325 assert!(!nan.is_sign_negative());
1326 assert_eq!(Fp::Nan, nan.classify());
1330 fn test_infinity() {
1331 let inf: f32 = f32::INFINITY;
1332 assert!(inf.is_infinite());
1333 assert!(!inf.is_finite());
1334 assert!(inf.is_sign_positive());
1335 assert!(!inf.is_sign_negative());
1336 assert!(!inf.is_nan());
1337 assert!(!inf.is_normal());
1338 assert_eq!(Fp::Infinite, inf.classify());
1342 fn test_neg_infinity() {
1343 let neg_inf: f32 = f32::NEG_INFINITY;
1344 assert!(neg_inf.is_infinite());
1345 assert!(!neg_inf.is_finite());
1346 assert!(!neg_inf.is_sign_positive());
1347 assert!(neg_inf.is_sign_negative());
1348 assert!(!neg_inf.is_nan());
1349 assert!(!neg_inf.is_normal());
1350 assert_eq!(Fp::Infinite, neg_inf.classify());
1355 let zero: f32 = 0.0f32;
1356 assert_eq!(0.0, zero);
1357 assert!(!zero.is_infinite());
1358 assert!(zero.is_finite());
1359 assert!(zero.is_sign_positive());
1360 assert!(!zero.is_sign_negative());
1361 assert!(!zero.is_nan());
1362 assert!(!zero.is_normal());
1363 assert_eq!(Fp::Zero, zero.classify());
1367 fn test_neg_zero() {
1368 let neg_zero: f32 = -0.0;
1369 assert_eq!(0.0, neg_zero);
1370 assert!(!neg_zero.is_infinite());
1371 assert!(neg_zero.is_finite());
1372 assert!(!neg_zero.is_sign_positive());
1373 assert!(neg_zero.is_sign_negative());
1374 assert!(!neg_zero.is_nan());
1375 assert!(!neg_zero.is_normal());
1376 assert_eq!(Fp::Zero, neg_zero.classify());
1381 let one: f32 = 1.0f32;
1382 assert_eq!(1.0, one);
1383 assert!(!one.is_infinite());
1384 assert!(one.is_finite());
1385 assert!(one.is_sign_positive());
1386 assert!(!one.is_sign_negative());
1387 assert!(!one.is_nan());
1388 assert!(one.is_normal());
1389 assert_eq!(Fp::Normal, one.classify());
1394 let nan: f32 = f32::NAN;
1395 let inf: f32 = f32::INFINITY;
1396 let neg_inf: f32 = f32::NEG_INFINITY;
1397 assert!(nan.is_nan());
1398 assert!(!0.0f32.is_nan());
1399 assert!(!5.3f32.is_nan());
1400 assert!(!(-10.732f32).is_nan());
1401 assert!(!inf.is_nan());
1402 assert!(!neg_inf.is_nan());
1406 fn test_is_infinite() {
1407 let nan: f32 = f32::NAN;
1408 let inf: f32 = f32::INFINITY;
1409 let neg_inf: f32 = f32::NEG_INFINITY;
1410 assert!(!nan.is_infinite());
1411 assert!(inf.is_infinite());
1412 assert!(neg_inf.is_infinite());
1413 assert!(!0.0f32.is_infinite());
1414 assert!(!42.8f32.is_infinite());
1415 assert!(!(-109.2f32).is_infinite());
1419 fn test_is_finite() {
1420 let nan: f32 = f32::NAN;
1421 let inf: f32 = f32::INFINITY;
1422 let neg_inf: f32 = f32::NEG_INFINITY;
1423 assert!(!nan.is_finite());
1424 assert!(!inf.is_finite());
1425 assert!(!neg_inf.is_finite());
1426 assert!(0.0f32.is_finite());
1427 assert!(42.8f32.is_finite());
1428 assert!((-109.2f32).is_finite());
1432 fn test_is_normal() {
1433 let nan: f32 = f32::NAN;
1434 let inf: f32 = f32::INFINITY;
1435 let neg_inf: f32 = f32::NEG_INFINITY;
1436 let zero: f32 = 0.0f32;
1437 let neg_zero: f32 = -0.0;
1438 assert!(!nan.is_normal());
1439 assert!(!inf.is_normal());
1440 assert!(!neg_inf.is_normal());
1441 assert!(!zero.is_normal());
1442 assert!(!neg_zero.is_normal());
1443 assert!(1f32.is_normal());
1444 assert!(1e-37f32.is_normal());
1445 assert!(!1e-38f32.is_normal());
1449 fn test_classify() {
1450 let nan: f32 = f32::NAN;
1451 let inf: f32 = f32::INFINITY;
1452 let neg_inf: f32 = f32::NEG_INFINITY;
1453 let zero: f32 = 0.0f32;
1454 let neg_zero: f32 = -0.0;
1455 assert_eq!(nan.classify(), Fp::Nan);
1456 assert_eq!(inf.classify(), Fp::Infinite);
1457 assert_eq!(neg_inf.classify(), Fp::Infinite);
1458 assert_eq!(zero.classify(), Fp::Zero);
1459 assert_eq!(neg_zero.classify(), Fp::Zero);
1460 assert_eq!(1f32.classify(), Fp::Normal);
1461 assert_eq!(1e-37f32.classify(), Fp::Normal);
1462 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1466 #[allow(deprecated)]
1467 fn test_integer_decode() {
1468 assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
1469 assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
1470 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
1471 assert_eq!(0f32.integer_decode(), (0, -150, 1));
1472 assert_eq!((-0f32).integer_decode(), (0, -150, -1));
1473 assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
1474 assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
1476 // Ignore the "sign" (quiet / signalling flag) of NAN.
1477 // It can vary between runtime operations and LLVM folding.
1478 let (nan_m, nan_e, _nan_s) = NAN.integer_decode();
1479 assert_eq!((nan_m, nan_e), (12582912, 105));
1484 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1485 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1486 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1487 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1488 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1489 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1490 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1491 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1492 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1493 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1498 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1499 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1500 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1501 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1502 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1503 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1504 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1505 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1506 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1507 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1512 assert_approx_eq!(1.0f32.round(), 1.0f32);
1513 assert_approx_eq!(1.3f32.round(), 1.0f32);
1514 assert_approx_eq!(1.5f32.round(), 2.0f32);
1515 assert_approx_eq!(1.7f32.round(), 2.0f32);
1516 assert_approx_eq!(0.0f32.round(), 0.0f32);
1517 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1518 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1519 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1520 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1521 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1526 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1527 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1528 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1529 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1530 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1531 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1532 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1533 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1534 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1535 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1540 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1541 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1542 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1543 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1544 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1545 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1546 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1547 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1548 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1549 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1554 assert_eq!(INFINITY.abs(), INFINITY);
1555 assert_eq!(1f32.abs(), 1f32);
1556 assert_eq!(0f32.abs(), 0f32);
1557 assert_eq!((-0f32).abs(), 0f32);
1558 assert_eq!((-1f32).abs(), 1f32);
1559 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1560 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1561 assert!(NAN.abs().is_nan());
1566 assert_eq!(INFINITY.signum(), 1f32);
1567 assert_eq!(1f32.signum(), 1f32);
1568 assert_eq!(0f32.signum(), 1f32);
1569 assert_eq!((-0f32).signum(), -1f32);
1570 assert_eq!((-1f32).signum(), -1f32);
1571 assert_eq!(NEG_INFINITY.signum(), -1f32);
1572 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1573 assert!(NAN.signum().is_nan());
1577 fn test_is_sign_positive() {
1578 assert!(INFINITY.is_sign_positive());
1579 assert!(1f32.is_sign_positive());
1580 assert!(0f32.is_sign_positive());
1581 assert!(!(-0f32).is_sign_positive());
1582 assert!(!(-1f32).is_sign_positive());
1583 assert!(!NEG_INFINITY.is_sign_positive());
1584 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1585 assert!(!NAN.is_sign_positive());
1589 fn test_is_sign_negative() {
1590 assert!(!INFINITY.is_sign_negative());
1591 assert!(!1f32.is_sign_negative());
1592 assert!(!0f32.is_sign_negative());
1593 assert!((-0f32).is_sign_negative());
1594 assert!((-1f32).is_sign_negative());
1595 assert!(NEG_INFINITY.is_sign_negative());
1596 assert!((1f32/NEG_INFINITY).is_sign_negative());
1597 assert!(!NAN.is_sign_negative());
1602 let nan: f32 = f32::NAN;
1603 let inf: f32 = f32::INFINITY;
1604 let neg_inf: f32 = f32::NEG_INFINITY;
1605 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1606 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1607 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1608 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1609 assert!(nan.mul_add(7.8, 9.0).is_nan());
1610 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1611 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1612 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1613 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1618 let nan: f32 = f32::NAN;
1619 let inf: f32 = f32::INFINITY;
1620 let neg_inf: f32 = f32::NEG_INFINITY;
1621 assert_eq!(1.0f32.recip(), 1.0);
1622 assert_eq!(2.0f32.recip(), 0.5);
1623 assert_eq!((-0.4f32).recip(), -2.5);
1624 assert_eq!(0.0f32.recip(), inf);
1625 assert!(nan.recip().is_nan());
1626 assert_eq!(inf.recip(), 0.0);
1627 assert_eq!(neg_inf.recip(), 0.0);
1632 let nan: f32 = f32::NAN;
1633 let inf: f32 = f32::INFINITY;
1634 let neg_inf: f32 = f32::NEG_INFINITY;
1635 assert_eq!(1.0f32.powi(1), 1.0);
1636 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1637 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1638 assert_eq!(8.3f32.powi(0), 1.0);
1639 assert!(nan.powi(2).is_nan());
1640 assert_eq!(inf.powi(3), inf);
1641 assert_eq!(neg_inf.powi(2), inf);
1646 let nan: f32 = f32::NAN;
1647 let inf: f32 = f32::INFINITY;
1648 let neg_inf: f32 = f32::NEG_INFINITY;
1649 assert_eq!(1.0f32.powf(1.0), 1.0);
1650 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1651 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1652 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1653 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1654 assert_eq!(8.3f32.powf(0.0), 1.0);
1655 assert!(nan.powf(2.0).is_nan());
1656 assert_eq!(inf.powf(2.0), inf);
1657 assert_eq!(neg_inf.powf(3.0), neg_inf);
1661 fn test_sqrt_domain() {
1662 assert!(NAN.sqrt().is_nan());
1663 assert!(NEG_INFINITY.sqrt().is_nan());
1664 assert!((-1.0f32).sqrt().is_nan());
1665 assert_eq!((-0.0f32).sqrt(), -0.0);
1666 assert_eq!(0.0f32.sqrt(), 0.0);
1667 assert_eq!(1.0f32.sqrt(), 1.0);
1668 assert_eq!(INFINITY.sqrt(), INFINITY);
1673 assert_eq!(1.0, 0.0f32.exp());
1674 assert_approx_eq!(2.718282, 1.0f32.exp());
1675 assert_approx_eq!(148.413162, 5.0f32.exp());
1677 let inf: f32 = f32::INFINITY;
1678 let neg_inf: f32 = f32::NEG_INFINITY;
1679 let nan: f32 = f32::NAN;
1680 assert_eq!(inf, inf.exp());
1681 assert_eq!(0.0, neg_inf.exp());
1682 assert!(nan.exp().is_nan());
1687 assert_eq!(32.0, 5.0f32.exp2());
1688 assert_eq!(1.0, 0.0f32.exp2());
1690 let inf: f32 = f32::INFINITY;
1691 let neg_inf: f32 = f32::NEG_INFINITY;
1692 let nan: f32 = f32::NAN;
1693 assert_eq!(inf, inf.exp2());
1694 assert_eq!(0.0, neg_inf.exp2());
1695 assert!(nan.exp2().is_nan());
1700 let nan: f32 = f32::NAN;
1701 let inf: f32 = f32::INFINITY;
1702 let neg_inf: f32 = f32::NEG_INFINITY;
1703 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1704 assert!(nan.ln().is_nan());
1705 assert_eq!(inf.ln(), inf);
1706 assert!(neg_inf.ln().is_nan());
1707 assert!((-2.3f32).ln().is_nan());
1708 assert_eq!((-0.0f32).ln(), neg_inf);
1709 assert_eq!(0.0f32.ln(), neg_inf);
1710 assert_approx_eq!(4.0f32.ln(), 1.386294);
1715 let nan: f32 = f32::NAN;
1716 let inf: f32 = f32::INFINITY;
1717 let neg_inf: f32 = f32::NEG_INFINITY;
1718 assert_eq!(10.0f32.log(10.0), 1.0);
1719 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1720 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1721 assert!(1.0f32.log(1.0).is_nan());
1722 assert!(1.0f32.log(-13.9).is_nan());
1723 assert!(nan.log(2.3).is_nan());
1724 assert_eq!(inf.log(10.0), inf);
1725 assert!(neg_inf.log(8.8).is_nan());
1726 assert!((-2.3f32).log(0.1).is_nan());
1727 assert_eq!((-0.0f32).log(2.0), neg_inf);
1728 assert_eq!(0.0f32.log(7.0), neg_inf);
1733 let nan: f32 = f32::NAN;
1734 let inf: f32 = f32::INFINITY;
1735 let neg_inf: f32 = f32::NEG_INFINITY;
1736 assert_approx_eq!(10.0f32.log2(), 3.321928);
1737 assert_approx_eq!(2.3f32.log2(), 1.201634);
1738 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1739 assert!(nan.log2().is_nan());
1740 assert_eq!(inf.log2(), inf);
1741 assert!(neg_inf.log2().is_nan());
1742 assert!((-2.3f32).log2().is_nan());
1743 assert_eq!((-0.0f32).log2(), neg_inf);
1744 assert_eq!(0.0f32.log2(), neg_inf);
1749 let nan: f32 = f32::NAN;
1750 let inf: f32 = f32::INFINITY;
1751 let neg_inf: f32 = f32::NEG_INFINITY;
1752 assert_eq!(10.0f32.log10(), 1.0);
1753 assert_approx_eq!(2.3f32.log10(), 0.361728);
1754 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1755 assert_eq!(1.0f32.log10(), 0.0);
1756 assert!(nan.log10().is_nan());
1757 assert_eq!(inf.log10(), inf);
1758 assert!(neg_inf.log10().is_nan());
1759 assert!((-2.3f32).log10().is_nan());
1760 assert_eq!((-0.0f32).log10(), neg_inf);
1761 assert_eq!(0.0f32.log10(), neg_inf);
1765 fn test_to_degrees() {
1766 let pi: f32 = consts::PI;
1767 let nan: f32 = f32::NAN;
1768 let inf: f32 = f32::INFINITY;
1769 let neg_inf: f32 = f32::NEG_INFINITY;
1770 assert_eq!(0.0f32.to_degrees(), 0.0);
1771 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1772 assert_eq!(pi.to_degrees(), 180.0);
1773 assert!(nan.to_degrees().is_nan());
1774 assert_eq!(inf.to_degrees(), inf);
1775 assert_eq!(neg_inf.to_degrees(), neg_inf);
1779 fn test_to_radians() {
1780 let pi: f32 = consts::PI;
1781 let nan: f32 = f32::NAN;
1782 let inf: f32 = f32::INFINITY;
1783 let neg_inf: f32 = f32::NEG_INFINITY;
1784 assert_eq!(0.0f32.to_radians(), 0.0);
1785 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1786 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1787 assert_eq!(180.0f32.to_radians(), pi);
1788 assert!(nan.to_radians().is_nan());
1789 assert_eq!(inf.to_radians(), inf);
1790 assert_eq!(neg_inf.to_radians(), neg_inf);
1794 #[allow(deprecated)]
1796 let f1 = 2.0f32.powi(-123);
1797 let f2 = 2.0f32.powi(-111);
1798 let f3 = 1.75 * 2.0f32.powi(-12);
1799 assert_eq!(f32::ldexp(1f32, -123), f1);
1800 assert_eq!(f32::ldexp(1f32, -111), f2);
1801 assert_eq!(f32::ldexp(1.75f32, -12), f3);
1803 assert_eq!(f32::ldexp(0f32, -123), 0f32);
1804 assert_eq!(f32::ldexp(-0f32, -123), -0f32);
1806 let inf: f32 = f32::INFINITY;
1807 let neg_inf: f32 = f32::NEG_INFINITY;
1808 let nan: f32 = f32::NAN;
1809 assert_eq!(f32::ldexp(inf, -123), inf);
1810 assert_eq!(f32::ldexp(neg_inf, -123), neg_inf);
1811 assert!(f32::ldexp(nan, -123).is_nan());
1815 #[allow(deprecated)]
1817 let f1 = 2.0f32.powi(-123);
1818 let f2 = 2.0f32.powi(-111);
1819 let f3 = 1.75 * 2.0f32.powi(-123);
1820 let (x1, exp1) = f1.frexp();
1821 let (x2, exp2) = f2.frexp();
1822 let (x3, exp3) = f3.frexp();
1823 assert_eq!((x1, exp1), (0.5f32, -122));
1824 assert_eq!((x2, exp2), (0.5f32, -110));
1825 assert_eq!((x3, exp3), (0.875f32, -122));
1826 assert_eq!(f32::ldexp(x1, exp1), f1);
1827 assert_eq!(f32::ldexp(x2, exp2), f2);
1828 assert_eq!(f32::ldexp(x3, exp3), f3);
1830 assert_eq!(0f32.frexp(), (0f32, 0));
1831 assert_eq!((-0f32).frexp(), (-0f32, 0));
1834 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1835 #[allow(deprecated)]
1836 fn test_frexp_nowin() {
1837 let inf: f32 = f32::INFINITY;
1838 let neg_inf: f32 = f32::NEG_INFINITY;
1839 let nan: f32 = f32::NAN;
1840 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1841 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1842 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1847 assert_eq!(0.0f32.asinh(), 0.0f32);
1848 assert_eq!((-0.0f32).asinh(), -0.0f32);
1850 let inf: f32 = f32::INFINITY;
1851 let neg_inf: f32 = f32::NEG_INFINITY;
1852 let nan: f32 = f32::NAN;
1853 assert_eq!(inf.asinh(), inf);
1854 assert_eq!(neg_inf.asinh(), neg_inf);
1855 assert!(nan.asinh().is_nan());
1856 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1857 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1862 assert_eq!(1.0f32.acosh(), 0.0f32);
1863 assert!(0.999f32.acosh().is_nan());
1865 let inf: f32 = f32::INFINITY;
1866 let neg_inf: f32 = f32::NEG_INFINITY;
1867 let nan: f32 = f32::NAN;
1868 assert_eq!(inf.acosh(), inf);
1869 assert!(neg_inf.acosh().is_nan());
1870 assert!(nan.acosh().is_nan());
1871 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1872 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1877 assert_eq!(0.0f32.atanh(), 0.0f32);
1878 assert_eq!((-0.0f32).atanh(), -0.0f32);
1880 let inf32: f32 = f32::INFINITY;
1881 let neg_inf32: f32 = f32::NEG_INFINITY;
1882 assert_eq!(1.0f32.atanh(), inf32);
1883 assert_eq!((-1.0f32).atanh(), neg_inf32);
1885 assert!(2f64.atanh().atanh().is_nan());
1886 assert!((-2f64).atanh().atanh().is_nan());
1888 let inf64: f32 = f32::INFINITY;
1889 let neg_inf64: f32 = f32::NEG_INFINITY;
1890 let nan32: f32 = f32::NAN;
1891 assert!(inf64.atanh().is_nan());
1892 assert!(neg_inf64.atanh().is_nan());
1893 assert!(nan32.atanh().is_nan());
1895 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1896 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1900 fn test_real_consts() {
1903 let pi: f32 = consts::PI;
1904 let frac_pi_2: f32 = consts::FRAC_PI_2;
1905 let frac_pi_3: f32 = consts::FRAC_PI_3;
1906 let frac_pi_4: f32 = consts::FRAC_PI_4;
1907 let frac_pi_6: f32 = consts::FRAC_PI_6;
1908 let frac_pi_8: f32 = consts::FRAC_PI_8;
1909 let frac_1_pi: f32 = consts::FRAC_1_PI;
1910 let frac_2_pi: f32 = consts::FRAC_2_PI;
1911 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1912 let sqrt2: f32 = consts::SQRT_2;
1913 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1914 let e: f32 = consts::E;
1915 let log2_e: f32 = consts::LOG2_E;
1916 let log10_e: f32 = consts::LOG10_E;
1917 let ln_2: f32 = consts::LN_2;
1918 let ln_10: f32 = consts::LN_10;
1920 assert_approx_eq!(frac_pi_2, pi / 2f32);
1921 assert_approx_eq!(frac_pi_3, pi / 3f32);
1922 assert_approx_eq!(frac_pi_4, pi / 4f32);
1923 assert_approx_eq!(frac_pi_6, pi / 6f32);
1924 assert_approx_eq!(frac_pi_8, pi / 8f32);
1925 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1926 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1927 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1928 assert_approx_eq!(sqrt2, 2f32.sqrt());
1929 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1930 assert_approx_eq!(log2_e, e.log2());
1931 assert_approx_eq!(log10_e, e.log10());
1932 assert_approx_eq!(ln_2, 2f32.ln());
1933 assert_approx_eq!(ln_10, 10f32.ln());
1937 fn test_float_bits_conv() {
1938 assert_eq!((1f32).to_bits(), 0x3f800000);
1939 assert_eq!((12.5f32).to_bits(), 0x41480000);
1940 assert_eq!((1337f32).to_bits(), 0x44a72000);
1941 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1942 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1943 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1944 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1945 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1948 fn test_snan_masking() {
1949 let snan: u32 = 0x7F801337;
1950 const PAYLOAD_MASK: u32 = 0x003FFFFF;
1951 const QNAN_MASK: u32 = 0x00400000;
1952 let nan_masked_fl = f32::from_bits(snan);
1953 let nan_masked = nan_masked_fl.to_bits();
1954 // Ensure that signaling NaNs don't stay the same
1955 assert_ne!(nan_masked, snan);
1956 // Ensure that we have a quiet NaN
1957 assert_ne!(nan_masked & QNAN_MASK, 0);
1958 assert!(nan_masked_fl.is_nan());
1959 // Ensure the payload wasn't touched during conversion
1960 assert_eq!(nan_masked & PAYLOAD_MASK, snan & PAYLOAD_MASK);