lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Dec 18, 2014, at 7:40 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

>>> As this point has come up several times, I have a quick quiz for this
>>> list. Without running the code, what are the results of the following
>>> expressions in Lua 5.2?
>>> 
>>>  string.rep("a", 2.7)
>>>  string.format("%d", 2.7)
>>>  string.rep("a", 2.3)
>>>  string.format("%d", 2.3)
>>> 
> 
>> Undefined, Undefined, Undefined, Undefined, or with GCC, "aaa", "3",
>> "aa", "2" (pretty sure GCC rounds properly? I may be wrong)
>> (Soni)
> 
>> About the quiz, in version 5.2, I believe that all values are considered
>> equal to 2.
>> (Charles)
> 
>> IIRC, in practice, lua always rounds down. so:
>> aa
>> 2
>> aa
>> 2
> (Daurnimator)
> 
> "Undefined" is correct according to the manual, but (unlike 5.3) the
> implementation thinks otherwise. And none of these answers match what my
> machine (an ordinary Pentium/Linux) does.  Anyone else want to try an
> answer?
> 
> -- Roberto
> 
> 
> -- Roberto
> 

I also thought the answer Daurnimator posted was correct. I tried it with 5.1, 5.2 and 5.3 (beta). With 5.1 and 5.2 I get the result I expected:
  aa
  2
  aa
  2
And, as expected on 5.3 (beta), I got the error "(number has no integer representation)” on all four expressions.

I am running on OS X 10.9, in case is make a difference to someone.

(Yes, I know I need to update my 5.3 to rc1...

-djd