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