@@ -463,15 +463,130 @@ impl f64 {
463463 /// # Examples
464464 ///
465465 /// ```
466- /// use std::f64;
467466 /// let v = f64::from_bits(0x4029000000000000);
468- /// let difference = (v - 12.5).abs();
469- /// assert!(difference <= 1e-5);
467+ /// assert_eq!(v, 12.5);
470468 /// ```
471469 #[ stable( feature = "float_bits_conv" , since = "1.20.0" ) ]
472470 #[ inline]
473471 pub fn from_bits ( v : u64 ) -> Self {
474472 // It turns out the safety issues with sNaN were overblown! Hooray!
475473 unsafe { mem:: transmute ( v) }
476474 }
475+
476+ /// Return the memory representation of this floating point number as a byte array in
477+ /// big-endian (network) byte order.
478+ ///
479+ /// # Examples
480+ ///
481+ /// ```
482+ /// #![feature(float_to_from_bytes)]
483+ /// let bytes = 12.5f64.to_be_bytes();
484+ /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
485+ /// ```
486+ #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
487+ #[ inline]
488+ pub fn to_be_bytes ( self ) -> [ u8 ; 8 ] {
489+ self . to_bits ( ) . to_be_bytes ( )
490+ }
491+
492+ /// Return the memory representation of this floating point number as a byte array in
493+ /// little-endian byte order.
494+ ///
495+ /// # Examples
496+ ///
497+ /// ```
498+ /// #![feature(float_to_from_bytes)]
499+ /// let bytes = 12.5f64.to_le_bytes();
500+ /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
501+ /// ```
502+ #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
503+ #[ inline]
504+ pub fn to_le_bytes ( self ) -> [ u8 ; 8 ] {
505+ self . to_bits ( ) . to_le_bytes ( )
506+ }
507+
508+ /// Return the memory representation of this floating point number as a byte array in
509+ /// native byte order.
510+ ///
511+ /// As the target platform's native endianness is used, portable code
512+ /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
513+ ///
514+ /// [`to_be_bytes`]: #method.to_be_bytes
515+ /// [`to_le_bytes`]: #method.to_le_bytes
516+ ///
517+ /// # Examples
518+ ///
519+ /// ```
520+ /// #![feature(float_to_from_bytes)]
521+ /// let bytes = 12.5f64.to_ne_bytes();
522+ /// assert_eq!(
523+ /// bytes,
524+ /// if cfg!(target_endian = "big") {
525+ /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
526+ /// } else {
527+ /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
528+ /// }
529+ /// );
530+ /// ```
531+ #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
532+ #[ inline]
533+ pub fn to_ne_bytes ( self ) -> [ u8 ; 8 ] {
534+ self . to_bits ( ) . to_ne_bytes ( )
535+ }
536+
537+ /// Create a floating point value from its representation as a byte array in big endian.
538+ ///
539+ /// # Examples
540+ ///
541+ /// ```
542+ /// #![feature(float_to_from_bytes)]
543+ /// let value = f64::from_be_bytes([0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
544+ /// assert_eq!(value, 12.5);
545+ /// ```
546+ #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
547+ #[ inline]
548+ pub fn from_be_bytes ( bytes : [ u8 ; 8 ] ) -> Self {
549+ Self :: from_bits ( u64:: from_be_bytes ( bytes) )
550+ }
551+
552+ /// Create a floating point value from its representation as a byte array in big endian.
553+ ///
554+ /// # Examples
555+ ///
556+ /// ```
557+ /// #![feature(float_to_from_bytes)]
558+ /// let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
559+ /// assert_eq!(value, 12.5);
560+ /// ```
561+ #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
562+ #[ inline]
563+ pub fn from_le_bytes ( bytes : [ u8 ; 8 ] ) -> Self {
564+ Self :: from_bits ( u64:: from_le_bytes ( bytes) )
565+ }
566+
567+ /// Create a floating point value from its representation as a byte array in big endian.
568+ ///
569+ /// As the target platform's native endianness is used, portable code
570+ /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
571+ /// appropriate instead.
572+ ///
573+ /// [`from_be_bytes`]: #method.from_be_bytes
574+ /// [`from_le_bytes`]: #method.from_le_bytes
575+ ///
576+ /// # Examples
577+ ///
578+ /// ```
579+ /// #![feature(float_to_from_bytes)]
580+ /// let value = f64::from_ne_bytes(if cfg!(target_endian = "big") {
581+ /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
582+ /// } else {
583+ /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
584+ /// });
585+ /// assert_eq!(value, 12.5);
586+ /// ```
587+ #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
588+ #[ inline]
589+ pub fn from_ne_bytes ( bytes : [ u8 ; 8 ] ) -> Self {
590+ Self :: from_bits ( u64:: from_ne_bytes ( bytes) )
591+ }
477592}
0 commit comments