Method `%()
- Method
`%
mixed`%(objectarg1,mixedarg2)
mixed`%(mixedarg1,objectarg2)
string`%(stringarg1,intarg2)
array`%(arrayarg1,intarg2)
float`%(floatarg1,float|intarg2)
float`%(intarg1,floatarg2)
int`%(intarg1,intarg2)- Description
Modulo.
Every expression with the
%operator becomes a call to this function, i.e.a%bis the same aspredef::`%(a,b).- Returns
If
arg1is an object that implements lfun::`%() then that function will be called witharg2as the single argument.If
arg2is an object that implements lfun::``%() then that function will be called witharg2as the single argument.Otherwise the result will be as follows:
arg1can have any of the following types:string|arrayIf
arg2is positive, the result will be the last`%(sizeof(elements ofarg1),arg2)arg1. Ifarg2is negative, the result will be the first`%(sizeof(elements ofarg1), -arg2)arg1.int|floatThe result will be
. The result will be a float if eitherarg1-arg2*floor(arg1/arg2)arg1orarg2is a float, and an int otherwise.For numbers, this means that
a % balways has the same sign asb(typicallybis positive; array size, rsa modulo, etc, andavaries a lot more thanb).The function
f(x) = x % nbehaves in a sane way; asxincreases,f(x)cycles through the values0,1, ..., n-1, 0, .... Nothing strange happens when you cross zero.The
%operator implements the binary "mod" operation, as defined by Donald Knuth (see the Art of Computer Programming, 1.2.4). It should be noted that Pike treats %-by-0 as an error rather than returning 0, though./and%are compatible, so thata == b*floor(a/b) + a%bfor allaandb.
- See also