lua-users home
lua-l archive

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




On Sat, May 2, 2015 at 10:12 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2015-05-03 3:14 GMT+02:00 Milind Gupta <milind.gupta@gmail.com>:
>>
>> Convert an overflow result to floating point seems tempting, but
>> we do not see it as a useful/practical option. First, it is quite
>> expensive to check multiplication overflows in ANSI C. Second, if you
>> are computing with integers, you probably need a correct result, not a
>> rough aproximation.
>>
>
> I was wondering so does it mean we have to be careful when we use integer
> number and floating number into our programs? If that is the case wouldn't
> it be easier if integer type was defined separately allowing its usage
> explicitly when needed and thought out?

You always have to be careful when using computer arithmetic
instead of exact arithmetic. It is true that the results of integer
overflow tend to to be very dramatic, for example causing a rocket
destined for space to reverse direction and crash [1], but the
results of naive reliance on floating point can be no less disastrous,
for example causing an anti-missile system to fail [2].

In the first case, US$370 million, in the second 28 lives, were lost
because programmers did not understand computer arithmetic
properly.

In both cases, integer and float, usage should be "thought out".
Anything that contributes to a false sense of security just makes
it harder to find bugs already at the design stage. I guess that
one in every 100 programmers does the kind of work for which
the distinction between integer and float is so important that the
"type" function should be overridden [3]. For the sake of the
other 99, this is not standard in Lua, but that one programmer
had better do it.

[1]   http://en.wikipedia.org/wiki/Cluster_(spacecraft)
[2]   http://en.wikipedia.org/wiki/MIM-104_Patriot#Failure_at_Dhahran
[3]   I.e. every program should start with the following lines:

local default_type = type
type = function(x)
   local T=default_type(x)
   if T=="number" then T=math.type(x) end
   return T
end

Thank you for explaining. It just seems that people or new comers who are developing apps that are not as critical as you refer would expect the behavior to revert to float when there is no explicit type system defined for the number type. Adding a floating point makes Lua understand and distinguish but hard for new comers, especially people who are not professional programmers and would not read the suttle points of the internal integer and float handling. 
         So to make these suttle behavior come out it may have been good to have the integer type.