rem
rem(x, y) %(x, y)
Remainder from Euclidean division, returning a value of the same sign as x
, and smaller in magnitude than y
. This value is always exact.
Examples
In the Julia programming language, the function rem(x, y)
or %
is used to calculate the remainder from Euclidean division. It returns a value of the same sign as x
and smaller in magnitude than y
. The resulting value is always exact.
julia> rem(10, 3)
1
julia> 10 % 3
1
-
Calculate the remainder of two integers:
julia> rem(15, 7) 1 julia> 15 % 7 1
In this example,
rem
calculates the remainder of dividing 15 by 7, which is 1. -
Compute the remainder with negative numbers:
julia> rem(-10, 3) -1 julia> -10 % 3 -1
The
rem
function considers the sign of bothx
andy
. In this case, -10 divided by 3 has a remainder of -1. -
Get the remainder of floating-point numbers:
julia> rem(5.5, 2.2) 0.9999999999999991 julia> 5.5 % 2.2 0.9999999999999991
The
rem
function can also be used with floating-point numbers. The remainder is returned as a floating-point value.
Common mistake example:
julia> rem(10, 0)
ERROR: DivideError: integer division error
In this example, rem
encounters an error because the second argument is zero. You should avoid dividing by zero to prevent such errors.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.