diff --git a/Sources/CBOR/Extensions/Float/Float+Float16.swift b/Sources/CBOR/Extensions/Float/Float+Float16.swift index 1ce1e54..68c5650 100644 --- a/Sources/CBOR/Extensions/Float/Float+Float16.swift +++ b/Sources/CBOR/Extensions/Float/Float+Float16.swift @@ -15,12 +15,15 @@ extension Float { init?(halfPrecision x: UInt16) { if (x & 0x7fff) > 0x7c00 { self = .nan + return } if x == 0x7c00 { self = .infinity + return } if x == 0xfc00 { self = -.infinity + return } var t1 = UInt32(x & 0x7fff) // Non-sign bits var t2 = UInt32(x & 0x8000) // Sign bit diff --git a/Tests/CBORTests/DecodableTests.swift b/Tests/CBORTests/DecodableTests.swift index d334f4a..6d427b2 100644 --- a/Tests/CBORTests/DecodableTests.swift +++ b/Tests/CBORTests/DecodableTests.swift @@ -272,4 +272,18 @@ struct DecodableTests { let value = try CBORDecoder().decode(Date.self, from: data) #expect(value == expected) } + + @Test(arguments: [ + ("F90000", 0), + ("F93C00", 1.0), + ("F9BE00", -1.5), + ("F97C00", .infinity), + ("F93E32", 1.548828125), + ("F9F021", -8456) + ]) + func float16(data: String, value: Float) throws { + let data = data.asHexData() + let decoded = try CBORDecoder().decode(Float.self, from: data) + #expect(decoded == value) + } }