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