]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
1a4d82fc JJ |
13 | // FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353 |
14 | #![allow(overflowing_literals)] | |
15 | ||
85aaf69f | 16 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 17 | |
e9174d1e | 18 | use prelude::v1::*; |
9346a6ac | 19 | |
1a4d82fc JJ |
20 | use intrinsics; |
21 | use mem; | |
9346a6ac | 22 | use num::{Float, ParseFloatError}; |
1a4d82fc | 23 | use num::FpCategory as Fp; |
1a4d82fc | 24 | |
c34b1796 | 25 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 26 | #[allow(missing_docs)] |
c34b1796 | 27 | pub const RADIX: u32 = 2; |
1a4d82fc | 28 | |
c34b1796 | 29 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 30 | #[allow(missing_docs)] |
c34b1796 AL |
31 | pub const MANTISSA_DIGITS: u32 = 24; |
32 | #[stable(feature = "rust1", since = "1.0.0")] | |
c1a9b12d | 33 | #[allow(missing_docs)] |
c34b1796 | 34 | pub const DIGITS: u32 = 6; |
1a4d82fc | 35 | |
85aaf69f | 36 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 37 | #[allow(missing_docs)] |
1a4d82fc JJ |
38 | pub const EPSILON: f32 = 1.19209290e-07_f32; |
39 | ||
85aaf69f SL |
40 | /// Smallest finite f32 value |
41 | #[stable(feature = "rust1", since = "1.0.0")] | |
42 | pub const MIN: f32 = -3.40282347e+38_f32; | |
43 | /// Smallest positive, normalized f32 value | |
44 | #[stable(feature = "rust1", since = "1.0.0")] | |
45 | pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; | |
46 | /// Largest finite f32 value | |
47 | #[stable(feature = "rust1", since = "1.0.0")] | |
48 | pub const MAX: f32 = 3.40282347e+38_f32; | |
49 | ||
c34b1796 | 50 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 51 | #[allow(missing_docs)] |
c34b1796 AL |
52 | pub const MIN_EXP: i32 = -125; |
53 | #[stable(feature = "rust1", since = "1.0.0")] | |
c1a9b12d | 54 | #[allow(missing_docs)] |
c34b1796 | 55 | pub const MAX_EXP: i32 = 128; |
1a4d82fc | 56 | |
c34b1796 | 57 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 58 | #[allow(missing_docs)] |
c34b1796 AL |
59 | pub const MIN_10_EXP: i32 = -37; |
60 | #[stable(feature = "rust1", since = "1.0.0")] | |
c1a9b12d | 61 | #[allow(missing_docs)] |
c34b1796 | 62 | pub const MAX_10_EXP: i32 = 38; |
1a4d82fc | 63 | |
85aaf69f | 64 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 65 | #[allow(missing_docs)] |
1a4d82fc | 66 | pub const NAN: f32 = 0.0_f32/0.0_f32; |
85aaf69f | 67 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 68 | #[allow(missing_docs)] |
1a4d82fc | 69 | pub const INFINITY: f32 = 1.0_f32/0.0_f32; |
85aaf69f | 70 | #[stable(feature = "rust1", since = "1.0.0")] |
c1a9b12d | 71 | #[allow(missing_docs)] |
1a4d82fc JJ |
72 | pub const NEG_INFINITY: f32 = -1.0_f32/0.0_f32; |
73 | ||
b039eaaf | 74 | /// Basic mathematical constants. |
c34b1796 | 75 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
76 | pub mod consts { |
77 | // FIXME: replace with mathematical constants from cmath. | |
78 | ||
79 | /// Archimedes' constant | |
c34b1796 | 80 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
81 | pub const PI: f32 = 3.14159265358979323846264338327950288_f32; |
82 | ||
1a4d82fc | 83 | /// pi/2.0 |
c34b1796 | 84 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
85 | pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32; |
86 | ||
87 | /// pi/3.0 | |
c34b1796 | 88 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
89 | pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32; |
90 | ||
91 | /// pi/4.0 | |
c34b1796 | 92 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
93 | pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32; |
94 | ||
95 | /// pi/6.0 | |
c34b1796 | 96 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
97 | pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32; |
98 | ||
99 | /// pi/8.0 | |
c34b1796 | 100 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
101 | pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32; |
102 | ||
103 | /// 1.0/pi | |
c34b1796 | 104 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
105 | pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32; |
106 | ||
107 | /// 2.0/pi | |
c34b1796 | 108 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
109 | pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32; |
110 | ||
111 | /// 2.0/sqrt(pi) | |
c34b1796 AL |
112 | #[stable(feature = "rust1", since = "1.0.0")] |
113 | pub const FRAC_2_SQRT_PI: f32 = 1.12837916709551257389615890312154517_f32; | |
114 | ||
1a4d82fc | 115 | /// sqrt(2.0) |
c34b1796 AL |
116 | #[stable(feature = "rust1", since = "1.0.0")] |
117 | pub const SQRT_2: f32 = 1.41421356237309504880168872420969808_f32; | |
118 | ||
1a4d82fc | 119 | /// 1.0/sqrt(2.0) |
c34b1796 AL |
120 | #[stable(feature = "rust1", since = "1.0.0")] |
121 | pub const FRAC_1_SQRT_2: f32 = 0.707106781186547524400844362104849039_f32; | |
122 | ||
1a4d82fc | 123 | /// Euler's number |
c34b1796 | 124 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
125 | pub const E: f32 = 2.71828182845904523536028747135266250_f32; |
126 | ||
127 | /// log2(e) | |
c34b1796 | 128 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
129 | pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32; |
130 | ||
131 | /// log10(e) | |
c34b1796 | 132 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
133 | pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32; |
134 | ||
135 | /// ln(2.0) | |
c34b1796 | 136 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
137 | pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32; |
138 | ||
139 | /// ln(10.0) | |
c34b1796 | 140 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
141 | pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32; |
142 | } | |
143 | ||
92a42be0 SL |
144 | #[unstable(feature = "core_float", |
145 | reason = "stable interface is via `impl f{32,64}` in later crates", | |
146 | issue = "27702")] | |
1a4d82fc JJ |
147 | impl Float for f32 { |
148 | #[inline] | |
149 | fn nan() -> f32 { NAN } | |
150 | ||
151 | #[inline] | |
152 | fn infinity() -> f32 { INFINITY } | |
153 | ||
154 | #[inline] | |
155 | fn neg_infinity() -> f32 { NEG_INFINITY } | |
156 | ||
157 | #[inline] | |
158 | fn zero() -> f32 { 0.0 } | |
159 | ||
160 | #[inline] | |
161 | fn neg_zero() -> f32 { -0.0 } | |
162 | ||
163 | #[inline] | |
164 | fn one() -> f32 { 1.0 } | |
165 | ||
9346a6ac AL |
166 | from_str_radix_float_impl! { f32 } |
167 | ||
1a4d82fc JJ |
168 | /// Returns `true` if the number is NaN. |
169 | #[inline] | |
170 | fn is_nan(self) -> bool { self != self } | |
171 | ||
172 | /// Returns `true` if the number is infinite. | |
173 | #[inline] | |
174 | fn is_infinite(self) -> bool { | |
175 | self == Float::infinity() || self == Float::neg_infinity() | |
176 | } | |
177 | ||
178 | /// Returns `true` if the number is neither infinite or NaN. | |
179 | #[inline] | |
180 | fn is_finite(self) -> bool { | |
181 | !(self.is_nan() || self.is_infinite()) | |
182 | } | |
183 | ||
184 | /// Returns `true` if the number is neither zero, infinite, subnormal or NaN. | |
185 | #[inline] | |
186 | fn is_normal(self) -> bool { | |
187 | self.classify() == Fp::Normal | |
188 | } | |
189 | ||
190 | /// Returns the floating point category of the number. If only one property | |
191 | /// is going to be tested, it is generally faster to use the specific | |
192 | /// predicate instead. | |
193 | fn classify(self) -> Fp { | |
194 | const EXP_MASK: u32 = 0x7f800000; | |
195 | const MAN_MASK: u32 = 0x007fffff; | |
196 | ||
197 | let bits: u32 = unsafe { mem::transmute(self) }; | |
198 | match (bits & MAN_MASK, bits & EXP_MASK) { | |
199 | (0, 0) => Fp::Zero, | |
200 | (_, 0) => Fp::Subnormal, | |
201 | (0, EXP_MASK) => Fp::Infinite, | |
202 | (_, EXP_MASK) => Fp::Nan, | |
203 | _ => Fp::Normal, | |
204 | } | |
205 | } | |
206 | ||
1a4d82fc JJ |
207 | /// Returns the mantissa, exponent and sign as integers. |
208 | fn integer_decode(self) -> (u64, i16, i8) { | |
209 | let bits: u32 = unsafe { mem::transmute(self) }; | |
210 | let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; | |
211 | let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; | |
212 | let mantissa = if exponent == 0 { | |
213 | (bits & 0x7fffff) << 1 | |
214 | } else { | |
215 | (bits & 0x7fffff) | 0x800000 | |
216 | }; | |
217 | // Exponent bias + mantissa shift | |
218 | exponent -= 127 + 23; | |
219 | (mantissa as u64, exponent, sign) | |
220 | } | |
221 | ||
1a4d82fc JJ |
222 | /// Computes the absolute value of `self`. Returns `Float::nan()` if the |
223 | /// number is `Float::nan()`. | |
224 | #[inline] | |
225 | fn abs(self) -> f32 { | |
226 | unsafe { intrinsics::fabsf32(self) } | |
227 | } | |
228 | ||
229 | /// Returns a number that represents the sign of `self`. | |
230 | /// | |
231 | /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` | |
232 | /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` | |
233 | /// - `Float::nan()` if the number is `Float::nan()` | |
234 | #[inline] | |
235 | fn signum(self) -> f32 { | |
236 | if self.is_nan() { | |
237 | Float::nan() | |
238 | } else { | |
239 | unsafe { intrinsics::copysignf32(1.0, self) } | |
240 | } | |
241 | } | |
242 | ||
243 | /// Returns `true` if `self` is positive, including `+0.0` and | |
244 | /// `Float::infinity()`. | |
245 | #[inline] | |
92a42be0 | 246 | fn is_sign_positive(self) -> bool { |
1a4d82fc JJ |
247 | self > 0.0 || (1.0 / self) == Float::infinity() |
248 | } | |
249 | ||
250 | /// Returns `true` if `self` is negative, including `-0.0` and | |
251 | /// `Float::neg_infinity()`. | |
252 | #[inline] | |
92a42be0 | 253 | fn is_sign_negative(self) -> bool { |
1a4d82fc JJ |
254 | self < 0.0 || (1.0 / self) == Float::neg_infinity() |
255 | } | |
256 | ||
1a4d82fc JJ |
257 | /// Returns the reciprocal (multiplicative inverse) of the number. |
258 | #[inline] | |
259 | fn recip(self) -> f32 { 1.0 / self } | |
260 | ||
261 | #[inline] | |
262 | fn powi(self, n: i32) -> f32 { | |
263 | unsafe { intrinsics::powif32(self, n) } | |
264 | } | |
265 | ||
1a4d82fc JJ |
266 | /// Converts to degrees, assuming the number is in radians. |
267 | #[inline] | |
268 | fn to_degrees(self) -> f32 { self * (180.0f32 / consts::PI) } | |
269 | ||
270 | /// Converts to radians, assuming the number is in degrees. | |
271 | #[inline] | |
272 | fn to_radians(self) -> f32 { | |
273 | let value: f32 = consts::PI; | |
274 | self * (value / 180.0f32) | |
275 | } | |
276 | } |