** Integer division; return 'm // n', that is, floor(m/n). ** C division truncates its result (rounds towards zero). ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, ** otherwise 'floor(q) == trunc(q) - 1'. */
| 550 | ** otherwise 'floor(q) == trunc(q) - 1'. |
| 551 | */ |
| 552 | lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { |
| 553 | if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ |
| 554 | if (n == 0) |
| 555 | luaG_runerror(L, "attempt to divide by zero"); |
| 556 | return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ |
| 557 | } |
| 558 | else { |
| 559 | lua_Integer q = m / n; /* perform C division */ |
| 560 | if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ |
| 561 | q -= 1; /* correct result for different rounding */ |
| 562 | return q; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /* |
no test coverage detected