From d175db7d6bf98b473b5634e60b9996793e911571 Mon Sep 17 00:00:00 2001 From: Jad Dayoub Date: Wed, 5 Aug 2026 14:22:09 +0200 Subject: [PATCH] [rntuple] Fix precision of 32 bit float in rntuple_test32 --- demo/node/rntuple_test32.js | 11 +++++++++-- demo/node/rntuple_test32.json | 8 ++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/demo/node/rntuple_test32.js b/demo/node/rntuple_test32.js index 8fecacdb9..cf984cbdc 100644 --- a/demo/node/rntuple_test32.js +++ b/demo/node/rntuple_test32.js @@ -4,11 +4,18 @@ import { readFileSync } from 'fs'; // convert float to hex representation based on IEEE-754 and C99 standard -function floatToHex(num) +function floatToHex(num, field) { if (num === 0) return (Object.is(num, -0) ? '-' : '') + '0x0p+0'; + // convert num to Float32 for correct precision + if (field.startsWith("Float")) { + const tmp = new DataView(new ArrayBuffer(4)); + tmp.setFloat32(0, num, false); + num = tmp.getFloat32(0, false); + } + const buf = new ArrayBuffer(8), // 8 Byte == 64 Bit view = new DataView(buf); view.setFloat64(0, num, false); @@ -59,7 +66,7 @@ async function read_file(input_root = 'rntuple_test32.root', input_ref = 'rntupl selector.Process = function(entryIndex) { for (const field of fields) { try { - const value = floatToHex(this.tgtobj[field]), expected = original[entryIndex][field]; + const value = floatToHex(this.tgtobj[field], field), expected = original[entryIndex][field]; if (value !== expected) { console.error(`FAILURE: ${field} at entry ${entryIndex} expected ${expected},`+ diff --git a/demo/node/rntuple_test32.json b/demo/node/rntuple_test32.json index bd8ba384c..a710765ce 100644 --- a/demo/node/rntuple_test32.json +++ b/demo/node/rntuple_test32.json @@ -1,15 +1,15 @@ [ { "FloatReal32Quant1": "-0x1p+0", - "FloatReal32Quant8": "-0x1.fdfdfdfdfdfep-2", - "FloatReal32Quant32": "0x1.0000000a1516p-2", + "FloatReal32Quant8": "-0x1.fdfdfep-2", + "FloatReal32Quant32": "0x1p-2", "DoubleReal32Quant1": "0x1p+0", "DoubleReal32Quant20": "0x1p+0", "DoubleReal32Quant32": "0x1.000000118p+1" }, { "FloatReal32Quant1": "0x1p+0", - "FloatReal32Quant8": "0x1.0101010101p-8", + "FloatReal32Quant8": "0x1.010102p-8", "FloatReal32Quant32": "0x1.921fbp-31", "DoubleReal32Quant1": "0x1p+0", "DoubleReal32Quant20": "0x1.00001p-20", @@ -17,7 +17,7 @@ }, { "FloatReal32Quant1": "0x1p+0", - "FloatReal32Quant8": "0x1.0101010101p-8", + "FloatReal32Quant8": "0x1.010102p-8", "FloatReal32Quant32": "0x1.921fbp-31", "DoubleReal32Quant1": "0x1p+0", "DoubleReal32Quant20": "0x1.00001p-20",