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;
26 pub const RADIX: uint = 2u;
29 pub const MANTISSA_DIGITS: uint = 24u;
31 pub const DIGITS: uint = 6u;
34 pub const EPSILON: f32 = 1.19209290e-07_f32;
36 /// Smallest finite f32 value
38 pub const MIN_VALUE: f32 = -3.40282347e+38_f32;
39 /// Smallest positive, normalized f32 value
41 pub const MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
42 /// Largest finite f32 value
44 pub const MAX_VALUE: f32 = 3.40282347e+38_f32;
47 pub const MIN_EXP: int = -125;
49 pub const MAX_EXP: int = 128;
52 pub const MIN_10_EXP: int = -37;
54 pub const MAX_10_EXP: int = 38;
57 pub const NAN: f32 = 0.0_f32/0.0_f32;
59 pub const INFINITY: f32 = 1.0_f32/0.0_f32;
61 pub const NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
63 /// Various useful constants.
64 #[unstable = "naming scheme needs to be revisited"]
66 // FIXME: replace with mathematical constants from cmath.
68 /// Archimedes' constant
69 pub const PI: f32 = 3.14159265358979323846264338327950288_f32;
72 pub const PI_2: f32 = 6.28318530717958647692528676655900576_f32;
75 pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
78 pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
81 pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
84 pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
87 pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
90 pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
93 pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
96 pub const FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
99 pub const SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
102 pub const FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
105 pub const E: f32 = 2.71828182845904523536028747135266250_f32;
108 pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
111 pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
114 pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32;
117 pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32;
120 #[unstable = "trait is unstable"]
123 fn nan() -> f32 { NAN }
126 fn infinity() -> f32 { INFINITY }
129 fn neg_infinity() -> f32 { NEG_INFINITY }
132 fn zero() -> f32 { 0.0 }
135 fn neg_zero() -> f32 { -0.0 }
138 fn one() -> f32 { 1.0 }
140 /// Returns `true` if the number is NaN.
142 fn is_nan(self) -> bool { self != self }
144 /// Returns `true` if the number is infinite.
146 fn is_infinite(self) -> bool {
147 self == Float::infinity() || self == Float::neg_infinity()
150 /// Returns `true` if the number is neither infinite or NaN.
152 fn is_finite(self) -> bool {
153 !(self.is_nan() || self.is_infinite())
156 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
158 fn is_normal(self) -> bool {
159 self.classify() == Fp::Normal
162 /// Returns the floating point category of the number. If only one property
163 /// is going to be tested, it is generally faster to use the specific
164 /// predicate instead.
165 fn classify(self) -> Fp {
166 const EXP_MASK: u32 = 0x7f800000;
167 const MAN_MASK: u32 = 0x007fffff;
169 let bits: u32 = unsafe { mem::transmute(self) };
170 match (bits & MAN_MASK, bits & EXP_MASK) {
172 (_, 0) => Fp::Subnormal,
173 (0, EXP_MASK) => Fp::Infinite,
174 (_, EXP_MASK) => Fp::Nan,
180 fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS }
183 fn digits(_: Option<f32>) -> uint { DIGITS }
186 fn epsilon() -> f32 { EPSILON }
189 fn min_exp(_: Option<f32>) -> int { MIN_EXP }
192 fn max_exp(_: Option<f32>) -> int { MAX_EXP }
195 fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
198 fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
201 fn min_value() -> f32 { MIN_VALUE }
204 fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
207 fn max_value() -> f32 { MAX_VALUE }
209 /// Returns the mantissa, exponent and sign as integers.
210 fn integer_decode(self) -> (u64, i16, i8) {
211 let bits: u32 = unsafe { mem::transmute(self) };
212 let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
213 let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
214 let mantissa = if exponent == 0 {
215 (bits & 0x7fffff) << 1
217 (bits & 0x7fffff) | 0x800000
219 // Exponent bias + mantissa shift
220 exponent -= 127 + 23;
221 (mantissa as u64, exponent, sign)
224 /// Rounds towards minus infinity.
226 fn floor(self) -> f32 {
227 unsafe { intrinsics::floorf32(self) }
230 /// Rounds towards plus infinity.
232 fn ceil(self) -> f32 {
233 unsafe { intrinsics::ceilf32(self) }
236 /// Rounds to nearest integer. Rounds half-way cases away from zero.
238 fn round(self) -> f32 {
239 unsafe { intrinsics::roundf32(self) }
242 /// Returns the integer part of the number (rounds towards zero).
244 fn trunc(self) -> f32 {
245 unsafe { intrinsics::truncf32(self) }
248 /// The fractional part of the number, satisfying:
251 /// use core::num::Float;
254 /// assert!(x == x.trunc() + x.fract())
257 fn fract(self) -> f32 { self - self.trunc() }
259 /// Computes the absolute value of `self`. Returns `Float::nan()` if the
260 /// number is `Float::nan()`.
262 fn abs(self) -> f32 {
263 unsafe { intrinsics::fabsf32(self) }
266 /// Returns a number that represents the sign of `self`.
268 /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
269 /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
270 /// - `Float::nan()` if the number is `Float::nan()`
272 fn signum(self) -> f32 {
276 unsafe { intrinsics::copysignf32(1.0, self) }
280 /// Returns `true` if `self` is positive, including `+0.0` and
281 /// `Float::infinity()`.
283 fn is_positive(self) -> bool {
284 self > 0.0 || (1.0 / self) == Float::infinity()
287 /// Returns `true` if `self` is negative, including `-0.0` and
288 /// `Float::neg_infinity()`.
290 fn is_negative(self) -> bool {
291 self < 0.0 || (1.0 / self) == Float::neg_infinity()
294 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
295 /// error. This produces a more accurate result with better performance than
296 /// a separate multiplication operation followed by an add.
298 fn mul_add(self, a: f32, b: f32) -> f32 {
299 unsafe { intrinsics::fmaf32(self, a, b) }
302 /// Returns the reciprocal (multiplicative inverse) of the number.
304 fn recip(self) -> f32 { 1.0 / self }
307 fn powi(self, n: i32) -> f32 {
308 unsafe { intrinsics::powif32(self, n) }
312 fn powf(self, n: f32) -> f32 {
313 unsafe { intrinsics::powf32(self, n) }
317 fn sqrt(self) -> f32 {
321 unsafe { intrinsics::sqrtf32(self) }
326 fn rsqrt(self) -> f32 { self.sqrt().recip() }
328 /// Returns the exponential of the number.
330 fn exp(self) -> f32 {
331 unsafe { intrinsics::expf32(self) }
334 /// Returns 2 raised to the power of the number.
336 fn exp2(self) -> f32 {
337 unsafe { intrinsics::exp2f32(self) }
340 /// Returns the natural logarithm of the number.
343 unsafe { intrinsics::logf32(self) }
346 /// Returns the logarithm of the number with respect to an arbitrary base.
348 fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
350 /// Returns the base 2 logarithm of the number.
352 fn log2(self) -> f32 {
353 unsafe { intrinsics::log2f32(self) }
356 /// Returns the base 10 logarithm of the number.
358 fn log10(self) -> f32 {
359 unsafe { intrinsics::log10f32(self) }
362 /// Converts to degrees, assuming the number is in radians.
364 fn to_degrees(self) -> f32 { self * (180.0f32 / consts::PI) }
366 /// Converts to radians, assuming the number is in degrees.
368 fn to_radians(self) -> f32 {
369 let value: f32 = consts::PI;
370 self * (value / 180.0f32)