lua-users home
lua-l archive

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


On 21 June 2016 at 02:30, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> Last time I checked, in all machines that implement the IEEE standard
> (that seems to be all machines), a float division by zero was a valid
> operation, which may result in -inf, inf, or NaN. We try to follow
> the IEEE standard as much as possible (in other words, we try to not
> interfere with it)

The issue is that it relies on undefined behaviour in C. This means
the compiler can do *anything*.

> If there was a test for (b == 0), what should it do
> in a positive case?

Desired behaviour:
  1/0 returns inf
  -1/0 returns -inf
  1/-0 returns -inf
  -1/-0 returns inf
  0/0 returns nan

To get the above desited behaviour, I think we end up with something like:

#define luai_numdiv(L,a,b)      ((void)L,
(b)?(a)/(b):(a)?(signbit((a))^signbit((b))?-HUGE_VAL:HUGE_VAL):nan(NULL))

However, this doesn't seem to get optimized away by compilers as I
half-expected.
Perhaps someone else can weigh in here?

Without the various standard libary functions introduced in C99 (such
as signbit(), copysign(), nan(), etc) it's trickier too.