I'm trying to understand how the Number::toString(x) abstract operation works.
From what I can make out, the gist of it seems to be that very large (n > 21) and very small (n < 6) numbers are represented in scientific notation (step 10):
String(1.5e22) // => "1.5+22"
...everything else is represented in decimal notation (steps 6-9)
String(1.5e1) // => "15"
This is the fifth step:
- Otherwise, let
n,k, andsbe integers such thatk ≥ 1, 10k - 1 ≤ s < 10k, the Number value forℝ(s) × 10ℝℝ(n) - ℝ(k)isx, andkis as small as possible. Note thatkis the number of digits in the decimal representation ofs, thatsis not divisible by10ℝ, and that the least significant digit ofsis not necessarily uniquely determined by these criteria.
Why are the constraints in step 5 required?
Namely, that
kis as small as possible,10k - 1 ≤ s < 10kandsis not divisible by10ℝ.How are the values of
nandkfound?For the number 1.5e1, the smallest
kfor10k - 1 ≤ s < 10kandk ≥ 1to both be true is 2, which would mean n = 2. But this doesn't seem to be correct, since it takes me to step 6 (ask ≤ n ≤ 21), which doesn't produce the string "15".What use case does step 6 correspond to?
- If
k≤n≤ 21, return the string-concatenation of:
- the code units of the
kdigits of the decimal representation ofs(in order, with no leading zeroes)n-koccurrences of the code unit 0x0030 (DIGIT ZERO)