# Julia: mod Vs rem functions

 Method Description rem(x, y) Return remainder (x%y). Sign of the result is same as sign of x.julia> rem(10, -3)1julia> rem(-10, -3)-1julia> rem(-10, 3)-1 mod(x, y) Performs modulus after division.julia> mod(10, -3)-2julia> mod(10, 3)1julia> mod(-10, 3)2julia> mod(-10, -3)-1

The remainder (rem) and modulus (mod) are defined as follows:

rem(X,Y) = X –(X/Y)*Y     (in which A/B in an integer)
mod(X,Y) = X – Y * N      (in which N is an integer)

`julia> rem(10, -3)1julia> mod(10, -3)-2julia> mod(10, -7)-4`

mod(10, -3) = 10 - 3 * 4 = 10 -12 = -2
mod(10, -7) = 10 – 7*2 = 10 -14 = -4

For positive integers both mod and rem behave like same.
`julia> mod(10, 3)1julia> rem(10, 3)1julia> rem(1.0, 0.1)0.09999999999999995julia> mod(1.0, 0.1)0.09999999999999995`

The result of the rem operator has the sign of its first operand while the result of the mod operators has the sign of the second operand.

`julia> rem(-10, 3)-1julia> rem(-10, -3)-1julia> rem(10, -3)1julia> mod(-10, 3)2julia> mod(-10, -3)-1julia> mod(10, -3)-2`

Previous                                                 Next                                                 Home

This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

# Share the post

Julia: mod Vs rem functions

×

# Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

×