• Subject: Re: Why do some math functions return -0
• From: bb <bblochl@...>
• Date: Mon, 20 Sep 2010 16:12:59 +0200

```Roberto Ierusalimschy schrieb:
```
```Have a look at the Lua reference manual:

Modulo is defined as

a % b == a - math.floor(a/b)*b

That is, it is the remainder of a division that rounds the
quotient towards minus infinity.
```
```"Defined as" is not the same as "implemented as". [...]
```
```
Then have a look at the code (http://www.lua.org/source/5.1/luaconf.h.html):

#define luai_nummod(a,b)        ((a) - floor((a)/(b))*(b))

As the problem seems to happen only in some platforms, there is a good
chance that something (floor itself?) is messing with the rounding mode.

-- Roberto

```
```

```
I use a lot of different scripting languages (JavaScript, Python, Ch - a C interpreter - and Lua) one more, the other less and compiled languages as well. Because of that lengthy discussion "Why do some math functions return -0" I actually checked the result of % of different languages:
```
JavaScript-C 1.8.0 pre-release 1 2009-02-16 : -7%-9 = -7, 9%-7 = 2, 7%-9 = 7

Ch Standard edition, version 6.3.0.14091:  -7%-9 = -7, 9%-7 = 2, 7%-9 = 7

```
java version "1.6.0_0" OpenJDK Runtime Environment (build 1.6.0_0-b11): -7%-9 = -7, 9%-7 = 2, 7%-9 = 7
```
```
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) [GCC 4.3.2] on linux2 : -7%-9 = -2, 9%-7 = -5, 7%-9 = -2
```
Lua 5.1.3 : -7%-9 = -2, 9%-7 = -5, 7%-9 = -2

Obviously Lua and python use the expression
a - (n * math.floor(a/n))
for the calculation of the %-operator.

```
The other languages obviously use another formula with a different result, that equals to the other "C-like" languages.
```
```
Concerning to some more studies in theat field I would no longer speak of a "bug" but different interpretations in different languages.
```
Lua and Python define the sign of the result from the divisor.
JavaScript and Ch define the sign of the result from the dividend.

```
I cannot say what is correct. I argue that there are good reasons for the one and for the other. As I did lots of C programming (and actually Java) I come into trouble with the Lua-%. So I prefer a c-view. But certainly a Python-lover will not agree.
```
```
There should be a strict math-definition of that modulo-operator, lets have a look at some CAS:
```
GNU Octave, version 3.0.1: -7%-9 = -7, 9%-7 = 9, 7%-9 = 7

```
Maxima 5.13.0 http://maxima.sourceforge.net Using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (aka GCL): mod(-7,-9) = -7, mod(9,-7) = -5, mod(7,-9) = -2
```
```
AXIOM Computer Algebra System Version: Axiom 3.9 (September 2005) Timestamp: Monday December 3, 2007 at 18:21:59 : rem(-7,-9) = -7, rem(9,-7) = 2, rem(7,-9) = 7
```
Please tell me when you have found the truth of the %-operator!

```
Personally I prefer the C-like %-operator, because I am familiar with. I think it would be a good idea to point out the difference of Lua to the other C.like languages! May be a Lua-code for a function for a c-like handling of the %-operator might be useful? So the user might have "religious freedom".
```
Regards BB

```