Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/web/src/lib/base-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function decimalToOctal(decimal: number | BigNumber): ConversionResult {
let magnitude = remainders.join("") || "0";

// Fractional part conversion using BigNumber for precision
let fractionalResult = "";
let fractionalResult: string;
if (hasFractionalPart) {
// Extract fractional part as BigNumber precisely
const intPart = absValue.integerValue(BigNumber.ROUND_DOWN);
Expand Down Expand Up @@ -316,7 +316,7 @@ export function decimalToHexadecimal(
let magnitude = remainders.join("") || "0";

// Fractional part conversion using BigNumber for precision (20 digits)
let fractionalResult = "";
let fractionalResult: string;
if (hasFractionalPart) {
const intPart = absValue.integerValue(BigNumber.ROUND_DOWN);
let fracPart = absValue.minus(intPart);
Expand Down
25 changes: 2 additions & 23 deletions apps/web/src/lib/conversions/decimal-to-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { formatDecimalOutput } from "../utils/formatting-utils";
*/
export function decimalToBinary(
decimal: number | BigNumberType,
specifiedBits?: number,
_specifiedBits?: number,
): ConversionResult {
// Handle BigNumber input
const isBigNumber = decimal instanceof BigNumber;
Expand Down Expand Up @@ -54,28 +54,7 @@ export function decimalToBinary(
magnitude = "0";
}

// Step 2: Determine bit width - adapt based on minimum required
let bitWidth: number;
if (specifiedBits) {
bitWidth = specifiedBits;
} else {
// For BigNumbers, estimate bit width from string length
const estimatedBits = isBigNumber
? bigDecimal.toString(2).length
: Math.ceil(Math.log2(Math.abs(bigDecimal.toNumber()) + 1));

// For negative numbers, use the same bit width as positive (two's complement handles the sign)
bitWidth = Math.max(estimatedBits, magnitude.length);
// Ensure minimum 8-bit alignment for readability
bitWidth = Math.max(bitWidth, 8);
}

// Don't pad small results - use natural binary representation
// Only pad very large numbers if needed for readability
// (This preserves the natural binary length for small numbers)

// Fractional part conversion with BigNumber precision (dynamic length)
let fractionalResult = "";
if (hasFractionalPart) {
const intPartBN = absValue.integerValue(BigNumber.ROUND_DOWN);
let fracBN = absValue.minus(intPartBN);
Expand All @@ -97,7 +76,7 @@ export function decimalToBinary(
fracBN = fracBN.minus(bitBN);
}

fractionalResult = bits.join("");
const fractionalResult = bits.join("");
if (fractionalResult) magnitude += "." + fractionalResult;
}

Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {},
"devDependencies": {
"@eslint/js": "^9.35.0",
"@eslint/js": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^8.44.0",
"@typescript-eslint/parser": "^8.44.0",
"eslint": "^9.35.0",
Expand Down