Difference between revisions of "Mod"
m (→Mod(x, y, pos)) |
m (→Mod(x, y, pos)) |
||
Line 8: | Line 8: | ||
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). | 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 ≤ [[Mod]](x,y) < [[Abs]](y)</code>. As | + | You can specify «pos» as true to force the result to be non-negative, in the range <code>0 ≤ [[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. | <code>Mod(x,1)</code> extracts the fractional portion of a number. |
Revision as of 00:52, 24 March 2016
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
Enable comment auto-refresher