Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple question about 59.99 + 0.01 #184

Closed
rijieli opened this issue Sep 9, 2023 · 1 comment
Closed

A simple question about 59.99 + 0.01 #184

rijieli opened this issue Sep 9, 2023 · 1 comment

Comments

@rijieli
Copy link

rijieli commented Sep 9, 2023

For the expression 59.99 + 0.01, the expected result is 60.0, but I'm getting 60.0999.... I understand that this discrepancy is due to floating-point computing. How can I avoid this issue?

I'm also writing a test case, and in most situations, the result is not 60.0. MathSolver is a simple wrapper for try str.evaluate().

func testExample() throws {
    // 59.99 + 0.01 -> ("60.099999999999994") is not equal to ("60.0")
    for n in 1...99 {
        let lhs = "59." + Array(repeating: "9", count: n).joined()
        let rhs = "0." + Array(repeating: "1", count: n).joined()
        let result = MathSolver.solve(lhs + " + " + rhs)!
        XCTAssertEqual(result, 60.0, "\n🔢 \(lhs)")
    }
}
@rijieli
Copy link
Author

rijieli commented Sep 9, 2023

Duplicate to #99

@rijieli rijieli closed this as completed Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant