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

The result of dividing two Decimal128 stored on Atlas backend is wrong. #7868

Closed
horatiu1921 opened this issue Jun 29, 2022 · 7 comments
Closed
Assignees

Comments

@horatiu1921
Copy link

horatiu1921 commented Jun 29, 2022

How frequently does the bug occur?

All the time

Description

I am having a strange issue when dividing two Decimal128 values. The result on device and printed on console is ok but the value synced to Atlas backend is wrong. I believe it is related to the significant digits.

I have this two values:

@Persisted var grossPrice: Decimal128 = 70
@Persisted var vatValue: Double = 1.09

grossPrice / Decimal128(floatLiteral: vatValue) = +6422018348623853211009174311926606E-32

The value stored on server is: 12.29721490089025582478677982706510.

On the device where the operation is made, everything is ok but, on other synced devices, all the results are wrong.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.28.1

What SDK flavour are you using?

MongoDB Realm (i.e. Sync, auth, functions)

Are you using encryption?

Yes, using encryption

Platform OS and version(s)

iOS 15.5

Build environment

Xcode version: 13.4.1

@dianaafanador3
Copy link
Contributor

Hi @horatiu1921 I do have a reproductions case for this

@MainActor func test() async throws {
        // Populate the Realm with the values and read the values from the local database, and upload it to the server
        _ = try await Task { @MainActor in
            let user1 = try await self.app.login(credentials: basicCredentials())
            let realm1 = try await Realm(configuration: user1.configuration(testName: #function))
            try realm1.write {
                let testObject = DecimalTestObject(grossPrice: 70, vatValue: 1.09)
                let decimal = testObject.grossPrice / Decimal128(floatLiteral: testObject.vatValue)
                testObject.resultValue = decimal
                XCTAssertEqual(testObject.grossPrice, 70)
                XCTAssertEqual(testObject.vatValue, 1.09)
                XCTAssertEqual(testObject.resultValue,  Decimal128(stringLiteral: "+6422018348623853211009174311926606E-32"))

                realm1.add(testObject)
                XCTAssertEqual(testObject.grossPrice, 70)
                XCTAssertEqual(testObject.vatValue, 1.09)
                XCTAssertEqual(testObject.resultValue,  Decimal128(stringLiteral: "+6422018348623853211009174311926606E-32"))
            }
            waitForUploads(for: realm1)
        }.value

        // Read the values from the server and check that the value is the same, this is where it fails
        _ = try await Task { @MainActor in
            let user2 = try await app.login(credentials: .anonymous)
            let realm2 = try await Realm(configuration: user2.configuration(testName: #function),
                                         downloadBeforeOpen: .once)
            let objects = realm2.objects(DecimalTestObject.self)
            XCTAssertEqual(objects.count, 1)
            XCTAssertEqual(objects[0].grossPrice, 70)
            XCTAssertEqual(objects[0].vatValue, 1.09)
            XCTAssertEqual(objects[0].resultValue,  Decimal128(stringLiteral: "+6422018348623853211009174311926606E-32"))
        }.value
    }

Data seems to be stored correctly on the database, but data sent to the server doesn't coincide with the one from the realm.
Then when we retrieve the data from the server the value is already incorrect.
Seems like an issue while we create the replication set for the server and this is done in core, @danieltabacaru can you help us creating a core test for this?

@danieltabacaru
Copy link

@dianaafanador3 I will create a core test for it and have a look to see if there is something wrong in the sync client.

@dianaafanador3
Copy link
Contributor

@danieltabacaru any luck with a test for this?

@danieltabacaru
Copy link

@dianaafanador3 yes, I can reproduce it in core. We'll take it from here.

@horatiu1921
Copy link
Author

Excellent! Looking forward for a fix!

@sync-by-unito sync-by-unito bot assigned jedelbo and unassigned danieltabacaru Jul 25, 2022
@sync-by-unito sync-by-unito bot closed this as completed Aug 1, 2022
@Jaycyn
Copy link

Jaycyn commented Aug 1, 2022

Was this issue addressed?

@tgoyne
Copy link
Member

tgoyne commented Aug 1, 2022

We've been having trouble getting the core release with the fix out and are still working on that.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants