# What does \`fastabs(z::Complex)\` do?

**URL:** https://discourse.julialang.org/t/what-does-fastabs-z-complex-do/59064
**Category:** Numerics
**Created:** [April 12, 2021, 1:42am UTC](https://discourse.julialang.org/t/what-does-fastabs-z-complex-do/59064 "2021-04-12T01:42:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![huangc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huangc/32/7413_2.png) [@huangc](https://discourse.julialang.org/u/huangc)
#### Post date: [April 12, 2021, 1:42am UTC](https://discourse.julialang.org/t/what-does-fastabs-z-complex-do/59064/1 "2021-04-12T01:42:19Z")

</div>

There is a `fastabs(z::Complex)` defined in both [SpecialFunctions.jl](https://github.com/JuliaMath/SpecialFunctions.jl/blob/d24f19ea0549f12f8c77a8d7ccbef4413edb4836/src/SpecialFunctions.jl#L91-L98) and [Base.Math](https://github.com/JuliaLang/julia/blob/93fb785831dcfcc442f82fab8746f0244c5274ae/base/special/trig.jl#L1057-L1065).

From its name, I would guess it computes the same thing as `abs(z::Complex)` but faster. However

```julia
julia> z = 3 + 4im
3 + 4im

julia> abs(z)
5.0

julia> Base.Math.fastabs(z)
7

```

Looking at the code, the definition is

```julia
"""
    fastabs(x::Number)
Faster `abs`-like function for rough magnitude comparisons.
`fastabs` is equivalent to `abs(x)` for most `x`,
but for complex `x` it computes `abs(real(x))+abs(imag(x))` rather
than requiring `hypot`.
"""
fastabs(x::Number) = abs(x)
fastabs(z::Complex) = abs(real(z)) + abs(imag(z))

```

What is this function used for?

I thought this was a bug and opened an [issue](https://github.com/JuliaLang/julia/issues/40440) and was told that there is some “intended behaviour”… what is the used case? It’s confusing when it says

> Faster abs-like function for rough magnitude comparisons.

while 5 and 7 seems to be a pretty big difference…

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 12, 2021, 1:45am UTC](https://discourse.julialang.org/t/what-does-fastabs-z-complex-do/59064/2 "2021-04-12T01:45:31Z")

</div>

The use case here is for special functions where you want to do something different for big and small inputs. Specifically, `fastabs` will be off by a maximum factor of `sqrt(2)`, so it’s very useful when a function needs to do something different for small inputs.

---

<div class="post-metadata">

### Author: ![huangc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huangc/32/7413_2.png) [@huangc](https://discourse.julialang.org/u/huangc)
#### Post date: [April 12, 2021, 2:10am UTC](https://discourse.julialang.org/t/what-does-fastabs-z-complex-do/59064/3 "2021-04-12T02:10:09Z")

</div>

Thanks for the explanation. I’m not familiar with the special functions – I will read more on it.

The reason that got me interested was that I noticed the `Base.abs` was indeed much slower than a hand-coded one, although there’s some small errors.

```julia
julia> using BenchmarkTools

julia> zz = rand(ComplexF64, 10000, 10000);

julia> complex_abs(z::Complex) = √(abs2(real(z)) + abs2(imag(z)))
complex_abs (generic function with 1 method)

julia> @btime abs.(zz);
  1.657 s (5 allocations: 762.94 MiB)

julia> @btime complex_abs.(zz);
  414.170 ms (5 allocations: 762.94 MiB)

julia> @btime hypot.(zz);
  1.533 s (5 allocations: 762.94 MiB)

julia> abs.(zz) == hypot.(zz)
true

julia> abs.(zz) == complex_abs.(zz)
false

julia> abs.(zz) ≈ complex_abs.(zz)
true

```

So at first I thought the reason for `fastabs` was just faster `abs` with some numerical error – not expecting a factor of `sqrt(2)`. But now it seems that’s just me not taking enough math classes 🙂

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 12, 2021, 2:14am UTC](https://discourse.julialang.org/t/what-does-fastabs-z-complex-do/59064/4 "2021-04-12T02:14:10Z")

</div>

If you want a faster version, you could try `@fastmath abs.(zz)` which should do something close to `complex_abs`.
