# Why does @fastmath have problems with promoting integers?

**URL:** <https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276>\
**Category:** General Usage\
**Created:** [April 21, 2025, 10:47pm UTC](https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276 "2025-04-21T22:47:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [April 21, 2025, 10:47pm UTC](https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276/1 "2025-04-21T22:47:13Z")

</div>

Why do I get the following error?

```julia
julia> (-1) * UInt(1)
0xffffffffffffffff

julia> @fastmath (-1) * UInt(1)
ERROR: InexactError: convert(UInt64, -1)

```

Tested with Julia 1.11.4.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 21, 2025, 11:46pm UTC](https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276/2 "2025-04-21T23:46:10Z")

</div>

`@fastmath` changes operations:

```julia
julia> @macroexpand @fastmath((-1) * UInt(1))
:(Base.FastMath.mul_fast(-1, UInt(1)))

```

which falls back to promotion and the original operation if it’s not among the floating point types flouting IEEE-754 for speed:

```julia
        $op_fast(x::Number, y::Number, zs::Number...) = # op_fast === mul_fast
            $op_fast(promote(x,y,zs...)...)
        # fall-back implementation that applies after promotion
        $op_fast(x::T,ys::T...) where {T<:Number} = $op(x,ys...) # op === *

```

Unfortunately, the promotion doesn’t work for `*(::Int, ::Uint)`:

```julia
julia> promote((-1), UInt(1))
ERROR: InexactError: convert(UInt64, -1)

```

I have no idea if this is intentional or why they’d take the extra step of promotion when the original operation would take care of it (and properly), I’d argue it’s a bug. The non-numeric method preceding the above numeric methods actually wouldn’t have this problem:

```julia
        # fall-back implementation for non-numeric types
        $op_fast(xs...) = $op(xs...)

```

Practically speaking, there’s no point in using `@fastmath` for anything that doesn’t involve floating point types, and involving negative signs with an unsigned number is an unusually tricky use of overflow.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [April 22, 2025, 12:55am UTC](https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276/3 "2025-04-22T00:55:05Z")

</div>

> [@Benny](#):
>
> there’s no point in using `@fastmath` for anything that doesn’t involve floating point types

True. However, one might have a function that accepts both integer and floating point types, and one adds `@fastmath` to have it for the floating point case.

Anyway, thanks!

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 22, 2025, 12:58am UTC](https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276/4 "2025-04-22T00:58:37Z")

</div>

> [@matthias314](#):
>
> However, one might have a function that accepts both integer and floating point types, and one adds `@fastmath` to have it for the floating point case.

Indeed, so proper fallbacks are still the right thing to do IMO. Forgot to mention, but the promotion for `*(::Int, ::UInt)` works by `%` overflow prior to promotion to get around the typical `InexactError` for converting negative signed integers to unsigned ones.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [April 22, 2025, 1:01am UTC](https://discourse.julialang.org/t/why-does-fastmath-have-problems-with-promoting-integers/128276/5 "2025-04-22T01:01:10Z")

</div>

Here is a link to a relevant GitHub issue: [JuliaLang/julia#15489](https://github.com/JuliaLang/julia/issues/15489#issuecomment-331261457). The consensus seems to be not to throw an error in such a case. I guess the `@fastmath` version was forgotten.

EDIT: I’ve created an issue on GitHub: [integer multiplication errors with `@fastmath` · Issue #58188 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/58188)
