Recent

Author Topic: Do we need a "number theory" compatible version of modulo?  (Read 2086 times)

uart

  • New Member
  • *
  • Posts: 40
Re: Do we need a "number theory" compatible version of modulo?
« Reply #30 on: February 22, 2020, 03:38:13 am »
Doesn't {$modeswitch ISOMOD} do what you want?

Thanks FPK. I wasn't aware of that compiler directive, but you are correct it does force it to do a "number theory compatible" version of mod, albeit for only positive values of the second argument (and a runtime error for negative values of the second argument).

Actually that answers another thing that I was wondering. While researching this earlier I noticed a few people were saying that ISO Pascal's DIV and MOD were broken, with DIV not even being self consistent with MOD.  As in: N <> (N div D) + (N mod D)

I knew that this was not the case with any of the versions of Pascal I've used (mainly Turbo Pascal, Delphi and Free Pascal), with DIV and MOD always working correctly together.  But it appears that in the original ISO Pascal specs this was not the case. They had DIV the same as it currently is, but then they had a number theory compatible MOD that didn't properly mesh with DIV.

So it seems that they messed this up initially, and probably should have had a separate REM (for remainder) operator to do what the current version of MOD does, and kept the original MOD operator as the number theory compatible definition.

Still at this stage, I think I'd rather inline my own version of number theory modulo than to risk breaking other code by redefining the meaning of the existing mod with that directive. Still, thanks for pointing it out to me, it's historically very interesting. :)
« Last Edit: February 22, 2020, 04:33:16 am by uart »

munair

  • Hero Member
  • *****
  • Posts: 696
  • keep it simple
    • SharpBASIC
Re: Do we need a "number theory" compatible version of modulo?
« Reply #31 on: February 22, 2020, 09:37:37 am »
What is addressed here I think is the difference between a modulo operation and a remainder operation:
Code: Pascal  [Select][+][-]
  1. function remx(const a, b: integer): integer;
  2. // remainder
  3. begin
  4.   result := a - b * trunc(a / b);
  5. end;
  6.  
  7. function modx(const a, b: integer): integer;
  8. // modulo
  9. begin
  10.   result := a - b * floor(a / b);
  11. end;

If both a and b are positive then modulo and remainder give the same result. If a and b have different signs then the result is different. Here modx(-8, 12) returns 4, whereas remx(-8, 12) returns -8.

Thaddy

  • Hero Member
  • *****
  • Posts: 10188
Re: Do we need a "number theory" compatible version of modulo?
« Reply #32 on: February 22, 2020, 10:06:15 am »
Those involve float operations and will slow down even further. modulo operations are quite expensive. (Also note I introduced a fmod  like mod operator already in 3.2.0 I have to look if that satisfies the rules suggested too. It is in math )
I am more like donkey than shrek

munair

  • Hero Member
  • *****
  • Posts: 696
  • keep it simple
    • SharpBASIC
Re: Do we need a "number theory" compatible version of modulo?
« Reply #33 on: February 22, 2020, 11:15:33 am »
Those involve float operations and will slow down even further. modulo operations are quite expensive. (Also note I introduced a fmod  like mod operator already in 3.2.0 I have to look if that satisfies the rules suggested too. It is in math )
The purpose of my example is to demonstrate the difference from a mathematical point of view in the clearest manner (trunc vs floor). It is not meant to provide the fastest solution.

MarkMLl

  • Hero Member
  • *****
  • Posts: 925
Re: Do we need a "number theory" compatible version of modulo?
« Reply #34 on: April 10, 2020, 10:00:50 pm »
Doesn't {$modeswitch ISOMOD} do what you want?

What version of FPC implements this?

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1694
  • Compiler Developer
Re: Do we need a "number theory" compatible version of modulo?
« Reply #35 on: April 11, 2020, 02:23:47 pm »
Doesn't {$modeswitch ISOMOD} do what you want?

What version of FPC implements this?

3.2 and newer.

MarkMLl

  • Hero Member
  • *****
  • Posts: 925
Re: Do we need a "number theory" compatible version of modulo?
« Reply #36 on: April 11, 2020, 04:13:12 pm »
Doesn't {$modeswitch ISOMOD} do what you want?

What version of FPC implements this?

3.2 and newer.

Thanks for that, I was using 3.0.4 and ended up overloading the >< operator so that it behaved the same way as was expected by a Python example I was transcribing... which is probably the first time I've been polite about Python.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

 

TinyPortal © 2005-2018