Skip to content

Commit

Permalink
Round decimal place in Vector3 and CFrame when converting to dom type
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Sep 25, 2023
1 parent 2e53cdc commit bfcd78c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- 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

Expand Down
2 changes: 2 additions & 0 deletions src/roblox/datatypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod extension;
pub mod result;
pub mod types;

mod util;

use result::*;

pub use crate::roblox::shared::userdata::*;
11 changes: 7 additions & 4 deletions src/roblox/datatypes/types/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use glam::Vec3;
use mlua::prelude::*;
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};

Expand Down Expand Up @@ -212,9 +215,9 @@ impl From<DomVector3> for Vector3 {
impl From<Vector3> for DomVector3 {
fn from(v: Vector3) -> Self {
DomVector3 {
x: v.0.x,
y: v.0.y,
z: v.0.z,
x: round_float_decimal(v.0.x),
y: round_float_decimal(v.0.y),
z: round_float_decimal(v.0.z),
}
}
}
16 changes: 16 additions & 0 deletions src/roblox/datatypes/util.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bfcd78c

Please sign in to comment.