Disabling DomainError (in sqrt())?

julia> fastsqrt(x) = @fastmath sqrt(x)
fastsqrt (generic function with 1 method)

julia> fastsqrt(-4)
ERROR: DomainError with -4.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
 [1] throw_complex_domainerror(f::Symbol, x::Float64)
   @ Base.Math ./math.jl:33
 [2] sqrt
   @ ./math.jl:591 [inlined]
 [3] sqrt
   @ ./math.jl:1372 [inlined]
 [4] sqrt_fast
   @ ./fastmath.jl:349 [inlined]
 [5] fastsqrt(x::Int64)
   @ Main ./REPL[1]:1
 [6] top-level scope
   @ REPL[2]:1

julia> fastsqrt(-4.0)
NaN

julia> fastsqrt(-4.0f0)
NaN32

Like I said, weird number types (like Int) can hit the fallback, checked version. Note the stacktrace; it’s just calling regular sqrt.
I’d only trust @fastmath to help with Float64 and Float32.

2 Likes