# Cos() allocates and is slow in fastmath?

**URL:** <https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990>\
**Category:** General Usage\
**Tags:** fast-math\
**Created:** [May 28, 2021, 1:23pm UTC](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990 "2021-05-28T13:23:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [May 28, 2021, 1:23pm UTC](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990/1 "2021-05-28T13:23:32Z")

</div>

in v1.6.1, when `--math-mode=fast`:

```julia
julia> @btime cos($1.2);
  217.234 ns (8 allocations: 576 bytes)
julia> @btime sin($1.2);
  175.154 ns (6 allocations: 544 bytes)
julia> @btime tan($1.2);
  188.837 ns (6 allocations: 544 bytes)

```

while in normal math mode:

```julia
julia> @btime cos($1.2);
  47.322 ns (2 allocations: 32 bytes)
julia> @btime sin($1.2);
  17.283 ns (0 allocations: 0 bytes)
julia> @btime tan($1.2);
  29.544 ns (0 allocations: 0 bytes)

```

two problems here:

1. `cos()` allocates in either math mode, while `sin()` and `tan()` does not in normal math. Supposed that elementary function like `cos()` should **not** allocate, right?
2. “fast” math is much slower? perhaps because of the allocation?

thanks.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 28, 2021, 1:28pm UTC](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990/2 "2021-05-28T13:28:19Z")

</div>

Something very strange is going on in your case–I can’t reproduce any of this in Julia 1.6.1 on Linux:

Normal math mode:

```julia
julia> @btime cos($1.2)
  7.360 ns (0 allocations: 0 bytes)
0.3623577544766736

julia> @btime sin($1.2)
  7.587 ns (0 allocations: 0 bytes)
0.9320390859672263

julia> @btime tan($1.2)
  11.403 ns (0 allocations: 0 bytes)
2.5721516221263188

```

`--math-mode=fast`

```julia
julia> @btime cos($1.2)
  5.883 ns (0 allocations: 0 bytes)
0.3623577544766736

julia> @btime sin($1.2)
  5.632 ns (0 allocations: 0 bytes)
0.9320390859672264

julia> @btime tan($1.2)
  8.081 ns (0 allocations: 0 bytes)
2.5721516221263188

```

What is your platform and how did you install Julia?

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [May 28, 2021, 1:29pm UTC](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990/3 "2021-05-28T13:29:33Z")

</div>

> [@rdeits](#):
>
> What is your platform and how did you install Julia?

Mac. Just downloaded the `.dmg` from `julialang` and simply installed it.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [May 28, 2021, 1:37pm UTC](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990/4 "2021-05-28T13:37:22Z")

</div>

I know the problem: I have overrided `Base.isnan()`.

Taking away my own `Base.isnan()` and everything back to normal.

lessons learned: functions in `Base` could only be extended, not to be overrided… 😰

thanks.
