I’m working on some Swift code that is intended to supply multipliers for conversions between some common volume units. Unless I’m using it incorrectly, I seems to have found an anomaly in conversions from quarts to cups.
Here’s a simple unit test I wrote to demonstrate the problem I’m running into:
func testUnitConversions() throws {
let quarts = Measurement(value: 1, unit: UnitVolume.quarts)
let conversion = quarts.converted(to: UnitVolume.cups)
let value = conversion.value
XCTAssertEqual(value, 4)
}
In Xcode 15.3 it fails with: "testUnitConversions(): XCTAssertEqual failed: ("3.9431375") is not equal to ("4.0")"
Every conversion site I look at on the web tells me that a quart equals 4 cups. Am I doing this conversion incorrectly or is it a Swift issue?
2
Answers
Looks like the gap between true measurement and real world use.
Most people don’t need the full accuracy to bake a cake so 0.94… is rounded to 1
Quote from
unitconverters.net
1 US cup = 1/4 quart but 1 US cup = 0.98578432 US legal cup and 4 * a legal cup is 3,94313728 so it looks like what
Measurement
uses forUnitVolume.cups
is a US Legal Cup