From d7eb3e4ed19d39a013c9c90a800149ea2f77a5f2 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 7 Oct 2024 16:02:35 +0000 Subject: [PATCH] fix floats being incorrectly rounded --- simdnbt/src/borrow/compound.rs | 9 +++++---- simdnbt/src/borrow/mod.rs | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/simdnbt/src/borrow/compound.rs b/simdnbt/src/borrow/compound.rs index ae957ef..455f55b 100644 --- a/simdnbt/src/borrow/compound.rs +++ b/simdnbt/src/borrow/compound.rs @@ -357,16 +357,17 @@ pub(crate) fn read_tag<'a>( } FLOAT_ID => { let float = data.read_f32()?; - tapes - .main - .push(TapeElement::new_with_u32(TapeTagKind::Float, float as u32)); + tapes.main.push(TapeElement::new_with_u32( + TapeTagKind::Float, + float.to_bits(), + )); } DOUBLE_ID => { let double = data.read_f64()?; tapes .main .push(TapeElement::new_with_0(TapeTagKind::Double)); - tapes.main.push(TapeElement::new(double as u64)); + tapes.main.push(TapeElement::new(double.to_bits())); } BYTE_ARRAY_ID => { let byte_array_ptr = data.cur; diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index 9d37c04..0463e82 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -386,14 +386,14 @@ impl<'a: 'tape, 'tape> NbtTag<'a, 'tape> { pub fn float(&self) -> Option { let el = self.element(); ensure_kind(el, TapeTagKind::Float)?; - Some(el.u32() as f32) + Some(f32::from_bits(el.u32())) } pub fn double(&self) -> Option { let el = self.element(); ensure_kind(el, TapeTagKind::Double)?; // the value is in the next element because doubles are too big to fit in a single element let value_el = unsafe { *self.element.add(1) }; - Some(value_el.u64() as f64) + Some(f64::from_bits(value_el.u64())) } pub fn byte_array(&self) -> Option<&'a [u8]> { let el = self.element();