From a2dfbaabe21b423767d1ba35523a4621f2b5e38b Mon Sep 17 00:00:00 2001 From: Jad Dayoub Date: Thu, 6 Aug 2026 17:10:50 +0200 Subject: [PATCH] Fix precision of real32quant for 32 bit float Real32Quant is stored as integers on disk and then transformed back to floating-point numbers using double-precision arithmetic. In the ROOT (C++) implementation, the value is then cast according to the requested field type, which can have either single-precision (float) or double-precision. Since JavaScript only has double-precision floats, Math.fround is used to get the nearest 32-bit single-precision number. --- jsroot/types/fundamental/README.md | 3 --- jsroot/types/fundamental/real32quant/read.mjs | 11 ++++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/jsroot/types/fundamental/README.md b/jsroot/types/fundamental/README.md index dc98213..9c1e5c3 100644 --- a/jsroot/types/fundamental/README.md +++ b/jsroot/types/fundamental/README.md @@ -5,6 +5,3 @@ * [`real`](real): `Real{16,32,64}`, `SplitReal{32,64}` * [`real32trunc`](real32trunc): `Real32Trunc` * [`real32quant`](real32quant): `Real32Quant` - -Note: -- the columns of `real32quant` are more precise in JSROOT, since JavaScript natively supports double-precision floats diff --git a/jsroot/types/fundamental/real32quant/read.mjs b/jsroot/types/fundamental/real32quant/read.mjs index 74bee6b..0d23ad9 100644 --- a/jsroot/types/fundamental/real32quant/read.mjs +++ b/jsroot/types/fundamental/real32quant/read.mjs @@ -1,5 +1,14 @@ import { read, floatToHex } from "../../../jsroot_reader.mjs"; +function formatFloat(num, { field }) { + // round num to single-precision float to match the required field precision + if (field.startsWith("Float")) { + num = Math.fround(num); + } + + return floatToHex(num); +} + const fields = [ "FloatReal32Quant1", "FloatReal32Quant8", @@ -12,4 +21,4 @@ const fields = [ const [input = "types.fundamental.real32quant.root", output = "types.fundamental.real32quant.json"] = process.argv.slice(2); -read(input, output, fields, floatToHex); +read(input, output, fields, formatFloat);