8000 Major precision loss when passing 64-bit values back and forth? · Issue #542 · mlua-rs/mlua · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Major precision loss when passing 64-bit values back and forth? #542

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

Closed
8000
ghost opened this issue Mar 3, 2025 · 3 comments
Closed

Major precision loss when passing 64-bit values back and forth? #542

ghost opened this issue Mar 3, 2025 · 3 comments

Comments

@ghost
Copy link
ghost commented Mar 3, 2025

Hey, I was testing some Discord API stuff in my project, which involves receiving and sending 64-bit values back and forth. I was under the assumption that Lua handled up to 64-bit numbers, but I ran into a bug when passing the application ID to Discord, from Lua, in Rust: the value received from Lua is off by quite a bit. You can test this yourself with this code:

fn test(_: &Lua, value: u64) -> mlua::Result<u64> {
    println!("rust_max: {}", u64::max_value());
    println!("rust: {value}");

    if value == 0 {
        Ok(u64::max_value())
    } else {
        Ok(value)
    }
}
local test = test(0)
print(test)
test(test)

Output:

rust_max: 18446744073709551615
rust: 0
18446744073709552000

In addition, mlua will crash when passing the test value in Lua back to Rust, with the error error converting Lua number to u64 (out of range).

@khvzak
Copy link
Member
khvzak commented Mar 3, 2025

Lua 5.4 API is limited to i64 integers.

mlua actually should reject pushing u64::MAX into Lua (similar to how it rejects vice-versa conversion).

For your case using strings can be more preferable.

@khvzak
Copy link
Member
khvzak commented Mar 3, 2025

This is the behaviour of Lua:

lua5.4 -e 'print(18446744073709551615)'
1.844674407371e+19

The same precision loss

@ghost
Copy link
Author
ghost commented Mar 3, 2025

Ah, alright. Thanks!

@ghost ghost closed this as completed Mar 3, 2025
This issue was closed.
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
0