From 0fd6190b4676142f56e0c11a05a14e1a95af1e4f Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Sat, 20 Jun 2026 09:48:49 +0200 Subject: [PATCH] fix: From Base operation produces wrong results for fractional inputs (#2285) Co-authored-by: Claude Opus 4.6 --- src/core/operations/FromBase.mjs | 3 +- tests/operations/tests/FromBase.mjs | 66 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/operations/tests/FromBase.mjs diff --git a/src/core/operations/FromBase.mjs b/src/core/operations/FromBase.mjs index 4abd5c44..8e69153b 100644 --- a/src/core/operations/FromBase.mjs +++ b/src/core/operations/FromBase.mjs @@ -51,9 +51,10 @@ class FromBase extends Operation { if (number.length === 1) return result; // Fractional part + const radixBN = new BigNumber(radix); for (let i = 0; i < number[1].length; i++) { const digit = new BigNumber(number[1][i], radix); - result += digit.div(Math.pow(radix, i+1)); + result = result.plus(digit.div(radixBN.pow(i + 1))); } return result; diff --git a/tests/operations/tests/FromBase.mjs b/tests/operations/tests/FromBase.mjs new file mode 100644 index 00000000..9f89a1f9 --- /dev/null +++ b/tests/operations/tests/FromBase.mjs @@ -0,0 +1,66 @@ +/** + * From Base operation tests. + * + * @author Willi Ballenthin + * @copyright Crown Copyright 2026 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "From Base: binary integer", + input: "1010", + expectedOutput: "10", + recipeConfig: [ + { + op: "From Base", + args: [2], + }, + ], + }, + { + name: "From Base: binary fraction", + input: "10.1", + expectedOutput: "2.5", + recipeConfig: [ + { + op: "From Base", + args: [2], + }, + ], + }, + { + name: "From Base: hex fraction", + input: "a.8", + expectedOutput: "10.5", + recipeConfig: [ + { + op: "From Base", + args: [16], + }, + ], + }, + { + name: "From Base: octal integer", + input: "77", + expectedOutput: "63", + recipeConfig: [ + { + op: "From Base", + args: [8], + }, + ], + }, + { + name: "From Base: octal fraction", + input: "7.4", + expectedOutput: "7.5", + recipeConfig: [ + { + op: "From Base", + args: [8], + }, + ], + }, +]);