skip to Main Content

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


  1. 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

    Definition: The quart (symbol: qt) is a unit of volume in the United States customary and imperial systems of measurement. Multiple definitions of the quart exist. In the US, a liquid quart is equal to approximately 0.946353 liters and a dry quart is equal to approximately 1.101221 liters. In the UK, the imperial quart is equal to 1.136523. In both the UK and the US, the quart is equal to ¼ of its respective gallon.

    Login or Signup to reply.
  2. 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 for UnitVolume.cups is a US Legal Cup

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search