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