]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/f32.rs
core: Split apart the global `core` feature
[rust.git] / src / libcore / num / f32.rs
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.
4 //
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.
10
11 //! Operations and constants for 32-bits floats (`f32` type)
12
13 #![doc(primitive = "f32")]
14 // FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
15 #![allow(overflowing_literals)]
16
17 #![stable(feature = "rust1", since = "1.0.0")]
18
19 use prelude::*;
20
21 use intrinsics;
22 use mem;
23 use num::{Float, ParseFloatError};
24 use num::FpCategory as Fp;
25
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub const RADIX: u32 = 2;
28
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub const MANTISSA_DIGITS: u32 = 24;
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub const DIGITS: u32 = 6;
33
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub const EPSILON: f32 = 1.19209290e-07_f32;
36
37 /// Smallest finite f32 value
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub const MIN: f32 = -3.40282347e+38_f32;
40 /// Smallest positive, normalized f32 value
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32;
43 /// Largest finite f32 value
44 #[stable(feature = "rust1", since = "1.0.0")]
45 pub const MAX: f32 = 3.40282347e+38_f32;
46
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub const MIN_EXP: i32 = -125;
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub const MAX_EXP: i32 = 128;
51
52 #[stable(feature = "rust1", since = "1.0.0")]
53 pub const MIN_10_EXP: i32 = -37;
54 #[stable(feature = "rust1", since = "1.0.0")]
55 pub const MAX_10_EXP: i32 = 38;
56
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub const NAN: f32 = 0.0_f32/0.0_f32;
59 #[stable(feature = "rust1", since = "1.0.0")]
60 pub const INFINITY: f32 = 1.0_f32/0.0_f32;
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub const NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
63
64 /// Basic mathematial constants.
65 #[stable(feature = "rust1", since = "1.0.0")]
66 pub mod consts {
67     // FIXME: replace with mathematical constants from cmath.
68
69     /// Archimedes' constant
70     #[stable(feature = "rust1", since = "1.0.0")]
71     pub const PI: f32 = 3.14159265358979323846264338327950288_f32;
72
73     /// pi * 2.0
74     #[unstable(feature = "float_consts",
75                reason = "unclear naming convention/usefulness")]
76     pub const PI_2: f32 = 6.28318530717958647692528676655900576_f32;
77
78     /// pi/2.0
79     #[stable(feature = "rust1", since = "1.0.0")]
80     pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
81
82     /// pi/3.0
83     #[stable(feature = "rust1", since = "1.0.0")]
84     pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
85
86     /// pi/4.0
87     #[stable(feature = "rust1", since = "1.0.0")]
88     pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
89
90     /// pi/6.0
91     #[stable(feature = "rust1", since = "1.0.0")]
92     pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
93
94     /// pi/8.0
95     #[stable(feature = "rust1", since = "1.0.0")]
96     pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
97
98     /// 1.0/pi
99     #[stable(feature = "rust1", since = "1.0.0")]
100     pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
101
102     /// 2.0/pi
103     #[stable(feature = "rust1", since = "1.0.0")]
104     pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
105
106     /// 2.0/sqrt(pi)
107     #[stable(feature = "rust1", since = "1.0.0")]
108     pub const FRAC_2_SQRT_PI: f32 = 1.12837916709551257389615890312154517_f32;
109
110     /// sqrt(2.0)
111     #[stable(feature = "rust1", since = "1.0.0")]
112     pub const SQRT_2: f32 = 1.41421356237309504880168872420969808_f32;
113
114     /// 1.0/sqrt(2.0)
115     #[stable(feature = "rust1", since = "1.0.0")]
116     pub const FRAC_1_SQRT_2: f32 = 0.707106781186547524400844362104849039_f32;
117
118     /// Euler's number
119     #[stable(feature = "rust1", since = "1.0.0")]
120     pub const E: f32 = 2.71828182845904523536028747135266250_f32;
121
122     /// log2(e)
123     #[stable(feature = "rust1", since = "1.0.0")]
124     pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
125
126     /// log10(e)
127     #[stable(feature = "rust1", since = "1.0.0")]
128     pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
129
130     /// ln(2.0)
131     #[stable(feature = "rust1", since = "1.0.0")]
132     pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32;
133
134     /// ln(10.0)
135     #[stable(feature = "rust1", since = "1.0.0")]
136     pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32;
137 }
138
139 impl Float for f32 {
140     #[inline]
141     fn nan() -> f32 { NAN }
142
143     #[inline]
144     fn infinity() -> f32 { INFINITY }
145
146     #[inline]
147     fn neg_infinity() -> f32 { NEG_INFINITY }
148
149     #[inline]
150     fn zero() -> f32 { 0.0 }
151
152     #[inline]
153     fn neg_zero() -> f32 { -0.0 }
154
155     #[inline]
156     fn one() -> f32 { 1.0 }
157
158     from_str_radix_float_impl! { f32 }
159
160     /// Returns `true` if the number is NaN.
161     #[inline]
162     fn is_nan(self) -> bool { self != self }
163
164     /// Returns `true` if the number is infinite.
165     #[inline]
166     fn is_infinite(self) -> bool {
167         self == Float::infinity() || self == Float::neg_infinity()
168     }
169
170     /// Returns `true` if the number is neither infinite or NaN.
171     #[inline]
172     fn is_finite(self) -> bool {
173         !(self.is_nan() || self.is_infinite())
174     }
175
176     /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
177     #[inline]
178     fn is_normal(self) -> bool {
179         self.classify() == Fp::Normal
180     }
181
182     /// Returns the floating point category of the number. If only one property
183     /// is going to be tested, it is generally faster to use the specific
184     /// predicate instead.
185     fn classify(self) -> Fp {
186         const EXP_MASK: u32 = 0x7f800000;
187         const MAN_MASK: u32 = 0x007fffff;
188
189         let bits: u32 = unsafe { mem::transmute(self) };
190         match (bits & MAN_MASK, bits & EXP_MASK) {
191             (0, 0)        => Fp::Zero,
192             (_, 0)        => Fp::Subnormal,
193             (0, EXP_MASK) => Fp::Infinite,
194             (_, EXP_MASK) => Fp::Nan,
195             _             => Fp::Normal,
196         }
197     }
198
199     /// Returns the mantissa, exponent and sign as integers.
200     fn integer_decode(self) -> (u64, i16, i8) {
201         let bits: u32 = unsafe { mem::transmute(self) };
202         let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
203         let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
204         let mantissa = if exponent == 0 {
205             (bits & 0x7fffff) << 1
206         } else {
207             (bits & 0x7fffff) | 0x800000
208         };
209         // Exponent bias + mantissa shift
210         exponent -= 127 + 23;
211         (mantissa as u64, exponent, sign)
212     }
213
214     /// Rounds towards minus infinity.
215     #[inline]
216     fn floor(self) -> f32 {
217         unsafe { intrinsics::floorf32(self) }
218     }
219
220     /// Rounds towards plus infinity.
221     #[inline]
222     fn ceil(self) -> f32 {
223         unsafe { intrinsics::ceilf32(self) }
224     }
225
226     /// Rounds to nearest integer. Rounds half-way cases away from zero.
227     #[inline]
228     fn round(self) -> f32 {
229         unsafe { intrinsics::roundf32(self) }
230     }
231
232     /// Returns the integer part of the number (rounds towards zero).
233     #[inline]
234     fn trunc(self) -> f32 {
235         unsafe { intrinsics::truncf32(self) }
236     }
237
238     /// The fractional part of the number, satisfying:
239     ///
240     /// ```
241     /// let x = 1.65f32;
242     /// assert!(x == x.trunc() + x.fract())
243     /// ```
244     #[inline]
245     fn fract(self) -> f32 { self - self.trunc() }
246
247     /// Computes the absolute value of `self`. Returns `Float::nan()` if the
248     /// number is `Float::nan()`.
249     #[inline]
250     fn abs(self) -> f32 {
251         unsafe { intrinsics::fabsf32(self) }
252     }
253
254     /// Returns a number that represents the sign of `self`.
255     ///
256     /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
257     /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
258     /// - `Float::nan()` if the number is `Float::nan()`
259     #[inline]
260     fn signum(self) -> f32 {
261         if self.is_nan() {
262             Float::nan()
263         } else {
264             unsafe { intrinsics::copysignf32(1.0, self) }
265         }
266     }
267
268     /// Returns `true` if `self` is positive, including `+0.0` and
269     /// `Float::infinity()`.
270     #[inline]
271     fn is_positive(self) -> bool {
272         self > 0.0 || (1.0 / self) == Float::infinity()
273     }
274
275     /// Returns `true` if `self` is negative, including `-0.0` and
276     /// `Float::neg_infinity()`.
277     #[inline]
278     fn is_negative(self) -> bool {
279         self < 0.0 || (1.0 / self) == Float::neg_infinity()
280     }
281
282     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
283     /// error. This produces a more accurate result with better performance than
284     /// a separate multiplication operation followed by an add.
285     #[inline]
286     fn mul_add(self, a: f32, b: f32) -> f32 {
287         unsafe { intrinsics::fmaf32(self, a, b) }
288     }
289
290     /// Returns the reciprocal (multiplicative inverse) of the number.
291     #[inline]
292     fn recip(self) -> f32 { 1.0 / self }
293
294     #[inline]
295     fn powi(self, n: i32) -> f32 {
296         unsafe { intrinsics::powif32(self, n) }
297     }
298
299     #[inline]
300     fn powf(self, n: f32) -> f32 {
301         unsafe { intrinsics::powf32(self, n) }
302     }
303
304     #[inline]
305     fn sqrt(self) -> f32 {
306         if self < 0.0 {
307             NAN
308         } else {
309             unsafe { intrinsics::sqrtf32(self) }
310         }
311     }
312
313     #[inline]
314     fn rsqrt(self) -> f32 { self.sqrt().recip() }
315
316     /// Returns the exponential of the number.
317     #[inline]
318     fn exp(self) -> f32 {
319         unsafe { intrinsics::expf32(self) }
320     }
321
322     /// Returns 2 raised to the power of the number.
323     #[inline]
324     fn exp2(self) -> f32 {
325         unsafe { intrinsics::exp2f32(self) }
326     }
327
328     /// Returns the natural logarithm of the number.
329     #[inline]
330     fn ln(self) -> f32 {
331         unsafe { intrinsics::logf32(self) }
332     }
333
334     /// Returns the logarithm of the number with respect to an arbitrary base.
335     #[inline]
336     fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
337
338     /// Returns the base 2 logarithm of the number.
339     #[inline]
340     fn log2(self) -> f32 {
341         unsafe { intrinsics::log2f32(self) }
342     }
343
344     /// Returns the base 10 logarithm of the number.
345     #[inline]
346     fn log10(self) -> f32 {
347         unsafe { intrinsics::log10f32(self) }
348     }
349
350     /// Converts to degrees, assuming the number is in radians.
351     #[inline]
352     fn to_degrees(self) -> f32 { self * (180.0f32 / consts::PI) }
353
354     /// Converts to radians, assuming the number is in degrees.
355     #[inline]
356     fn to_radians(self) -> f32 {
357         let value: f32 = consts::PI;
358         self * (value / 180.0f32)
359     }
360 }