1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 23:44:40 +00:00

fix floats being incorrectly rounded

This commit is contained in:
mat 2024-10-07 16:02:35 +00:00
commit d7eb3e4ed1
2 changed files with 7 additions and 6 deletions

View file

@ -357,16 +357,17 @@ pub(crate) fn read_tag<'a>(
} }
FLOAT_ID => { FLOAT_ID => {
let float = data.read_f32()?; let float = data.read_f32()?;
tapes tapes.main.push(TapeElement::new_with_u32(
.main TapeTagKind::Float,
.push(TapeElement::new_with_u32(TapeTagKind::Float, float as u32)); float.to_bits(),
));
} }
DOUBLE_ID => { DOUBLE_ID => {
let double = data.read_f64()?; let double = data.read_f64()?;
tapes tapes
.main .main
.push(TapeElement::new_with_0(TapeTagKind::Double)); .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 => { BYTE_ARRAY_ID => {
let byte_array_ptr = data.cur; let byte_array_ptr = data.cur;

View file

@ -386,14 +386,14 @@ impl<'a: 'tape, 'tape> NbtTag<'a, 'tape> {
pub fn float(&self) -> Option<f32> { pub fn float(&self) -> Option<f32> {
let el = self.element(); let el = self.element();
ensure_kind(el, TapeTagKind::Float)?; ensure_kind(el, TapeTagKind::Float)?;
Some(el.u32() as f32) Some(f32::from_bits(el.u32()))
} }
pub fn double(&self) -> Option<f64> { pub fn double(&self) -> Option<f64> {
let el = self.element(); let el = self.element();
ensure_kind(el, TapeTagKind::Double)?; ensure_kind(el, TapeTagKind::Double)?;
// the value is in the next element because doubles are too big to fit in a single element // 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) }; 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]> { pub fn byte_array(&self) -> Option<&'a [u8]> {
let el = self.element(); let el = self.element();