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 //! The 32-bit floating point type.
13 //! *[See also the `f32` primitive type](../primitive.f32.html).*
15 #![stable(feature = "rust1", since = "1.0.0")]
16 #![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 fmaxf(a: c_float, b: c_float) -> c_float;
50 pub fn fminf(a: c_float, b: c_float) -> c_float;
51 pub fn fmodf(a: c_float, b: c_float) -> c_float;
52 pub fn ilogbf(n: c_float) -> c_int;
53 pub fn logbf(n: c_float) -> c_float;
54 pub fn log1pf(n: c_float) -> c_float;
55 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
56 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
57 pub fn tgammaf(n: c_float) -> c_float;
59 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
60 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
61 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
62 pub fn hypotf(x: c_float, y: c_float) -> c_float;
65 // See the comments in `core::float::Float::floor` for why MSVC is special
67 #[cfg(not(target_env = "msvc"))]
69 pub fn acosf(n: c_float) -> c_float;
70 pub fn asinf(n: c_float) -> c_float;
71 pub fn atan2f(a: c_float, b: c_float) -> c_float;
72 pub fn atanf(n: c_float) -> c_float;
73 pub fn coshf(n: c_float) -> c_float;
74 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
75 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
76 pub fn sinhf(n: c_float) -> c_float;
77 pub fn tanf(n: c_float) -> c_float;
78 pub fn tanhf(n: c_float) -> c_float;
81 #[cfg(target_env = "msvc")]
82 pub use self::shims::*;
83 #[cfg(target_env = "msvc")]
85 use libc::{c_float, c_int};
87 pub unsafe fn acosf(n: c_float) -> c_float {
88 f64::acos(n as f64) as c_float
91 pub unsafe fn asinf(n: c_float) -> c_float {
92 f64::asin(n as f64) as c_float
95 pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
96 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
103 pub unsafe fn coshf(n: c_float) -> c_float {
104 f64::cosh(n as f64) as c_float
107 pub unsafe fn frexpf(x: c_float, value: &mut c_int) -> c_float {
108 let (a, b) = f64::frexp(x as f64);
113 pub unsafe fn ldexpf(x: c_float, n: c_int) -> c_float {
114 f64::ldexp(x as f64, n as isize) as c_float
117 pub unsafe fn sinhf(n: c_float) -> c_float {
118 f64::sinh(n as f64) as c_float
121 pub unsafe fn tanf(n: c_float) -> c_float {
122 f64::tan(n as f64) as c_float
125 pub unsafe fn tanhf(n: c_float) -> c_float {
126 f64::tanh(n as f64) as c_float
134 /// Returns `true` if this value is `NaN` and false otherwise.
139 /// let nan = f32::NAN;
142 /// assert!(nan.is_nan());
143 /// assert!(!f.is_nan());
145 #[stable(feature = "rust1", since = "1.0.0")]
147 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
149 /// Returns `true` if this value is positive infinity or negative infinity and
156 /// let inf = f32::INFINITY;
157 /// let neg_inf = f32::NEG_INFINITY;
158 /// let nan = f32::NAN;
160 /// assert!(!f.is_infinite());
161 /// assert!(!nan.is_infinite());
163 /// assert!(inf.is_infinite());
164 /// assert!(neg_inf.is_infinite());
166 #[stable(feature = "rust1", since = "1.0.0")]
168 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
170 /// Returns `true` if this number is neither infinite nor `NaN`.
176 /// let inf = f32::INFINITY;
177 /// let neg_inf = f32::NEG_INFINITY;
178 /// let nan = f32::NAN;
180 /// assert!(f.is_finite());
182 /// assert!(!nan.is_finite());
183 /// assert!(!inf.is_finite());
184 /// assert!(!neg_inf.is_finite());
186 #[stable(feature = "rust1", since = "1.0.0")]
188 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
190 /// Returns `true` if the number is neither zero, infinite,
191 /// [subnormal][subnormal], or `NaN`.
196 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
197 /// let max = f32::MAX;
198 /// let lower_than_min = 1.0e-40_f32;
199 /// let zero = 0.0_f32;
201 /// assert!(min.is_normal());
202 /// assert!(max.is_normal());
204 /// assert!(!zero.is_normal());
205 /// assert!(!f32::NAN.is_normal());
206 /// assert!(!f32::INFINITY.is_normal());
207 /// // Values between `0` and `min` are Subnormal.
208 /// assert!(!lower_than_min.is_normal());
210 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
211 #[stable(feature = "rust1", since = "1.0.0")]
213 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
215 /// Returns the floating point category of the number. If only one property
216 /// is going to be tested, it is generally faster to use the specific
217 /// predicate instead.
220 /// use std::num::FpCategory;
223 /// let num = 12.4_f32;
224 /// let inf = f32::INFINITY;
226 /// assert_eq!(num.classify(), FpCategory::Normal);
227 /// assert_eq!(inf.classify(), FpCategory::Infinite);
229 #[stable(feature = "rust1", since = "1.0.0")]
231 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
233 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
234 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
235 /// The floating point encoding is documented in the [Reference][floating-point].
238 /// #![feature(float_extras)]
242 /// let num = 2.0f32;
244 /// // (8388608, -22, 1)
245 /// let (mantissa, exponent, sign) = num.integer_decode();
246 /// let sign_f = sign as f32;
247 /// let mantissa_f = mantissa as f32;
248 /// let exponent_f = num.powf(exponent as f32);
250 /// // 1 * 8388608 * 2^(-22) == 2
251 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
253 /// assert!(abs_difference <= f32::EPSILON);
255 /// [floating-point]: ../../../../../reference.html#machine-types
256 #[unstable(feature = "float_extras", reason = "signature is undecided",
259 pub fn integer_decode(self) -> (u64, i16, i8) {
260 num::Float::integer_decode(self)
263 /// Returns the largest integer less than or equal to a number.
266 /// let f = 3.99_f32;
269 /// assert_eq!(f.floor(), 3.0);
270 /// assert_eq!(g.floor(), 3.0);
272 #[stable(feature = "rust1", since = "1.0.0")]
274 pub fn floor(self) -> f32 {
277 // On MSVC LLVM will lower many math intrinsics to a call to the
278 // corresponding function. On MSVC, however, many of these functions
279 // aren't actually available as symbols to call, but rather they are all
280 // `static inline` functions in header files. This means that from a C
281 // perspective it's "compatible", but not so much from an ABI
282 // perspective (which we're worried about).
284 // The inline header functions always just cast to a f64 and do their
285 // operation, so we do that here as well, but only for MSVC targets.
287 // Note that there are many MSVC-specific float operations which
288 // redirect to this comment, so `floorf` is just one case of a missing
289 // function on MSVC, but there are many others elsewhere.
290 #[cfg(target_env = "msvc")]
291 fn floorf(f: f32) -> f32 { (f as f64).floor() as f32 }
292 #[cfg(not(target_env = "msvc"))]
293 fn floorf(f: f32) -> f32 { unsafe { intrinsics::floorf32(f) } }
296 /// Returns the smallest integer greater than or equal to a number.
299 /// let f = 3.01_f32;
302 /// assert_eq!(f.ceil(), 4.0);
303 /// assert_eq!(g.ceil(), 4.0);
305 #[stable(feature = "rust1", since = "1.0.0")]
307 pub fn ceil(self) -> f32 {
310 // see notes above in `floor`
311 #[cfg(target_env = "msvc")]
312 fn ceilf(f: f32) -> f32 { (f as f64).ceil() as f32 }
313 #[cfg(not(target_env = "msvc"))]
314 fn ceilf(f: f32) -> f32 { unsafe { intrinsics::ceilf32(f) } }
317 /// Returns the nearest integer to a number. Round half-way cases away from
322 /// let g = -3.3_f32;
324 /// assert_eq!(f.round(), 3.0);
325 /// assert_eq!(g.round(), -3.0);
327 #[stable(feature = "rust1", since = "1.0.0")]
329 pub fn round(self) -> f32 {
330 unsafe { intrinsics::roundf32(self) }
333 /// Returns the integer part of a number.
337 /// let g = -3.7_f32;
339 /// assert_eq!(f.trunc(), 3.0);
340 /// assert_eq!(g.trunc(), -3.0);
342 #[stable(feature = "rust1", since = "1.0.0")]
344 pub fn trunc(self) -> f32 {
345 unsafe { intrinsics::truncf32(self) }
348 /// Returns the fractional part of a number.
354 /// let y = -3.5_f32;
355 /// let abs_difference_x = (x.fract() - 0.5).abs();
356 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
358 /// assert!(abs_difference_x <= f32::EPSILON);
359 /// assert!(abs_difference_y <= f32::EPSILON);
361 #[stable(feature = "rust1", since = "1.0.0")]
363 pub fn fract(self) -> f32 { self - self.trunc() }
365 /// Computes the absolute value of `self`. Returns `NAN` if the
372 /// let y = -3.5_f32;
374 /// let abs_difference_x = (x.abs() - x).abs();
375 /// let abs_difference_y = (y.abs() - (-y)).abs();
377 /// assert!(abs_difference_x <= f32::EPSILON);
378 /// assert!(abs_difference_y <= f32::EPSILON);
380 /// assert!(f32::NAN.abs().is_nan());
382 #[stable(feature = "rust1", since = "1.0.0")]
384 pub fn abs(self) -> f32 { num::Float::abs(self) }
386 /// Returns a number that represents the sign of `self`.
388 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
389 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
390 /// - `NAN` if the number is `NAN`
397 /// assert_eq!(f.signum(), 1.0);
398 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
400 /// assert!(f32::NAN.signum().is_nan());
402 #[stable(feature = "rust1", since = "1.0.0")]
404 pub fn signum(self) -> f32 { num::Float::signum(self) }
406 /// Returns `true` if `self`'s sign bit is positive, including
407 /// `+0.0` and `INFINITY`.
412 /// let nan = f32::NAN;
414 /// let g = -7.0_f32;
416 /// assert!(f.is_sign_positive());
417 /// assert!(!g.is_sign_positive());
418 /// // Requires both tests to determine if is `NaN`
419 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
421 #[stable(feature = "rust1", since = "1.0.0")]
423 pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
425 /// Returns `true` if `self`'s sign is negative, including `-0.0`
426 /// and `NEG_INFINITY`.
431 /// let nan = f32::NAN;
435 /// assert!(!f.is_sign_negative());
436 /// assert!(g.is_sign_negative());
437 /// // Requires both tests to determine if is `NaN`.
438 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
440 #[stable(feature = "rust1", since = "1.0.0")]
442 pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
444 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
445 /// error. This produces a more accurate result with better performance than
446 /// a separate multiplication operation followed by an add.
451 /// let m = 10.0_f32;
453 /// let b = 60.0_f32;
456 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
458 /// assert!(abs_difference <= f32::EPSILON);
460 #[stable(feature = "rust1", since = "1.0.0")]
462 pub fn mul_add(self, a: f32, b: f32) -> f32 {
463 unsafe { intrinsics::fmaf32(self, a, b) }
466 /// Takes the reciprocal (inverse) of a number, `1/x`.
472 /// let abs_difference = (x.recip() - (1.0/x)).abs();
474 /// assert!(abs_difference <= f32::EPSILON);
476 #[stable(feature = "rust1", since = "1.0.0")]
478 pub fn recip(self) -> f32 { num::Float::recip(self) }
480 /// Raises a number to an integer power.
482 /// Using this function is generally faster than using `powf`
488 /// let abs_difference = (x.powi(2) - x*x).abs();
490 /// assert!(abs_difference <= f32::EPSILON);
492 #[stable(feature = "rust1", since = "1.0.0")]
494 pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
496 /// Raises a number to a floating point power.
502 /// let abs_difference = (x.powf(2.0) - x*x).abs();
504 /// assert!(abs_difference <= f32::EPSILON);
506 #[stable(feature = "rust1", since = "1.0.0")]
508 pub fn powf(self, n: f32) -> f32 {
509 return powf(self, n);
511 // see notes above in `floor`
512 #[cfg(target_env = "msvc")]
513 fn powf(f: f32, n: f32) -> f32 { (f as f64).powf(n as f64) as f32 }
514 #[cfg(not(target_env = "msvc"))]
515 fn powf(f: f32, n: f32) -> f32 { unsafe { intrinsics::powf32(f, n) } }
518 /// Takes the square root of a number.
520 /// Returns NaN if `self` is a negative number.
525 /// let positive = 4.0_f32;
526 /// let negative = -4.0_f32;
528 /// let abs_difference = (positive.sqrt() - 2.0).abs();
530 /// assert!(abs_difference <= f32::EPSILON);
531 /// assert!(negative.sqrt().is_nan());
533 #[stable(feature = "rust1", since = "1.0.0")]
535 pub fn sqrt(self) -> f32 {
539 unsafe { intrinsics::sqrtf32(self) }
543 /// Returns `e^(self)`, (the exponential function).
548 /// let one = 1.0f32;
550 /// let e = one.exp();
552 /// // ln(e) - 1 == 0
553 /// let abs_difference = (e.ln() - 1.0).abs();
555 /// assert!(abs_difference <= f32::EPSILON);
557 #[stable(feature = "rust1", since = "1.0.0")]
559 pub fn exp(self) -> f32 {
562 // see notes above in `floor`
563 #[cfg(target_env = "msvc")]
564 fn expf(f: f32) -> f32 { (f as f64).exp() as f32 }
565 #[cfg(not(target_env = "msvc"))]
566 fn expf(f: f32) -> f32 { unsafe { intrinsics::expf32(f) } }
569 /// Returns `2^(self)`.
577 /// let abs_difference = (f.exp2() - 4.0).abs();
579 /// assert!(abs_difference <= f32::EPSILON);
581 #[stable(feature = "rust1", since = "1.0.0")]
583 pub fn exp2(self) -> f32 {
584 unsafe { intrinsics::exp2f32(self) }
587 /// Returns the natural logarithm of the number.
592 /// let one = 1.0f32;
594 /// let e = one.exp();
596 /// // ln(e) - 1 == 0
597 /// let abs_difference = (e.ln() - 1.0).abs();
599 /// assert!(abs_difference <= f32::EPSILON);
601 #[stable(feature = "rust1", since = "1.0.0")]
603 pub fn ln(self) -> f32 {
606 // see notes above in `floor`
607 #[cfg(target_env = "msvc")]
608 fn logf(f: f32) -> f32 { (f as f64).ln() as f32 }
609 #[cfg(not(target_env = "msvc"))]
610 fn logf(f: f32) -> f32 { unsafe { intrinsics::logf32(f) } }
613 /// Returns the logarithm of the number with respect to an arbitrary base.
618 /// let ten = 10.0f32;
619 /// let two = 2.0f32;
621 /// // log10(10) - 1 == 0
622 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
624 /// // log2(2) - 1 == 0
625 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
627 /// assert!(abs_difference_10 <= f32::EPSILON);
628 /// assert!(abs_difference_2 <= f32::EPSILON);
630 #[stable(feature = "rust1", since = "1.0.0")]
632 pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
634 /// Returns the base 2 logarithm of the number.
639 /// let two = 2.0f32;
641 /// // log2(2) - 1 == 0
642 /// let abs_difference = (two.log2() - 1.0).abs();
644 /// assert!(abs_difference <= f32::EPSILON);
646 #[stable(feature = "rust1", since = "1.0.0")]
648 pub fn log2(self) -> f32 {
649 unsafe { intrinsics::log2f32(self) }
652 /// Returns the base 10 logarithm of the number.
657 /// let ten = 10.0f32;
659 /// // log10(10) - 1 == 0
660 /// let abs_difference = (ten.log10() - 1.0).abs();
662 /// assert!(abs_difference <= f32::EPSILON);
664 #[stable(feature = "rust1", since = "1.0.0")]
666 pub fn log10(self) -> f32 {
669 // see notes above in `floor`
670 #[cfg(target_env = "msvc")]
671 fn log10f(f: f32) -> f32 { (f as f64).log10() as f32 }
672 #[cfg(not(target_env = "msvc"))]
673 fn log10f(f: f32) -> f32 { unsafe { intrinsics::log10f32(f) } }
676 /// Converts radians to degrees.
679 /// #![feature(float_extras)]
681 /// use std::f32::{self, consts};
683 /// let angle = consts::PI;
685 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
687 /// assert!(abs_difference <= f32::EPSILON);
689 #[unstable(feature = "float_extras", reason = "desirability is unclear",
692 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
694 /// Converts degrees to radians.
697 /// #![feature(float_extras)]
699 /// use std::f32::{self, consts};
701 /// let angle = 180.0f32;
703 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
705 /// assert!(abs_difference <= f32::EPSILON);
707 #[unstable(feature = "float_extras", reason = "desirability is unclear",
710 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
712 /// Constructs a floating point number of `x*2^exp`.
715 /// #![feature(float_extras)]
718 /// // 3*2^2 - 12 == 0
719 /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
721 /// assert!(abs_difference <= f32::EPSILON);
723 #[unstable(feature = "float_extras",
724 reason = "pending integer conventions",
727 pub fn ldexp(x: f32, exp: isize) -> f32 {
728 unsafe { cmath::ldexpf(x, exp as c_int) }
731 /// Breaks the number into a normalized fraction and a base-2 exponent,
734 /// * `self = x * 2^exp`
735 /// * `0.5 <= abs(x) < 1.0`
738 /// #![feature(float_extras)]
744 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
745 /// let f = x.frexp();
746 /// let abs_difference_0 = (f.0 - 0.5).abs();
747 /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
749 /// assert!(abs_difference_0 <= f32::EPSILON);
750 /// assert!(abs_difference_1 <= f32::EPSILON);
752 #[unstable(feature = "float_extras",
753 reason = "pending integer conventions",
756 pub fn frexp(self) -> (f32, isize) {
759 let x = cmath::frexpf(self, &mut exp);
764 /// Returns the next representable floating-point value in the direction of
768 /// #![feature(float_extras)]
774 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
776 /// assert!(abs_diff <= f32::EPSILON);
778 #[unstable(feature = "float_extras",
779 reason = "unsure about its place in the world",
782 pub fn next_after(self, other: f32) -> f32 {
783 unsafe { cmath::nextafterf(self, other) }
786 /// Returns the maximum of the two numbers.
792 /// assert_eq!(x.max(y), y);
795 /// If one of the arguments is NaN, then the other argument is returned.
796 #[stable(feature = "rust1", since = "1.0.0")]
798 pub fn max(self, other: f32) -> f32 {
799 unsafe { cmath::fmaxf(self, other) }
802 /// Returns the minimum of the two numbers.
808 /// assert_eq!(x.min(y), x);
811 /// If one of the arguments is NaN, then the other argument is returned.
812 #[stable(feature = "rust1", since = "1.0.0")]
814 pub fn min(self, other: f32) -> f32 {
815 unsafe { cmath::fminf(self, other) }
818 /// The positive difference of two numbers.
820 /// * If `self <= other`: `0:0`
821 /// * Else: `self - other`
829 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
830 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
832 /// assert!(abs_difference_x <= f32::EPSILON);
833 /// assert!(abs_difference_y <= f32::EPSILON);
835 #[stable(feature = "rust1", since = "1.0.0")]
837 pub fn abs_sub(self, other: f32) -> f32 {
838 unsafe { cmath::fdimf(self, other) }
841 /// Takes the cubic root of a number.
848 /// // x^(1/3) - 2 == 0
849 /// let abs_difference = (x.cbrt() - 2.0).abs();
851 /// assert!(abs_difference <= f32::EPSILON);
853 #[stable(feature = "rust1", since = "1.0.0")]
855 pub fn cbrt(self) -> f32 {
856 unsafe { cmath::cbrtf(self) }
859 /// Calculates the length of the hypotenuse of a right-angle triangle given
860 /// legs of length `x` and `y`.
868 /// // sqrt(x^2 + y^2)
869 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
871 /// assert!(abs_difference <= f32::EPSILON);
873 #[stable(feature = "rust1", since = "1.0.0")]
875 pub fn hypot(self, other: f32) -> f32 {
876 unsafe { cmath::hypotf(self, other) }
879 /// Computes the sine of a number (in radians).
884 /// let x = f32::consts::PI/2.0;
886 /// let abs_difference = (x.sin() - 1.0).abs();
888 /// assert!(abs_difference <= f32::EPSILON);
890 #[stable(feature = "rust1", since = "1.0.0")]
892 pub fn sin(self) -> f32 {
895 // see notes in `core::f32::Float::floor`
896 #[cfg(target_env = "msvc")]
897 fn sinf(f: f32) -> f32 { (f as f64).sin() as f32 }
898 #[cfg(not(target_env = "msvc"))]
899 fn sinf(f: f32) -> f32 { unsafe { intrinsics::sinf32(f) } }
902 /// Computes the cosine of a number (in radians).
907 /// let x = 2.0*f32::consts::PI;
909 /// let abs_difference = (x.cos() - 1.0).abs();
911 /// assert!(abs_difference <= f32::EPSILON);
913 #[stable(feature = "rust1", since = "1.0.0")]
915 pub fn cos(self) -> f32 {
918 // see notes in `core::f32::Float::floor`
919 #[cfg(target_env = "msvc")]
920 fn cosf(f: f32) -> f32 { (f as f64).cos() as f32 }
921 #[cfg(not(target_env = "msvc"))]
922 fn cosf(f: f32) -> f32 { unsafe { intrinsics::cosf32(f) } }
925 /// Computes the tangent of a number (in radians).
930 /// let x = f64::consts::PI/4.0;
931 /// let abs_difference = (x.tan() - 1.0).abs();
933 /// assert!(abs_difference < 1e-10);
935 #[stable(feature = "rust1", since = "1.0.0")]
937 pub fn tan(self) -> f32 {
938 unsafe { cmath::tanf(self) }
941 /// Computes the arcsine of a number. Return value is in radians in
942 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
948 /// let f = f32::consts::PI / 2.0;
950 /// // asin(sin(pi/2))
951 /// let abs_difference = f.sin().asin().abs_sub(f32::consts::PI / 2.0);
953 /// assert!(abs_difference <= f32::EPSILON);
955 #[stable(feature = "rust1", since = "1.0.0")]
957 pub fn asin(self) -> f32 {
958 unsafe { cmath::asinf(self) }
961 /// Computes the arccosine of a number. Return value is in radians in
962 /// the range [0, pi] or NaN if the number is outside the range
968 /// let f = f32::consts::PI / 4.0;
970 /// // acos(cos(pi/4))
971 /// let abs_difference = f.cos().acos().abs_sub(f32::consts::PI / 4.0);
973 /// assert!(abs_difference <= f32::EPSILON);
975 #[stable(feature = "rust1", since = "1.0.0")]
977 pub fn acos(self) -> f32 {
978 unsafe { cmath::acosf(self) }
981 /// Computes the arctangent of a number. Return value is in radians in the
982 /// range [-pi/2, pi/2];
990 /// let abs_difference = f.tan().atan().abs_sub(1.0);
992 /// assert!(abs_difference <= f32::EPSILON);
994 #[stable(feature = "rust1", since = "1.0.0")]
996 pub fn atan(self) -> f32 {
997 unsafe { cmath::atanf(self) }
1000 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
1002 /// * `x = 0`, `y = 0`: `0`
1003 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
1004 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
1005 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
1010 /// let pi = f32::consts::PI;
1011 /// // All angles from horizontal right (+x)
1012 /// // 45 deg counter-clockwise
1013 /// let x1 = 3.0f32;
1014 /// let y1 = -3.0f32;
1016 /// // 135 deg clockwise
1017 /// let x2 = -3.0f32;
1018 /// let y2 = 3.0f32;
1020 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
1021 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
1023 /// assert!(abs_difference_1 <= f32::EPSILON);
1024 /// assert!(abs_difference_2 <= f32::EPSILON);
1026 #[stable(feature = "rust1", since = "1.0.0")]
1028 pub fn atan2(self, other: f32) -> f32 {
1029 unsafe { cmath::atan2f(self, other) }
1032 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
1033 /// `(sin(x), cos(x))`.
1038 /// let x = f32::consts::PI/4.0;
1039 /// let f = x.sin_cos();
1041 /// let abs_difference_0 = (f.0 - x.sin()).abs();
1042 /// let abs_difference_1 = (f.1 - x.cos()).abs();
1044 /// assert!(abs_difference_0 <= f32::EPSILON);
1045 /// assert!(abs_difference_0 <= f32::EPSILON);
1047 #[stable(feature = "rust1", since = "1.0.0")]
1049 pub fn sin_cos(self) -> (f32, f32) {
1050 (self.sin(), self.cos())
1053 /// Returns `e^(self) - 1` in a way that is accurate even if the
1054 /// number is close to zero.
1059 /// // e^(ln(7)) - 1
1060 /// let abs_difference = x.ln().exp_m1().abs_sub(6.0);
1062 /// assert!(abs_difference < 1e-10);
1064 #[stable(feature = "rust1", since = "1.0.0")]
1066 pub fn exp_m1(self) -> f32 {
1067 unsafe { cmath::expm1f(self) }
1070 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
1071 /// the operations were performed separately.
1076 /// let x = f32::consts::E - 1.0;
1078 /// // ln(1 + (e - 1)) == ln(e) == 1
1079 /// let abs_difference = (x.ln_1p() - 1.0).abs();
1081 /// assert!(abs_difference <= f32::EPSILON);
1083 #[stable(feature = "rust1", since = "1.0.0")]
1085 pub fn ln_1p(self) -> f32 {
1086 unsafe { cmath::log1pf(self) }
1089 /// Hyperbolic sine function.
1094 /// let e = f32::consts::E;
1097 /// let f = x.sinh();
1098 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
1099 /// let g = (e*e - 1.0)/(2.0*e);
1100 /// let abs_difference = (f - g).abs();
1102 /// assert!(abs_difference <= f32::EPSILON);
1104 #[stable(feature = "rust1", since = "1.0.0")]
1106 pub fn sinh(self) -> f32 {
1107 unsafe { cmath::sinhf(self) }
1110 /// Hyperbolic cosine function.
1115 /// let e = f32::consts::E;
1117 /// let f = x.cosh();
1118 /// // Solving cosh() at 1 gives this result
1119 /// let g = (e*e + 1.0)/(2.0*e);
1120 /// let abs_difference = f.abs_sub(g);
1123 /// assert!(abs_difference <= f32::EPSILON);
1125 #[stable(feature = "rust1", since = "1.0.0")]
1127 pub fn cosh(self) -> f32 {
1128 unsafe { cmath::coshf(self) }
1131 /// Hyperbolic tangent function.
1136 /// let e = f32::consts::E;
1139 /// let f = x.tanh();
1140 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1141 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1142 /// let abs_difference = (f - g).abs();
1144 /// assert!(abs_difference <= f32::EPSILON);
1146 #[stable(feature = "rust1", since = "1.0.0")]
1148 pub fn tanh(self) -> f32 {
1149 unsafe { cmath::tanhf(self) }
1152 /// Inverse hyperbolic sine function.
1158 /// let f = x.sinh().asinh();
1160 /// let abs_difference = (f - x).abs();
1162 /// assert!(abs_difference <= f32::EPSILON);
1164 #[stable(feature = "rust1", since = "1.0.0")]
1166 pub fn asinh(self) -> f32 {
1168 NEG_INFINITY => NEG_INFINITY,
1169 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1173 /// Inverse hyperbolic cosine function.
1179 /// let f = x.cosh().acosh();
1181 /// let abs_difference = (f - x).abs();
1183 /// assert!(abs_difference <= f32::EPSILON);
1185 #[stable(feature = "rust1", since = "1.0.0")]
1187 pub fn acosh(self) -> f32 {
1189 x if x < 1.0 => ::f32::NAN,
1190 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1194 /// Inverse hyperbolic tangent function.
1199 /// let e = f32::consts::E;
1200 /// let f = e.tanh().atanh();
1202 /// let abs_difference = f.abs_sub(e);
1204 /// assert!(abs_difference <= f32::EPSILON);
1206 #[stable(feature = "rust1", since = "1.0.0")]
1208 pub fn atanh(self) -> f32 {
1209 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1218 use num::FpCategory as Fp;
1222 test_num(10f32, 2f32);
1227 assert_eq!(NAN.min(2.0), 2.0);
1228 assert_eq!(2.0f32.min(NAN), 2.0);
1233 assert_eq!(NAN.max(2.0), 2.0);
1234 assert_eq!(2.0f32.max(NAN), 2.0);
1239 let nan: f32 = f32::NAN;
1240 assert!(nan.is_nan());
1241 assert!(!nan.is_infinite());
1242 assert!(!nan.is_finite());
1243 assert!(!nan.is_normal());
1244 assert!(!nan.is_sign_positive());
1245 assert!(!nan.is_sign_negative());
1246 assert_eq!(Fp::Nan, nan.classify());
1250 fn test_infinity() {
1251 let inf: f32 = f32::INFINITY;
1252 assert!(inf.is_infinite());
1253 assert!(!inf.is_finite());
1254 assert!(inf.is_sign_positive());
1255 assert!(!inf.is_sign_negative());
1256 assert!(!inf.is_nan());
1257 assert!(!inf.is_normal());
1258 assert_eq!(Fp::Infinite, inf.classify());
1262 fn test_neg_infinity() {
1263 let neg_inf: f32 = f32::NEG_INFINITY;
1264 assert!(neg_inf.is_infinite());
1265 assert!(!neg_inf.is_finite());
1266 assert!(!neg_inf.is_sign_positive());
1267 assert!(neg_inf.is_sign_negative());
1268 assert!(!neg_inf.is_nan());
1269 assert!(!neg_inf.is_normal());
1270 assert_eq!(Fp::Infinite, neg_inf.classify());
1275 let zero: f32 = 0.0f32;
1276 assert_eq!(0.0, zero);
1277 assert!(!zero.is_infinite());
1278 assert!(zero.is_finite());
1279 assert!(zero.is_sign_positive());
1280 assert!(!zero.is_sign_negative());
1281 assert!(!zero.is_nan());
1282 assert!(!zero.is_normal());
1283 assert_eq!(Fp::Zero, zero.classify());
1287 fn test_neg_zero() {
1288 let neg_zero: f32 = -0.0;
1289 assert_eq!(0.0, neg_zero);
1290 assert!(!neg_zero.is_infinite());
1291 assert!(neg_zero.is_finite());
1292 assert!(!neg_zero.is_sign_positive());
1293 assert!(neg_zero.is_sign_negative());
1294 assert!(!neg_zero.is_nan());
1295 assert!(!neg_zero.is_normal());
1296 assert_eq!(Fp::Zero, neg_zero.classify());
1301 let one: f32 = 1.0f32;
1302 assert_eq!(1.0, one);
1303 assert!(!one.is_infinite());
1304 assert!(one.is_finite());
1305 assert!(one.is_sign_positive());
1306 assert!(!one.is_sign_negative());
1307 assert!(!one.is_nan());
1308 assert!(one.is_normal());
1309 assert_eq!(Fp::Normal, one.classify());
1314 let nan: f32 = f32::NAN;
1315 let inf: f32 = f32::INFINITY;
1316 let neg_inf: f32 = f32::NEG_INFINITY;
1317 assert!(nan.is_nan());
1318 assert!(!0.0f32.is_nan());
1319 assert!(!5.3f32.is_nan());
1320 assert!(!(-10.732f32).is_nan());
1321 assert!(!inf.is_nan());
1322 assert!(!neg_inf.is_nan());
1326 fn test_is_infinite() {
1327 let nan: f32 = f32::NAN;
1328 let inf: f32 = f32::INFINITY;
1329 let neg_inf: f32 = f32::NEG_INFINITY;
1330 assert!(!nan.is_infinite());
1331 assert!(inf.is_infinite());
1332 assert!(neg_inf.is_infinite());
1333 assert!(!0.0f32.is_infinite());
1334 assert!(!42.8f32.is_infinite());
1335 assert!(!(-109.2f32).is_infinite());
1339 fn test_is_finite() {
1340 let nan: f32 = f32::NAN;
1341 let inf: f32 = f32::INFINITY;
1342 let neg_inf: f32 = f32::NEG_INFINITY;
1343 assert!(!nan.is_finite());
1344 assert!(!inf.is_finite());
1345 assert!(!neg_inf.is_finite());
1346 assert!(0.0f32.is_finite());
1347 assert!(42.8f32.is_finite());
1348 assert!((-109.2f32).is_finite());
1352 fn test_is_normal() {
1353 let nan: f32 = f32::NAN;
1354 let inf: f32 = f32::INFINITY;
1355 let neg_inf: f32 = f32::NEG_INFINITY;
1356 let zero: f32 = 0.0f32;
1357 let neg_zero: f32 = -0.0;
1358 assert!(!nan.is_normal());
1359 assert!(!inf.is_normal());
1360 assert!(!neg_inf.is_normal());
1361 assert!(!zero.is_normal());
1362 assert!(!neg_zero.is_normal());
1363 assert!(1f32.is_normal());
1364 assert!(1e-37f32.is_normal());
1365 assert!(!1e-38f32.is_normal());
1369 fn test_classify() {
1370 let nan: f32 = f32::NAN;
1371 let inf: f32 = f32::INFINITY;
1372 let neg_inf: f32 = f32::NEG_INFINITY;
1373 let zero: f32 = 0.0f32;
1374 let neg_zero: f32 = -0.0;
1375 assert_eq!(nan.classify(), Fp::Nan);
1376 assert_eq!(inf.classify(), Fp::Infinite);
1377 assert_eq!(neg_inf.classify(), Fp::Infinite);
1378 assert_eq!(zero.classify(), Fp::Zero);
1379 assert_eq!(neg_zero.classify(), Fp::Zero);
1380 assert_eq!(1f32.classify(), Fp::Normal);
1381 assert_eq!(1e-37f32.classify(), Fp::Normal);
1382 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1386 fn test_integer_decode() {
1387 assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
1388 assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
1389 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
1390 assert_eq!(0f32.integer_decode(), (0, -150, 1));
1391 assert_eq!((-0f32).integer_decode(), (0, -150, -1));
1392 assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
1393 assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
1394 assert_eq!(NAN.integer_decode(), (12582912, 105, 1));
1399 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1400 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1401 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1402 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1403 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1404 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1405 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1406 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1407 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1408 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1413 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1414 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1415 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1416 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1417 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1418 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1419 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1420 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1421 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1422 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1427 assert_approx_eq!(1.0f32.round(), 1.0f32);
1428 assert_approx_eq!(1.3f32.round(), 1.0f32);
1429 assert_approx_eq!(1.5f32.round(), 2.0f32);
1430 assert_approx_eq!(1.7f32.round(), 2.0f32);
1431 assert_approx_eq!(0.0f32.round(), 0.0f32);
1432 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1433 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1434 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1435 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1436 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1441 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1442 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1443 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1444 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1445 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1446 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1447 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1448 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1449 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1450 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1455 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1456 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1457 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1458 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1459 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1460 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1461 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1462 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1463 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1464 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1469 assert_eq!(INFINITY.abs(), INFINITY);
1470 assert_eq!(1f32.abs(), 1f32);
1471 assert_eq!(0f32.abs(), 0f32);
1472 assert_eq!((-0f32).abs(), 0f32);
1473 assert_eq!((-1f32).abs(), 1f32);
1474 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1475 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1476 assert!(NAN.abs().is_nan());
1481 assert_eq!(INFINITY.signum(), 1f32);
1482 assert_eq!(1f32.signum(), 1f32);
1483 assert_eq!(0f32.signum(), 1f32);
1484 assert_eq!((-0f32).signum(), -1f32);
1485 assert_eq!((-1f32).signum(), -1f32);
1486 assert_eq!(NEG_INFINITY.signum(), -1f32);
1487 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1488 assert!(NAN.signum().is_nan());
1492 fn test_is_sign_positive() {
1493 assert!(INFINITY.is_sign_positive());
1494 assert!(1f32.is_sign_positive());
1495 assert!(0f32.is_sign_positive());
1496 assert!(!(-0f32).is_sign_positive());
1497 assert!(!(-1f32).is_sign_positive());
1498 assert!(!NEG_INFINITY.is_sign_positive());
1499 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1500 assert!(!NAN.is_sign_positive());
1504 fn test_is_sign_negative() {
1505 assert!(!INFINITY.is_sign_negative());
1506 assert!(!1f32.is_sign_negative());
1507 assert!(!0f32.is_sign_negative());
1508 assert!((-0f32).is_sign_negative());
1509 assert!((-1f32).is_sign_negative());
1510 assert!(NEG_INFINITY.is_sign_negative());
1511 assert!((1f32/NEG_INFINITY).is_sign_negative());
1512 assert!(!NAN.is_sign_negative());
1517 let nan: f32 = f32::NAN;
1518 let inf: f32 = f32::INFINITY;
1519 let neg_inf: f32 = f32::NEG_INFINITY;
1520 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1521 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1522 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1523 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1524 assert!(nan.mul_add(7.8, 9.0).is_nan());
1525 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1526 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1527 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1528 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1533 let nan: f32 = f32::NAN;
1534 let inf: f32 = f32::INFINITY;
1535 let neg_inf: f32 = f32::NEG_INFINITY;
1536 assert_eq!(1.0f32.recip(), 1.0);
1537 assert_eq!(2.0f32.recip(), 0.5);
1538 assert_eq!((-0.4f32).recip(), -2.5);
1539 assert_eq!(0.0f32.recip(), inf);
1540 assert!(nan.recip().is_nan());
1541 assert_eq!(inf.recip(), 0.0);
1542 assert_eq!(neg_inf.recip(), 0.0);
1547 let nan: f32 = f32::NAN;
1548 let inf: f32 = f32::INFINITY;
1549 let neg_inf: f32 = f32::NEG_INFINITY;
1550 assert_eq!(1.0f32.powi(1), 1.0);
1551 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1552 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1553 assert_eq!(8.3f32.powi(0), 1.0);
1554 assert!(nan.powi(2).is_nan());
1555 assert_eq!(inf.powi(3), inf);
1556 assert_eq!(neg_inf.powi(2), inf);
1561 let nan: f32 = f32::NAN;
1562 let inf: f32 = f32::INFINITY;
1563 let neg_inf: f32 = f32::NEG_INFINITY;
1564 assert_eq!(1.0f32.powf(1.0), 1.0);
1565 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1566 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1567 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1568 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1569 assert_eq!(8.3f32.powf(0.0), 1.0);
1570 assert!(nan.powf(2.0).is_nan());
1571 assert_eq!(inf.powf(2.0), inf);
1572 assert_eq!(neg_inf.powf(3.0), neg_inf);
1576 fn test_sqrt_domain() {
1577 assert!(NAN.sqrt().is_nan());
1578 assert!(NEG_INFINITY.sqrt().is_nan());
1579 assert!((-1.0f32).sqrt().is_nan());
1580 assert_eq!((-0.0f32).sqrt(), -0.0);
1581 assert_eq!(0.0f32.sqrt(), 0.0);
1582 assert_eq!(1.0f32.sqrt(), 1.0);
1583 assert_eq!(INFINITY.sqrt(), INFINITY);
1588 assert_eq!(1.0, 0.0f32.exp());
1589 assert_approx_eq!(2.718282, 1.0f32.exp());
1590 assert_approx_eq!(148.413162, 5.0f32.exp());
1592 let inf: f32 = f32::INFINITY;
1593 let neg_inf: f32 = f32::NEG_INFINITY;
1594 let nan: f32 = f32::NAN;
1595 assert_eq!(inf, inf.exp());
1596 assert_eq!(0.0, neg_inf.exp());
1597 assert!(nan.exp().is_nan());
1602 assert_eq!(32.0, 5.0f32.exp2());
1603 assert_eq!(1.0, 0.0f32.exp2());
1605 let inf: f32 = f32::INFINITY;
1606 let neg_inf: f32 = f32::NEG_INFINITY;
1607 let nan: f32 = f32::NAN;
1608 assert_eq!(inf, inf.exp2());
1609 assert_eq!(0.0, neg_inf.exp2());
1610 assert!(nan.exp2().is_nan());
1615 let nan: f32 = f32::NAN;
1616 let inf: f32 = f32::INFINITY;
1617 let neg_inf: f32 = f32::NEG_INFINITY;
1618 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1619 assert!(nan.ln().is_nan());
1620 assert_eq!(inf.ln(), inf);
1621 assert!(neg_inf.ln().is_nan());
1622 assert!((-2.3f32).ln().is_nan());
1623 assert_eq!((-0.0f32).ln(), neg_inf);
1624 assert_eq!(0.0f32.ln(), neg_inf);
1625 assert_approx_eq!(4.0f32.ln(), 1.386294);
1630 let nan: f32 = f32::NAN;
1631 let inf: f32 = f32::INFINITY;
1632 let neg_inf: f32 = f32::NEG_INFINITY;
1633 assert_eq!(10.0f32.log(10.0), 1.0);
1634 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1635 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1636 assert!(1.0f32.log(1.0).is_nan());
1637 assert!(1.0f32.log(-13.9).is_nan());
1638 assert!(nan.log(2.3).is_nan());
1639 assert_eq!(inf.log(10.0), inf);
1640 assert!(neg_inf.log(8.8).is_nan());
1641 assert!((-2.3f32).log(0.1).is_nan());
1642 assert_eq!((-0.0f32).log(2.0), neg_inf);
1643 assert_eq!(0.0f32.log(7.0), neg_inf);
1648 let nan: f32 = f32::NAN;
1649 let inf: f32 = f32::INFINITY;
1650 let neg_inf: f32 = f32::NEG_INFINITY;
1651 assert_approx_eq!(10.0f32.log2(), 3.321928);
1652 assert_approx_eq!(2.3f32.log2(), 1.201634);
1653 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1654 assert!(nan.log2().is_nan());
1655 assert_eq!(inf.log2(), inf);
1656 assert!(neg_inf.log2().is_nan());
1657 assert!((-2.3f32).log2().is_nan());
1658 assert_eq!((-0.0f32).log2(), neg_inf);
1659 assert_eq!(0.0f32.log2(), neg_inf);
1664 let nan: f32 = f32::NAN;
1665 let inf: f32 = f32::INFINITY;
1666 let neg_inf: f32 = f32::NEG_INFINITY;
1667 assert_eq!(10.0f32.log10(), 1.0);
1668 assert_approx_eq!(2.3f32.log10(), 0.361728);
1669 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1670 assert_eq!(1.0f32.log10(), 0.0);
1671 assert!(nan.log10().is_nan());
1672 assert_eq!(inf.log10(), inf);
1673 assert!(neg_inf.log10().is_nan());
1674 assert!((-2.3f32).log10().is_nan());
1675 assert_eq!((-0.0f32).log10(), neg_inf);
1676 assert_eq!(0.0f32.log10(), neg_inf);
1680 fn test_to_degrees() {
1681 let pi: f32 = consts::PI;
1682 let nan: f32 = f32::NAN;
1683 let inf: f32 = f32::INFINITY;
1684 let neg_inf: f32 = f32::NEG_INFINITY;
1685 assert_eq!(0.0f32.to_degrees(), 0.0);
1686 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1687 assert_eq!(pi.to_degrees(), 180.0);
1688 assert!(nan.to_degrees().is_nan());
1689 assert_eq!(inf.to_degrees(), inf);
1690 assert_eq!(neg_inf.to_degrees(), neg_inf);
1694 fn test_to_radians() {
1695 let pi: f32 = consts::PI;
1696 let nan: f32 = f32::NAN;
1697 let inf: f32 = f32::INFINITY;
1698 let neg_inf: f32 = f32::NEG_INFINITY;
1699 assert_eq!(0.0f32.to_radians(), 0.0);
1700 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1701 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1702 assert_eq!(180.0f32.to_radians(), pi);
1703 assert!(nan.to_radians().is_nan());
1704 assert_eq!(inf.to_radians(), inf);
1705 assert_eq!(neg_inf.to_radians(), neg_inf);
1710 let f1 = 2.0f32.powi(-123);
1711 let f2 = 2.0f32.powi(-111);
1712 let f3 = 1.75 * 2.0f32.powi(-12);
1713 assert_eq!(f32::ldexp(1f32, -123), f1);
1714 assert_eq!(f32::ldexp(1f32, -111), f2);
1715 assert_eq!(f32::ldexp(1.75f32, -12), f3);
1717 assert_eq!(f32::ldexp(0f32, -123), 0f32);
1718 assert_eq!(f32::ldexp(-0f32, -123), -0f32);
1720 let inf: f32 = f32::INFINITY;
1721 let neg_inf: f32 = f32::NEG_INFINITY;
1722 let nan: f32 = f32::NAN;
1723 assert_eq!(f32::ldexp(inf, -123), inf);
1724 assert_eq!(f32::ldexp(neg_inf, -123), neg_inf);
1725 assert!(f32::ldexp(nan, -123).is_nan());
1730 let f1 = 2.0f32.powi(-123);
1731 let f2 = 2.0f32.powi(-111);
1732 let f3 = 1.75 * 2.0f32.powi(-123);
1733 let (x1, exp1) = f1.frexp();
1734 let (x2, exp2) = f2.frexp();
1735 let (x3, exp3) = f3.frexp();
1736 assert_eq!((x1, exp1), (0.5f32, -122));
1737 assert_eq!((x2, exp2), (0.5f32, -110));
1738 assert_eq!((x3, exp3), (0.875f32, -122));
1739 assert_eq!(f32::ldexp(x1, exp1), f1);
1740 assert_eq!(f32::ldexp(x2, exp2), f2);
1741 assert_eq!(f32::ldexp(x3, exp3), f3);
1743 assert_eq!(0f32.frexp(), (0f32, 0));
1744 assert_eq!((-0f32).frexp(), (-0f32, 0));
1747 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1748 fn test_frexp_nowin() {
1749 let inf: f32 = f32::INFINITY;
1750 let neg_inf: f32 = f32::NEG_INFINITY;
1751 let nan: f32 = f32::NAN;
1752 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1753 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1754 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1759 assert_eq!((-1f32).abs_sub(1f32), 0f32);
1760 assert_eq!(1f32.abs_sub(1f32), 0f32);
1761 assert_eq!(1f32.abs_sub(0f32), 1f32);
1762 assert_eq!(1f32.abs_sub(-1f32), 2f32);
1763 assert_eq!(NEG_INFINITY.abs_sub(0f32), 0f32);
1764 assert_eq!(INFINITY.abs_sub(1f32), INFINITY);
1765 assert_eq!(0f32.abs_sub(NEG_INFINITY), INFINITY);
1766 assert_eq!(0f32.abs_sub(INFINITY), 0f32);
1770 fn test_abs_sub_nowin() {
1771 assert!(NAN.abs_sub(-1f32).is_nan());
1772 assert!(1f32.abs_sub(NAN).is_nan());
1777 assert_eq!(0.0f32.asinh(), 0.0f32);
1778 assert_eq!((-0.0f32).asinh(), -0.0f32);
1780 let inf: f32 = f32::INFINITY;
1781 let neg_inf: f32 = f32::NEG_INFINITY;
1782 let nan: f32 = f32::NAN;
1783 assert_eq!(inf.asinh(), inf);
1784 assert_eq!(neg_inf.asinh(), neg_inf);
1785 assert!(nan.asinh().is_nan());
1786 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1787 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1792 assert_eq!(1.0f32.acosh(), 0.0f32);
1793 assert!(0.999f32.acosh().is_nan());
1795 let inf: f32 = f32::INFINITY;
1796 let neg_inf: f32 = f32::NEG_INFINITY;
1797 let nan: f32 = f32::NAN;
1798 assert_eq!(inf.acosh(), inf);
1799 assert!(neg_inf.acosh().is_nan());
1800 assert!(nan.acosh().is_nan());
1801 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1802 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1807 assert_eq!(0.0f32.atanh(), 0.0f32);
1808 assert_eq!((-0.0f32).atanh(), -0.0f32);
1810 let inf32: f32 = f32::INFINITY;
1811 let neg_inf32: f32 = f32::NEG_INFINITY;
1812 assert_eq!(1.0f32.atanh(), inf32);
1813 assert_eq!((-1.0f32).atanh(), neg_inf32);
1815 assert!(2f64.atanh().atanh().is_nan());
1816 assert!((-2f64).atanh().atanh().is_nan());
1818 let inf64: f32 = f32::INFINITY;
1819 let neg_inf64: f32 = f32::NEG_INFINITY;
1820 let nan32: f32 = f32::NAN;
1821 assert!(inf64.atanh().is_nan());
1822 assert!(neg_inf64.atanh().is_nan());
1823 assert!(nan32.atanh().is_nan());
1825 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1826 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1830 fn test_real_consts() {
1833 let pi: f32 = consts::PI;
1834 let frac_pi_2: f32 = consts::FRAC_PI_2;
1835 let frac_pi_3: f32 = consts::FRAC_PI_3;
1836 let frac_pi_4: f32 = consts::FRAC_PI_4;
1837 let frac_pi_6: f32 = consts::FRAC_PI_6;
1838 let frac_pi_8: f32 = consts::FRAC_PI_8;
1839 let frac_1_pi: f32 = consts::FRAC_1_PI;
1840 let frac_2_pi: f32 = consts::FRAC_2_PI;
1841 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1842 let sqrt2: f32 = consts::SQRT_2;
1843 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1844 let e: f32 = consts::E;
1845 let log2_e: f32 = consts::LOG2_E;
1846 let log10_e: f32 = consts::LOG10_E;
1847 let ln_2: f32 = consts::LN_2;
1848 let ln_10: f32 = consts::LN_10;
1850 assert_approx_eq!(frac_pi_2, pi / 2f32);
1851 assert_approx_eq!(frac_pi_3, pi / 3f32);
1852 assert_approx_eq!(frac_pi_4, pi / 4f32);
1853 assert_approx_eq!(frac_pi_6, pi / 6f32);
1854 assert_approx_eq!(frac_pi_8, pi / 8f32);
1855 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1856 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1857 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1858 assert_approx_eq!(sqrt2, 2f32.sqrt());
1859 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1860 assert_approx_eq!(log2_e, e.log2());
1861 assert_approx_eq!(log10_e, e.log10());
1862 assert_approx_eq!(ln_2, 2f32.ln());
1863 assert_approx_eq!(ln_10, 10f32.ln());