Difference between revisions of "Mod"

From Free Pascal wiki
Jump to navigationJump to search
m
m (Added back link page)
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{mod}}
 
{{mod}}
<br>
+
 
Mod ('''mod'''ulus) divides two numbers and returns only the remainder that is a whole number.
+
 
 +
Back to [[Reserved words]].
 +
 
 +
 
 +
Mod ('''mod'''ulus) divides two numbers and returns only the remainder.
 
For instance, the expression "a:= 13 mod 4;" would evaluate to 1 (a=1), while "b := 12 mod 4;" would evaluate to 0 (b=0).
 
For instance, the expression "a:= 13 mod 4;" would evaluate to 1 (a=1), while "b := 12 mod 4;" would evaluate to 0 (b=0).
  
From the [http://www.freepascal.org/docs-html/ref/refsu29.html language reference]:
+
From the [http://freepascal.org/docs-html/current/ref/refsu45.html#x148-17000012.8.1 language reference]:
  
: The sign of the result of a Mod operator is the same as the sign of the left side operand of the Mod operator. In fact, the Mod operator is equivalent to the following operation :
+
:"The sign of the result of a Mod operator is the same as the sign of the left side operand of the Mod operator. In fact, the Mod operator is equivalent to the following operation :"
 
    
 
    
   I mod J = I - (I div J) * J  
+
   I mod J = I - (I div J) * J
 +
 
 +
For example "c:= -13 mod 4;" results in c = -1 and "c:= 10 mod -3;" results in c = 1.
 +
 
 +
This is also what most other languages like C++ and Java do (see note)
 +
 
 +
From version 3.1.1 FreePascal also supports the mod operator for floating point values when you include the math unit.<br>
 +
The precision used is the highest precision available for the platform.
 +
for instance, the expression "a:= 12.9 mod 2.2;" would evaluate to 1.9. This is equivalent to I mod J = I - int(I / J) * J.
 +
 
 +
The result is the same as the fmod function for the highest precision available for the platform.
 +
 
 +
In older versions of FPC that support operator overloading you can add this feature yourself. Here's an example for double precision modulo.
  
For example "c := -13 mod 4;" results in c = -1.
+
<syntaxhighlight lang=pascal>
 +
operator mod(const a, b: double) c: double; inline;
 +
begin
 +
  c:= a - b * Int(a / b);
 +
end;
 +
</syntaxhighlight>
  
  
But — this is what Delphi does. The ISO 7185 Pascal standard states:
+
== Note regarding Delphi compatibility ==
  
: Evaluation of a term of the form x mod y is an error if y is less than or equal to zero; otherwise there is an integer k such that x mod y satisfies the following relation :
+
Delphi conforms to ISO 7185 Pascal instead of the de facto standard. This ISO standard states:
 +
: Evaluation of a term of the form x mod y is an error if y is less than or equal to zero; otherwise there is an integer k such that x mod y satisfies the following relation:
  
 
   0 <= x mod y = x - k * y < y.
 
   0 <= x mod y = x - k * y < y.
  
[[Category:FPC]]
+
That means that in Delphi "c:= 10 mod -3;" results in an error and an exception will be thrown at run-time.
[[Category:Pascal]]
 

Latest revision as of 01:06, 21 February 2020

Deutsch (de) English (en) français (fr)


Back to Reserved words.


Mod (modulus) divides two numbers and returns only the remainder. For instance, the expression "a:= 13 mod 4;" would evaluate to 1 (a=1), while "b := 12 mod 4;" would evaluate to 0 (b=0).

From the language reference:

"The sign of the result of a Mod operator is the same as the sign of the left side operand of the Mod operator. In fact, the Mod operator is equivalent to the following operation :"
 I mod J = I - (I div J) * J

For example "c:= -13 mod 4;" results in c = -1 and "c:= 10 mod -3;" results in c = 1.

This is also what most other languages like C++ and Java do (see note)

From version 3.1.1 FreePascal also supports the mod operator for floating point values when you include the math unit.
The precision used is the highest precision available for the platform. for instance, the expression "a:= 12.9 mod 2.2;" would evaluate to 1.9. This is equivalent to I mod J = I - int(I / J) * J.

The result is the same as the fmod function for the highest precision available for the platform.

In older versions of FPC that support operator overloading you can add this feature yourself. Here's an example for double precision modulo.

operator mod(const a, b: double) c: double; inline;
begin
  c:= a - b * Int(a / b);
end;


Note regarding Delphi compatibility

Delphi conforms to ISO 7185 Pascal instead of the de facto standard. This ISO standard states:

Evaluation of a term of the form x mod y is an error if y is less than or equal to zero; otherwise there is an integer k such that x mod y satisfies the following relation:
 0 <= x mod y = x - k * y < y.

That means that in Delphi "c:= 10 mod -3;" results in an error and an exception will be thrown at run-time.