]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/f32.rs
Auto merge of #42430 - nagisa:core-float, r=alexcrichton
[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 // FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
14 #![allow(overflowing_literals)]
15
16 #![stable(feature = "rust1", since = "1.0.0")]
17
18 use intrinsics;
19 use mem;
20 use num::Float;
21 use num::FpCategory as Fp;
22
23 /// The radix or base of the internal representation of `f32`.
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub const RADIX: u32 = 2;
26
27 /// Number of significant digits in base 2.
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub const MANTISSA_DIGITS: u32 = 24;
30 /// Approximate number of significant digits in base 10.
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub const DIGITS: u32 = 6;
33
34 /// Difference between `1.0` and the next largest representable number.
35 #[stable(feature = "rust1", since = "1.0.0")]
36 pub const EPSILON: f32 = 1.19209290e-07_f32;
37
38 /// Smallest finite `f32` value.
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub const MIN: f32 = -3.40282347e+38_f32;
41 /// Smallest positive normal `f32` value.
42 #[stable(feature = "rust1", since = "1.0.0")]
43 pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32;
44 /// Largest finite `f32` value.
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub const MAX: f32 = 3.40282347e+38_f32;
47
48 /// One greater than the minimum possible normal power of 2 exponent.
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub const MIN_EXP: i32 = -125;
51 /// Maximum possible power of 2 exponent.
52 #[stable(feature = "rust1", since = "1.0.0")]
53 pub const MAX_EXP: i32 = 128;
54
55 /// Minimum possible normal power of 10 exponent.
56 #[stable(feature = "rust1", since = "1.0.0")]
57 pub const MIN_10_EXP: i32 = -37;
58 /// Maximum possible power of 10 exponent.
59 #[stable(feature = "rust1", since = "1.0.0")]
60 pub const MAX_10_EXP: i32 = 38;
61
62 /// Not a Number (NaN).
63 #[stable(feature = "rust1", since = "1.0.0")]
64 pub const NAN: f32 = 0.0_f32 / 0.0_f32;
65 /// Infinity (∞).
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub const INFINITY: f32 = 1.0_f32 / 0.0_f32;
68 /// Negative infinity (-∞).
69 #[stable(feature = "rust1", since = "1.0.0")]
70 pub const NEG_INFINITY: f32 = -1.0_f32 / 0.0_f32;
71
72 /// Basic mathematical constants.
73 #[stable(feature = "rust1", since = "1.0.0")]
74 pub mod consts {
75     // FIXME: replace with mathematical constants from cmath.
76
77     /// Archimedes' constant (π)
78     #[stable(feature = "rust1", since = "1.0.0")]
79     pub const PI: f32 = 3.14159265358979323846264338327950288_f32;
80
81     /// π/2
82     #[stable(feature = "rust1", since = "1.0.0")]
83     pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
84
85     /// π/3
86     #[stable(feature = "rust1", since = "1.0.0")]
87     pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
88
89     /// π/4
90     #[stable(feature = "rust1", since = "1.0.0")]
91     pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
92
93     /// π/6
94     #[stable(feature = "rust1", since = "1.0.0")]
95     pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
96
97     /// π/8
98     #[stable(feature = "rust1", since = "1.0.0")]
99     pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
100
101     /// 1/π
102     #[stable(feature = "rust1", since = "1.0.0")]
103     pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
104
105     /// 2/π
106     #[stable(feature = "rust1", since = "1.0.0")]
107     pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
108
109     /// 2/sqrt(π)
110     #[stable(feature = "rust1", since = "1.0.0")]
111     pub const FRAC_2_SQRT_PI: f32 = 1.12837916709551257389615890312154517_f32;
112
113     /// sqrt(2)
114     #[stable(feature = "rust1", since = "1.0.0")]
115     pub const SQRT_2: f32 = 1.41421356237309504880168872420969808_f32;
116
117     /// 1/sqrt(2)
118     #[stable(feature = "rust1", since = "1.0.0")]
119     pub const FRAC_1_SQRT_2: f32 = 0.707106781186547524400844362104849039_f32;
120
121     /// Euler's number (e)
122     #[stable(feature = "rust1", since = "1.0.0")]
123     pub const E: f32 = 2.71828182845904523536028747135266250_f32;
124
125     /// log<sub>2</sub>(e)
126     #[stable(feature = "rust1", since = "1.0.0")]
127     pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
128
129     /// log<sub>10</sub>(e)
130     #[stable(feature = "rust1", since = "1.0.0")]
131     pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
132
133     /// ln(2)
134     #[stable(feature = "rust1", since = "1.0.0")]
135     pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32;
136
137     /// ln(10)
138     #[stable(feature = "rust1", since = "1.0.0")]
139     pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32;
140 }
141
142 #[unstable(feature = "core_float",
143            reason = "stable interface is via `impl f{32,64}` in later crates",
144            issue = "32110")]
145 impl Float for f32 {
146     /// Returns `true` if the number is NaN.
147     #[inline]
148     fn is_nan(self) -> bool {
149         self != self
150     }
151
152     /// Returns `true` if the number is infinite.
153     #[inline]
154     fn is_infinite(self) -> bool {
155         self == INFINITY || self == NEG_INFINITY
156     }
157
158     /// Returns `true` if the number is neither infinite or NaN.
159     #[inline]
160     fn is_finite(self) -> bool {
161         !(self.is_nan() || self.is_infinite())
162     }
163
164     /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
165     #[inline]
166     fn is_normal(self) -> bool {
167         self.classify() == Fp::Normal
168     }
169
170     /// Returns the floating point category of the number. If only one property
171     /// is going to be tested, it is generally faster to use the specific
172     /// predicate instead.
173     fn classify(self) -> Fp {
174         const EXP_MASK: u32 = 0x7f800000;
175         const MAN_MASK: u32 = 0x007fffff;
176
177         let bits: u32 = unsafe { mem::transmute(self) };
178         match (bits & MAN_MASK, bits & EXP_MASK) {
179             (0, 0) => Fp::Zero,
180             (_, 0) => Fp::Subnormal,
181             (0, EXP_MASK) => Fp::Infinite,
182             (_, EXP_MASK) => Fp::Nan,
183             _ => Fp::Normal,
184         }
185     }
186
187     /// Computes the absolute value of `self`. Returns `Float::nan()` if the
188     /// number is `Float::nan()`.
189     #[inline]
190     fn abs(self) -> f32 {
191         unsafe { intrinsics::fabsf32(self) }
192     }
193
194     /// Returns a number that represents the sign of `self`.
195     ///
196     /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
197     /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
198     /// - `Float::nan()` if the number is `Float::nan()`
199     #[inline]
200     fn signum(self) -> f32 {
201         if self.is_nan() {
202             NAN
203         } else {
204             unsafe { intrinsics::copysignf32(1.0, self) }
205         }
206     }
207
208     /// Returns `true` if `self` is positive, including `+0.0` and
209     /// `Float::infinity()`.
210     #[inline]
211     fn is_sign_positive(self) -> bool {
212         self > 0.0 || (1.0 / self) == INFINITY
213     }
214
215     /// Returns `true` if `self` is negative, including `-0.0` and
216     /// `Float::neg_infinity()`.
217     #[inline]
218     fn is_sign_negative(self) -> bool {
219         self < 0.0 || (1.0 / self) == NEG_INFINITY
220     }
221
222     /// Returns the reciprocal (multiplicative inverse) of the number.
223     #[inline]
224     fn recip(self) -> f32 {
225         1.0 / self
226     }
227
228     #[inline]
229     fn powi(self, n: i32) -> f32 {
230         unsafe { intrinsics::powif32(self, n) }
231     }
232
233     /// Converts to degrees, assuming the number is in radians.
234     #[inline]
235     fn to_degrees(self) -> f32 {
236         self * (180.0f32 / consts::PI)
237     }
238
239     /// Converts to radians, assuming the number is in degrees.
240     #[inline]
241     fn to_radians(self) -> f32 {
242         let value: f32 = consts::PI;
243         self * (value / 180.0f32)
244     }
245
246     /// Returns the maximum of the two numbers.
247     #[inline]
248     fn max(self, other: f32) -> f32 {
249         // IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
250         // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
251         // is either x or y, canonicalized (this means results might differ among implementations).
252         // When either x or y is a signalingNaN, then the result is according to 6.2.
253         //
254         // Since we do not support sNaN in Rust yet, we do not need to handle them.
255         // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
256         // multiplying by 1.0. Should switch to the `canonicalize` when it works.
257         (if self < other || self.is_nan() { other } else { self }) * 1.0
258     }
259
260     /// Returns the minimum of the two numbers.
261     #[inline]
262     fn min(self, other: f32) -> f32 {
263         // IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
264         // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
265         // is either x or y, canonicalized (this means results might differ among implementations).
266         // When either x or y is a signalingNaN, then the result is according to 6.2.
267         //
268         // Since we do not support sNaN in Rust yet, we do not need to handle them.
269         // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
270         // multiplying by 1.0. Should switch to the `canonicalize` when it works.
271         (if self < other || other.is_nan() { self } else { other }) * 1.0
272     }
273 }