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