Skip to content

Commit

Permalink
Added ceiling and ceiling.math
Browse files Browse the repository at this point in the history
  • Loading branch information
heetbeet committed Mar 19, 2024
1 parent 95adf09 commit e5297eb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,38 @@ roundup(x, n=0) = ceil(x; digits=n)

rounddown(x, n=0) = floor(x; digits=n)

function ceiling(x, significance)
struct _Ceiling
math::Base.Callable # Use a more specific type as needed
end

function _ceiling(x, significance)
return ceil(x / significance) * significance
end

function _ceiling_math(x, significance=1.0, mode=0)
if significance == 0
return typeof(x)(0)
end

# Adjusting significance for negative numbers based on mode
adjusted_significance = significance
if mode != 0 && x < 0
adjusted_significance = -abs(significance)
else
adjusted_significance = abs(significance)
end

return ceil(x / adjusted_significance) * adjusted_significance
end

function (::_Ceiling)(x, significance)
return _ceiling(x, significance)
end


ceiling = _Ceiling(_ceiling_math)


function choose(index, args...)
return args[int(index)]
end
Expand Down
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,36 @@ using XLFunctions
@test_throws MethodError "2019-01-01" + 1
end

@testitem "ceiling.math" begin
@test 11 == ceiling.math(10.5, 1)
@test 10.5 == ceiling.math(10.1, 0.5)
@test -6 == ceiling.math(-5.5, 2, 1)
@test -4 == ceiling.math(-5.5, 2)
@test 12.3 == ceiling.math(12.25, 0.1)
@test 15 == ceiling.math(12, 5)
@test -15 == ceiling.math(-12, 5, 1)
@test -10 == ceiling.math(-12, -5)
@test 12.5 == ceiling.math(12.5, -2.5, 0)
@test 0 == ceiling.math(15, 0)
@test 7.8 ceiling.math(7.8, 0.2)
@test -7.8 ceiling.math(-7.8, 0.2, 1)
@test 0 == ceiling.math(0, 10)
@test -3 == ceiling.math(-2, 3, 1)
@test -2.5 == ceiling.math(-2.5, 0.5, 1)
@test 5 == ceiling.math(5, -1)
@test 5.5 == ceiling.math(5.5, -1.1)
@test -5.5 == ceiling.math(-5.5, -1.1, 1)
@test 123.456 == ceiling.math(123.456, 0.001)
@test -123.456 == ceiling.math(-123.456, 0.001, 1)
@test 99.99 ceiling.math(99.99, 0.01)
@test 99 == ceiling.math(99, 33)
@test -99 == ceiling.math(-99, 33, 1)
@test 100 == ceiling.math(100, 100)
@test -100 == ceiling.math(-100, 100, 0)
@test -100 == ceiling.math(-100, 100, 1)
@test 11 == ceiling.math(10.5)
end

@testitem "dateif" begin
@test datedif("2001-01-01", "2003-01-01", "Y") == 2
@test datedif("2001-06-01", "2002-08-15", "D") == 440
Expand Down

0 comments on commit e5297eb

Please sign in to comment.