Difference between revisions of "Mod"

(→‎IsInteger: Change incorrect "==" operator to "=")
 
(8 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
   
 
   
==Mod(X,Y)==
+
==Mod(x, y'', pos'')==
  
The remainder (modulus) of X/Y.
+
The remainder (modulus) of «x» / «y».
 +
 
 +
The sign of the result of <code>Mod(x,y)</code> is the same as the sign of «x» -- i.e., if «x» is negative, then so is the result. The sign of «y» does not impact the sign of the result. This variation is the same convention used by the corresponding operator in C/C++ (ISO 1999), C#, Java, PHP, Visual Basic, AMPL and many other languages. Some languages, including MATLAB, Lisp, Fortran, and Ada, have two different modulo operators, with one corresponding to this convention. The modulo operator in a few other languages, including Mathematica, R and Excel, do not follow this convention (these use the sign of the denominator).
 +
 
 +
You can specify «pos» as true to force the result to be non-negative, in the range <code>0 &le; Mod(x,y) < Abs(y)</code>. As «x» increases through all negative and positive numbers, the result cycles through this range.
 +
 
 +
<code>Mod(x,1)</code> extracts the fractional portion of a number.
 +
 
 +
==Examples ==
 +
 
 +
:<code>Mod(20,7) &rarr; 6</code>
 +
:<code>Mod(-20,7) &rarr; -6</code>
 +
:<code>Mod(-20,-7) &rarr; -6</code>
 +
:<code>Mod(20,-7) &rarr; 6</code>
 +
:<code>Mod(-20,7,pos: true) &rarr; 1</code>
 +
:<code>Mod(Pi,1) &rarr; 0.141592653589793</code>
 +
 
 +
== Special uses ==
 +
 
 +
=== IsOdd and IsEven ===
 +
 
 +
To test whether an integer <code>x</code> is odd use <code>Mod(x, 2) = 1</code>, and similarly, to test whether an integer is even use <code>Mod(x, 2) = 0</code>.
 +
 
 +
=== IsInteger ===
 +
 
 +
To test whether a number <code>x</code> is an integer, use <code>Mod(x,1) = 0</code>
 +
 
 +
=== The fractional part of a number  ===
 +
:<code>[[Mod]]( Pi, 1)</code> &rarr; 0.141592653589793
 +
:<code>[[Mod]]( -Pi,1)</code> &rarr; -0.141592653589793
 +
 
 +
 
 +
=== Time part of a date-time number ===
 +
 
 +
(''new in [[Analytica 6.0]]'')  When you have a date-time number, <code>[[Mod]](d,1)</code> returns the time part and returns it as a date-time number instead of a float.
  
 
==See Also==
 
==See Also==
 +
* [[Abs]]
 
* [[GCD]]
 
* [[GCD]]
 +
* [[IsNumber]]
 
* [[Array Abstraction]]
 
* [[Array Abstraction]]
 +
* [[Math functions]]

Latest revision as of 03:03, 29 April 2025


Mod(x, y, pos)

The remainder (modulus) of «x» / «y».

The sign of the result of Mod(x,y) is the same as the sign of «x» -- i.e., if «x» is negative, then so is the result. The sign of «y» does not impact the sign of the result. This variation is the same convention used by the corresponding operator in C/C++ (ISO 1999), C#, Java, PHP, Visual Basic, AMPL and many other languages. Some languages, including MATLAB, Lisp, Fortran, and Ada, have two different modulo operators, with one corresponding to this convention. The modulo operator in a few other languages, including Mathematica, R and Excel, do not follow this convention (these use the sign of the denominator).

You can specify «pos» as true to force the result to be non-negative, in the range 0 ≤ Mod(x,y) < Abs(y). As «x» increases through all negative and positive numbers, the result cycles through this range.

Mod(x,1) extracts the fractional portion of a number.

Examples

Mod(20,7) → 6
Mod(-20,7) → -6
Mod(-20,-7) → -6
Mod(20,-7) → 6
Mod(-20,7,pos: true) → 1
Mod(Pi,1) → 0.141592653589793

Special uses

IsOdd and IsEven

To test whether an integer x is odd use Mod(x, 2) = 1, and similarly, to test whether an integer is even use Mod(x, 2) = 0.

IsInteger

To test whether a number x is an integer, use Mod(x,1) = 0

The fractional part of a number

Mod( Pi, 1) → 0.141592653589793
Mod( -Pi,1) → -0.141592653589793


Time part of a date-time number

(new in Analytica 6.0) When you have a date-time number, Mod(d,1) returns the time part and returns it as a date-time number instead of a float.

See Also

Comments


You are not allowed to post comments.