Group Abstract Group Abstract

Message Boards Message Boards

0
|
554 Views
|
16 Replies
|
13 Total Likes
View groups...
Share
Share this post:

Traditional division of integers with remainder in Wolfram

Posted 22 days ago

As is well known, division of integers with remainder can be defined in different ways. The definition in Wolfram and the definition in theoretical mathematics are different. How to express traditional division with remainder in Wolfram? Here is a variant solution:

quotient[m_, n_] := (m - Mod[m, Abs[n]])/n
remainder[m_, n_] := Mod[m, Abs[n]]

Is there anything more elegant?

POSTED BY: Sasha Mandra
16 Replies
Posted 21 days ago

I think you could define your preference like this:

altQuotientRemainder[m_, n_?Negative] := -QuotientRemainder[-m, n];
altQuotientRemainder[m_, n_] := QuotientRemainder[m, n]
POSTED BY: Eric Rimbey
Posted 21 days ago

The residuals in your definition may be negative. It shouldn't be.

POSTED BY: Sasha Mandra
Posted 21 days ago

Did you actually test it? If you provide an example where the implementation I suggested differs from the implementation you provided, then I can work on a better implementation.

POSTED BY: Eric Rimbey
Posted 21 days ago
altQuotientRemainder[m, n] /. {m -> 17, n -> -5}
    {-4, -3}

It should be {-3, 2}

PS This is weird

altQuotientRemainder[17, -5]
{-3, 2}
POSTED BY: Sasha Mandra
Posted 21 days ago

Well, yeah, you ran the function on symbols which then resolved to the built in QuotientRemainder. Why would you use ReplaceAll like that? But if that's the way you want to use it, then you can add constraints on the arguments.

POSTED BY: Eric Rimbey
Posted 21 days ago

Daniel Lichtblau already mentioned that! Thanks! Here's what you can do Sasha:

altQuotientRemainder[m_Integer, n_Integer?Negative] := -QuotientRemainder[-m, n];
altQuotientRemainder[m_Integer, n_Integer] := QuotientRemainder[m, n]

altQuotientRemainder[m, n] /. {m -> 17, n -> -5}
(* {-3, 2} *)

Also, I should probably clarify that I'm not saying this implementation is "right" in any sense. Either of your other implementations is just fine as far as I'm concerned. You asked for alternatives, and since QuotientRemainder is already built in and gives you almost everything you want, using it seemed like a reasonable alternative.

POSTED BY: Eric Rimbey

Restrict inputs to integers. That avoids inadvertent misuse as a symbolic function.

POSTED BY: Daniel Lichtblau
Posted 22 days ago

Have you tried the function QuotientRemainder ?

In[6]:= QuotientRemainder[14, 5]
Out[6]= {2, 4}
POSTED BY: Hans Milton

POSTED BY: Mariusz Iwaniuk
Posted 22 days ago

Thank you! I knew Wolfram had thought of everything.

POSTED BY: Sasha Mandra
Posted 21 days ago

A closer look revealed that the functions Quotient[m,n,0], Mod[m,n,0] and QuotientRemainder do not compute the traditional quotient and remainder.

An alternative definition of traditional quotient and remainder via the Floor function could be as follows:

**remainder[m_, n_] := m - Abs[n] Floor[m/Abs[n]];
quotient[m_, n_] := Sign[n] Floor[m/Abs[n]]**
POSTED BY: Sasha Mandra
Posted 21 days ago

Could you clarify what you find wrong, or non-traditional, with the QuotientRemainder results below

POSTED BY: Hans Milton
Posted 21 days ago

In the traditional definition of dividing integers with a remainder, the remainder cannot be negative. This is neither right nor wrong. It is a different definition.

POSTED BY: Sasha Mandra
Posted 21 days ago

Okay, I understand that this is just a matter of conventions. Just out of curiosity: Do you have any reference to support that positive remainders is the common and established convention?

POSTED BY: Hans Milton
Posted 21 days ago

Any number theory textbook (not for programmers) defines the traditional division of integers with remainder. For example, Burton, Elementary number theory (2011). This is necessary to determine modulo equivalence. The number of remainders when dividing by n, must be |n|. So it's strange to me that Wolfram didn't provide this function.

POSTED BY: Sasha Mandra

One can use Mod to get a remainder between 0 and n-1. Or a different set of contiguous residue classes if so desired.

As for definitions of quotient and remainder, there are conventions. It isn’t one-size-fits-all.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard