# Make fastmath use optional in a package

**URL:** https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527
**Category:** General Usage
**Tags:** fast-math
**Created:** [March 31, 2025, 3:03am UTC](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527 "2025-03-31T03:03:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)
#### Post date: [March 31, 2025, 3:03am UTC](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527/1 "2025-03-31T03:03:22Z")

</div>

Suppose that I write a package in which I would like to give the user the option to use fast math inside the package, but not necessarily elsewhere. What would be the best way of accomplishing that?

The following is one possibility (for addition), but I doubt that it’s optimal. Thoughts?

```julia
if haskey( ENV, "foomath" )
    import Base.FastMath.add_fast as +
else
    import Base.+ as +
end

```

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 31, 2025, 7:05am UTC](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527/2 "2025-03-31T07:05:31Z")

</div>

Perhaps Preferences.jl?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [March 31, 2025, 9:22am UTC](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527/3 "2025-03-31T09:22:40Z")

</div>

While at first glance such a switch seems like good solution, it has one major drawback: dependencies you use in your code won’t be affected by it. That is, functions like `sum` or various BLAS library functions (`*` on matrices, for example) will still use the non-`fastmath` versions. So unless you want to code everything yourself, this will likely result in confusing differences in performance & semantics (“fast math” makes some assumptions about the input data, like not having NaN or Inf, among others).

So, what effect would you like such a switch to have? As is, I’m not sure there is enough information here to give a meaningful answer.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [March 31, 2025, 11:47am UTC](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527/4 "2025-03-31T11:47:47Z")

</div>

Maybe a conditional macro definition, like

```julia
if haskey( ENV, "foomath" )
    macro foomath(expr)
        :(@fastmath $(esc(expr)))
    end
else
    macro foomath(expr)
        esc(expr)
    end
end

```

In that case, `@foomath` does nothing unless `foomath` option is present.
