Skip to content

Commit

Permalink
Editorial: Define + use StringToNumber
Browse files Browse the repository at this point in the history
Formerly, the procedure for applying ToNumber to the String type
was spread over three widely-separated prose paragraphs
in two different clauses.

This commit brings all that together, expresses it as an actual algorithm,
and gives it the name StringToNumber.

Also, the definitions of the syntax-direction operation
NumericValue mostly just delegated to a prose description.
This commit replaces that with actual algorithms,
similar to that for StringToNumber.
  • Loading branch information
jmdyck committed Dec 3, 2020
1 parent ce2f9ac commit 8124e2e
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -4277,7 +4277,7 @@ <h1>ToNumber ( _argument_ )</h1>
String
</td>
<td>
See grammar and conversion algorithm below.
Return ! StringToNumber(_argument_).
</td>
</tr>
<tr>
Expand Down Expand Up @@ -4314,7 +4314,7 @@ <h1>ToNumber ( _argument_ )</h1>

<emu-clause id="sec-tonumber-applied-to-the-string-type">
<h1>ToNumber Applied to the String Type</h1>
<p>ToNumber applied to Strings applies the following grammar to the input String interpreted as a sequence of UTF-16 encoded code points (<emu-xref href="#sec-ecmascript-language-types-string-type"></emu-xref>). If the grammar cannot interpret the String as an expansion of |StringNumericLiteral|, then the result of ToNumber is *NaN*.</p>
<p>The abstract operation StringToNumber specifies how to convert a String value to a Number value. Overall, the process is similar to the determination of the NumericValue of a numeric literal (see <emu-xref href="#sec-literals-numeric-literals"></emu-xref>), but some of the details are different.</p>
<h2>Syntax</h2>
<emu-grammar type="definition">
StringNumericLiteral :::
Expand Down Expand Up @@ -4370,7 +4370,6 @@ <h2>Syntax</h2>

<emu-clause id="sec-runtime-semantics-mv-s">
<h1>Runtime Semantics: MV</h1>
<p>The conversion of a String to a Number value is similar overall to the determination of the Number value for a numeric literal (see <emu-xref href="#sec-literals-numeric-literals"></emu-xref>), but some of the details are different, so the process for converting a String numeric literal to a value of Number type is given here. This value is determined in two steps: first, a mathematical value (MV) is derived from the String numeric literal; second, this mathematical value is rounded as described below. The MV on any grammar symbol, not provided below, is the MV for that symbol defined in <emu-xref href="#sec-static-semantics-mv"></emu-xref>.</p>
<ul>
<li>
The MV of <emu-grammar>StringNumericLiteral ::: [empty]</emu-grammar> is 0.
Expand Down Expand Up @@ -4406,7 +4405,29 @@ <h1>Runtime Semantics: MV</h1>
The MV of <emu-grammar>StrUnsignedDecimalLiteral ::: DecimalDigits ExponentPart</emu-grammar> is the MV of |DecimalDigits| times 10<sup>_e_</sup>, where _e_ is the MV of |ExponentPart|.
</li>
</ul>
<p>Once the exact MV for a String numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is *+0*<sub>𝔽</sub> unless the first non white space code point in the String numeric literal is `-`, in which case the rounded value is *-0*<sub>𝔽</sub>. Otherwise, the rounded value must be the Number value for the MV (in the sense defined in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>), unless the literal includes a |StrUnsignedDecimalLiteral| and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit and then incrementing the literal at the 20th digit position. A digit is significant if it is not part of an |ExponentPart| and</p>
<p>For the MV of any production not shown above, see <emu-xref href="#sec-static-semantics-mv"></emu-xref>.</p>
</emu-clause>

<emu-clause id="sec-stringtonumber" aoid="StringToNumber">
<h1>StringToNumber ( _str_ )</h1>
<p>The abstract operation StringToNumber takes argument _str_ (a String) and returns a Number. It performs the following steps when called:</p>
<emu-alg>
1. Let _text_ be ! StringToCodePoints(_str_).
1. Let _literal_ be ParseText(_text_, |StringNumericLiteral|).
1. If _literal_ is a List of errors, return *NaN*.
1. If _literal_ contains a |StrUnsignedDecimalLiteral| and _literal_ has more than 20 significant digits, then
1. Let _lit_ be an implementation-defined choice of:
* _literal_
* a literal produced from _literal_ by replacing each significant digit after the 20th with a `0` digit
* a literal produced from _literal_ by replacing each significant digit after the 20th with a `0` digit and then incrementing the literal at the 20th significant digit position
1. Else,
1. Let _lit_ be _literal_.
1. Let _mv_ be the MV of _lit_.
1. If _mv_ is 0 and the first non white space code point in _text_ is `-`, return *-0*<sub>𝔽</sub>.
1. Return 𝔽(_mv_).
</emu-alg>

<p>A digit is <em>significant</em> if it is not part of an |ExponentPart| and</p>
<ul>
<li>
it is not `0`; or
Expand Down Expand Up @@ -11027,7 +11048,7 @@ <h2>Syntax</h2>

<emu-clause id="sec-static-semantics-mv">
<h1>Static Semantics: MV</h1>
<p>A numeric literal stands for a value of the Number type or the BigInt type.</p>
<p>A numeric literal stands for a value of the Number type or the BigInt type, as specified by the syntax-directed operation NumericValue. Determining this value involves deriving a mathematical value (MV) from the literal, according to the following rules.</p>
<ul>
<li>
The MV of <emu-grammar>NumericLiteral :: DecimalLiteral</emu-grammar> is the MV of |DecimalLiteral|.
Expand Down Expand Up @@ -11193,15 +11214,26 @@ <h1>Static Semantics: MV</h1>

<emu-clause id="sec-numericvalue">
<h1>Static Semantics: NumericValue</h1>
<emu-grammar>NumericLiteral :: DecimalLiteral</emu-grammar>
<emu-grammar>NumericLiteral :: NonDecimalIntegerLiteral</emu-grammar>
<emu-alg>
1. Return the Number value that results from rounding the MV of |DecimalLiteral| as described below.
1. Let _mv_ be the MV of |NonDecimalIntegerLiteral|.
1. Return 𝔽(_mv_).
</emu-alg>
<emu-grammar>NumericLiteral :: NonDecimalIntegerLiteral</emu-grammar>

<emu-grammar>NumericLiteral :: DecimalLiteral</emu-grammar>
<emu-alg>
1. Return the Number value that results from rounding the MV of |NonDecimalIntegerLiteral| as described below.
1. If |DecimalLiteral| has more than 20 significant digits, then
1. Let _lit_ be an implementation-defined choice of:
* |DecimalLiteral|
* a literal produced from |DecimalLiteral| by replacing each significant digit after the 20th with a `0` digit
* a literal produced from |DecimalLiteral| by replacing each significant digit after the 20th with a `0` digit and then incrementing the literal at the 20th significant digit position
1. Else,
1. Let _lit_ be |DecimalLiteral|.
1. Let _mv_ be the MV of _lit_.
1. Return 𝔽(_mv_).
</emu-alg>
<p>Once the exact MV for a numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is *+0*<sub>𝔽</sub>; otherwise, the rounded value must be the Number value for the MV (as specified in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>), unless the literal is a |DecimalLiteral| and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a `0` digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a `0` digit and then incrementing the literal at the 20th significant digit position. A digit is <em>significant</em> if it is not part of an |ExponentPart| and</p>

<p>A digit is <em>significant</em> if it is not part of an |ExponentPart| and</p>
<ul>
<li>
it is not `0`; or
Expand Down

0 comments on commit 8124e2e

Please sign in to comment.