1 // Copyright 2012-2014 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 //! Operations and constants for 32-bits floats (`f32` type)
13 #![doc(primitive = "f32")]
14 // FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
15 #![allow(overflowing_literals)]
22 use num::FpCategory as Fp;
23 use num::from_str_radix;
27 pub const RADIX: uint = 2u;
30 pub const MANTISSA_DIGITS: uint = 24u;
32 pub const DIGITS: uint = 6u;
35 pub const EPSILON: f32 = 1.19209290e-07_f32;
37 /// Smallest finite f32 value
39 pub const MIN_VALUE: f32 = -3.40282347e+38_f32;
40 /// Smallest positive, normalized f32 value
42 pub const MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
43 /// Largest finite f32 value
45 pub const MAX_VALUE: f32 = 3.40282347e+38_f32;
48 pub const MIN_EXP: int = -125;
50 pub const MAX_EXP: int = 128;
53 pub const MIN_10_EXP: int = -37;
55 pub const MAX_10_EXP: int = 38;
58 pub const NAN: f32 = 0.0_f32/0.0_f32;
60 pub const INFINITY: f32 = 1.0_f32/0.0_f32;
62 pub const NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
64 /// Various useful constants.
65 #[unstable = "naming scheme needs to be revisited"]
67 // FIXME: replace with mathematical constants from cmath.
69 /// Archimedes' constant
70 pub const PI: f32 = 3.14159265358979323846264338327950288_f32;
73 pub const PI_2: f32 = 6.28318530717958647692528676655900576_f32;
76 pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
79 pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
82 pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
85 pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
88 pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
91 pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
94 pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
97 pub const FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
100 pub const SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
103 pub const FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
106 pub const E: f32 = 2.71828182845904523536028747135266250_f32;
109 pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
112 pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
115 pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32;
118 pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32;
121 #[unstable = "trait is unstable"]
124 fn nan() -> f32 { NAN }
127 fn infinity() -> f32 { INFINITY }
130 fn neg_infinity() -> f32 { NEG_INFINITY }
133 fn zero() -> f32 { 0.0 }
136 fn neg_zero() -> f32 { -0.0 }
139 fn one() -> f32 { 1.0 }
141 /// Returns `true` if the number is NaN.
143 fn is_nan(self) -> bool { self != self }
145 /// Returns `true` if the number is infinite.
147 fn is_infinite(self) -> bool {
148 self == Float::infinity() || self == Float::neg_infinity()
151 /// Returns `true` if the number is neither infinite or NaN.
153 fn is_finite(self) -> bool {
154 !(self.is_nan() || self.is_infinite())
157 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
159 fn is_normal(self) -> bool {
160 self.classify() == Fp::Normal
163 /// Returns the floating point category of the number. If only one property
164 /// is going to be tested, it is generally faster to use the specific
165 /// predicate instead.
166 fn classify(self) -> Fp {
167 const EXP_MASK: u32 = 0x7f800000;
168 const MAN_MASK: u32 = 0x007fffff;
170 let bits: u32 = unsafe { mem::transmute(self) };
171 match (bits & MAN_MASK, bits & EXP_MASK) {
173 (_, 0) => Fp::Subnormal,
174 (0, EXP_MASK) => Fp::Infinite,
175 (_, EXP_MASK) => Fp::Nan,
181 fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS }
184 fn digits(_: Option<f32>) -> uint { DIGITS }
187 fn epsilon() -> f32 { EPSILON }
190 fn min_exp(_: Option<f32>) -> int { MIN_EXP }
193 fn max_exp(_: Option<f32>) -> int { MAX_EXP }
196 fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
199 fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
202 fn min_value() -> f32 { MIN_VALUE }
205 fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
208 fn max_value() -> f32 { MAX_VALUE }
210 /// Returns the mantissa, exponent and sign as integers.
211 fn integer_decode(self) -> (u64, i16, i8) {
212 let bits: u32 = unsafe { mem::transmute(self) };
213 let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
214 let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
215 let mantissa = if exponent == 0 {
216 (bits & 0x7fffff) << 1
218 (bits & 0x7fffff) | 0x800000
220 // Exponent bias + mantissa shift
221 exponent -= 127 + 23;
222 (mantissa as u64, exponent, sign)
225 /// Rounds towards minus infinity.
227 fn floor(self) -> f32 {
228 unsafe { intrinsics::floorf32(self) }
231 /// Rounds towards plus infinity.
233 fn ceil(self) -> f32 {
234 unsafe { intrinsics::ceilf32(self) }
237 /// Rounds to nearest integer. Rounds half-way cases away from zero.
239 fn round(self) -> f32 {
240 unsafe { intrinsics::roundf32(self) }
243 /// Returns the integer part of the number (rounds towards zero).
245 fn trunc(self) -> f32 {
246 unsafe { intrinsics::truncf32(self) }
249 /// The fractional part of the number, satisfying:
252 /// use core::num::Float;
255 /// assert!(x == x.trunc() + x.fract())
258 fn fract(self) -> f32 { self - self.trunc() }
260 /// Computes the absolute value of `self`. Returns `Float::nan()` if the
261 /// number is `Float::nan()`.
263 fn abs(self) -> f32 {
264 unsafe { intrinsics::fabsf32(self) }
267 /// Returns a number that represents the sign of `self`.
269 /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
270 /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
271 /// - `Float::nan()` if the number is `Float::nan()`
273 fn signum(self) -> f32 {
277 unsafe { intrinsics::copysignf32(1.0, self) }
281 /// Returns `true` if `self` is positive, including `+0.0` and
282 /// `Float::infinity()`.
284 fn is_positive(self) -> bool {
285 self > 0.0 || (1.0 / self) == Float::infinity()
288 /// Returns `true` if `self` is negative, including `-0.0` and
289 /// `Float::neg_infinity()`.
291 fn is_negative(self) -> bool {
292 self < 0.0 || (1.0 / self) == Float::neg_infinity()
295 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
296 /// error. This produces a more accurate result with better performance than
297 /// a separate multiplication operation followed by an add.
299 fn mul_add(self, a: f32, b: f32) -> f32 {
300 unsafe { intrinsics::fmaf32(self, a, b) }
303 /// Returns the reciprocal (multiplicative inverse) of the number.
305 fn recip(self) -> f32 { 1.0 / self }
308 fn powi(self, n: i32) -> f32 {
309 unsafe { intrinsics::powif32(self, n) }
313 fn powf(self, n: f32) -> f32 {
314 unsafe { intrinsics::powf32(self, n) }
319 fn sqrt2() -> f32 { consts::SQRT2 }
323 fn frac_1_sqrt2() -> f32 { consts::FRAC_1_SQRT2 }
326 fn sqrt(self) -> f32 {
330 unsafe { intrinsics::sqrtf32(self) }
335 fn rsqrt(self) -> f32 { self.sqrt().recip() }
337 /// Archimedes' constant
339 fn pi() -> f32 { consts::PI }
343 fn two_pi() -> f32 { consts::PI_2 }
347 fn frac_pi_2() -> f32 { consts::FRAC_PI_2 }
351 fn frac_pi_3() -> f32 { consts::FRAC_PI_3 }
355 fn frac_pi_4() -> f32 { consts::FRAC_PI_4 }
359 fn frac_pi_6() -> f32 { consts::FRAC_PI_6 }
363 fn frac_pi_8() -> f32 { consts::FRAC_PI_8 }
367 fn frac_1_pi() -> f32 { consts::FRAC_1_PI }
371 fn frac_2_pi() -> f32 { consts::FRAC_2_PI }
375 fn frac_2_sqrtpi() -> f32 { consts::FRAC_2_SQRTPI }
379 fn e() -> f32 { consts::E }
383 fn log2_e() -> f32 { consts::LOG2_E }
387 fn log10_e() -> f32 { consts::LOG10_E }
391 fn ln_2() -> f32 { consts::LN_2 }
395 fn ln_10() -> f32 { consts::LN_10 }
397 /// Returns the exponential of the number.
399 fn exp(self) -> f32 {
400 unsafe { intrinsics::expf32(self) }
403 /// Returns 2 raised to the power of the number.
405 fn exp2(self) -> f32 {
406 unsafe { intrinsics::exp2f32(self) }
409 /// Returns the natural logarithm of the number.
412 unsafe { intrinsics::logf32(self) }
415 /// Returns the logarithm of the number with respect to an arbitrary base.
417 fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
419 /// Returns the base 2 logarithm of the number.
421 fn log2(self) -> f32 {
422 unsafe { intrinsics::log2f32(self) }
425 /// Returns the base 10 logarithm of the number.
427 fn log10(self) -> f32 {
428 unsafe { intrinsics::log10f32(self) }
431 /// Converts to degrees, assuming the number is in radians.
433 fn to_degrees(self) -> f32 { self * (180.0f32 / consts::PI) }
435 /// Converts to radians, assuming the number is in degrees.
437 fn to_radians(self) -> f32 {
438 let value: f32 = consts::PI;
439 self * (value / 180.0f32)
444 #[allow(missing_docs)]
445 #[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
446 pub fn from_str_hex(src: &str) -> Option<f32> {
447 from_str_radix(src, 16)