Skip to content

Commit df53a3f

Browse files
committed
Add documentation to float conversion methods
1 parent ad47f08 commit df53a3f

File tree

2 files changed

+144
-20
lines changed

2 files changed

+144
-20
lines changed

src/libcore/num/f32.rs

+62-17
Original file line numberDiff line numberDiff line change
@@ -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 {

src/libcore/num/f64.rs

+82-3
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,8 @@ 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]
@@ -475,36 +473,117 @@ impl f64 {
475473
unsafe { mem::transmute(v) }
476474
}
477475

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+
/// ```
478486
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
479487
#[inline]
480488
pub fn to_be_bytes(self) -> [u8; 8] {
481489
self.to_bits().to_be_bytes()
482490
}
483491

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+
/// ```
484502
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
485503
#[inline]
486504
pub fn to_le_bytes(self) -> [u8; 8] {
487505
self.to_bits().to_le_bytes()
488506
}
489507

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+
/// ```
490531
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
491532
#[inline]
492533
pub fn to_ne_bytes(self) -> [u8; 8] {
493534
self.to_bits().to_ne_bytes()
494535
}
495536

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+
/// ```
496546
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
497547
#[inline]
498548
pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
499549
Self::from_bits(u64::from_be_bytes(bytes))
500550
}
501551

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+
/// ```
502561
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
503562
#[inline]
504563
pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
505564
Self::from_bits(u64::from_le_bytes(bytes))
506565
}
507566

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+
/// ```
508587
#[unstable(feature = "float_to_from_bytes", issue = "60446")]
509588
#[inline]
510589
pub fn from_ne_bytes(bytes: [u8; 8]) -> Self {

0 commit comments

Comments
 (0)