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