lua-users home
lua-l archive

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


Hi there,
 
Been using lua a while in a couple of projects, and I haven't really found anything I'd like to add to this elegant and simple language...  anything except perhaps integer support.
 
Proposal:
 
My proposal would be to implement integers as the default internal format of 'numbers' assigned by integer constants or exclusively integer expressions.  Integers would look and act just like the number type, and would only be distinguishable in two ways...
 
1. They would return the string "integer" from the type() funciton.
2. They would print in integer format and not floating point format with the "print" function.
 
In all other ways, they would work identically, and be indistinguishable from numbers.
 
Rationale:
 
1. As a scripting language, lua must interface with C code which is largely integer based, and contains numerous instances of bit flagged variables, shifting, and integer arithmatic.  Representing these values, and performing these operations in lua presently is either poorly handled or not possible.
 
2. Representation of floating point values as doubles is required because lua does not support true integers.  In game applications where floats are the primary floating point value used, constant conversion of floats to doubles and floats to integers take place.
 
3. Certain operations which return imprecise results in floating point (division, multiplication, etc.) are better handled using integer arithmetic.
 
4. In many cases, integer arithmetic in the VM is faster than floating point arithmetic.
 
5. Integer supported added in the way proposed would not break existing programs (except those which may depend on all numeric variables returning the string "number" for the function type()).
 
Details:
 
Integer constants would be represented in lua as integers and not floating point values.  Assignment of an integer value to a variable would result in an integer variable.
 
i.e.
 
i = 100
 
Would be stored internally as an integer.. while
 
i = 100.0
 
Would be stored internally as a float value.
 
Addition and subtraction of two integers would return an integer result..
 
i.e.
 
i = 100 - 50
 
would result in an integer 50.
 
The multiplication of two integers would result in an integer unless the number overflowed, in which case it would be a number.  The overflow would be dictated by the size of the integer, which would default in compiler flags to 64 bit, just as float values default presently to 'double' floats.
 
i = 100 * 100
 
would result in integer 10000
 
while
 
i = 10 * 2000000000000000000000 (not sure where 64 bits would overflow)
 
would result in the floating point value
 
20000000000000000000000.0
 
Any _expression_ mixing integers and numbers would result in a number. 
 
i.e.
 
i = 100 - 50.0
i = 100.0 - 50
i = 10 * 5.0
i = 100 / 2.0
 
would result in a number 50.0
 
The following methods would support integer operations.
 
i = tointeger(n)
 
Would convert a string or a number to an integer.
 
d,r = idiv(a,b)
 
Would return the result of an integer division, returning its integer result and the remainder.
 
i = imult(a,b)
 
Would return the integer result of multiplying integer values a,b and would not convert to a number on overflow.  If a and b were not integers, they would be converted to integers.
 
i = ishift(i, bits[, precision])
 
Would return the bits of integer i shifted by d.  The optional precision value would be 8 for 8 bit precision, 16 for 16 bit, 32 for 32 bits, and 64 for 64 bits.
 
i = iand(a,b)
 
Performs an integer bitwise AND operation on two values.
 
i = ior(a,b)
 
Performs a bitwise integer OR operation on two values.
 
i = inot(i)
 
Performs a bitwise integer NOT operation on two values.
 
i = ixor(i)
 
Peforms a bitwise integer XOR operation on two values.
 
The function
 
type(i)
 
would return "integer" if the value passed was an integer.