@@ -450,10 +450,8 @@ impl f32 {
450450 /// # Examples
451451 ///
452452 /// ```
453- /// use std::f32;
454453 /// let v = f32::from_bits(0x41480000);
455- /// let difference = (v - 12.5).abs();
456- /// assert!(difference <= 1e-5);
454+ /// assert_eq!(v, 12.5);
457455 /// ```
458456 #[ stable( feature = "float_bits_conv" , since = "1.20.0" ) ]
459457 #[ inline]
@@ -462,50 +460,59 @@ impl f32 {
462460 unsafe { mem:: transmute ( v) }
463461 }
464462
465- /// Return the floating point value as a byte array in big-endian byte order.
463+ /// Return the memory representation of this floating point number as a byte array in
464+ /// big-endian (network) byte order.
466465 ///
467466 /// # Examples
468467 ///
469468 /// ```
470- /// assert_eq!(0.0f32.to_be_bytes(), [0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000]);
471- /// assert_eq!(1.0f32.to_be_bytes(), [0b0111_1111, 0b1000_0000, 0b0000_0000, 0b0000_0000]);
469+ /// #![feature(float_to_from_bytes)]
470+ /// let bytes = 12.5f32.to_be_bytes();
471+ /// assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]);
472472 /// ```
473473 #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
474474 #[ inline]
475475 pub fn to_be_bytes ( self ) -> [ u8 ; 4 ] {
476476 self . to_bits ( ) . to_be_bytes ( )
477477 }
478478
479- /// Return the floating point value as a byte array in little-endian byte order.
479+ /// Return the memory representation of this floating point number as a byte array in
480+ /// little-endian byte order.
480481 ///
481482 /// # Examples
482483 ///
483484 /// ```
484- /// assert_eq!(0.0f32.to_le_bytes(), [0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000]);
485- /// assert_eq!(1.0f32.to_le_bytes(), [0b0000_0000, 0b0000_0000, 0b1000_0000, 0b0111_1111]);
485+ /// #![feature(float_to_from_bytes)]
486+ /// let bytes = 12.5f32.to_le_bytes();
487+ /// assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]);
486488 /// ```
487489 #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
488490 #[ inline]
489491 pub fn to_le_bytes ( self ) -> [ u8 ; 4 ] {
490492 self . to_bits ( ) . to_le_bytes ( )
491493 }
492494
493- /// Return the floating point value as a byte array in native byte order.
494- ///
495+ /// Return the memory representation of this floating point number as a byte array in
496+ /// native byte order.
495497 ///
496498 /// As the target platform's native endianness is used, portable code
497499 /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
498500 ///
501+ /// [`to_be_bytes`]: #method.to_be_bytes
502+ /// [`to_le_bytes`]: #method.to_le_bytes
503+ ///
499504 /// # Examples
500505 ///
501506 /// ```
507+ /// #![feature(float_to_from_bytes)]
508+ /// let bytes = 12.5f32.to_ne_bytes();
502509 /// assert_eq!(
503- /// u32::from_ne_bytes(0.0f32.to_ne_bytes()) ,
504- /// 0b0000_0000_0000_0000_0000_0000_0000_0000,
505- /// );
506- /// assert_eq!(
507- /// u32::from_ne_bytes(1.0f32.to_ne_bytes()),
508- /// 0b0111_1111_1000_0000_0000_0000_0000_0000,
510+ /// bytes ,
511+ /// if cfg!(target_endian = "big") {
512+ /// [0x41, 0x48, 0x00, 0x00]
513+ /// } else {
514+ /// [0x00, 0x00, 0x48, 0x41]
515+ /// }
509516 /// );
510517 /// ```
511518 #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
@@ -514,18 +521,56 @@ impl f32 {
514521 self . to_bits ( ) . to_ne_bytes ( )
515522 }
516523
524+ /// Create a floating point value from its representation as a byte array in big endian.
525+ ///
526+ /// # Examples
527+ ///
528+ /// ```
529+ /// #![feature(float_to_from_bytes)]
530+ /// let value = f32::from_be_bytes([0x41, 0x48, 0x00, 0x00]);
531+ /// assert_eq!(value, 12.5);
532+ /// ```
517533 #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
518534 #[ inline]
519535 pub fn from_be_bytes ( bytes : [ u8 ; 4 ] ) -> Self {
520536 Self :: from_bits ( u32:: from_be_bytes ( bytes) )
521537 }
522538
539+ /// Create a floating point value from its representation as a byte array in big endian.
540+ ///
541+ /// # Examples
542+ ///
543+ /// ```
544+ /// #![feature(float_to_from_bytes)]
545+ /// let value = f32::from_le_bytes([0x00, 0x00, 0x48, 0x41]);
546+ /// assert_eq!(value, 12.5);
547+ /// ```
523548 #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
524549 #[ inline]
525550 pub fn from_le_bytes ( bytes : [ u8 ; 4 ] ) -> Self {
526551 Self :: from_bits ( u32:: from_le_bytes ( bytes) )
527552 }
528553
554+ /// Create a floating point value from its representation as a byte array in big endian.
555+ ///
556+ /// As the target platform's native endianness is used, portable code
557+ /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
558+ /// appropriate instead.
559+ ///
560+ /// [`from_be_bytes`]: #method.from_be_bytes
561+ /// [`from_le_bytes`]: #method.from_le_bytes
562+ ///
563+ /// # Examples
564+ ///
565+ /// ```
566+ /// #![feature(float_to_from_bytes)]
567+ /// let value = f32::from_ne_bytes(if cfg!(target_endian = "big") {
568+ /// [0x41, 0x48, 0x00, 0x00]
569+ /// } else {
570+ /// [0x00, 0x00, 0x48, 0x41]
571+ /// });
572+ /// assert_eq!(value, 12.5);
573+ /// ```
529574 #[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
530575 #[ inline]
531576 pub fn from_ne_bytes ( bytes : [ u8 ; 4 ] ) -> Self {
0 commit comments