# Help to create a macro do avoid integer overflow

**URL:** <https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354>\
**Category:** General Usage\
**Tags:** integer-overflow\
**Created:** [June 2, 2018, 12:02am UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354 "2018-06-02T00:02:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 12:02am UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/1 "2018-06-02T00:02:56Z")

</div>

Hi guys!

I need help to create a macro to do the following:

- On every integer operation, check if overflow will happen.
- If it does, then promote the arguments to integer representations with higher number of bits.

I do not know how the compiler will work, but I am wondering if this can be done without _much_ performance decrease if the operands do not lead to an integer overflow.

My usage is because I have in my code things like:

```julia
f100 = C1*Δt^2 + C2*Δt^3

```

where `Δt` is selected by the user and can be a very big number. Hence, if the user input a big integer, the `^` will overflow.

Of course I can always promote the `Δt` to Float64, but I want to try this to see how it works.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 2, 2018, 12:15am UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/2 "2018-06-02T00:15:21Z")

</div>

> [@Ronis\_BR](#):
>
> I need help to create a macro to do the following:

Macros don’t know about types, so macros can’t do this. If you want different integer semantics, you generally need to define a different integer type. (See e.g. [GitHub - JeffreySarnoff/SaferIntegers.jl: These integer types use checked arithmetic, otherwise they are as system types.](https://github.com/JeffreySarnoff/SaferIntegers.jl))

If you only want to look at operations on integer _literals_, that could be done with a macro, but I’m not sure how useful it would be.

> If it does, then promote the arguments to integer representations with higher number of bits. I do not know how the compiler will work, but I am wondering if this can be done without much performance decrease if the operands do not lead to an integer overflow.

I doubt that this can be done at runtime without a substantial performance hit, but you are welcome to try to define a type that behaves in this way.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 12:18am UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/3 "2018-06-02T00:18:35Z")

</div>

Hi @stevengj,

Thanks for the answer, but by macro, I mean something like `@inbounds` that I can add to certain operations so that they can behave like I described.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 2, 2018, 8:20am UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/4 "2018-06-02T08:20:48Z")

</div>

A macro is just a rewrite of an expression into another. What expression do you want to apply the macro to and what expression do you want to come out in the end?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 1:50pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/5 "2018-06-02T13:50:19Z")

</div>

I understood. But, I really do not know 😃 I want something that, at every integer operation (+, \* and ^) it checks the operands and, if overflow will happen, then change the type to a representation with higher bit. I have no idea how to code this in Julia.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 2, 2018, 1:59pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/6 "2018-06-02T13:59:08Z")

</div>

Why don’t you use [https://github.com/JeffreySarnoff/SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl) ?

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 2, 2018, 2:02pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/7 "2018-06-02T14:02:47Z")

</div>

> [@Ronis\_BR](#):
>
> if overflow will happen

There is no way to know that at compile time, so macros will not help you. You should use `try` blocks with the saferintegers.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 2:07pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/8 "2018-06-02T14:07:19Z")

</div>

For three reasons:

1. It does not support (at least it is not documented) the function `^`.
2. I want to learn new things 😃
3. I want to see the performance hit when doing such things like check the type and promoting if necessary.

> [@favba](#):
>
> There is no way to know that at compile time, so macros will not help you. You should use `try` blocks with the saferintegers.

Actually, I am thinking of a macro that overload / change (do not know the correct name) the operations. Something like, change `+` to another function that performs the verification as I described. This macro will be used just trigger such behavior like:

```julia
@check_overflow f100 = C1*Δt^2 + C2*Δt^3

```

However, if I give up and use SafeIntegers, I would do something like:

```julia
try
    f100 = C1*Δt^2 + C2*Δt^3
catch
    f100 = C1*BigInt(Δt)^ + C2*BigInt(Δt)^3
end

```

Correct?

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 2, 2018, 2:17pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/9 "2018-06-02T14:17:19Z")

</div>

> [@Ronis\_BR](#):
>
> change `+` to another function that performs the verification as I described.

I _think_ this is exactly what SaferIntegers does. It creates primitives that work the same way as integers but checks for overflow by defining their own methods for common functions.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 2:21pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/10 "2018-06-02T14:21:26Z")

</div>

Nice! I will see 🙂 The macro I mentioned is because I just want to do this in some parts of the code, because there are other in which I know it is not necessary. Hence, I think if I can trigger this behavior only when necessary, the performance hit will not be big (at least I hope).

Anyway, in some tests here with SafeIntegers and such things, I am starting to see that my best approach in terms of performance is to convert everything to Float64 just as MATLAB does 😃

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 2, 2018, 5:21pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/11 "2018-06-02T17:21:04Z")

</div>

You could a macro that does something where `@check_overflow f100 = C1*Δt^2 + C2*Δt^3` lowers to

```julia
try
    f100 = C1*SafeInt(Δt)^2 + C2*SafeInt(Δt)^3
catch
    f100 = C1*BigInt(Δt)^ + C2*BigInt(Δt)^3
end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 2, 2018, 5:22pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/12 "2018-06-02T17:22:06Z")

</div>

Presumably there was a reason you used Integers in the first place?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 5:36pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/13 "2018-06-02T17:36:01Z")

</div>

Actually I am using Numbers. The user decide. The input variable is the number of seconds from a specific epoch. Hence, many users will like to write something like:

```julia
propagate!(orbs, 1000)

```

Instead of

```julia
propagate!(orbs, 100.0)

```

I would not like to force them to use Float literals because this is targeted to MATLAB users.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 2, 2018, 5:40pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/14 "2018-06-02T17:40:28Z")

</div>

In this case you could do something along

```julia
function propagate!(a,s::Float64)
#define function here
...
end

propagate!(a,s::Number) = propagate!(a,float(s))
```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 2, 2018, 5:43pm UTC](https://discourse.julialang.org/t/help-to-create-a-macro-do-avoid-integer-overflow/11354/15 "2018-06-02T17:43:16Z")

</div>

Yeah, that is what I mentioned before. I am starting to think this is the best approach, which is basically what MATLAB does, convert everything to Double.
