EN VI

Swift VolumeUnit conversation from quarts to cups seems off?

2024-03-10 19:30:05
How to Swift VolumeUnit conversation from quarts to cups seems off

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?

Solution:

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

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login