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)]
21 use num::{FpCategory, ParseFloatError};
23 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
24 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
25 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
26 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
27 pub use core::f32::consts;
31 use libc::{c_float, c_int};
34 pub fn cbrtf(n: c_float) -> c_float;
35 pub fn erff(n: c_float) -> c_float;
36 pub fn erfcf(n: c_float) -> c_float;
37 pub fn expm1f(n: c_float) -> c_float;
38 pub fn fdimf(a: c_float, b: c_float) -> c_float;
39 pub fn fmaxf(a: c_float, b: c_float) -> c_float;
40 pub fn fminf(a: c_float, b: c_float) -> c_float;
41 pub fn fmodf(a: c_float, b: c_float) -> c_float;
42 pub fn ilogbf(n: c_float) -> c_int;
43 pub fn logbf(n: c_float) -> c_float;
44 pub fn log1pf(n: c_float) -> c_float;
45 pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
46 pub fn nextafterf(x: c_float, y: c_float) -> c_float;
47 pub fn tgammaf(n: c_float) -> c_float;
49 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
50 pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
51 #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
52 pub fn hypotf(x: c_float, y: c_float) -> c_float;
55 // See the comments in `core::float::Float::floor` for why MSVC is special
57 #[cfg(not(target_env = "msvc"))]
59 pub fn acosf(n: c_float) -> c_float;
60 pub fn asinf(n: c_float) -> c_float;
61 pub fn atan2f(a: c_float, b: c_float) -> c_float;
62 pub fn atanf(n: c_float) -> c_float;
63 pub fn coshf(n: c_float) -> c_float;
64 pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
65 pub fn ldexpf(x: c_float, n: c_int) -> c_float;
66 pub fn sinhf(n: c_float) -> c_float;
67 pub fn tanf(n: c_float) -> c_float;
68 pub fn tanhf(n: c_float) -> c_float;
71 #[cfg(target_env = "msvc")]
72 pub use self::shims::*;
73 #[cfg(target_env = "msvc")]
75 use libc::{c_float, c_int};
77 pub unsafe fn acosf(n: c_float) -> c_float {
78 f64::acos(n as f64) as c_float
81 pub unsafe fn asinf(n: c_float) -> c_float {
82 f64::asin(n as f64) as c_float
85 pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
86 f64::atan2(n as f64, b as f64) as c_float
89 pub unsafe fn atanf(n: c_float) -> c_float {
90 f64::atan(n as f64) as c_float
93 pub unsafe fn coshf(n: c_float) -> c_float {
94 f64::cosh(n as f64) as c_float
97 pub unsafe fn frexpf(x: c_float, value: &mut c_int) -> c_float {
98 let (a, b) = f64::frexp(x as f64);
103 pub unsafe fn ldexpf(x: c_float, n: c_int) -> c_float {
104 f64::ldexp(x as f64, n as isize) as c_float
107 pub unsafe fn sinhf(n: c_float) -> c_float {
108 f64::sinh(n as f64) as c_float
111 pub unsafe fn tanf(n: c_float) -> c_float {
112 f64::tan(n as f64) as c_float
115 pub unsafe fn tanhf(n: c_float) -> c_float {
116 f64::tanh(n as f64) as c_float
123 #[stable(feature = "rust1", since = "1.0.0")]
125 /// Parses a float as with a given radix
126 #[unstable(feature = "float_from_str_radix", reason = "recently moved API")]
127 pub fn from_str_radix(s: &str, radix: u32) -> Result<f32, ParseFloatError> {
128 num::Float::from_str_radix(s, radix)
131 /// Returns `true` if this value is `NaN` and false otherwise.
136 /// let nan = f32::NAN;
139 /// assert!(nan.is_nan());
140 /// assert!(!f.is_nan());
142 #[stable(feature = "rust1", since = "1.0.0")]
144 pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
146 /// Returns `true` if this value is positive infinity or negative infinity and
153 /// let inf = f32::INFINITY;
154 /// let neg_inf = f32::NEG_INFINITY;
155 /// let nan = f32::NAN;
157 /// assert!(!f.is_infinite());
158 /// assert!(!nan.is_infinite());
160 /// assert!(inf.is_infinite());
161 /// assert!(neg_inf.is_infinite());
163 #[stable(feature = "rust1", since = "1.0.0")]
165 pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
167 /// Returns `true` if this number is neither infinite nor `NaN`.
173 /// let inf = f32::INFINITY;
174 /// let neg_inf = f32::NEG_INFINITY;
175 /// let nan = f32::NAN;
177 /// assert!(f.is_finite());
179 /// assert!(!nan.is_finite());
180 /// assert!(!inf.is_finite());
181 /// assert!(!neg_inf.is_finite());
183 #[stable(feature = "rust1", since = "1.0.0")]
185 pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
187 /// Returns `true` if the number is neither zero, infinite,
188 /// [subnormal][subnormal], or `NaN`.
193 /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
194 /// let max = f32::MAX;
195 /// let lower_than_min = 1.0e-40_f32;
196 /// let zero = 0.0_f32;
198 /// assert!(min.is_normal());
199 /// assert!(max.is_normal());
201 /// assert!(!zero.is_normal());
202 /// assert!(!f32::NAN.is_normal());
203 /// assert!(!f32::INFINITY.is_normal());
204 /// // Values between `0` and `min` are Subnormal.
205 /// assert!(!lower_than_min.is_normal());
207 /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
208 #[stable(feature = "rust1", since = "1.0.0")]
210 pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
212 /// Returns the floating point category of the number. If only one property
213 /// is going to be tested, it is generally faster to use the specific
214 /// predicate instead.
217 /// use std::num::FpCategory;
220 /// let num = 12.4_f32;
221 /// let inf = f32::INFINITY;
223 /// assert_eq!(num.classify(), FpCategory::Normal);
224 /// assert_eq!(inf.classify(), FpCategory::Infinite);
226 #[stable(feature = "rust1", since = "1.0.0")]
228 pub fn classify(self) -> FpCategory { num::Float::classify(self) }
230 /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
231 /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
232 /// The floating point encoding is documented in the [Reference][floating-point].
235 /// #![feature(float_extras)]
239 /// let num = 2.0f32;
241 /// // (8388608, -22, 1)
242 /// let (mantissa, exponent, sign) = num.integer_decode();
243 /// let sign_f = sign as f32;
244 /// let mantissa_f = mantissa as f32;
245 /// let exponent_f = num.powf(exponent as f32);
247 /// // 1 * 8388608 * 2^(-22) == 2
248 /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
250 /// assert!(abs_difference <= f32::EPSILON);
252 /// [floating-point]: ../../../../../reference.html#machine-types
253 #[unstable(feature = "float_extras", reason = "signature is undecided")]
255 pub fn integer_decode(self) -> (u64, i16, i8) {
256 num::Float::integer_decode(self)
259 /// Returns the largest integer less than or equal to a number.
262 /// let f = 3.99_f32;
265 /// assert_eq!(f.floor(), 3.0);
266 /// assert_eq!(g.floor(), 3.0);
268 #[stable(feature = "rust1", since = "1.0.0")]
270 pub fn floor(self) -> f32 {
273 // On MSVC LLVM will lower many math intrinsics to a call to the
274 // corresponding function. On MSVC, however, many of these functions
275 // aren't actually available as symbols to call, but rather they are all
276 // `static inline` functions in header files. This means that from a C
277 // perspective it's "compatible", but not so much from an ABI
278 // perspective (which we're worried about).
280 // The inline header functions always just cast to a f64 and do their
281 // operation, so we do that here as well, but only for MSVC targets.
283 // Note that there are many MSVC-specific float operations which
284 // redirect to this comment, so `floorf` is just one case of a missing
285 // function on MSVC, but there are many others elsewhere.
286 #[cfg(target_env = "msvc")]
287 fn floorf(f: f32) -> f32 { (f as f64).floor() as f32 }
288 #[cfg(not(target_env = "msvc"))]
289 fn floorf(f: f32) -> f32 { unsafe { intrinsics::floorf32(f) } }
292 /// Returns the smallest integer greater than or equal to a number.
295 /// let f = 3.01_f32;
298 /// assert_eq!(f.ceil(), 4.0);
299 /// assert_eq!(g.ceil(), 4.0);
301 #[stable(feature = "rust1", since = "1.0.0")]
303 pub fn ceil(self) -> f32 {
306 // see notes above in `floor`
307 #[cfg(target_env = "msvc")]
308 fn ceilf(f: f32) -> f32 { (f as f64).ceil() as f32 }
309 #[cfg(not(target_env = "msvc"))]
310 fn ceilf(f: f32) -> f32 { unsafe { intrinsics::ceilf32(f) } }
313 /// Returns the nearest integer to a number. Round half-way cases away from
318 /// let g = -3.3_f32;
320 /// assert_eq!(f.round(), 3.0);
321 /// assert_eq!(g.round(), -3.0);
323 #[stable(feature = "rust1", since = "1.0.0")]
325 pub fn round(self) -> f32 {
326 unsafe { intrinsics::roundf32(self) }
329 /// Returns the integer part of a number.
333 /// let g = -3.7_f32;
335 /// assert_eq!(f.trunc(), 3.0);
336 /// assert_eq!(g.trunc(), -3.0);
338 #[stable(feature = "rust1", since = "1.0.0")]
340 pub fn trunc(self) -> f32 {
341 unsafe { intrinsics::truncf32(self) }
344 /// Returns the fractional part of a number.
350 /// let y = -3.5_f32;
351 /// let abs_difference_x = (x.fract() - 0.5).abs();
352 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
354 /// assert!(abs_difference_x <= f32::EPSILON);
355 /// assert!(abs_difference_y <= f32::EPSILON);
357 #[stable(feature = "rust1", since = "1.0.0")]
359 pub fn fract(self) -> f32 { self - self.trunc() }
361 /// Computes the absolute value of `self`. Returns `NAN` if the
368 /// let y = -3.5_f32;
370 /// let abs_difference_x = (x.abs() - x).abs();
371 /// let abs_difference_y = (y.abs() - (-y)).abs();
373 /// assert!(abs_difference_x <= f32::EPSILON);
374 /// assert!(abs_difference_y <= f32::EPSILON);
376 /// assert!(f32::NAN.abs().is_nan());
378 #[stable(feature = "rust1", since = "1.0.0")]
380 pub fn abs(self) -> f32 { num::Float::abs(self) }
382 /// Returns a number that represents the sign of `self`.
384 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
385 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
386 /// - `NAN` if the number is `NAN`
393 /// assert_eq!(f.signum(), 1.0);
394 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
396 /// assert!(f32::NAN.signum().is_nan());
398 #[stable(feature = "rust1", since = "1.0.0")]
400 pub fn signum(self) -> f32 { num::Float::signum(self) }
402 /// Returns `true` if `self`'s sign bit is positive, including
403 /// `+0.0` and `INFINITY`.
408 /// let nan = f32::NAN;
410 /// let g = -7.0_f32;
412 /// assert!(f.is_sign_positive());
413 /// assert!(!g.is_sign_positive());
414 /// // Requires both tests to determine if is `NaN`
415 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
417 #[stable(feature = "rust1", since = "1.0.0")]
419 pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) }
421 /// Returns `true` if `self`'s sign is negative, including `-0.0`
422 /// and `NEG_INFINITY`.
427 /// let nan = f32::NAN;
431 /// assert!(!f.is_sign_negative());
432 /// assert!(g.is_sign_negative());
433 /// // Requires both tests to determine if is `NaN`.
434 /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
436 #[stable(feature = "rust1", since = "1.0.0")]
438 pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) }
440 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
441 /// error. This produces a more accurate result with better performance than
442 /// a separate multiplication operation followed by an add.
447 /// let m = 10.0_f32;
449 /// let b = 60.0_f32;
452 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
454 /// assert!(abs_difference <= f32::EPSILON);
456 #[stable(feature = "rust1", since = "1.0.0")]
458 pub fn mul_add(self, a: f32, b: f32) -> f32 {
459 unsafe { intrinsics::fmaf32(self, a, b) }
462 /// Takes the reciprocal (inverse) of a number, `1/x`.
468 /// let abs_difference = (x.recip() - (1.0/x)).abs();
470 /// assert!(abs_difference <= f32::EPSILON);
472 #[stable(feature = "rust1", since = "1.0.0")]
474 pub fn recip(self) -> f32 { num::Float::recip(self) }
476 /// Raises a number to an integer power.
478 /// Using this function is generally faster than using `powf`
484 /// let abs_difference = (x.powi(2) - x*x).abs();
486 /// assert!(abs_difference <= f32::EPSILON);
488 #[stable(feature = "rust1", since = "1.0.0")]
490 pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
492 /// Raises a number to a floating point power.
498 /// let abs_difference = (x.powf(2.0) - x*x).abs();
500 /// assert!(abs_difference <= f32::EPSILON);
502 #[stable(feature = "rust1", since = "1.0.0")]
504 pub fn powf(self, n: f32) -> f32 {
505 return powf(self, n);
507 // see notes above in `floor`
508 #[cfg(target_env = "msvc")]
509 fn powf(f: f32, n: f32) -> f32 { (f as f64).powf(n as f64) as f32 }
510 #[cfg(not(target_env = "msvc"))]
511 fn powf(f: f32, n: f32) -> f32 { unsafe { intrinsics::powf32(f, n) } }
514 /// Takes the square root of a number.
516 /// Returns NaN if `self` is a negative number.
521 /// let positive = 4.0_f32;
522 /// let negative = -4.0_f32;
524 /// let abs_difference = (positive.sqrt() - 2.0).abs();
526 /// assert!(abs_difference <= f32::EPSILON);
527 /// assert!(negative.sqrt().is_nan());
529 #[stable(feature = "rust1", since = "1.0.0")]
531 pub fn sqrt(self) -> f32 {
535 unsafe { intrinsics::sqrtf32(self) }
539 /// Returns `e^(self)`, (the exponential function).
544 /// let one = 1.0f32;
546 /// let e = one.exp();
548 /// // ln(e) - 1 == 0
549 /// let abs_difference = (e.ln() - 1.0).abs();
551 /// assert!(abs_difference <= f32::EPSILON);
553 #[stable(feature = "rust1", since = "1.0.0")]
555 pub fn exp(self) -> f32 {
558 // see notes above in `floor`
559 #[cfg(target_env = "msvc")]
560 fn expf(f: f32) -> f32 { (f as f64).exp() as f32 }
561 #[cfg(not(target_env = "msvc"))]
562 fn expf(f: f32) -> f32 { unsafe { intrinsics::expf32(f) } }
565 /// Returns `2^(self)`.
573 /// let abs_difference = (f.exp2() - 4.0).abs();
575 /// assert!(abs_difference <= f32::EPSILON);
577 #[stable(feature = "rust1", since = "1.0.0")]
579 pub fn exp2(self) -> f32 {
580 unsafe { intrinsics::exp2f32(self) }
583 /// Returns the natural logarithm of the number.
588 /// let one = 1.0f32;
590 /// let e = one.exp();
592 /// // ln(e) - 1 == 0
593 /// let abs_difference = (e.ln() - 1.0).abs();
595 /// assert!(abs_difference <= f32::EPSILON);
597 #[stable(feature = "rust1", since = "1.0.0")]
599 pub fn ln(self) -> f32 {
602 // see notes above in `floor`
603 #[cfg(target_env = "msvc")]
604 fn logf(f: f32) -> f32 { (f as f64).ln() as f32 }
605 #[cfg(not(target_env = "msvc"))]
606 fn logf(f: f32) -> f32 { unsafe { intrinsics::logf32(f) } }
609 /// Returns the logarithm of the number with respect to an arbitrary base.
614 /// let ten = 10.0f32;
615 /// let two = 2.0f32;
617 /// // log10(10) - 1 == 0
618 /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
620 /// // log2(2) - 1 == 0
621 /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
623 /// assert!(abs_difference_10 <= f32::EPSILON);
624 /// assert!(abs_difference_2 <= f32::EPSILON);
626 #[stable(feature = "rust1", since = "1.0.0")]
628 pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
630 /// Returns the base 2 logarithm of the number.
635 /// let two = 2.0f32;
637 /// // log2(2) - 1 == 0
638 /// let abs_difference = (two.log2() - 1.0).abs();
640 /// assert!(abs_difference <= f32::EPSILON);
642 #[stable(feature = "rust1", since = "1.0.0")]
644 pub fn log2(self) -> f32 {
645 unsafe { intrinsics::log2f32(self) }
648 /// Returns the base 10 logarithm of the number.
653 /// let ten = 10.0f32;
655 /// // log10(10) - 1 == 0
656 /// let abs_difference = (ten.log10() - 1.0).abs();
658 /// assert!(abs_difference <= f32::EPSILON);
660 #[stable(feature = "rust1", since = "1.0.0")]
662 pub fn log10(self) -> f32 {
665 // see notes above in `floor`
666 #[cfg(target_env = "msvc")]
667 fn log10f(f: f32) -> f32 { (f as f64).log10() as f32 }
668 #[cfg(not(target_env = "msvc"))]
669 fn log10f(f: f32) -> f32 { unsafe { intrinsics::log10f32(f) } }
672 /// Converts radians to degrees.
675 /// #![feature(float_extras)]
677 /// use std::f32::{self, consts};
679 /// let angle = consts::PI;
681 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
683 /// assert!(abs_difference <= f32::EPSILON);
685 #[unstable(feature = "float_extras", reason = "desirability is unclear")]
687 pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
689 /// Converts degrees to radians.
692 /// #![feature(float_extras)]
694 /// use std::f32::{self, consts};
696 /// let angle = 180.0f32;
698 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
700 /// assert!(abs_difference <= f32::EPSILON);
702 #[unstable(feature = "float_extras", reason = "desirability is unclear")]
704 pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
706 /// Constructs a floating point number of `x*2^exp`.
709 /// #![feature(float_extras)]
712 /// // 3*2^2 - 12 == 0
713 /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
715 /// assert!(abs_difference <= f32::EPSILON);
717 #[unstable(feature = "float_extras",
718 reason = "pending integer conventions")]
720 pub fn ldexp(x: f32, exp: isize) -> f32 {
721 unsafe { cmath::ldexpf(x, exp as c_int) }
724 /// Breaks the number into a normalized fraction and a base-2 exponent,
727 /// * `self = x * 2^exp`
728 /// * `0.5 <= abs(x) < 1.0`
731 /// #![feature(float_extras)]
737 /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
738 /// let f = x.frexp();
739 /// let abs_difference_0 = (f.0 - 0.5).abs();
740 /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
742 /// assert!(abs_difference_0 <= f32::EPSILON);
743 /// assert!(abs_difference_1 <= f32::EPSILON);
745 #[unstable(feature = "float_extras",
746 reason = "pending integer conventions")]
748 pub fn frexp(self) -> (f32, isize) {
751 let x = cmath::frexpf(self, &mut exp);
756 /// Returns the next representable floating-point value in the direction of
760 /// #![feature(float_extras)]
766 /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
768 /// assert!(abs_diff <= f32::EPSILON);
770 #[unstable(feature = "float_extras",
771 reason = "unsure about its place in the world")]
773 pub fn next_after(self, other: f32) -> f32 {
774 unsafe { cmath::nextafterf(self, other) }
777 /// Returns the maximum of the two numbers.
783 /// assert_eq!(x.max(y), y);
786 /// If one of the arguments is NaN, then the other argument is returned.
787 #[stable(feature = "rust1", since = "1.0.0")]
789 pub fn max(self, other: f32) -> f32 {
790 unsafe { cmath::fmaxf(self, other) }
793 /// Returns the minimum of the two numbers.
799 /// assert_eq!(x.min(y), x);
802 /// If one of the arguments is NaN, then the other argument is returned.
803 #[stable(feature = "rust1", since = "1.0.0")]
805 pub fn min(self, other: f32) -> f32 {
806 unsafe { cmath::fminf(self, other) }
809 /// The positive difference of two numbers.
811 /// * If `self <= other`: `0:0`
812 /// * Else: `self - other`
820 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
821 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
823 /// assert!(abs_difference_x <= f32::EPSILON);
824 /// assert!(abs_difference_y <= f32::EPSILON);
826 #[stable(feature = "rust1", since = "1.0.0")]
828 pub fn abs_sub(self, other: f32) -> f32 {
829 unsafe { cmath::fdimf(self, other) }
832 /// Takes the cubic root of a number.
839 /// // x^(1/3) - 2 == 0
840 /// let abs_difference = (x.cbrt() - 2.0).abs();
842 /// assert!(abs_difference <= f32::EPSILON);
844 #[stable(feature = "rust1", since = "1.0.0")]
846 pub fn cbrt(self) -> f32 {
847 unsafe { cmath::cbrtf(self) }
850 /// Calculates the length of the hypotenuse of a right-angle triangle given
851 /// legs of length `x` and `y`.
859 /// // sqrt(x^2 + y^2)
860 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
862 /// assert!(abs_difference <= f32::EPSILON);
864 #[stable(feature = "rust1", since = "1.0.0")]
866 pub fn hypot(self, other: f32) -> f32 {
867 unsafe { cmath::hypotf(self, other) }
870 /// Computes the sine of a number (in radians).
875 /// let x = f32::consts::PI/2.0;
877 /// let abs_difference = (x.sin() - 1.0).abs();
879 /// assert!(abs_difference <= f32::EPSILON);
881 #[stable(feature = "rust1", since = "1.0.0")]
883 pub fn sin(self) -> f32 {
886 // see notes in `core::f32::Float::floor`
887 #[cfg(target_env = "msvc")]
888 fn sinf(f: f32) -> f32 { (f as f64).sin() as f32 }
889 #[cfg(not(target_env = "msvc"))]
890 fn sinf(f: f32) -> f32 { unsafe { intrinsics::sinf32(f) } }
893 /// Computes the cosine of a number (in radians).
898 /// let x = 2.0*f32::consts::PI;
900 /// let abs_difference = (x.cos() - 1.0).abs();
902 /// assert!(abs_difference <= f32::EPSILON);
904 #[stable(feature = "rust1", since = "1.0.0")]
906 pub fn cos(self) -> f32 {
909 // see notes in `core::f32::Float::floor`
910 #[cfg(target_env = "msvc")]
911 fn cosf(f: f32) -> f32 { (f as f64).cos() as f32 }
912 #[cfg(not(target_env = "msvc"))]
913 fn cosf(f: f32) -> f32 { unsafe { intrinsics::cosf32(f) } }
916 /// Computes the tangent of a number (in radians).
921 /// let x = f64::consts::PI/4.0;
922 /// let abs_difference = (x.tan() - 1.0).abs();
924 /// assert!(abs_difference < 1e-10);
926 #[stable(feature = "rust1", since = "1.0.0")]
928 pub fn tan(self) -> f32 {
929 unsafe { cmath::tanf(self) }
932 /// Computes the arcsine of a number. Return value is in radians in
933 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
939 /// let f = f32::consts::PI / 2.0;
941 /// // asin(sin(pi/2))
942 /// let abs_difference = f.sin().asin().abs_sub(f32::consts::PI / 2.0);
944 /// assert!(abs_difference <= f32::EPSILON);
946 #[stable(feature = "rust1", since = "1.0.0")]
948 pub fn asin(self) -> f32 {
949 unsafe { cmath::asinf(self) }
952 /// Computes the arccosine of a number. Return value is in radians in
953 /// the range [0, pi] or NaN if the number is outside the range
959 /// let f = f32::consts::PI / 4.0;
961 /// // acos(cos(pi/4))
962 /// let abs_difference = f.cos().acos().abs_sub(f32::consts::PI / 4.0);
964 /// assert!(abs_difference <= f32::EPSILON);
966 #[stable(feature = "rust1", since = "1.0.0")]
968 pub fn acos(self) -> f32 {
969 unsafe { cmath::acosf(self) }
972 /// Computes the arctangent of a number. Return value is in radians in the
973 /// range [-pi/2, pi/2];
981 /// let abs_difference = f.tan().atan().abs_sub(1.0);
983 /// assert!(abs_difference <= f32::EPSILON);
985 #[stable(feature = "rust1", since = "1.0.0")]
987 pub fn atan(self) -> f32 {
988 unsafe { cmath::atanf(self) }
991 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
993 /// * `x = 0`, `y = 0`: `0`
994 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
995 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
996 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
1001 /// let pi = f32::consts::PI;
1002 /// // All angles from horizontal right (+x)
1003 /// // 45 deg counter-clockwise
1004 /// let x1 = 3.0f32;
1005 /// let y1 = -3.0f32;
1007 /// // 135 deg clockwise
1008 /// let x2 = -3.0f32;
1009 /// let y2 = 3.0f32;
1011 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
1012 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
1014 /// assert!(abs_difference_1 <= f32::EPSILON);
1015 /// assert!(abs_difference_2 <= f32::EPSILON);
1017 #[stable(feature = "rust1", since = "1.0.0")]
1019 pub fn atan2(self, other: f32) -> f32 {
1020 unsafe { cmath::atan2f(self, other) }
1023 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
1024 /// `(sin(x), cos(x))`.
1029 /// let x = f32::consts::PI/4.0;
1030 /// let f = x.sin_cos();
1032 /// let abs_difference_0 = (f.0 - x.sin()).abs();
1033 /// let abs_difference_1 = (f.1 - x.cos()).abs();
1035 /// assert!(abs_difference_0 <= f32::EPSILON);
1036 /// assert!(abs_difference_0 <= f32::EPSILON);
1038 #[stable(feature = "rust1", since = "1.0.0")]
1040 pub fn sin_cos(self) -> (f32, f32) {
1041 (self.sin(), self.cos())
1044 /// Returns `e^(self) - 1` in a way that is accurate even if the
1045 /// number is close to zero.
1050 /// // e^(ln(7)) - 1
1051 /// let abs_difference = x.ln().exp_m1().abs_sub(6.0);
1053 /// assert!(abs_difference < 1e-10);
1055 #[stable(feature = "rust1", since = "1.0.0")]
1057 pub fn exp_m1(self) -> f32 {
1058 unsafe { cmath::expm1f(self) }
1061 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
1062 /// the operations were performed separately.
1067 /// let x = f32::consts::E - 1.0;
1069 /// // ln(1 + (e - 1)) == ln(e) == 1
1070 /// let abs_difference = (x.ln_1p() - 1.0).abs();
1072 /// assert!(abs_difference <= f32::EPSILON);
1074 #[stable(feature = "rust1", since = "1.0.0")]
1076 pub fn ln_1p(self) -> f32 {
1077 unsafe { cmath::log1pf(self) }
1080 /// Hyperbolic sine function.
1085 /// let e = f32::consts::E;
1088 /// let f = x.sinh();
1089 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
1090 /// let g = (e*e - 1.0)/(2.0*e);
1091 /// let abs_difference = (f - g).abs();
1093 /// assert!(abs_difference <= f32::EPSILON);
1095 #[stable(feature = "rust1", since = "1.0.0")]
1097 pub fn sinh(self) -> f32 {
1098 unsafe { cmath::sinhf(self) }
1101 /// Hyperbolic cosine function.
1106 /// let e = f32::consts::E;
1108 /// let f = x.cosh();
1109 /// // Solving cosh() at 1 gives this result
1110 /// let g = (e*e + 1.0)/(2.0*e);
1111 /// let abs_difference = f.abs_sub(g);
1114 /// assert!(abs_difference <= f32::EPSILON);
1116 #[stable(feature = "rust1", since = "1.0.0")]
1118 pub fn cosh(self) -> f32 {
1119 unsafe { cmath::coshf(self) }
1122 /// Hyperbolic tangent function.
1127 /// let e = f32::consts::E;
1130 /// let f = x.tanh();
1131 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1132 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1133 /// let abs_difference = (f - g).abs();
1135 /// assert!(abs_difference <= f32::EPSILON);
1137 #[stable(feature = "rust1", since = "1.0.0")]
1139 pub fn tanh(self) -> f32 {
1140 unsafe { cmath::tanhf(self) }
1143 /// Inverse hyperbolic sine function.
1149 /// let f = x.sinh().asinh();
1151 /// let abs_difference = (f - x).abs();
1153 /// assert!(abs_difference <= f32::EPSILON);
1155 #[stable(feature = "rust1", since = "1.0.0")]
1157 pub fn asinh(self) -> f32 {
1159 NEG_INFINITY => NEG_INFINITY,
1160 x => (x + ((x * x) + 1.0).sqrt()).ln(),
1164 /// Inverse hyperbolic cosine function.
1170 /// let f = x.cosh().acosh();
1172 /// let abs_difference = (f - x).abs();
1174 /// assert!(abs_difference <= f32::EPSILON);
1176 #[stable(feature = "rust1", since = "1.0.0")]
1178 pub fn acosh(self) -> f32 {
1180 x if x < 1.0 => ::f32::NAN,
1181 x => (x + ((x * x) - 1.0).sqrt()).ln(),
1185 /// Inverse hyperbolic tangent function.
1190 /// let e = f32::consts::E;
1191 /// let f = e.tanh().atanh();
1193 /// let abs_difference = f.abs_sub(e);
1195 /// assert!(abs_difference <= f32::EPSILON);
1197 #[stable(feature = "rust1", since = "1.0.0")]
1199 pub fn atanh(self) -> f32 {
1200 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1209 use num::FpCategory as Fp;
1213 test_num(10f32, 2f32);
1218 assert_eq!(NAN.min(2.0), 2.0);
1219 assert_eq!(2.0f32.min(NAN), 2.0);
1224 assert_eq!(NAN.max(2.0), 2.0);
1225 assert_eq!(2.0f32.max(NAN), 2.0);
1230 let nan: f32 = f32::NAN;
1231 assert!(nan.is_nan());
1232 assert!(!nan.is_infinite());
1233 assert!(!nan.is_finite());
1234 assert!(!nan.is_normal());
1235 assert!(!nan.is_sign_positive());
1236 assert!(!nan.is_sign_negative());
1237 assert_eq!(Fp::Nan, nan.classify());
1241 fn test_infinity() {
1242 let inf: f32 = f32::INFINITY;
1243 assert!(inf.is_infinite());
1244 assert!(!inf.is_finite());
1245 assert!(inf.is_sign_positive());
1246 assert!(!inf.is_sign_negative());
1247 assert!(!inf.is_nan());
1248 assert!(!inf.is_normal());
1249 assert_eq!(Fp::Infinite, inf.classify());
1253 fn test_neg_infinity() {
1254 let neg_inf: f32 = f32::NEG_INFINITY;
1255 assert!(neg_inf.is_infinite());
1256 assert!(!neg_inf.is_finite());
1257 assert!(!neg_inf.is_sign_positive());
1258 assert!(neg_inf.is_sign_negative());
1259 assert!(!neg_inf.is_nan());
1260 assert!(!neg_inf.is_normal());
1261 assert_eq!(Fp::Infinite, neg_inf.classify());
1266 let zero: f32 = 0.0f32;
1267 assert_eq!(0.0, zero);
1268 assert!(!zero.is_infinite());
1269 assert!(zero.is_finite());
1270 assert!(zero.is_sign_positive());
1271 assert!(!zero.is_sign_negative());
1272 assert!(!zero.is_nan());
1273 assert!(!zero.is_normal());
1274 assert_eq!(Fp::Zero, zero.classify());
1278 fn test_neg_zero() {
1279 let neg_zero: f32 = -0.0;
1280 assert_eq!(0.0, neg_zero);
1281 assert!(!neg_zero.is_infinite());
1282 assert!(neg_zero.is_finite());
1283 assert!(!neg_zero.is_sign_positive());
1284 assert!(neg_zero.is_sign_negative());
1285 assert!(!neg_zero.is_nan());
1286 assert!(!neg_zero.is_normal());
1287 assert_eq!(Fp::Zero, neg_zero.classify());
1292 let one: f32 = 1.0f32;
1293 assert_eq!(1.0, one);
1294 assert!(!one.is_infinite());
1295 assert!(one.is_finite());
1296 assert!(one.is_sign_positive());
1297 assert!(!one.is_sign_negative());
1298 assert!(!one.is_nan());
1299 assert!(one.is_normal());
1300 assert_eq!(Fp::Normal, one.classify());
1305 let nan: f32 = f32::NAN;
1306 let inf: f32 = f32::INFINITY;
1307 let neg_inf: f32 = f32::NEG_INFINITY;
1308 assert!(nan.is_nan());
1309 assert!(!0.0f32.is_nan());
1310 assert!(!5.3f32.is_nan());
1311 assert!(!(-10.732f32).is_nan());
1312 assert!(!inf.is_nan());
1313 assert!(!neg_inf.is_nan());
1317 fn test_is_infinite() {
1318 let nan: f32 = f32::NAN;
1319 let inf: f32 = f32::INFINITY;
1320 let neg_inf: f32 = f32::NEG_INFINITY;
1321 assert!(!nan.is_infinite());
1322 assert!(inf.is_infinite());
1323 assert!(neg_inf.is_infinite());
1324 assert!(!0.0f32.is_infinite());
1325 assert!(!42.8f32.is_infinite());
1326 assert!(!(-109.2f32).is_infinite());
1330 fn test_is_finite() {
1331 let nan: f32 = f32::NAN;
1332 let inf: f32 = f32::INFINITY;
1333 let neg_inf: f32 = f32::NEG_INFINITY;
1334 assert!(!nan.is_finite());
1335 assert!(!inf.is_finite());
1336 assert!(!neg_inf.is_finite());
1337 assert!(0.0f32.is_finite());
1338 assert!(42.8f32.is_finite());
1339 assert!((-109.2f32).is_finite());
1343 fn test_is_normal() {
1344 let nan: f32 = f32::NAN;
1345 let inf: f32 = f32::INFINITY;
1346 let neg_inf: f32 = f32::NEG_INFINITY;
1347 let zero: f32 = 0.0f32;
1348 let neg_zero: f32 = -0.0;
1349 assert!(!nan.is_normal());
1350 assert!(!inf.is_normal());
1351 assert!(!neg_inf.is_normal());
1352 assert!(!zero.is_normal());
1353 assert!(!neg_zero.is_normal());
1354 assert!(1f32.is_normal());
1355 assert!(1e-37f32.is_normal());
1356 assert!(!1e-38f32.is_normal());
1360 fn test_classify() {
1361 let nan: f32 = f32::NAN;
1362 let inf: f32 = f32::INFINITY;
1363 let neg_inf: f32 = f32::NEG_INFINITY;
1364 let zero: f32 = 0.0f32;
1365 let neg_zero: f32 = -0.0;
1366 assert_eq!(nan.classify(), Fp::Nan);
1367 assert_eq!(inf.classify(), Fp::Infinite);
1368 assert_eq!(neg_inf.classify(), Fp::Infinite);
1369 assert_eq!(zero.classify(), Fp::Zero);
1370 assert_eq!(neg_zero.classify(), Fp::Zero);
1371 assert_eq!(1f32.classify(), Fp::Normal);
1372 assert_eq!(1e-37f32.classify(), Fp::Normal);
1373 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1377 fn test_integer_decode() {
1378 assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
1379 assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
1380 assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
1381 assert_eq!(0f32.integer_decode(), (0, -150, 1));
1382 assert_eq!((-0f32).integer_decode(), (0, -150, -1));
1383 assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
1384 assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
1385 assert_eq!(NAN.integer_decode(), (12582912, 105, 1));
1390 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1391 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1392 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1393 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1394 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1395 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1396 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1397 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1398 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1399 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1404 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1405 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1406 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1407 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1408 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1409 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1410 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1411 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1412 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1413 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1418 assert_approx_eq!(1.0f32.round(), 1.0f32);
1419 assert_approx_eq!(1.3f32.round(), 1.0f32);
1420 assert_approx_eq!(1.5f32.round(), 2.0f32);
1421 assert_approx_eq!(1.7f32.round(), 2.0f32);
1422 assert_approx_eq!(0.0f32.round(), 0.0f32);
1423 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1424 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1425 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1426 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1427 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1432 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1433 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1434 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1435 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1436 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1437 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1438 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1439 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1440 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1441 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1446 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1447 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1448 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1449 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1450 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1451 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1452 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1453 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1454 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1455 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1460 assert_eq!(INFINITY.abs(), INFINITY);
1461 assert_eq!(1f32.abs(), 1f32);
1462 assert_eq!(0f32.abs(), 0f32);
1463 assert_eq!((-0f32).abs(), 0f32);
1464 assert_eq!((-1f32).abs(), 1f32);
1465 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1466 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1467 assert!(NAN.abs().is_nan());
1472 assert_eq!(INFINITY.signum(), 1f32);
1473 assert_eq!(1f32.signum(), 1f32);
1474 assert_eq!(0f32.signum(), 1f32);
1475 assert_eq!((-0f32).signum(), -1f32);
1476 assert_eq!((-1f32).signum(), -1f32);
1477 assert_eq!(NEG_INFINITY.signum(), -1f32);
1478 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1479 assert!(NAN.signum().is_nan());
1483 fn test_is_sign_positive() {
1484 assert!(INFINITY.is_sign_positive());
1485 assert!(1f32.is_sign_positive());
1486 assert!(0f32.is_sign_positive());
1487 assert!(!(-0f32).is_sign_positive());
1488 assert!(!(-1f32).is_sign_positive());
1489 assert!(!NEG_INFINITY.is_sign_positive());
1490 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1491 assert!(!NAN.is_sign_positive());
1495 fn test_is_sign_negative() {
1496 assert!(!INFINITY.is_sign_negative());
1497 assert!(!1f32.is_sign_negative());
1498 assert!(!0f32.is_sign_negative());
1499 assert!((-0f32).is_sign_negative());
1500 assert!((-1f32).is_sign_negative());
1501 assert!(NEG_INFINITY.is_sign_negative());
1502 assert!((1f32/NEG_INFINITY).is_sign_negative());
1503 assert!(!NAN.is_sign_negative());
1508 let nan: f32 = f32::NAN;
1509 let inf: f32 = f32::INFINITY;
1510 let neg_inf: f32 = f32::NEG_INFINITY;
1511 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1512 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1513 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1514 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1515 assert!(nan.mul_add(7.8, 9.0).is_nan());
1516 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1517 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1518 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1519 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1524 let nan: f32 = f32::NAN;
1525 let inf: f32 = f32::INFINITY;
1526 let neg_inf: f32 = f32::NEG_INFINITY;
1527 assert_eq!(1.0f32.recip(), 1.0);
1528 assert_eq!(2.0f32.recip(), 0.5);
1529 assert_eq!((-0.4f32).recip(), -2.5);
1530 assert_eq!(0.0f32.recip(), inf);
1531 assert!(nan.recip().is_nan());
1532 assert_eq!(inf.recip(), 0.0);
1533 assert_eq!(neg_inf.recip(), 0.0);
1538 let nan: f32 = f32::NAN;
1539 let inf: f32 = f32::INFINITY;
1540 let neg_inf: f32 = f32::NEG_INFINITY;
1541 assert_eq!(1.0f32.powi(1), 1.0);
1542 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1543 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1544 assert_eq!(8.3f32.powi(0), 1.0);
1545 assert!(nan.powi(2).is_nan());
1546 assert_eq!(inf.powi(3), inf);
1547 assert_eq!(neg_inf.powi(2), inf);
1552 let nan: f32 = f32::NAN;
1553 let inf: f32 = f32::INFINITY;
1554 let neg_inf: f32 = f32::NEG_INFINITY;
1555 assert_eq!(1.0f32.powf(1.0), 1.0);
1556 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1557 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1558 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1559 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1560 assert_eq!(8.3f32.powf(0.0), 1.0);
1561 assert!(nan.powf(2.0).is_nan());
1562 assert_eq!(inf.powf(2.0), inf);
1563 assert_eq!(neg_inf.powf(3.0), neg_inf);
1567 fn test_sqrt_domain() {
1568 assert!(NAN.sqrt().is_nan());
1569 assert!(NEG_INFINITY.sqrt().is_nan());
1570 assert!((-1.0f32).sqrt().is_nan());
1571 assert_eq!((-0.0f32).sqrt(), -0.0);
1572 assert_eq!(0.0f32.sqrt(), 0.0);
1573 assert_eq!(1.0f32.sqrt(), 1.0);
1574 assert_eq!(INFINITY.sqrt(), INFINITY);
1579 assert_eq!(1.0, 0.0f32.exp());
1580 assert_approx_eq!(2.718282, 1.0f32.exp());
1581 assert_approx_eq!(148.413162, 5.0f32.exp());
1583 let inf: f32 = f32::INFINITY;
1584 let neg_inf: f32 = f32::NEG_INFINITY;
1585 let nan: f32 = f32::NAN;
1586 assert_eq!(inf, inf.exp());
1587 assert_eq!(0.0, neg_inf.exp());
1588 assert!(nan.exp().is_nan());
1593 assert_eq!(32.0, 5.0f32.exp2());
1594 assert_eq!(1.0, 0.0f32.exp2());
1596 let inf: f32 = f32::INFINITY;
1597 let neg_inf: f32 = f32::NEG_INFINITY;
1598 let nan: f32 = f32::NAN;
1599 assert_eq!(inf, inf.exp2());
1600 assert_eq!(0.0, neg_inf.exp2());
1601 assert!(nan.exp2().is_nan());
1606 let nan: f32 = f32::NAN;
1607 let inf: f32 = f32::INFINITY;
1608 let neg_inf: f32 = f32::NEG_INFINITY;
1609 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1610 assert!(nan.ln().is_nan());
1611 assert_eq!(inf.ln(), inf);
1612 assert!(neg_inf.ln().is_nan());
1613 assert!((-2.3f32).ln().is_nan());
1614 assert_eq!((-0.0f32).ln(), neg_inf);
1615 assert_eq!(0.0f32.ln(), neg_inf);
1616 assert_approx_eq!(4.0f32.ln(), 1.386294);
1621 let nan: f32 = f32::NAN;
1622 let inf: f32 = f32::INFINITY;
1623 let neg_inf: f32 = f32::NEG_INFINITY;
1624 assert_eq!(10.0f32.log(10.0), 1.0);
1625 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1626 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1627 assert!(1.0f32.log(1.0).is_nan());
1628 assert!(1.0f32.log(-13.9).is_nan());
1629 assert!(nan.log(2.3).is_nan());
1630 assert_eq!(inf.log(10.0), inf);
1631 assert!(neg_inf.log(8.8).is_nan());
1632 assert!((-2.3f32).log(0.1).is_nan());
1633 assert_eq!((-0.0f32).log(2.0), neg_inf);
1634 assert_eq!(0.0f32.log(7.0), neg_inf);
1639 let nan: f32 = f32::NAN;
1640 let inf: f32 = f32::INFINITY;
1641 let neg_inf: f32 = f32::NEG_INFINITY;
1642 assert_approx_eq!(10.0f32.log2(), 3.321928);
1643 assert_approx_eq!(2.3f32.log2(), 1.201634);
1644 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1645 assert!(nan.log2().is_nan());
1646 assert_eq!(inf.log2(), inf);
1647 assert!(neg_inf.log2().is_nan());
1648 assert!((-2.3f32).log2().is_nan());
1649 assert_eq!((-0.0f32).log2(), neg_inf);
1650 assert_eq!(0.0f32.log2(), neg_inf);
1655 let nan: f32 = f32::NAN;
1656 let inf: f32 = f32::INFINITY;
1657 let neg_inf: f32 = f32::NEG_INFINITY;
1658 assert_eq!(10.0f32.log10(), 1.0);
1659 assert_approx_eq!(2.3f32.log10(), 0.361728);
1660 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1661 assert_eq!(1.0f32.log10(), 0.0);
1662 assert!(nan.log10().is_nan());
1663 assert_eq!(inf.log10(), inf);
1664 assert!(neg_inf.log10().is_nan());
1665 assert!((-2.3f32).log10().is_nan());
1666 assert_eq!((-0.0f32).log10(), neg_inf);
1667 assert_eq!(0.0f32.log10(), neg_inf);
1671 fn test_to_degrees() {
1672 let pi: f32 = consts::PI;
1673 let nan: f32 = f32::NAN;
1674 let inf: f32 = f32::INFINITY;
1675 let neg_inf: f32 = f32::NEG_INFINITY;
1676 assert_eq!(0.0f32.to_degrees(), 0.0);
1677 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1678 assert_eq!(pi.to_degrees(), 180.0);
1679 assert!(nan.to_degrees().is_nan());
1680 assert_eq!(inf.to_degrees(), inf);
1681 assert_eq!(neg_inf.to_degrees(), neg_inf);
1685 fn test_to_radians() {
1686 let pi: f32 = consts::PI;
1687 let nan: f32 = f32::NAN;
1688 let inf: f32 = f32::INFINITY;
1689 let neg_inf: f32 = f32::NEG_INFINITY;
1690 assert_eq!(0.0f32.to_radians(), 0.0);
1691 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1692 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1693 assert_eq!(180.0f32.to_radians(), pi);
1694 assert!(nan.to_radians().is_nan());
1695 assert_eq!(inf.to_radians(), inf);
1696 assert_eq!(neg_inf.to_radians(), neg_inf);
1701 // We have to use from_str until base-2 exponents
1702 // are supported in floating-point literals
1703 let f1: f32 = f32::from_str_radix("1p-123", 16).unwrap();
1704 let f2: f32 = f32::from_str_radix("1p-111", 16).unwrap();
1705 let f3: f32 = f32::from_str_radix("1.Cp-12", 16).unwrap();
1706 assert_eq!(f32::ldexp(1f32, -123), f1);
1707 assert_eq!(f32::ldexp(1f32, -111), f2);
1708 assert_eq!(f32::ldexp(1.75f32, -12), f3);
1710 assert_eq!(f32::ldexp(0f32, -123), 0f32);
1711 assert_eq!(f32::ldexp(-0f32, -123), -0f32);
1713 let inf: f32 = f32::INFINITY;
1714 let neg_inf: f32 = f32::NEG_INFINITY;
1715 let nan: f32 = f32::NAN;
1716 assert_eq!(f32::ldexp(inf, -123), inf);
1717 assert_eq!(f32::ldexp(neg_inf, -123), neg_inf);
1718 assert!(f32::ldexp(nan, -123).is_nan());
1723 // We have to use from_str until base-2 exponents
1724 // are supported in floating-point literals
1725 let f1: f32 = f32::from_str_radix("1p-123", 16).unwrap();
1726 let f2: f32 = f32::from_str_radix("1p-111", 16).unwrap();
1727 let f3: f32 = f32::from_str_radix("1.Cp-123", 16).unwrap();
1728 let (x1, exp1) = f1.frexp();
1729 let (x2, exp2) = f2.frexp();
1730 let (x3, exp3) = f3.frexp();
1731 assert_eq!((x1, exp1), (0.5f32, -122));
1732 assert_eq!((x2, exp2), (0.5f32, -110));
1733 assert_eq!((x3, exp3), (0.875f32, -122));
1734 assert_eq!(f32::ldexp(x1, exp1), f1);
1735 assert_eq!(f32::ldexp(x2, exp2), f2);
1736 assert_eq!(f32::ldexp(x3, exp3), f3);
1738 assert_eq!(0f32.frexp(), (0f32, 0));
1739 assert_eq!((-0f32).frexp(), (-0f32, 0));
1742 #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
1743 fn test_frexp_nowin() {
1744 let inf: f32 = f32::INFINITY;
1745 let neg_inf: f32 = f32::NEG_INFINITY;
1746 let nan: f32 = f32::NAN;
1747 assert_eq!(match inf.frexp() { (x, _) => x }, inf);
1748 assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
1749 assert!(match nan.frexp() { (x, _) => x.is_nan() })
1754 assert_eq!((-1f32).abs_sub(1f32), 0f32);
1755 assert_eq!(1f32.abs_sub(1f32), 0f32);
1756 assert_eq!(1f32.abs_sub(0f32), 1f32);
1757 assert_eq!(1f32.abs_sub(-1f32), 2f32);
1758 assert_eq!(NEG_INFINITY.abs_sub(0f32), 0f32);
1759 assert_eq!(INFINITY.abs_sub(1f32), INFINITY);
1760 assert_eq!(0f32.abs_sub(NEG_INFINITY), INFINITY);
1761 assert_eq!(0f32.abs_sub(INFINITY), 0f32);
1765 fn test_abs_sub_nowin() {
1766 assert!(NAN.abs_sub(-1f32).is_nan());
1767 assert!(1f32.abs_sub(NAN).is_nan());
1772 assert_eq!(0.0f32.asinh(), 0.0f32);
1773 assert_eq!((-0.0f32).asinh(), -0.0f32);
1775 let inf: f32 = f32::INFINITY;
1776 let neg_inf: f32 = f32::NEG_INFINITY;
1777 let nan: f32 = f32::NAN;
1778 assert_eq!(inf.asinh(), inf);
1779 assert_eq!(neg_inf.asinh(), neg_inf);
1780 assert!(nan.asinh().is_nan());
1781 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1782 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1787 assert_eq!(1.0f32.acosh(), 0.0f32);
1788 assert!(0.999f32.acosh().is_nan());
1790 let inf: f32 = f32::INFINITY;
1791 let neg_inf: f32 = f32::NEG_INFINITY;
1792 let nan: f32 = f32::NAN;
1793 assert_eq!(inf.acosh(), inf);
1794 assert!(neg_inf.acosh().is_nan());
1795 assert!(nan.acosh().is_nan());
1796 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1797 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1802 assert_eq!(0.0f32.atanh(), 0.0f32);
1803 assert_eq!((-0.0f32).atanh(), -0.0f32);
1805 let inf32: f32 = f32::INFINITY;
1806 let neg_inf32: f32 = f32::NEG_INFINITY;
1807 assert_eq!(1.0f32.atanh(), inf32);
1808 assert_eq!((-1.0f32).atanh(), neg_inf32);
1810 assert!(2f64.atanh().atanh().is_nan());
1811 assert!((-2f64).atanh().atanh().is_nan());
1813 let inf64: f32 = f32::INFINITY;
1814 let neg_inf64: f32 = f32::NEG_INFINITY;
1815 let nan32: f32 = f32::NAN;
1816 assert!(inf64.atanh().is_nan());
1817 assert!(neg_inf64.atanh().is_nan());
1818 assert!(nan32.atanh().is_nan());
1820 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1821 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1825 fn test_real_consts() {
1828 let pi: f32 = consts::PI;
1829 let two_pi: f32 = consts::PI_2;
1830 let frac_pi_2: f32 = consts::FRAC_PI_2;
1831 let frac_pi_3: f32 = consts::FRAC_PI_3;
1832 let frac_pi_4: f32 = consts::FRAC_PI_4;
1833 let frac_pi_6: f32 = consts::FRAC_PI_6;
1834 let frac_pi_8: f32 = consts::FRAC_PI_8;
1835 let frac_1_pi: f32 = consts::FRAC_1_PI;
1836 let frac_2_pi: f32 = consts::FRAC_2_PI;
1837 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1838 let sqrt2: f32 = consts::SQRT_2;
1839 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1840 let e: f32 = consts::E;
1841 let log2_e: f32 = consts::LOG2_E;
1842 let log10_e: f32 = consts::LOG10_E;
1843 let ln_2: f32 = consts::LN_2;
1844 let ln_10: f32 = consts::LN_10;
1846 assert_approx_eq!(two_pi, 2f32 * pi);
1847 assert_approx_eq!(frac_pi_2, pi / 2f32);
1848 assert_approx_eq!(frac_pi_3, pi / 3f32);
1849 assert_approx_eq!(frac_pi_4, pi / 4f32);
1850 assert_approx_eq!(frac_pi_6, pi / 6f32);
1851 assert_approx_eq!(frac_pi_8, pi / 8f32);
1852 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1853 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1854 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1855 assert_approx_eq!(sqrt2, 2f32.sqrt());
1856 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1857 assert_approx_eq!(log2_e, e.log2());
1858 assert_approx_eq!(log10_e, e.log10());
1859 assert_approx_eq!(ln_2, 2f32.ln());
1860 assert_approx_eq!(ln_10, 10f32.ln());