You can use SimpleTraits.jl for this:

using SimpleTraits

# Using Jeffrey's function, define good defaults:
function isexact{X}(::Type{X})
    if X<:Integer || X<:Rational
        true
    elseif X<:Complex
        isexact(real(X))
    else
        false
    end
end

@traitdef IsExact{X}
@generated SimpleTraits.trait{X}(::Type{IsExact{X}}) =
    isexact(X) ? :(IsExact{X}) : :(Not{IsExact{X}})

@traitfn f(x::::IsExact) = println("exact algo with $x")
@traitfn f(x::::(!IsExact)) = println("in-exact algo with $x")

f(5) # exact algo with 5
f(5.0)

# make a new type which is exact
type MyNumber end
isexact(::Type{MyNumber}) = true # note, the default is to be not exact
f(MyNumber())

Let me know if you got questions.

On Mon, 2016-10-24 at 20:09, jw3126 <jw3...@gmail.com> wrote:
> A couple of times I was in a situation, where I had two algorithms solving
> the same problem, one which was faster and one which was more numerically
> stable. Now for some types of numbers (FloatingPoint, Complex{Float64}...)
> numerical stability is important while for others it does not matter (e.g.
> Integer, Complex{Int64}...) etc.
> In such situations it would be handy to have a mechanism (a trait) which
> decides whether a type is exact or prone to rounding.
> Maybe there is already such a thing somewhere?

Reply via email to