]>
Commit | Line | Data |
---|---|---|
ff7c6d11 XL |
1 | //! This module provides constants which are specific to the implementation |
2 | //! of the `f64` floating point data type. | |
3 | //! | |
ff7c6d11 | 4 | //! *[See also the `f64` primitive type](../../std/primitive.f64.html).* |
94b46f34 XL |
5 | //! |
6 | //! Mathematically significant numbers are provided in the `consts` sub-module. | |
1a4d82fc | 7 | |
85aaf69f | 8 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 9 | |
60c5eb7d XL |
10 | #[cfg(not(bootstrap))] |
11 | use crate::convert::FloatToInt; | |
dc9dc135 XL |
12 | #[cfg(not(test))] |
13 | use crate::intrinsics; | |
48663c56 XL |
14 | use crate::mem; |
15 | use crate::num::FpCategory; | |
1a4d82fc | 16 | |
5bcae85e | 17 | /// The radix or base of the internal representation of `f64`. |
c34b1796 AL |
18 | #[stable(feature = "rust1", since = "1.0.0")] |
19 | pub const RADIX: u32 = 2; | |
1a4d82fc | 20 | |
5bcae85e | 21 | /// Number of significant digits in base 2. |
c34b1796 AL |
22 | #[stable(feature = "rust1", since = "1.0.0")] |
23 | pub const MANTISSA_DIGITS: u32 = 53; | |
5bcae85e | 24 | /// Approximate number of significant digits in base 10. |
c34b1796 AL |
25 | #[stable(feature = "rust1", since = "1.0.0")] |
26 | pub const DIGITS: u32 = 15; | |
1a4d82fc | 27 | |
94b46f34 XL |
28 | /// [Machine epsilon] value for `f64`. |
29 | /// | |
60c5eb7d | 30 | /// This is the difference between `1.0` and the next larger representable number. |
94b46f34 XL |
31 | /// |
32 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon | |
85aaf69f | 33 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
34 | pub const EPSILON: f64 = 2.2204460492503131e-16_f64; |
35 | ||
5bcae85e | 36 | /// Smallest finite `f64` value. |
85aaf69f SL |
37 | #[stable(feature = "rust1", since = "1.0.0")] |
38 | pub const MIN: f64 = -1.7976931348623157e+308_f64; | |
5bcae85e | 39 | /// Smallest positive normal `f64` value. |
85aaf69f SL |
40 | #[stable(feature = "rust1", since = "1.0.0")] |
41 | pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64; | |
5bcae85e | 42 | /// Largest finite `f64` value. |
85aaf69f SL |
43 | #[stable(feature = "rust1", since = "1.0.0")] |
44 | pub const MAX: f64 = 1.7976931348623157e+308_f64; | |
45 | ||
5bcae85e | 46 | /// One greater than the minimum possible normal power of 2 exponent. |
c34b1796 AL |
47 | #[stable(feature = "rust1", since = "1.0.0")] |
48 | pub const MIN_EXP: i32 = -1021; | |
5bcae85e | 49 | /// Maximum possible power of 2 exponent. |
c34b1796 AL |
50 | #[stable(feature = "rust1", since = "1.0.0")] |
51 | pub const MAX_EXP: i32 = 1024; | |
1a4d82fc | 52 | |
5bcae85e | 53 | /// Minimum possible normal power of 10 exponent. |
c34b1796 AL |
54 | #[stable(feature = "rust1", since = "1.0.0")] |
55 | pub const MIN_10_EXP: i32 = -307; | |
5bcae85e | 56 | /// Maximum possible power of 10 exponent. |
c34b1796 AL |
57 | #[stable(feature = "rust1", since = "1.0.0")] |
58 | pub const MAX_10_EXP: i32 = 308; | |
1a4d82fc | 59 | |
5bcae85e | 60 | /// Not a Number (NaN). |
85aaf69f | 61 | #[stable(feature = "rust1", since = "1.0.0")] |
c30ab7b3 | 62 | pub const NAN: f64 = 0.0_f64 / 0.0_f64; |
5bcae85e | 63 | /// Infinity (∞). |
85aaf69f | 64 | #[stable(feature = "rust1", since = "1.0.0")] |
c30ab7b3 | 65 | pub const INFINITY: f64 = 1.0_f64 / 0.0_f64; |
5bcae85e | 66 | /// Negative infinity (-∞). |
85aaf69f | 67 | #[stable(feature = "rust1", since = "1.0.0")] |
c30ab7b3 | 68 | pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64; |
1a4d82fc | 69 | |
b039eaaf | 70 | /// Basic mathematical constants. |
c34b1796 | 71 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
72 | pub mod consts { |
73 | // FIXME: replace with mathematical constants from cmath. | |
74 | ||
5bcae85e | 75 | /// Archimedes' constant (π) |
c34b1796 | 76 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
77 | pub const PI: f64 = 3.14159265358979323846264338327950288_f64; |
78 | ||
60c5eb7d XL |
79 | /// The full circle constant (τ) |
80 | /// | |
81 | /// Equal to 2π. | |
82 | #[unstable(feature = "tau_constant", issue = "66770")] | |
83 | pub const TAU: f64 = 6.28318530717958647692528676655900577_f64; | |
84 | ||
5bcae85e | 85 | /// π/2 |
c34b1796 | 86 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
87 | pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64; |
88 | ||
5bcae85e | 89 | /// π/3 |
c34b1796 | 90 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
91 | pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64; |
92 | ||
5bcae85e | 93 | /// π/4 |
c34b1796 | 94 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
95 | pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64; |
96 | ||
5bcae85e | 97 | /// π/6 |
c34b1796 | 98 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
99 | pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64; |
100 | ||
5bcae85e | 101 | /// π/8 |
c34b1796 | 102 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
103 | pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64; |
104 | ||
5bcae85e | 105 | /// 1/π |
c34b1796 | 106 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
107 | pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64; |
108 | ||
5bcae85e | 109 | /// 2/π |
c34b1796 | 110 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
111 | pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64; |
112 | ||
5bcae85e | 113 | /// 2/sqrt(π) |
c34b1796 AL |
114 | #[stable(feature = "rust1", since = "1.0.0")] |
115 | pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64; | |
116 | ||
5bcae85e | 117 | /// sqrt(2) |
c34b1796 AL |
118 | #[stable(feature = "rust1", since = "1.0.0")] |
119 | pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64; | |
120 | ||
5bcae85e | 121 | /// 1/sqrt(2) |
c34b1796 AL |
122 | #[stable(feature = "rust1", since = "1.0.0")] |
123 | pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64; | |
124 | ||
5bcae85e | 125 | /// Euler's number (e) |
c34b1796 | 126 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
127 | pub const E: f64 = 2.71828182845904523536028747135266250_f64; |
128 | ||
94b46f34 XL |
129 | /// log<sub>2</sub>(10) |
130 | #[unstable(feature = "extra_log_consts", issue = "50540")] | |
131 | pub const LOG2_10: f64 = 3.32192809488736234787031942948939018_f64; | |
132 | ||
5bcae85e | 133 | /// log<sub>2</sub>(e) |
c34b1796 | 134 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
135 | pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64; |
136 | ||
94b46f34 XL |
137 | /// log<sub>10</sub>(2) |
138 | #[unstable(feature = "extra_log_consts", issue = "50540")] | |
139 | pub const LOG10_2: f64 = 0.301029995663981195213738894724493027_f64; | |
140 | ||
5bcae85e | 141 | /// log<sub>10</sub>(e) |
c34b1796 | 142 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
143 | pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64; |
144 | ||
5bcae85e | 145 | /// ln(2) |
c34b1796 | 146 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
147 | pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64; |
148 | ||
5bcae85e | 149 | /// ln(10) |
c34b1796 | 150 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
151 | pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64; |
152 | } | |
153 | ||
94b46f34 XL |
154 | #[lang = "f64"] |
155 | #[cfg(not(test))] | |
156 | impl f64 { | |
9fa01778 | 157 | /// Returns `true` if this value is `NaN`. |
83c7162d XL |
158 | /// |
159 | /// ``` | |
160 | /// use std::f64; | |
161 | /// | |
162 | /// let nan = f64::NAN; | |
163 | /// let f = 7.0_f64; | |
164 | /// | |
165 | /// assert!(nan.is_nan()); | |
166 | /// assert!(!f.is_nan()); | |
167 | /// ``` | |
168 | #[stable(feature = "rust1", since = "1.0.0")] | |
169 | #[inline] | |
94b46f34 XL |
170 | pub fn is_nan(self) -> bool { |
171 | self != self | |
172 | } | |
83c7162d | 173 | |
0731742a XL |
174 | // FIXME(#50145): `abs` is publicly unavailable in libcore due to |
175 | // concerns about portability, so this implementation is for | |
176 | // private use internally. | |
177 | #[inline] | |
178 | fn abs_private(self) -> f64 { | |
179 | f64::from_bits(self.to_bits() & 0x7fff_ffff_ffff_ffff) | |
180 | } | |
181 | ||
9fa01778 XL |
182 | /// Returns `true` if this value is positive infinity or negative infinity, and |
183 | /// `false` otherwise. | |
83c7162d XL |
184 | /// |
185 | /// ``` | |
186 | /// use std::f64; | |
187 | /// | |
188 | /// let f = 7.0f64; | |
189 | /// let inf = f64::INFINITY; | |
190 | /// let neg_inf = f64::NEG_INFINITY; | |
191 | /// let nan = f64::NAN; | |
192 | /// | |
193 | /// assert!(!f.is_infinite()); | |
194 | /// assert!(!nan.is_infinite()); | |
195 | /// | |
196 | /// assert!(inf.is_infinite()); | |
197 | /// assert!(neg_inf.is_infinite()); | |
198 | /// ``` | |
199 | #[stable(feature = "rust1", since = "1.0.0")] | |
200 | #[inline] | |
94b46f34 | 201 | pub fn is_infinite(self) -> bool { |
0731742a | 202 | self.abs_private() == INFINITY |
94b46f34 | 203 | } |
83c7162d XL |
204 | |
205 | /// Returns `true` if this number is neither infinite nor `NaN`. | |
206 | /// | |
207 | /// ``` | |
208 | /// use std::f64; | |
209 | /// | |
210 | /// let f = 7.0f64; | |
211 | /// let inf: f64 = f64::INFINITY; | |
212 | /// let neg_inf: f64 = f64::NEG_INFINITY; | |
213 | /// let nan: f64 = f64::NAN; | |
214 | /// | |
215 | /// assert!(f.is_finite()); | |
216 | /// | |
217 | /// assert!(!nan.is_finite()); | |
218 | /// assert!(!inf.is_finite()); | |
219 | /// assert!(!neg_inf.is_finite()); | |
220 | /// ``` | |
221 | #[stable(feature = "rust1", since = "1.0.0")] | |
222 | #[inline] | |
94b46f34 | 223 | pub fn is_finite(self) -> bool { |
0731742a XL |
224 | // There's no need to handle NaN separately: if self is NaN, |
225 | // the comparison is not true, exactly as desired. | |
226 | self.abs_private() < INFINITY | |
94b46f34 | 227 | } |
83c7162d XL |
228 | |
229 | /// Returns `true` if the number is neither zero, infinite, | |
230 | /// [subnormal][subnormal], or `NaN`. | |
231 | /// | |
232 | /// ``` | |
233 | /// use std::f64; | |
234 | /// | |
235 | /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64 | |
236 | /// let max = f64::MAX; | |
237 | /// let lower_than_min = 1.0e-308_f64; | |
238 | /// let zero = 0.0f64; | |
239 | /// | |
240 | /// assert!(min.is_normal()); | |
241 | /// assert!(max.is_normal()); | |
242 | /// | |
243 | /// assert!(!zero.is_normal()); | |
244 | /// assert!(!f64::NAN.is_normal()); | |
245 | /// assert!(!f64::INFINITY.is_normal()); | |
246 | /// // Values between `0` and `min` are Subnormal. | |
247 | /// assert!(!lower_than_min.is_normal()); | |
248 | /// ``` | |
249 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number | |
250 | #[stable(feature = "rust1", since = "1.0.0")] | |
251 | #[inline] | |
94b46f34 XL |
252 | pub fn is_normal(self) -> bool { |
253 | self.classify() == FpCategory::Normal | |
254 | } | |
83c7162d XL |
255 | |
256 | /// Returns the floating point category of the number. If only one property | |
257 | /// is going to be tested, it is generally faster to use the specific | |
258 | /// predicate instead. | |
259 | /// | |
260 | /// ``` | |
261 | /// use std::num::FpCategory; | |
262 | /// use std::f64; | |
263 | /// | |
264 | /// let num = 12.4_f64; | |
265 | /// let inf = f64::INFINITY; | |
266 | /// | |
267 | /// assert_eq!(num.classify(), FpCategory::Normal); | |
268 | /// assert_eq!(inf.classify(), FpCategory::Infinite); | |
269 | /// ``` | |
270 | #[stable(feature = "rust1", since = "1.0.0")] | |
94b46f34 XL |
271 | pub fn classify(self) -> FpCategory { |
272 | const EXP_MASK: u64 = 0x7ff0000000000000; | |
273 | const MAN_MASK: u64 = 0x000fffffffffffff; | |
274 | ||
275 | let bits = self.to_bits(); | |
276 | match (bits & MAN_MASK, bits & EXP_MASK) { | |
277 | (0, 0) => FpCategory::Zero, | |
278 | (_, 0) => FpCategory::Subnormal, | |
279 | (0, EXP_MASK) => FpCategory::Infinite, | |
280 | (_, EXP_MASK) => FpCategory::Nan, | |
281 | _ => FpCategory::Normal, | |
282 | } | |
283 | } | |
83c7162d | 284 | |
9fa01778 | 285 | /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with |
83c7162d XL |
286 | /// positive sign bit and positive infinity. |
287 | /// | |
288 | /// ``` | |
289 | /// let f = 7.0_f64; | |
290 | /// let g = -7.0_f64; | |
291 | /// | |
292 | /// assert!(f.is_sign_positive()); | |
293 | /// assert!(!g.is_sign_positive()); | |
294 | /// ``` | |
295 | #[stable(feature = "rust1", since = "1.0.0")] | |
296 | #[inline] | |
94b46f34 XL |
297 | pub fn is_sign_positive(self) -> bool { |
298 | !self.is_sign_negative() | |
299 | } | |
83c7162d XL |
300 | |
301 | #[stable(feature = "rust1", since = "1.0.0")] | |
302 | #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")] | |
303 | #[inline] | |
304 | #[doc(hidden)] | |
94b46f34 XL |
305 | pub fn is_positive(self) -> bool { |
306 | self.is_sign_positive() | |
307 | } | |
83c7162d | 308 | |
9fa01778 | 309 | /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with |
83c7162d XL |
310 | /// negative sign bit and negative infinity. |
311 | /// | |
312 | /// ``` | |
313 | /// let f = 7.0_f64; | |
314 | /// let g = -7.0_f64; | |
315 | /// | |
316 | /// assert!(!f.is_sign_negative()); | |
317 | /// assert!(g.is_sign_negative()); | |
318 | /// ``` | |
319 | #[stable(feature = "rust1", since = "1.0.0")] | |
320 | #[inline] | |
94b46f34 XL |
321 | pub fn is_sign_negative(self) -> bool { |
322 | self.to_bits() & 0x8000_0000_0000_0000 != 0 | |
323 | } | |
83c7162d XL |
324 | |
325 | #[stable(feature = "rust1", since = "1.0.0")] | |
326 | #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")] | |
327 | #[inline] | |
328 | #[doc(hidden)] | |
94b46f34 XL |
329 | pub fn is_negative(self) -> bool { |
330 | self.is_sign_negative() | |
331 | } | |
83c7162d XL |
332 | |
333 | /// Takes the reciprocal (inverse) of a number, `1/x`. | |
334 | /// | |
335 | /// ``` | |
336 | /// let x = 2.0_f64; | |
e1599b0c | 337 | /// let abs_difference = (x.recip() - (1.0 / x)).abs(); |
83c7162d XL |
338 | /// |
339 | /// assert!(abs_difference < 1e-10); | |
340 | /// ``` | |
341 | #[stable(feature = "rust1", since = "1.0.0")] | |
342 | #[inline] | |
94b46f34 XL |
343 | pub fn recip(self) -> f64 { |
344 | 1.0 / self | |
345 | } | |
83c7162d XL |
346 | |
347 | /// Converts radians to degrees. | |
348 | /// | |
349 | /// ``` | |
350 | /// use std::f64::consts; | |
351 | /// | |
352 | /// let angle = consts::PI; | |
353 | /// | |
354 | /// let abs_difference = (angle.to_degrees() - 180.0).abs(); | |
355 | /// | |
356 | /// assert!(abs_difference < 1e-10); | |
357 | /// ``` | |
358 | #[stable(feature = "rust1", since = "1.0.0")] | |
359 | #[inline] | |
94b46f34 XL |
360 | pub fn to_degrees(self) -> f64 { |
361 | // The division here is correctly rounded with respect to the true | |
362 | // value of 180/π. (This differs from f32, where a constant must be | |
363 | // used to ensure a correctly rounded result.) | |
364 | self * (180.0f64 / consts::PI) | |
365 | } | |
83c7162d XL |
366 | |
367 | /// Converts degrees to radians. | |
368 | /// | |
369 | /// ``` | |
370 | /// use std::f64::consts; | |
371 | /// | |
372 | /// let angle = 180.0_f64; | |
373 | /// | |
374 | /// let abs_difference = (angle.to_radians() - consts::PI).abs(); | |
375 | /// | |
376 | /// assert!(abs_difference < 1e-10); | |
377 | /// ``` | |
378 | #[stable(feature = "rust1", since = "1.0.0")] | |
379 | #[inline] | |
94b46f34 XL |
380 | pub fn to_radians(self) -> f64 { |
381 | let value: f64 = consts::PI; | |
382 | self * (value / 180.0) | |
383 | } | |
83c7162d XL |
384 | |
385 | /// Returns the maximum of the two numbers. | |
386 | /// | |
387 | /// ``` | |
388 | /// let x = 1.0_f64; | |
389 | /// let y = 2.0_f64; | |
390 | /// | |
391 | /// assert_eq!(x.max(y), y); | |
392 | /// ``` | |
393 | /// | |
394 | /// If one of the arguments is NaN, then the other argument is returned. | |
395 | #[stable(feature = "rust1", since = "1.0.0")] | |
396 | #[inline] | |
397 | pub fn max(self, other: f64) -> f64 { | |
dc9dc135 | 398 | intrinsics::maxnumf64(self, other) |
83c7162d XL |
399 | } |
400 | ||
401 | /// Returns the minimum of the two numbers. | |
402 | /// | |
403 | /// ``` | |
404 | /// let x = 1.0_f64; | |
405 | /// let y = 2.0_f64; | |
406 | /// | |
407 | /// assert_eq!(x.min(y), x); | |
408 | /// ``` | |
409 | /// | |
410 | /// If one of the arguments is NaN, then the other argument is returned. | |
411 | #[stable(feature = "rust1", since = "1.0.0")] | |
412 | #[inline] | |
413 | pub fn min(self, other: f64) -> f64 { | |
dc9dc135 | 414 | intrinsics::minnumf64(self, other) |
83c7162d XL |
415 | } |
416 | ||
60c5eb7d XL |
417 | /// Rounds toward zero and converts to any primitive integer type, |
418 | /// assuming that the value is finite and fits in that type. | |
419 | /// | |
420 | /// ``` | |
421 | /// #![feature(float_approx_unchecked_to)] | |
422 | /// | |
423 | /// let value = 4.6_f32; | |
424 | /// let rounded = unsafe { value.approx_unchecked_to::<u16>() }; | |
425 | /// assert_eq!(rounded, 4); | |
426 | /// | |
427 | /// let value = -128.9_f32; | |
428 | /// let rounded = unsafe { value.approx_unchecked_to::<i8>() }; | |
429 | /// assert_eq!(rounded, std::i8::MIN); | |
430 | /// ``` | |
431 | /// | |
432 | /// # Safety | |
433 | /// | |
434 | /// The value must: | |
435 | /// | |
436 | /// * Not be `NaN` | |
437 | /// * Not be infinite | |
438 | /// * Be representable in the return type `Int`, after truncating off its fractional part | |
439 | #[cfg(not(bootstrap))] | |
440 | #[unstable(feature = "float_approx_unchecked_to", issue = "67058")] | |
441 | #[inline] | |
442 | pub unsafe fn approx_unchecked_to<Int>(self) -> Int | |
443 | where | |
444 | Self: FloatToInt<Int>, | |
445 | { | |
446 | FloatToInt::<Int>::approx_unchecked(self) | |
447 | } | |
448 | ||
83c7162d XL |
449 | /// Raw transmutation to `u64`. |
450 | /// | |
451 | /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms. | |
452 | /// | |
453 | /// See `from_bits` for some discussion of the portability of this operation | |
454 | /// (there are almost no issues). | |
455 | /// | |
456 | /// Note that this function is distinct from `as` casting, which attempts to | |
457 | /// preserve the *numeric* value, and not the bitwise value. | |
458 | /// | |
459 | /// # Examples | |
460 | /// | |
461 | /// ``` | |
462 | /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting! | |
463 | /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000); | |
464 | /// | |
465 | /// ``` | |
466 | #[stable(feature = "float_bits_conv", since = "1.20.0")] | |
467 | #[inline] | |
468 | pub fn to_bits(self) -> u64 { | |
60c5eb7d | 469 | // SAFETY: `u64` is a plain old datatype so we can always transmute to it |
94b46f34 | 470 | unsafe { mem::transmute(self) } |
83c7162d XL |
471 | } |
472 | ||
473 | /// Raw transmutation from `u64`. | |
474 | /// | |
475 | /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms. | |
476 | /// It turns out this is incredibly portable, for two reasons: | |
477 | /// | |
478 | /// * Floats and Ints have the same endianness on all supported platforms. | |
479 | /// * IEEE-754 very precisely specifies the bit layout of floats. | |
480 | /// | |
481 | /// However there is one caveat: prior to the 2008 version of IEEE-754, how | |
482 | /// to interpret the NaN signaling bit wasn't actually specified. Most platforms | |
483 | /// (notably x86 and ARM) picked the interpretation that was ultimately | |
484 | /// standardized in 2008, but some didn't (notably MIPS). As a result, all | |
485 | /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa. | |
486 | /// | |
487 | /// Rather than trying to preserve signaling-ness cross-platform, this | |
488 | /// implementation favours preserving the exact bits. This means that | |
489 | /// any payloads encoded in NaNs will be preserved even if the result of | |
490 | /// this method is sent over the network from an x86 machine to a MIPS one. | |
491 | /// | |
492 | /// If the results of this method are only manipulated by the same | |
493 | /// architecture that produced them, then there is no portability concern. | |
494 | /// | |
495 | /// If the input isn't NaN, then there is no portability concern. | |
496 | /// | |
497 | /// If you don't care about signalingness (very likely), then there is no | |
498 | /// portability concern. | |
499 | /// | |
500 | /// Note that this function is distinct from `as` casting, which attempts to | |
501 | /// preserve the *numeric* value, and not the bitwise value. | |
502 | /// | |
503 | /// # Examples | |
504 | /// | |
505 | /// ``` | |
83c7162d | 506 | /// let v = f64::from_bits(0x4029000000000000); |
416331ca | 507 | /// assert_eq!(v, 12.5); |
83c7162d XL |
508 | /// ``` |
509 | #[stable(feature = "float_bits_conv", since = "1.20.0")] | |
510 | #[inline] | |
511 | pub fn from_bits(v: u64) -> Self { | |
60c5eb7d | 512 | // SAFETY: `u64` is a plain old datatype so we can always transmute from it |
94b46f34 XL |
513 | // It turns out the safety issues with sNaN were overblown! Hooray! |
514 | unsafe { mem::transmute(v) } | |
83c7162d | 515 | } |
416331ca XL |
516 | |
517 | /// Return the memory representation of this floating point number as a byte array in | |
518 | /// big-endian (network) byte order. | |
519 | /// | |
520 | /// # Examples | |
521 | /// | |
522 | /// ``` | |
416331ca XL |
523 | /// let bytes = 12.5f64.to_be_bytes(); |
524 | /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); | |
525 | /// ``` | |
e74abb32 | 526 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
527 | #[inline] |
528 | pub fn to_be_bytes(self) -> [u8; 8] { | |
529 | self.to_bits().to_be_bytes() | |
530 | } | |
531 | ||
532 | /// Return the memory representation of this floating point number as a byte array in | |
533 | /// little-endian byte order. | |
534 | /// | |
535 | /// # Examples | |
536 | /// | |
537 | /// ``` | |
416331ca XL |
538 | /// let bytes = 12.5f64.to_le_bytes(); |
539 | /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]); | |
540 | /// ``` | |
e74abb32 | 541 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
542 | #[inline] |
543 | pub fn to_le_bytes(self) -> [u8; 8] { | |
544 | self.to_bits().to_le_bytes() | |
545 | } | |
546 | ||
547 | /// Return the memory representation of this floating point number as a byte array in | |
548 | /// native byte order. | |
549 | /// | |
550 | /// As the target platform's native endianness is used, portable code | |
551 | /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead. | |
552 | /// | |
553 | /// [`to_be_bytes`]: #method.to_be_bytes | |
554 | /// [`to_le_bytes`]: #method.to_le_bytes | |
555 | /// | |
556 | /// # Examples | |
557 | /// | |
558 | /// ``` | |
416331ca XL |
559 | /// let bytes = 12.5f64.to_ne_bytes(); |
560 | /// assert_eq!( | |
561 | /// bytes, | |
562 | /// if cfg!(target_endian = "big") { | |
563 | /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] | |
564 | /// } else { | |
565 | /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40] | |
566 | /// } | |
567 | /// ); | |
568 | /// ``` | |
e74abb32 | 569 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
570 | #[inline] |
571 | pub fn to_ne_bytes(self) -> [u8; 8] { | |
572 | self.to_bits().to_ne_bytes() | |
573 | } | |
574 | ||
575 | /// Create a floating point value from its representation as a byte array in big endian. | |
576 | /// | |
577 | /// # Examples | |
578 | /// | |
579 | /// ``` | |
416331ca XL |
580 | /// let value = f64::from_be_bytes([0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); |
581 | /// assert_eq!(value, 12.5); | |
582 | /// ``` | |
e74abb32 | 583 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
584 | #[inline] |
585 | pub fn from_be_bytes(bytes: [u8; 8]) -> Self { | |
586 | Self::from_bits(u64::from_be_bytes(bytes)) | |
587 | } | |
588 | ||
589 | /// Create a floating point value from its representation as a byte array in little endian. | |
590 | /// | |
591 | /// # Examples | |
592 | /// | |
593 | /// ``` | |
416331ca XL |
594 | /// let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]); |
595 | /// assert_eq!(value, 12.5); | |
596 | /// ``` | |
e74abb32 | 597 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
598 | #[inline] |
599 | pub fn from_le_bytes(bytes: [u8; 8]) -> Self { | |
600 | Self::from_bits(u64::from_le_bytes(bytes)) | |
601 | } | |
602 | ||
603 | /// Create a floating point value from its representation as a byte array in native endian. | |
604 | /// | |
605 | /// As the target platform's native endianness is used, portable code | |
606 | /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as | |
607 | /// appropriate instead. | |
608 | /// | |
609 | /// [`from_be_bytes`]: #method.from_be_bytes | |
610 | /// [`from_le_bytes`]: #method.from_le_bytes | |
611 | /// | |
612 | /// # Examples | |
613 | /// | |
614 | /// ``` | |
416331ca XL |
615 | /// let value = f64::from_ne_bytes(if cfg!(target_endian = "big") { |
616 | /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] | |
617 | /// } else { | |
618 | /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40] | |
619 | /// }); | |
620 | /// assert_eq!(value, 12.5); | |
621 | /// ``` | |
e74abb32 | 622 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
623 | #[inline] |
624 | pub fn from_ne_bytes(bytes: [u8; 8]) -> Self { | |
625 | Self::from_bits(u64::from_ne_bytes(bytes)) | |
626 | } | |
83c7162d | 627 | } |