Number::toString() abstract operation

Viewed 64

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:

  1. Otherwise, let n, k, and s be integers such that k ≥ 1, 10k - 1 ≤ s < 10k, the Number value for ℝ(s) × 10ℝℝ(n) - ℝ(k) is x, and k is as small as possible. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10ℝ, and that the least significant digit of s is not necessarily uniquely determined by these criteria.
  1. Why are the constraints in step 5 required?

    Namely, that k is as small as possible, 10k - 1 ≤ s < 10k and s is not divisible by 10ℝ.

  2. How are the values of n and k found?

    For the number 1.5e1, the smallest k for 10k - 1 ≤ s < 10k and k ≥ 1 to 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 (as k ≤ n ≤ 21), which doesn't produce the string "15".

  3. What use case does step 6 correspond to?

  1. If k ≤ n ≤ 21, return the string-concatenation of:
    • the code units of the k digits of the decimal representation of s (in order, with no leading zeroes)
    • n - k occurrences of the code unit 0x0030 (DIGIT ZERO)
0 Answers
Related