Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rounding and transposition methods #42

Merged
merged 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Nulls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ for f in (:(!), :(+), :(-), :(Base.identity), :(Base.zero),
:(Base.sin), :(Base.sinh), :(Base.cos), :(Base.cosh), :(Base.tan), :(Base.tanh),
:(Base.exp), :(Base.exp2), :(Base.expm1), :(Base.log), :(Base.log10), :(Base.log1p),
:(Base.log2), :(Base.exponent), :(Base.sqrt), :(Base.gamma), :(Base.lgamma),
:(Base.ceil), :(Base.floor), :(Base.round), :(Base.trunc),
:(Base.iseven), :(Base.ispow2), :(Base.isfinite), :(Base.isinf), :(Base.isodd),
:(Base.isinteger), :(Base.isreal), :(Base.isimag), :(Base.isnan), :(Base.isempty),
:(Base.iszero))
:(Base.iszero), :(Base.transpose), :(Base.ctranspose))
@eval $(f)(d::Null) = null
end

Expand All @@ -81,6 +80,15 @@ for f in (:(+), :(-), :(*), :(/), :(^),
end
end

# Rounding and related functions
for f in (:(Base.ceil), :(Base.floor), :(Base.round), :(Base.trunc))
@eval begin
($f)(::Null, digits::Integer=0, base::Integer=0) = null
($f)(::Type{>:Null}, ::Null) = null
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is a bit special: I've considered round(Int, null) should throw a NullException, but round(Union{Int, Null}, null) should return null. That's safer than returning null whatever the requested type. When NullException supports error messages, we should use it to explain what to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

($f)(::Type, ::Null) = throw(NullException())
end
end

# to avoid ambiguity warnings
(^)(::Null, ::Integer) = null

Expand Down
14 changes: 12 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ using Base.Test, Nulls
elementary_functions = [abs, abs2, sign,
acos, acosh, asin, asinh, atan, atanh, sin, sinh,
conj, cos, cosh, tan, tanh,
ceil, floor, round, trunc,
exp, exp2, expm1, log, log10, log1p, log2,
exponent, sqrt, gamma, lgamma,
identity, zero,
iseven, isodd, ispow2,
isfinite, isinf, isnan, iszero,
isinteger, isreal, isimag,
isempty]
isempty, transpose, ctranspose]

rounding_functions = [ceil, floor, round, trunc]

# All unary operators return null when evaluating null
for f in [!, +, -]
Expand All @@ -46,6 +47,15 @@ using Base.Test, Nulls
@test isnull(f(null))
end

# All rounding functions return null when evaluating null as first argument
for f in rounding_functions
@test isnull(f(null))
@test isnull(f(null, 1))
@test isnull(f(null, 1, 1))
@test isnull(f(Union{Int, Null}, null))
@test_throws NullException f(Int, null)
end

@test zero(Union{Int, Null}) === 0
@test zero(Union{Float64, Null}) === 0.0

Expand Down