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

Float value changes after writing to realm even for integer part of the complete number #6293

Closed
gauravbordoloi opened this issue Dec 1, 2023 · 4 comments

Comments

@gauravbordoloi
Copy link

gauravbordoloi commented Dec 1, 2023

How frequently does the bug occur?

Always

Description

Float value changes after writing to realm even for integer part of the complete number

import Realm from "realm"

class TData extends Realm.Object {
    static schema = {
        name: "CategoryModel",
        properties: {
            b: "string",
            a: "double",
            c: "float",
        },
        primaryKey: 'b',
    }
}

const db = {
    //path: DB_PATH,
    schema: [TData]
}

let realm = await Realm.open(db);

let p = `${Date.now()}`

let v = 1628899821

realm.write(() => {
    realm.create(TData, { a: v, c: v, b: p })
})

console.log(realm.objectForPrimaryKey(TData, p).toJSON())![image](https://github.com/realm/realm-js/assets/23193185/2c6a3b80-86dc-45fd-8990-3a05104f5ad3)

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

12.3.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Windows 11

Build environment

No response

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Dec 1, 2023

@gauravbordoloi

Thank you for reporting. We (JS + Kotlin) need to investigate why float is behaving this time.

@gauravbordoloi
Copy link
Author

@kneth This hit us on a production environment. Any support on quick will be really helpful else I have to migrate the keys to double. Double is working fine.

@gauravbordoloi
Copy link
Author

Hi @kneth Any update on this?

@nirinchev
Copy link
Member

Float values cannot precisely represent integers above ~16 million. This is because a 32-bit float uses 24 bits for the mantissa and 8 bits for the exponent. If you need to represent large integers, you'd need to either use an int, double, or Decimal128. You can find more technical information about this in the answers of this SO question.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
@sync-by-unito sync-by-unito bot closed this as completed Mar 14, 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

3 participants