Macaulay2 / M2

The primary source code repository for Macaulay2, a system for computing in commutative algebra, algebraic geometry and related fields.
https://macaulay2.com
333 stars 228 forks source link

quotient and modulus for non-integers are wrong #3340

Open mahrud opened 2 weeks ago

mahrud commented 2 weeks ago

Unless I'm way off about what we want // and % to mean, I think these are all wrong: https://github.com/Macaulay2/M2/blob/ec9e9ac60ed4a8e791448942202f077a88a87e15/M2/Macaulay2/m2/reals.m2#L213-L226

Some examples: (afterprints are silenced)

i1 : 13.3 // 4.0

o1 = 3.325

i2 : 13.3 % 4.0

o2 = 0

i3 : 13.3 // 4

o3 = 3.325

i4 : 13.3 % 4

o4 = 0

i5 : 13.3 // (4/1)

o5 = 3.325

i6 : 13.3 % (4/1)

o6 = 0

i7 : (pi*ii) // 3.0

o7 = 1.0471975511966*ii

i8 : (pi*ii) % 3.0

o8 = 0

What's the point of having // be a synonym for /, and why even define % for non-integers if the answer is always zero anyway?!

d-torrance commented 2 weeks ago

I think they're correct. My understanding is that // and % give the quotient and remainder from division in a Euclidean domain, and fields are trivially Euclidean domains since $x = y(xy^{-1}) + 0$ for nonzero $y$.

mahrud commented 2 weeks ago

But that's just / division in fields, whereas // and % also work on domains.

I should have also included an example from python for contrast:

>>> 13.3 // 4.0
3.0
>>> 13.3 % 4.0
1.3000000000000007
>>> 13.3 // 4
3.0
>>> 13.3 % 4
1.3000000000000007
>>> 13.3 // (4/1)
3.0
>>> 13.3 % (4/1)
1.3000000000000007

I think this makes more sense, both mathematically and in terms of what is actually useful. Currently computing the non-zero value of 13.3 % 4 in Macaulay2 takes several steps:

i13 : 13.3 - 4 * floor(13.3 // 4)

o13 = 1.3
d-torrance commented 2 weeks ago

Ok, I think I'm convinced that it's wrong, too. :)

Here's an article with a few different possibilities for how // and % might be defined over the reals: Boute, The Euclidean Definition of the Functions div and mod.