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