# \#fast-math

**URL:** https://discourse.julialang.org/tag/fast-math/427.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [\[ANN\] XRationals.jl](https://discourse.julialang.org/t/ann-xrationals-jl/136347)

<div class="topic-metadata">

**Author:** [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Replies:** 0\
**Last updated:** [March 23, 2026, 7:48pm UTC](https://discourse.julialang.org/t/ann-xrationals-jl/136347 "2026-03-23T19:48:19Z")

</div>

From the README.md: Exact rational arithmetic with IEEE-like special values (NaN, Inf, -Inf), overflow-safe saturation, and lazy normalization for impressive throughput. Types Type Alias Backing Overflow Normalizat…

---

## [Make fastmath use optional in a package](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527)

<div class="topic-metadata">

**Author:** [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Replies:** 3\
**Last updated:** [March 31, 2025, 11:47am UTC](https://discourse.julialang.org/t/make-fastmath-use-optional-in-a-package/127527 "2025-03-31T11:47:47Z")

</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 on…

---

## [X \* y + z does not automatically use FMA instruction](https://discourse.julialang.org/t/x-y-z-does-not-automatically-use-fma-instruction/100640)

<div class="topic-metadata">

**Author:** [@GLOBEX\_CORP](https://discourse.julialang.org/u/GLOBEX_CORP)\
**Replies:** 31\
**Last updated:** [July 27, 2023, 11:14pm UTC](https://discourse.julialang.org/t/x-y-z-does-not-automatically-use-fma-instruction/100640 "2023-07-27T23:14:11Z")

</div>

I am new to Julia and was wondering why the code x \* y + z does not use fmadd where all values are floats, but instead fmul followed by fadd. On the other hand, if I use the @fastmath decorator, the @code\_native changes …

---

## [\`@fastmath\` is not applied to macros](https://discourse.julialang.org/t/fastmath-is-not-applied-to-macros/101835)

<div class="topic-metadata">

**Author:** [@Keluaa](https://discourse.julialang.org/u/Keluaa)\
**Replies:** 5\
**Last updated:** [July 20, 2023, 4:21pm UTC](https://discourse.julialang.org/t/fastmath-is-not-applied-to-macros/101835 "2023-07-20T16:21:30Z")

</div>

Today I discovered that @fastmath is not applied to inlined function calls (maybe this should be in the docs?), I understand that there is a good reason behind it. However I would have expected this to work on expressio…

---

## [Does the julia intrinsic sum() apply fastmath by default?](https://discourse.julialang.org/t/does-the-julia-intrinsic-sum-apply-fastmath-by-default/94815)

<div class="topic-metadata">

**Author:** [@Ender\_L](https://discourse.julialang.org/u/Ender_L)\
**Replies:** 15\
**Last updated:** [February 21, 2023, 4:05am UTC](https://discourse.julialang.org/t/does-the-julia-intrinsic-sum-apply-fastmath-by-default/94815 "2023-02-21T04:05:00Z")

</div>

I noticed the output of sum() is different from custom loops julia\> function mysum(A) out = 0.0 for ii in eachindex(A) out += A\[ii\] end return out end my…

---

## [@fastmath for library functions? (2x slowdown of binary search because of NaN checks in Base)](https://discourse.julialang.org/t/fastmath-for-library-functions-2x-slowdown-of-binary-search-because-of-nan-checks-in-base/93487)

<div class="topic-metadata">

**Author:** [@Az9DBvyVLWBjs8g](https://discourse.julialang.org/u/Az9DBvyVLWBjs8g)\
**Replies:** 14\
**Last updated:** [January 30, 2023, 7:15pm UTC](https://discourse.julialang.org/t/fastmath-for-library-functions-2x-slowdown-of-binary-search-because-of-nan-checks-in-base/93487 "2023-01-30T19:15:52Z")

</div>

Hello everyone, I’ve benchmarked Julia’s implementation of binary search (searchsortedlast) to test whether Julia can match C performance (spoiler: it can). However, I found that isnan checks within isless lead to a 1.5…

---

## [Rank of matrix on Z2 field](https://discourse.julialang.org/t/rank-of-matrix-on-z2-field/81078)

<div class="topic-metadata">

**Author:** [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Replies:** 12\
**Last updated:** [May 17, 2022, 8:56am UTC](https://discourse.julialang.org/t/rank-of-matrix-on-z2-field/81078 "2022-05-17T08:56:41Z")

</div>

Hi Suppose I have a symmetric matrix defined on the Z2 field {0,1}. I want to calculate its rank. Do we have any function to do this? Thanks

---

## [How to convert the binary form of a float to an integer, and vice versa?](https://discourse.julialang.org/t/how-to-convert-the-binary-form-of-a-float-to-an-integer-and-vice-versa/70083)

<div class="topic-metadata">

**Author:** [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Replies:** 17\
**Last updated:** [January 28, 2022, 9:29am UTC](https://discourse.julialang.org/t/how-to-convert-the-binary-form-of-a-float-to-an-integer-and-vice-versa/70083 "2022-01-28T09:29:00Z")

</div>

I am trying to implement the “fast inverse square root” algorithm in Julia: function rsqrt(x::Float32) xₛ = x int32 = reinterpret(UInt32, xₛ) int32 = 0x5f3759df - int32 \>\> 1 xₛ = reinterpret(Float32, int…

---

## [What's going on with exp() and --math-mode=fast?](https://discourse.julialang.org/t/whats-going-on-with-exp-and-math-mode-fast/64619)

<div class="topic-metadata">

**Author:** [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Replies:** 29\
**Last updated:** [October 23, 2021, 5:13pm UTC](https://discourse.julialang.org/t/whats-going-on-with-exp-and-math-mode-fast/64619 "2021-10-23T17:13:28Z")

</div>

I was surprised to see results that are significantly different (by much more than I would except when using normal ffast-math optimizations) when running julia with --math-mode=fast. It seems like a very rough approxim…

---

## [Two nearly identical loops — why is one faster?](https://discourse.julialang.org/t/two-nearly-identical-loops-why-is-one-faster/67998)

<div class="topic-metadata">

**Author:** [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Replies:** 2\
**Last updated:** [September 10, 2021, 7:03pm UTC](https://discourse.julialang.org/t/two-nearly-identical-loops-why-is-one-faster/67998 "2021-09-10T19:03:49Z")

</div>

The following two functions both evaluate Chebyshev polynomials, using slightly different recurrences that nevertheless require nearly the same number of operations, both are type-stable with 0 allocations, and yet one o…

---

## [How to improve performance of sum()](https://discourse.julialang.org/t/how-to-improve-performance-of-sum/64928)

<div class="topic-metadata">

**Author:** [@andrewkhardy](https://discourse.julialang.org/u/andrewkhardy)\
**Replies:** 19\
**Last updated:** [July 19, 2021, 8:13pm UTC](https://discourse.julialang.org/t/how-to-improve-performance-of-sum/64928 "2021-07-19T20:13:40Z")

</div>

Hello. I recently tried out Julia because I was hoping for a speed improvement over numpy.sum(). I’m trying to benchmark a sum for a 2D integral and so far my Julia code is much slower. I’m wondering if there’s any perf…

---

## [Fastmath error?](https://discourse.julialang.org/t/fastmath-error/64750)

<div class="topic-metadata">

**Author:** [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Replies:** 2\
**Last updated:** [July 16, 2021, 1:56pm UTC](https://discourse.julialang.org/t/fastmath-error/64750 "2021-07-16T13:56:34Z")

</div>

v1.6.1, when --math-mode=fast: julia\> f() = exp(1.0); julia\> f() - exp(1.0) 0.002427216033242807 which is obviously wrong. It’s wrong even in fastmath mode! while when --math-mode=ieee, no problem: julia\> f() = exp(…

---

## [Improving Computational Performance](https://discourse.julialang.org/t/improving-computational-performance/63210)

<div class="topic-metadata">

**Author:** [@nullmap](https://discourse.julialang.org/u/nullmap)\
**Replies:** 6\
**Last updated:** [June 20, 2021, 10:13pm UTC](https://discourse.julialang.org/t/improving-computational-performance/63210 "2021-06-20T22:13:29Z")

</div>

I have three questions and I was curious if someone can provide some clarity on the results I’m seeing with a couple variations of a simple polynomial evaluation computation. I wrote a Horner’s method implementation whi…

---

## [Cos() allocates and is slow in fastmath?](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990)

<div class="topic-metadata">

**Author:** [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Replies:** 3\
**Last updated:** [May 28, 2021, 1:37pm UTC](https://discourse.julialang.org/t/cos-allocates-and-is-slow-in-fastmath/61990 "2021-05-28T13:37:22Z")

</div>

in v1.6.1, when --math-mode=fast: 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: 5…

---

## [A subtle bug?](https://discourse.julialang.org/t/a-subtle-bug/61725)

<div class="topic-metadata">

**Author:** [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Replies:** 21\
**Last updated:** [May 25, 2021, 1:45pm UTC](https://discourse.julialang.org/t/a-subtle-bug/61725 "2021-05-25T13:45:59Z")

</div>

I got a Float64 number from the p-value of a GLM model. I could NOT show the fitted model with the following error: ERROR: p-values must be in \[0; 1\] Stacktrace: \[1\] error(::String) at ./error.jl:33 \[2\] StatsBase.PVa…

---

## [Surprising @fastmath behavior](https://discourse.julialang.org/t/surprising-fastmath-behavior/54600)

<div class="topic-metadata">

**Author:** [@Christian\_Rorvik](https://discourse.julialang.org/u/Christian_Rorvik)\
**Replies:** 3\
**Last updated:** [February 4, 2021, 10:45am UTC](https://discourse.julialang.org/t/surprising-fastmath-behavior/54600 "2021-02-04T10:45:54Z")

</div>

Here’s something that caught me off guard. Example is reduced to trivial code, but I caught this in a much more complex context. function foo(a, b, c) @fastmath x = a \* b + c isnan(x) ? 0.0 : x end code\_native(f…

---

## [Investigating numerical change in function return value between v1.4 vs v1.5](https://discourse.julialang.org/t/investigating-numerical-change-in-function-return-value-between-v1-4-vs-v1-5/41332)

<div class="topic-metadata">

**Author:** [@jmert](https://discourse.julialang.org/u/jmert)\
**Replies:** 7\
**Last updated:** [June 20, 2020, 6:29pm UTC](https://discourse.julialang.org/t/investigating-numerical-change-in-function-return-value-between-v1-4-vs-v1-5/41332 "2020-06-20T18:29:33Z")

</div>

I’ve got a function which is exhibiting a change to the numerical return value when comparing Julia versions 1.2 through 1.4 versus the 1.5beta (and master). I think this is actually an LLVM “error” (or a misunderstandin…

---

## [@fastmath macro accuracy](https://discourse.julialang.org/t/fastmath-macro-accuracy/38847)

<div class="topic-metadata">

**Author:** [@s-baumann](https://discourse.julialang.org/u/s-baumann)\
**Replies:** 26\
**Last updated:** [May 12, 2020, 9:06pm UTC](https://discourse.julialang.org/t/fastmath-macro-accuracy/38847 "2020-05-12T21:06:42Z")

</div>

As I understand it @fastmath explores whatever functions you write and replaces them with fast and rough versions of these functions. For instance rather than the proper exp function you get a faster and less accurate ve…

---

## [How to tell VS Code to use the fast math option?](https://discourse.julialang.org/t/how-to-tell-vs-code-to-use-the-fast-math-option/18257)

<div class="topic-metadata">

**Author:** [@Juan](https://discourse.julialang.org/u/Juan)\
**Replies:** 30\
**Last updated:** [December 7, 2018, 5:03pm UTC](https://discourse.julialang.org/t/how-to-tell-vs-code-to-use-the-fast-math-option/18257 "2018-12-07T17:03:10Z")

</div>

I’ve never used it before but I’ve read we can start Julia with the command line option: Julia --math-mode=fast How should we do it if we use Julia from VS Code or Juno with fastmath? is it enough to add it to the “ex…

---

## [Incorrect results for sinpi and cospi when using fastmath mode?](https://discourse.julialang.org/t/incorrect-results-for-sinpi-and-cospi-when-using-fastmath-mode/17662)

<div class="topic-metadata">

**Author:** [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Replies:** 4\
**Last updated:** [November 18, 2018, 3:35pm UTC](https://discourse.julialang.org/t/incorrect-results-for-sinpi-and-cospi-when-using-fastmath-mode/17662 "2018-11-18T15:35:25Z")

</div>

When firing up Julia with the --math-mode=fast option, I get incorrect results for sinpi and cospi functions. Is this a bug or am I missing something? With the default math mode: julia\> a = rand(5) 5-element Array{Floa…

---

## [Slow exp()?](https://discourse.julialang.org/t/slow-exp/13606)

<div class="topic-metadata">

**Author:** [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Replies:** 12\
**Last updated:** [August 17, 2018, 11:10am UTC](https://discourse.julialang.org/t/slow-exp/13606 "2018-08-17T11:10:44Z")

</div>

julia\> exp(1.3) 3.6692966676192444 julia\> exp(1) 2.718281828459045 julia\> 2.718281828459045 ^ 1.3 3.669296667619244 julia\> @benchmark exp(1.3) BenchmarkTools.Trial: memory estimate: 0 bytes allocs estimate: 0 …

---

## [@fastmath switched off in Julia 0.7-Dev, deliberately?](https://discourse.julialang.org/t/fastmath-switched-off-in-julia-0-7-dev-deliberately/11898)

<div class="topic-metadata">

**Author:** [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Replies:** 7\
**Last updated:** [June 24, 2018, 9:41am UTC](https://discourse.julialang.org/t/fastmath-switched-off-in-julia-0-7-dev-deliberately/11898 "2018-06-24T09:41:03Z")

</div>

I noticed that the @fastmath macro is no longer effective in version 0.7-Dev. Has it been switched off deliberately, or something wrong with my installation is preventing it from taking off? I tested on both Windows 10 a…

---

## [Fastmath and forwarddiff](https://discourse.julialang.org/t/fastmath-and-forwarddiff/9934)

<div class="topic-metadata">

**Author:** [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Replies:** 1\
**Last updated:** [March 24, 2018, 1:43am UTC](https://discourse.julialang.org/t/fastmath-and-forwarddiff/9934 "2018-03-24T01:43:00Z")

</div>

Is there a straightforward way to combine @fastmath with ForwardDiff? The idea is that I might define a function f(x) using @fastmath and I then want to evaluate f'(x) using ForwardDiff. My naive attempt generates an e…

---

## [@fastmath for matrices?](https://discourse.julialang.org/t/fastmath-for-matrices/5484)

<div class="topic-metadata">

**Author:** [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Replies:** 6\
**Last updated:** [August 21, 2017, 4:30pm UTC](https://discourse.julialang.org/t/fastmath-for-matrices/5484 "2017-08-21T16:30:50Z")

</div>

I wonder if there is a package that can figure out the fastest way to multiply out a few matrices by each other using dynamic programming or any other approach. For example, if I am doing A\*B\*c where A is 100x100, B is 1…

---

## [Why are calls to this function slow, compared to Python+Numpy?](https://discourse.julialang.org/t/why-are-calls-to-this-function-slow-compared-to-python-numpy/5346)

<div class="topic-metadata">

**Author:** [@vgdev](https://discourse.julialang.org/u/vgdev)\
**Replies:** 14\
**Last updated:** [August 13, 2017, 9:47am UTC](https://discourse.julialang.org/t/why-are-calls-to-this-function-slow-compared-to-python-numpy/5346 "2017-08-13T09:47:48Z")

</div>

I have simple question. I have a function b, below which i make multiple calls too. But I am experiencing that it is about x3 slower than a Numpy implementaion in Python. Any idea on how to speed this up? function b(a, …
