Skip to content

Commit 6cebde9

Browse files
committed
SafeReal
1 parent 51e9c6d commit 6cebde9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/methods.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ const diadic = [max, min, hypot, atan, mod, rem, copysign,
1919
polygamma, beta, logbeta]
2020
const previously_declared_for = Set([])
2121

22-
# TODO: it's not possible to dispatch on the symtype! (only problem is Parameter{})
22+
#################### SafeReal #########################
23+
export SafeReal
24+
25+
# ideally the relationship should be the other way around
26+
abstract type SafeReal <: Real end
27+
28+
#######################################################
2329

2430
assert_like(f, T) = nothing
2531
function assert_like(f, T, a, b...)
@@ -86,19 +92,44 @@ for f in diadic
8692
@eval promote_symtype(::$(typeof(f)),
8793
T::Type{<:Number},
8894
S::Type{<:Number}) = promote_type(T, S)
95+
@eval function promote_symtype(::$(typeof(f)),
96+
T::Type{<:SafeReal},
97+
S::Type{<:Real})
98+
X = promote_type(T, Real)
99+
X == Real ? SafeReal : X
100+
end
101+
@eval function promote_symtype(::$(typeof(f)),
102+
T::Type{<:Real},
103+
S::Type{<:SafeReal})
104+
X = promote_type(Real, S)
105+
X == Real ? SafeReal : X
106+
end
107+
@eval function promote_symtype(::$(typeof(f)),
108+
T::Type{<:SafeReal},
109+
S::Type{<:SafeReal})
110+
X = promote_type(Real, Real)
111+
X == Real ? SafeReal : X
112+
end
89113
end
90114

91115
for f in [+, -, *, \, /, ^]
92116
@eval promote_symtype(::$(typeof(f)),
93117
T::Type{<:Number},
94118
S::Type{<:Number}) = promote_type(T, S)
119+
@eval function promote_symtype(::$(typeof(f)),
120+
T::Type{<:SafeReal},
121+
S::Type{<:Number})
122+
X = promote_type(Real, S)
123+
X == Real ? SafeReal : X
124+
end
95125
end
96126

97127
promote_symtype(::typeof(rem2pi), T::Type{<:Number}, mode) = T
98128
Base.rem2pi(x::Symbolic{<:Number}, mode::Base.RoundingMode) = term(rem2pi, x, mode)
99129

100130
for f in monadic
101131
@eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = promote_type(T, Real)
132+
@eval promote_symtype(::$(typeof(f)), T::Type{<:SafeReal}) = SafeReal
102133
@eval (::$(typeof(f)))(a::Symbolic{<:Number}) = term($f, a)
103134
end
104135

src/types.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,9 @@ maybe_intcoeff(x::Rational) = isone(x.den) ? x.num : x
923923
maybe_intcoeff(x) = x
924924

925925
function (::Type{Div{T}})(n, d, simplified=false; metadata=nothing) where {T}
926+
if T<:Number && !(T<:SafeReal)
927+
n, d = quick_cancel(n, d)
928+
end
926929
_iszero(n) && return zero(typeof(n))
927930
_isone(d) && return n
928931

0 commit comments

Comments
 (0)