lua-users home
lua-l archive

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


On 5/17/14, Philipp Janda <siffiejoe@gmx.net> wrote:
> 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.
>

Let's add some rigor. In order to be consistent, Arithmetic is build
on top of a algebraic field.  Here I only consider computer
implementations where numbers are represented by finite-size words,
therefore, the cardinality of the set of numbers is necessarily
finite. One  can _exactly_ represent finite fields like Galois's
GF(n).
But any field with infinite number of elements cannot be exactly
represented this way.
But one can come close. Rational numbers  form the field which is almost there
(one has to handle overflows in numerator or denominator outside of the field).
AFAIK,  Lua does not support rationals natively. On the other hand,
integers (Z) do NOT form a field at all.
The result of division of two integers can be integer (4/2) or is not
an integer. And this fact depends on the values of the operands. If
you do not have Rationals (Q)
as a separate type you either map the result back to int (truncation)
and brake the properties
of the math operations in the original field, or convert the result to
a float which constitutes a type change.
Essentially, you are either type consistent or math-consistent or need
rationals.

In any case, computational mathematics is a very tricky field and has
to be treated very gently.
Simple hand-waving does not cut it. If interested, please, refer to [1]

[1] Knuth "TAOCP", Vol.2 Seminumerical Algorithms, section 4.2.2.

--Leo--