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