is the Mod operator broken?

Or I’m missing something?

Dim a As Integer = 3
Dim b As Integer = 0
Dim c As Integer = a Mod b

Dim d As Double = 3
Dim e As Double = 0
Dim f As Double = d Mod e


Dim i As Integer = 3 Mod 0
Dim j As Double = 3.1 Mod 0
Dim k As Integer = 0.0 Mod 0.0

Dim l As Int64 = 3
Dim m As Int64 = 0
Dim n As Int64 = l Mod m

When the app breaks:
c = 3
f = 3

i = 2147483647
j = 2147483647
k = 2147483647

n =11540826243203400

Shouldn’t be the same on all cases?

<https://xojo.com/issue/56395>

https://math.stackexchange.com/questions/516251/why-is-n-mod-0-undefined

A MOD B = ‘how many times can you fit B in A before you reach a number less than B’

eg 11 MOD 4 = 3 because you get 2 x 4, with 3 left over, which is less than 4

MOD 0 could literally end up with an infinite value, as you never reach a point where the remainder is equal to or less than the value of 0

“divide by Zero” should be familiar.

Jeff: one of the best answer I ever read !

I think MOD returns the remainder, so 11 MOD 4 = 3 (and not 2).

Actually is one of the worst. It doesn’t even answer the question :-p

The Modulo operation is not an actuall division and can be subject to the conventions applied in number theory. Even the "answer in the lik states “it depends on how you define what it means to mod out by a number”

So 3 Mod 0 could be 3 in modular arithmetic, could be NaN in a simple division, could be an exeption of “divide by 0” or could be 0 as convention. It all deppends on the implementation of the language.

Any way, the Mod operator in xojo some times is modular arithmetic, some times undefined. It should be consice on all cases.

Yep.

OK . Bye.

Jeff, remember that Ivan was joking…

Yes

http://documentation.xojo.com/api/math/mod.html

You shouldn’t be expecting any coherent answer from x mod 0 as you shouldn’t be doing it :slight_smile: other languages may crash or error if you try this, xojo just kindly allows you to do it without a crash/error so you need to check for >0 before you do the mod.

Added this code to your test on macOS 10.12.6:

Dim o As Currency = 3 Dim p As Currency = 0 Dim q As Currency = o Mod p
q is always 0

Your code, first run:
c = 7864842877184770160
f = 1393747860.
i, j, k the same as you
n = 7864842877184770160
q = 0

Stopped the app and ran again:
c = 8665368139180605441
f = 1559201684.
i, j, k the same as you
n = 8665368139180605441
q = 0

Third time:
c = -6560992361120595946
f = 1463322516.
i, j, k the same as you
n = -6560992361120595946
q = 0

MOD can be semantically defined in a couple ways

IF you define X MOD Y strictly as “divide X by Y and return the remainder” then X mod 0 makes no sense as division by 0 makes no sense (ie literally as MOD = X - X\Y will give you errors)

IF you define it as MOD = X - KY for some value of k such that kY <= X then X mod 0 HAS a well defined value since k*Y will always be 0 and X MOD 0 returns X

math stackoverfow has a great discussion of MOD

Xojo COULD simply check X mod Y for the special case of Y= 0 and return X
Right now Xojo opts for the first and so you get undefined behaviour - and you many get DIFFERENT undefined behaviour per platform because the underlying C++ runtimes are not guaranteed to give consistent undefined behaviour