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