lua-users home
lua-l archive

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

2015-05-03 3:14 GMT+02:00 Milind Gupta <>:
>> 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

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.

[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