fix: From Base operation produces wrong results for fractional inputs (#2285)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Willi Ballenthin 2026-06-20 09:48:49 +02:00 committed by GitHub
parent f0468d391d
commit 0fd6190b46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 1 deletions

View File

@ -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;

View File

@ -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],
},
],
},
]);