Skip to content

Min/Max: implementation and dectests #83

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

Merged
merged 1 commit into from
Nov 3, 2024
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
6 changes: 6 additions & 0 deletions scripts/dectest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function print_operation(io, operation, operands)
print_compare(io, operands...)
elseif operation == "divide"
print_divide(io, operands...)
elseif operation == "max"
print_max(io, operands...)
elseif operation == "min"
print_min(io, operands...)
elseif operation == "minus"
print_minus(io, operands...)
elseif operation == "multiply"
Expand All @@ -85,6 +89,8 @@ print_add(io, x, y) = print(io, decimal(x), " + ", decimal(y))
print_apply(io, x) = print(io, decimal(x))
print_compare(io, x, y) = print(io, "cmp(", decimal(x), ", ", decimal(y), ")")
print_divide(io, x, y) = print(io, decimal(x), " / ", decimal(y))
print_max(io, x, y) = print(io, "max(", decimal(x), ", ", decimal(y), ")")
print_min(io, x, y) = print(io, "min(", decimal(x), ", ", decimal(y), ")")
print_minus(io, x) = print(io, "-(", decimal(x), ")")
print_multiply(io, x, y) = print(io, decimal(x), " * ", decimal(y))
print_plus(io, x) = print(io, "+(", decimal(x), ")")
Expand Down
62 changes: 59 additions & 3 deletions src/equals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Base.cmp(x::Decimal, y::Decimal)
# If both x.c and x.q is greater (or equal, or less) than y.c and y.q,
# then x is greater (or equal, or less) than y
if cmp_c == cmp_q
return cmp_c
return ifelse(x.s, -cmp_c, cmp_c)
end

# Adjusted exponent of x and y
Expand Down Expand Up @@ -81,6 +81,64 @@ Base.:(==)(x::Decimal, y::Decimal) = iszero(cmp(x, y))
Base.:(<)(x::Decimal, y::Decimal) = cmp(x, y) < 0
Base.:(<=)(x::Decimal, y::Decimal) = cmp(x, y) <= 0

function Base.min(x::Decimal, y::Decimal)
c = cmp(x, y)
if c < 0
return fix(x)
elseif c > 0
return fix(y)
end

# The operands are numerically equal

# If the signs differ, the negative operand is returned
if x.s != y.s
return x.s ? fix(x) : fix(y)
end

# If the signs and exponents are equal, either can be returned
if x.q == y.q
return fix(x)
end

# If the signs are positive, the operand with the minimum exponent is returned
# If the signs are negative, the operand with the maximum exponent is returned
if x.s
return x.q ≤ y.q ? fix(x) : fix(y)
else
return x.q ≤ y.q ? fix(y) : fix(x)
end
end

function Base.max(x::Decimal, y::Decimal)
c = cmp(x, y)
if c < 0
return fix(y)
elseif c > 0
return fix(x)
end

# The operands are numerically equal

# If the signs differ, the positive operand is returned
if x.s != y.s
return x.s ? fix(y) : fix(x)
end

# If the signs and exponents are equal, either can be returned
if x.q == y.q
return fix(x)
end

# If the signs are positive, the operand with the maximum exponent is returned
# If the signs are negative, the operand with the minimum exponent is returned
if x.s
return x.q ≤ y.q ? fix(y) : fix(x)
else
return x.q ≤ y.q ? fix(x) : fix(y)
end
end

# Special case equality with AbstractFloat to allow comparison against Inf/Nan
# which are not representable in Decimal

Expand All @@ -99,7 +157,6 @@ function Base.min(a::Decimal, b::AbstractFloat)
!signbit(b) && isinf(b) && return convert(promote_type(typeof(a), typeof(b)), a)
min(promote(a, b)...)
end
Base.min(a::Decimal, b::Decimal) = invoke(min, Tuple{T, T} where T<:AbstractFloat, a, b)

function Base.max(a::AbstractFloat, b::Decimal)
signbit(a) && isinf(a) && return convert(promote_type(typeof(a), typeof(b)), b)
Expand All @@ -109,4 +166,3 @@ function Base.max(a::Decimal, b::AbstractFloat)
signbit(b) && isinf(b) && return convert(promote_type(typeof(a), typeof(b)), a)
max(promote(a, b)...)
end
Base.max(a::Decimal, b::Decimal) = invoke(max, Tuple{T, T} where T<:AbstractFloat, a, b)
Loading