diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-12-05 12:57:40 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-12-05 12:57:40 -0300 |
commit | e174f43807d46a7c0a9ab5eeb3fc4434bcb0091f (patch) | |
tree | 680638661435782b498d4de40400247bba06e386 | |
parent | 81f2401c6dc6afc819787a0b651f9e4be241e942 (diff) | |
download | lua-e174f43807d46a7c0a9ab5eeb3fc4434bcb0091f.tar.gz lua-e174f43807d46a7c0a9ab5eeb3fc4434bcb0091f.tar.bz2 lua-e174f43807d46a7c0a9ab5eeb3fc4434bcb0091f.zip |
Manual a little more clear about string->number coersions
-rw-r--r-- | manual/manual.of | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/manual/manual.of b/manual/manual.of index 61d4afac..d5b4a572 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -1406,10 +1406,9 @@ It has the following syntax: | |||
1406 | exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} | 1406 | exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} |
1407 | } | 1407 | } |
1408 | The given identifier (@bnfNter{Name}) defines the control variable, | 1408 | The given identifier (@bnfNter{Name}) defines the control variable, |
1409 | which is local to the loop body (@emph{block}). | 1409 | which is a new variable local to the loop body (@emph{block}). |
1410 | 1410 | ||
1411 | The loop starts by evaluating once the three control expressions; | 1411 | The loop starts by evaluating once the three control expressions. |
1412 | they must all result in numbers. | ||
1413 | Their values are called respectively | 1412 | Their values are called respectively |
1414 | the @emph{initial value}, the @emph{limit}, and the @emph{step}. | 1413 | the @emph{initial value}, the @emph{limit}, and the @emph{step}. |
1415 | If the step is absent, it defaults @N{to 1}. | 1414 | If the step is absent, it defaults @N{to 1}. |
@@ -1417,8 +1416,9 @@ If the step is absent, it defaults @N{to 1}. | |||
1417 | If both the initial value and the step are integers, | 1416 | If both the initial value and the step are integers, |
1418 | the loop is done with integers; | 1417 | the loop is done with integers; |
1419 | note that the limit may not be an integer. | 1418 | note that the limit may not be an integer. |
1420 | Otherwise, the loop is done with floats. | 1419 | Otherwise, the three values are converted to |
1421 | (Beware of floating-point accuracy in this case.) | 1420 | floats and the loop is done with floats. |
1421 | Beware of floating-point accuracy in this case. | ||
1422 | 1422 | ||
1423 | After that initialization, | 1423 | After that initialization, |
1424 | the loop body is repeated with the value of the control variable | 1424 | the loop body is repeated with the value of the control variable |
@@ -1773,9 +1773,24 @@ If it does, that representation is the result. | |||
1773 | Otherwise, the conversion fails. | 1773 | Otherwise, the conversion fails. |
1774 | 1774 | ||
1775 | Several places in Lua coerce strings to numbers when necessary. | 1775 | Several places in Lua coerce strings to numbers when necessary. |
1776 | In particular, | ||
1777 | the string library sets metamethods that try to coerce | ||
1778 | strings to numbers in all arithmetic operations. | ||
1779 | If the conversion fails, | ||
1780 | the library calls the metamethod of the other operand | ||
1781 | (if present) or it raises an error. | ||
1782 | Note that bitwise operators do not do this coercion. | ||
1783 | |||
1784 | Nonetheless, it is always a good practice not to rely on these | ||
1785 | implicit coercions, as they are not always applied; | ||
1786 | in particular, @T{"1"==1} is false and @T{"1"<1} raises an error | ||
1787 | @see{rel-ops}. | ||
1788 | These coercions exist mainly for compatibility and may be removed | ||
1789 | in future versions of the language. | ||
1790 | |||
1776 | A string is converted to an integer or a float | 1791 | A string is converted to an integer or a float |
1777 | following its syntax and the rules of the Lua lexer. | 1792 | following its syntax and the rules of the Lua lexer. |
1778 | (The string may have also leading and trailing whitespaces and a sign.) | 1793 | The string may have also leading and trailing whitespaces and a sign. |
1779 | All conversions from strings to numbers | 1794 | All conversions from strings to numbers |
1780 | accept both a dot and the current locale mark | 1795 | accept both a dot and the current locale mark |
1781 | as the radix character. | 1796 | as the radix character. |
@@ -1783,15 +1798,9 @@ as the radix character. | |||
1783 | If the string is not a valid numeral, | 1798 | If the string is not a valid numeral, |
1784 | the conversion fails. | 1799 | the conversion fails. |
1785 | If necessary, the result of this first step is then converted | 1800 | If necessary, the result of this first step is then converted |
1786 | to the required number subtype following the previous rules | 1801 | to a specific number subtype following the previous rules |
1787 | for conversions between floats and integers. | 1802 | for conversions between floats and integers. |
1788 | 1803 | ||
1789 | The string library uses metamethods that try to coerce | ||
1790 | strings to numbers in all arithmetic operations. | ||
1791 | If the conversion fails, | ||
1792 | the library calls the metamethod of the other operand | ||
1793 | (if present) or it raises an error. | ||
1794 | |||
1795 | The conversion from numbers to strings uses a | 1804 | The conversion from numbers to strings uses a |
1796 | non-specified human-readable format. | 1805 | non-specified human-readable format. |
1797 | To convert numbers to strings in any specific way, | 1806 | To convert numbers to strings in any specific way, |
@@ -7687,8 +7696,8 @@ This library provides basic mathematical functions. | |||
7687 | It provides all its functions and constants inside the table @defid{math}. | 7696 | It provides all its functions and constants inside the table @defid{math}. |
7688 | Functions with the annotation @St{integer/float} give | 7697 | Functions with the annotation @St{integer/float} give |
7689 | integer results for integer arguments | 7698 | integer results for integer arguments |
7690 | and float results for float (or mixed) arguments. | 7699 | and float results for non-integer arguments. |
7691 | the rounding functions | 7700 | The rounding functions |
7692 | @Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf} | 7701 | @Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf} |
7693 | return an integer when the result fits in the range of an integer, | 7702 | return an integer when the result fits in the range of an integer, |
7694 | or a float otherwise. | 7703 | or a float otherwise. |
@@ -7843,7 +7852,7 @@ The results from this function have good statistical qualities, | |||
7843 | but they are not cryptographically secure. | 7852 | but they are not cryptographically secure. |
7844 | (For instance, there are no guarantees that it is hard | 7853 | (For instance, there are no guarantees that it is hard |
7845 | to predict future results based on the observation of | 7854 | to predict future results based on the observation of |
7846 | some number of previous results.) | 7855 | some previous results.) |
7847 | 7856 | ||
7848 | } | 7857 | } |
7849 | 7858 | ||