@@ -450,10 +450,8 @@ impl f32 {
450
450
/// # Examples
451
451
///
452
452
/// ```
453
- /// use std::f32;
454
453
/// let v = f32::from_bits(0x41480000);
455
- /// let difference = (v - 12.5).abs();
456
- /// assert!(difference <= 1e-5);
454
+ /// assert_eq!(v, 12.5);
457
455
/// ```
458
456
#[ stable( feature = "float_bits_conv" , since = "1.20.0" ) ]
459
457
#[ inline]
@@ -462,50 +460,59 @@ impl f32 {
462
460
unsafe { mem:: transmute ( v) }
463
461
}
464
462
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.
466
465
///
467
466
/// # Examples
468
467
///
469
468
/// ```
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]);
472
472
/// ```
473
473
#[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
474
474
#[ inline]
475
475
pub fn to_be_bytes ( self ) -> [ u8 ; 4 ] {
476
476
self . to_bits ( ) . to_be_bytes ( )
477
477
}
478
478
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.
480
481
///
481
482
/// # Examples
482
483
///
483
484
/// ```
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]);
486
488
/// ```
487
489
#[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
488
490
#[ inline]
489
491
pub fn to_le_bytes ( self ) -> [ u8 ; 4 ] {
490
492
self . to_bits ( ) . to_le_bytes ( )
491
493
}
492
494
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.
495
497
///
496
498
/// As the target platform's native endianness is used, portable code
497
499
/// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
498
500
///
501
+ /// [`to_be_bytes`]: #method.to_be_bytes
502
+ /// [`to_le_bytes`]: #method.to_le_bytes
503
+ ///
499
504
/// # Examples
500
505
///
501
506
/// ```
507
+ /// #![feature(float_to_from_bytes)]
508
+ /// let bytes = 12.5f32.to_ne_bytes();
502
509
/// 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
+ /// }
509
516
/// );
510
517
/// ```
511
518
#[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
@@ -514,18 +521,56 @@ impl f32 {
514
521
self . to_bits ( ) . to_ne_bytes ( )
515
522
}
516
523
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
+ /// ```
517
533
#[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
518
534
#[ inline]
519
535
pub fn from_be_bytes ( bytes : [ u8 ; 4 ] ) -> Self {
520
536
Self :: from_bits ( u32:: from_be_bytes ( bytes) )
521
537
}
522
538
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
+ /// ```
523
548
#[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
524
549
#[ inline]
525
550
pub fn from_le_bytes ( bytes : [ u8 ; 4 ] ) -> Self {
526
551
Self :: from_bits ( u32:: from_le_bytes ( bytes) )
527
552
}
528
553
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
+ /// ```
529
574
#[ unstable( feature = "float_to_from_bytes" , issue = "60446" ) ]
530
575
#[ inline]
531
576
pub fn from_ne_bytes ( bytes : [ u8 ; 4 ] ) -> Self {
0 commit comments