From 8a75be1b2053208eeb11e982034f6540408b5609 Mon Sep 17 00:00:00 2001 From: atsiv sivat Date: Sun, 8 Mar 2026 00:55:06 +0000 Subject: [PATCH] add function normaliseInput to help sanatise and validate input before parsing --- src/core/lib/IEEEBinary.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/lib/IEEEBinary.mjs b/src/core/lib/IEEEBinary.mjs index 27360b520..16e838eeb 100644 --- a/src/core/lib/IEEEBinary.mjs +++ b/src/core/lib/IEEEBinary.mjs @@ -22,6 +22,12 @@ const MAX_EXP = (1n << EXP_BITS) - 1n; const EXP_BITS_NUM = Number(EXP_BITS); const MANT_BITS_NUM = Number(MANT_BITS); +function normaliseInput(input) { + if (input === null || input === undefined) + throw new Error("Invalid decimal number"); + return String(input).trim(); +} + /** * Compute 10^exp as BigInt without using ** on BigInt, to avoid @@ -183,7 +189,7 @@ export function FromIEEE754Float64(binary64String) { * ToIEEE754Float64("23.300000000000000710542735760100185871124267578125"); */ export function ToIEEE754Float64(input) { - input = String(input).trim(); + input = normaliseInput(input); const expOnes = "1".repeat(EXP_BITS_NUM); const mantZeros = "0".repeat(MANT_BITS_NUM);