From a567ae76aab05f011a7201cf962860b65cb5c65b Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 29 Oct 2026 00:56:34 +0000 Subject: [PATCH] Number.toString returns its string, and converts in any radix - Number.toString had no `return`: the caller read what was left in the result register, which under gc happened to be the string and under rc, none and own crashed. - number.toString(radix) converts in radix 2 to 36 as JavaScript does: 16 was C's "%a" (0x1.fe00000000000p+7 for 255) and any other radix was ignored. Co-Authored-By: Claude Opus 5.5 --- src/lib.ts | 46 ++++++++++++++++++++++++++++----- tests/number_object_tostring.ts | 22 ++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 tests/number_object_tostring.ts diff --git a/src/lib.ts b/src/lib.ts index f9bf5d9..ab41cd9 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -56,13 +56,47 @@ namespace __Number { return convertNumber(50, `%.${precision}g`, this); } + // In `radix` (2 to 36), as JavaScript writes it: `(255).toString(16)` is "ff" (it was C's "%a", + // 0x1.fe00000000000p+7) and `(10.5).toString(2)` is "1010.1" (any radix but 16 was ignored). export function toString(this: number, radix = 10) { - switch (radix) { - case 16: - return convertNumber(50, "%a", this); - default: - return this; + if (radix == 10) { + return this; + } + + assert(radix >= 2 && radix <= 36, "toString() radix must be between 2 and 36"); + + const value = this; + if (!isFinite(value)) { + return value; + } + + const digits = "0123456789abcdefghijklmnopqrstuvwxyz"; + const negative = value < 0; + const magnitude = negative ? -value : value; + + let whole = Math.floor(magnitude); + let fraction = magnitude - whole; + + let integerPart = ""; + do { + const digit = (whole % radix); + integerPart = digits.substring(digit, digit + 1) + integerPart; + whole = Math.floor(whole / radix); + } while (whole > 0); + + // a double's fraction ends in a power-of-two radix, each step exact (1074 binary digits at + // most); in another radix it may not end, and is cut at 52 digits + const limit = (radix & (radix - 1)) == 0 ? 1100 : 52; + let fractionPart = ""; + for (let i = 0; i < limit && fraction > 0; i++) { + fraction *= radix; + const digit = Math.floor(fraction); + fractionPart += digits.substring(digit, digit + 1); + fraction -= digit; } + + const sign = negative ? "-" : ""; + return fractionPart.length > 0 ? sign + integerPart + "." + fractionPart : sign + integerPart; } } @@ -138,7 +172,7 @@ export class Number { } public toString(radix = 10) { - this.value.toString(radix); + return this.value.toString(radix); } public valueOf() { diff --git a/tests/number_object_tostring.ts b/tests/number_object_tostring.ts new file mode 100644 index 0000000..40afc3d --- /dev/null +++ b/tests/number_object_tostring.ts @@ -0,0 +1,22 @@ +// `Number.toString` returned nothing: the caller read whatever was left in the result register, which +// under gc happened to be the string and under rc, none and own crashed. A radix other than 10 now converts. +function main() { + const n = new Number(5); + assert(n.toString() == "5", "toString of a Number"); + assert(new Number(255).toString(16) == "ff", "toString with a radix"); + assert(n.valueOf() + 1 == 6, "valueOf"); + + // a radix other than 10 was ignored, and 16 was C's "%a" + const x: number = 255; + assert(x.toString(16) == "ff", "radix 16"); + assert(x.toString(2) == "11111111", "radix 2"); + assert(x.toString(36) == "73", "radix 36"); + const y: number = -10.5; + assert(y.toString(2) == "-1010.1", "a negative fraction"); + const z: number = 0; + assert(z.toString(8) == "0", "zero"); + const tenth: number = 0.1; + assert(tenth.toString(2) == "0.0001100110011001100110011001100110011001100110011001101", "every binary digit"); + + console.log("ALL DONE"); +}