Method `%()
- Method `%
mixed
`%(object
arg1
,mixed
arg2
)
mixed
`%(mixed
arg1
,object
arg2
)
string
`%(string
arg1
,int
arg2
)
array
`%(array
arg1
,int
arg2
)
float
`%(float
arg1
,float
|int
arg2
)
float
`%(int
arg1
,float
arg2
)
int
`%(int
arg1
,int
arg2
)- Description
Modulo.
Every expression with the
%
operator becomes a call to this function, i.e.a%b
is the same aspredef::`%(a,b)
.- Returns
If
arg1
is an object that implements lfun::`%() then that function will be called witharg2
as the single argument.If
arg2
is an object that implements lfun::``%() then that function will be called witharg2
as the single argument.Otherwise the result will be as follows:
arg1
can have any of the following types:string
|array
If
arg2
is positive, the result will be the last`%(sizeof(
elements ofarg1
),arg2
)arg1
. Ifarg2
is negative, the result will be the first`%(sizeof(
elements ofarg1
), -arg2
)arg1
.int
|float
The result will be
. The result will be a float if eitherarg1
-arg2
*floor(arg1
/arg2
)arg1
orarg2
is a float, and an int otherwise.For numbers, this means that
a % b
always has the same sign asb
(typicallyb
is positive; array size, rsa modulo, etc, anda
varies a lot more thanb
).The function
f(x) = x % n
behaves in a sane way; asx
increases,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%b
for alla
andb
.
- See also