lua-users home
lua-l archive

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


Am 17.05.2014 03:43 schröbte Leo Razoumov:
On 4/29/14, Tim Hill <drtimhill@gmail.com> wrote:

“I agree with the original rationale (as outlined by Roberto, with some
reservations) that only the types of values affect the outcome, not the
values”.


In math the type of a result of a numeric operation DOES depend on the values
of the operands because some arithmetic operations (notably division)
are not closed in integers.

In maths, division is a partial function on integers (Z x Z -> Z), or an almost total function on rationals (Q x Q -> Q) (or some other compatible field). Integer division in C (and Lua it seems) just extends the partial function used in maths and does "something useful" for the undefined values, but the operation is still Z x Z -> Z, so the result type only depends on the operation, *not* the operand values.

Btw., does anyone know who invented integer division as defined in C in the first place? Is it used outside of programming?


For example, take division: 4/2 == 2: two integers go in and one
integer goes out and all the math properties of division
are preserved.

Or: Two rationals go in, one rational comes out.

On the other hand, 5/3 poses a problem. You either
perform a floating point division which  changes
the type, or preserve the type by doing an integer division which
breaks associativity of the division.

I can see no problem: Two rationals go in, one rational comes out, *or* two integers go in, and the result is undefined (in maths)/some arbitrary integer (in C and Lua). Obviously, if you get a result that is undefined in maths, you can't expect it to follow the usual rules.

If you insist you can have full type consistency but then do not call
them the arithmetic operations.

Why not?


By demanding type invariance one goes back to the arithmetic model of C
where you have to carefully track the numeric types.

It's easier to track types than values when looking at source code. And Lua's model is even easier than C's (at least for division): You don't have to know the operand types, just looking at the operator is enough to figure out the result type.

Lua has a simple and consistent math based upon a single numeric type
-- 64-bit float.

That's about to change in Lua 5.3 (the "based upon a single numeric type" at least). The number of operations increased significantly, but I still think it's simple and consistent.


--Leo--


Philipp