Skip to content

Commit

Permalink
Clean up mod1
Browse files Browse the repository at this point in the history
Implement fld1 and fldmod1.
Add test cases.
Deprecate rem1.
  • Loading branch information
eschnett committed Nov 25, 2015
1 parent 944dcbd commit a0b956c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,15 @@ end
@deprecate chol(A::Number, ::Type{Val{:L}}) ctranspose(chol(A))
@deprecate chol(A::AbstractMatrix, ::Type{Val{:L}}) ctranspose(chol(A))

# Number updates

# rem1 is inconsistent for x==0: The result should both have the same
# sign as x, and should be non-zero.
function rem1{T<:Real}(x::T, y::T)
depwarn("`rem1(x,y)` is discontinued, as it cannot be defined consistently for `x==0`. Rewrite the expression using `mod1` instead.", :rem1)
rem(x-1,y)+1
end

# Filesystem module updates

@deprecate_binding FS Filesystem
Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ export
factor,
factorial,
fld,
fld1,
fldmod,
fldmod1,
flipsign,
float,
tryparse,
Expand Down
7 changes: 4 additions & 3 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ const % = rem
.%(x::Real, y::Real) = x%y
const ÷ = div

# mod returns in [0,y) whereas mod1 returns in (0,y]
# mod returns in [0,y) or (y,0] (for negative y),
# whereas mod1 returns in (0,y] or [y,0)
mod1{T<:Real}(x::T, y::T) = (m=mod(x,y); ifelse(m==0, y, m))
rem1{T<:Real}(x::T, y::T) = rem(x-1,y)+1
fld1{T<:Real}(x::T, y::T) = fld(x-1,y)+1
fld1{T<:Real}(x::T, y::T) = fld(x - mod1(x,y), y)
fldmod1{T<:Real}(x::T, y::T) = (m=mod1(x,y); (fld(x-m, y), m))

# transpose
transpose(x) = x
Expand Down
6 changes: 6 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,12 @@ end
# test second branch, after all small primes in list have been searched
@test factor(10009 * Int128(1000000000000037)) == Dict(10009=>1,1000000000000037=>1)

@test all(x -> (m=mod1(x,3); 0<m<=3), -5:+5)
@test all(x -> x == fld1(x,3)*3 + mod1(x,3), -5:+5)
@test all(x -> fldmod1(x,3) == (fld1(x,3), mod1(x,3)))
@test all(x -> (m=mod1(x,-3); -3<=m<0), -5:+5)
@test all(x -> x == fld1(x,-3)*-3 + mod1(x,-3), -5:+5)
@test all(x -> fldmod1(x,-3) == (fld1(x,-3), mod1(x,-3)))
#Issue #5570
@test map(x -> Int(mod1(UInt(x),UInt(5))), 0:15) == [5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

Expand Down

0 comments on commit a0b956c

Please sign in to comment.