Round decimal place in Vector3 and CFrame when converting to dom type

This commit is contained in:
Filip Tibell 2023-09-25 13:43:59 -05:00
parent 2e53cdcad7
commit bfcd78c43e
No known key found for this signature in database
4 changed files with 26 additions and 4 deletions

View file

@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Update to Luau version `0.594` - Update to Luau version `0.594`
- CFrame and Vector3 values are now rounded to the nearest 2 ^ 16 decimal place to reduce floating point errors and diff noise. Note that this does not affect intermediate calculations done in lua, and only happens when a property value is set on an Instance.
### Fixed ### Fixed

View file

@ -6,6 +6,8 @@ pub mod extension;
pub mod result; pub mod result;
pub mod types; pub mod types;
mod util;
use result::*; use result::*;
pub use crate::roblox::shared::userdata::*; pub use crate::roblox::shared::userdata::*;

View file

@ -5,7 +5,10 @@ use glam::Vec3;
use mlua::prelude::*; use mlua::prelude::*;
use rbx_dom_weak::types::Vector3 as DomVector3; use rbx_dom_weak::types::Vector3 as DomVector3;
use crate::{lune::util::TableBuilder, roblox::exports::LuaExportsTable}; use crate::{
lune::util::TableBuilder,
roblox::{datatypes::util::round_float_decimal, exports::LuaExportsTable},
};
use super::{super::*, EnumItem}; use super::{super::*, EnumItem};
@ -212,9 +215,9 @@ impl From<DomVector3> for Vector3 {
impl From<Vector3> for DomVector3 { impl From<Vector3> for DomVector3 {
fn from(v: Vector3) -> Self { fn from(v: Vector3) -> Self {
DomVector3 { DomVector3 {
x: v.0.x, x: round_float_decimal(v.0.x),
y: v.0.y, y: round_float_decimal(v.0.y),
z: v.0.z, z: round_float_decimal(v.0.z),
} }
} }
} }

View file

@ -0,0 +1,16 @@
// HACK: We round to the nearest Very Small Decimal
// to reduce writing out floating point accumulation
// errors to files (mostly relevant for xml formats)
const ROUNDING: usize = 65_536; // 2 ^ 16
pub fn round_float_decimal(value: f32) -> f32 {
let place = ROUNDING as f32;
// Round only the fractional part, we do not want to
// lose any float precision in case a user for some
// reason has very very large float numbers in files
let whole = value.trunc();
let fract = (value.fract() * place).round() / place;
whole + fract
}