Skip to content

Commit e0d2c86

Browse files
committed
Add direct .bytes() methods to Double and Float
Since IEEE 754 is THE canonical transformation (not one of many), add direct instance methods matching FixedWidthInteger pattern. This is additive - all existing namespace methods remain. Changes: - Add Double.bytes(endianness:) instance method - Add Float.bytes(endianness:) instance method - Both delegate to authoritative IEEE_754.Binary64/Binary32 implementations - Add 4 new tests demonstrating direct access pattern Multiple ergonomic options now available: - value.bytes() - NEW direct canonical (like FixedWidthInteger) - value.ieee754.bytes() - existing namespace access - [UInt8](value) - existing unlabeled init - Double(bytes: data) - existing canonical init All patterns work and delegate to the same authoritative implementation. Tests: All 16 tests passing (12 existing + 4 new)
1 parent 55be653 commit e0d2c86

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

Sources/IEEE_754/Double+IEEE_754.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ extension Double {
5454
}
5555
}
5656

57+
// MARK: - Canonical Serialization
58+
59+
extension Double {
60+
/// Returns IEEE 754 binary64 byte representation
61+
///
62+
/// Canonical serialization following FixedWidthInteger pattern.
63+
/// IEEE 754 is THE authoritative representation for floating-point values.
64+
///
65+
/// Delegates to `IEEE_754.Binary64.bytes(from:endianness:)`.
66+
///
67+
/// - Parameter endianness: Byte order (little or big)
68+
/// - Returns: 8-byte array in IEEE 754 binary64 format
69+
///
70+
/// Example:
71+
/// ```swift
72+
/// let bytes = value.bytes()
73+
/// let bytes = value.bytes(endianness: .big)
74+
/// ```
75+
public func bytes(endianness: [UInt8].Endianness = .little) -> [UInt8] {
76+
IEEE_754.Binary64.bytes(from: self, endianness: endianness)
77+
}
78+
}
79+
5780
// MARK: - Type-level Methods
5881

5982
extension Double {

Sources/IEEE_754/Float+IEEE_754.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ extension Float {
5454
}
5555
}
5656

57+
// MARK: - Canonical Serialization
58+
59+
extension Float {
60+
/// Returns IEEE 754 binary32 byte representation
61+
///
62+
/// Canonical serialization following FixedWidthInteger pattern.
63+
/// IEEE 754 is THE authoritative representation for floating-point values.
64+
///
65+
/// Delegates to `IEEE_754.Binary32.bytes(from:endianness:)`.
66+
///
67+
/// - Parameter endianness: Byte order (little or big)
68+
/// - Returns: 4-byte array in IEEE 754 binary32 format
69+
///
70+
/// Example:
71+
/// ```swift
72+
/// let bytes = value.bytes()
73+
/// let bytes = value.bytes(endianness: .big)
74+
/// ```
75+
public func bytes(endianness: [UInt8].Endianness = .little) -> [UInt8] {
76+
IEEE_754.Binary32.bytes(from: self, endianness: endianness)
77+
}
78+
}
79+
5780
// MARK: - Type-level Methods
5881

5982
extension Float {

Tests/IEEE_754_Tests/IEEE_754_Tests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ struct Binary64Tests {
4444
#expect(restored == value)
4545
}
4646

47+
@Test("Double direct bytes method")
48+
func doubleDirectBytes() {
49+
let value: Double = 2.71828
50+
let bytes = value.bytes()
51+
let restored = Double(bytes: bytes)
52+
53+
#expect(restored == value)
54+
}
55+
56+
@Test("Double direct bytes big-endian")
57+
func doubleDirectBytesBigEndian() {
58+
let value: Double = 1.41421
59+
let bytes = value.bytes(endianness: .big)
60+
let restored = Double(bytes: bytes, endianness: .big)
61+
62+
#expect(restored == value)
63+
}
64+
4765
@Test("Double special values")
4866
func doubleSpecialValues() {
4967
// Zero
@@ -98,6 +116,24 @@ struct Binary32Tests {
98116
#expect(restored == value)
99117
}
100118

119+
@Test("Float direct bytes method")
120+
func floatDirectBytes() {
121+
let value: Float = 2.71828
122+
let bytes = value.bytes()
123+
let restored = Float(bytes: bytes)
124+
125+
#expect(restored == value)
126+
}
127+
128+
@Test("Float direct bytes big-endian")
129+
func floatDirectBytesBigEndian() {
130+
let value: Float = 1.41421
131+
let bytes = value.bytes(endianness: .big)
132+
let restored = Float(bytes: bytes, endianness: .big)
133+
134+
#expect(restored == value)
135+
}
136+
101137
@Test("Float special values")
102138
func floatSpecialValues() {
103139
// Zero

0 commit comments

Comments
 (0)