Sunday, 17 January 2016

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)
1

julia> rem(-10, -3)
-1

julia> rem(-10, 3)
-1
mod(x, y)
Performs modulus after division.

julia> mod(10, -3)
-2

julia> mod(10, 3)
1

julia> mod(-10, 3)
2

julia> 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)
1

julia> mod(10, -3)
-2

julia> 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)
1

julia> rem(10, 3)
1

julia> rem(1.0, 0.1)
0.09999999999999995

julia> 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)
-1

julia> rem(-10, -3)
-1

julia> rem(10, -3)
1

julia> mod(-10, 3)
2

julia> mod(-10, -3)
-1

julia> mod(10, -3)
-2








Previous                                                 Next                                                 Home

No comments:

Post a Comment