Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with "MOD"
#2
Both are right.   It's a lot like saying, "What's the square root of 4?"  Well heck, that's 4 AND -4!  Both are correct.  Wink

Quote:Floored division:
In this method, Quotient is determined by the floor division of Dividend and Divisor. It rounds off the quotient to the nearest smallest integer. So remainder can be computed from the following expression:

r = a – b* floor(a/b), where r, a, b are Remainder, Dividend and Divisor respectively.


Example:

=> -7 % 5 = -7 – 5 *( floor(-7/5) )
= -7 – 5*( floor(-1.4) )
= -7 – 5*( -2)
= -7+10
= 3


In Python, floor division is used to determine the remainder.

Truncated division:
In this method, Quotient is determined by the truncate division of Dividend and Divisor. It rounds off the quotient towards zero. So remainder can be computed from the following expression:

r = a – b* trunc(a/b), where r, a, b are Remainder, Dividend and Divisor respectively.


Example:

=> -7 % 5 = -7 – 5 *( trunc(-7/5) )
= -7 – 5*( trunc(-1.4) )
= -7 – 5*( -1)
= -7+5
= -2


In C/C++, truncate division is used to determine the remainder

Thus, In programming languages which uses truncate division to find the remainder, we always find remainder as (a%b + b)%b (add quotient to remainder and again take remainder) to avoid negative remainder and get the correct result.

So it you always want a positive value, do this:

result = (num MOD mod_value + mod_value) MOD mod_value

Or, specifically in your case:

result = (-6 MOD 7 + 7) MOD 7

Code: (Select All)
Print -6 Mod 7
Print (-6 Mod 7 + 7) Mod 7
Reply


Messages In This Thread
Problem with "MOD" - by Kernelpanic - 01-05-2025, 09:30 PM
RE: Problem with "MOD" - by SMcNeill - 01-05-2025, 09:57 PM
RE: Problem with "MOD" - by Kernelpanic - 01-05-2025, 10:39 PM
RE: Problem with "MOD" - by Pete - 01-06-2025, 02:01 AM
RE: Problem with "MOD" - by SMcNeill - 01-06-2025, 02:22 AM
RE: Problem with "MOD" - by Pete - 01-06-2025, 02:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Big problem for me. MystikShadows 11 1,935 01-16-2025, 05:11 AM
Last Post: JRace
  Problem with _Resize dano 7 1,234 10-22-2024, 07:22 PM
Last Post: dano
  Operator MOD Chris 71 13,355 06-12-2024, 04:55 AM
Last Post: grymmjack
  Menu Run (No EXE) Problem GareBear 5 1,144 10-06-2023, 05:41 AM
Last Post: GareBear
  Possible desktop reporting problem doppler 3 960 08-07-2023, 02:48 PM
Last Post: SpriggsySpriggs

Forum Jump:


Users browsing this thread: 1 Guest(s)