Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/RawStructuredFieldValues/PseudoDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ extension String {
// Swift Numerics would let us do this for all the floating point types.
extension Double {
public init(_ pseudoDecimal: PseudoDecimal) {
self = Double(pseudoDecimal.mantissa) * pow(10, Double(pseudoDecimal.exponent))
// Divide by an exact power of ten: multiplying by an inexact negative power of ten rounds twice.
self = Double(pseudoDecimal.mantissa) / pow(10, -Double(pseudoDecimal.exponent))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ final class StructuredFieldDecoderTests: XCTestCase {
XCTAssertEqual(ItemField(Double(5.0)), try StructuredFieldValueDecoder().decode(from: Array(headerField.utf8)))
}

func testDecodingDecimalAsDoubleIsCorrectlyRounded() throws {
XCTAssertEqual(ItemField(Double(0.3)), try StructuredFieldValueDecoder().decode(from: Array("0.3".utf8)))
XCTAssertEqual(
ItemField(Double(-19.993)),
try StructuredFieldValueDecoder().decode(from: Array("-19.993".utf8))
)
}

func testAskingForTheWrongType() throws {
let headerField = "gzip"
let intField = "5"
Expand Down