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:
parent
f0468d391d
commit
0fd6190b46
@ -51,9 +51,10 @@ class FromBase extends Operation {
|
|||||||
if (number.length === 1) return result;
|
if (number.length === 1) return result;
|
||||||
|
|
||||||
// Fractional part
|
// Fractional part
|
||||||
|
const radixBN = new BigNumber(radix);
|
||||||
for (let i = 0; i < number[1].length; i++) {
|
for (let i = 0; i < number[1].length; i++) {
|
||||||
const digit = new BigNumber(number[1][i], radix);
|
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;
|
return result;
|
||||||
|
|||||||
66
tests/operations/tests/FromBase.mjs
Normal file
66
tests/operations/tests/FromBase.mjs
Normal 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],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user