# Discussion about integer overflow

**URL:** <https://discourse.julialang.org/t/discussion-about-integer-overflow/69627>\
**Category:** Internals & Design\
**Tags:** numbers, integer-overflow\
**Created:** [October 11, 2021, 12:44pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627 "2021-10-11T12:44:33Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 9:16pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/82 "2021-10-12T21:16:23Z")

</div>

> [@johnmyleswhite](#):
>
> I think this claim is where this thread has gotten so far off the rails. To make this claim, you ought to do a few things:
> 
> 1. Define a formal metric of correctness.
> 2. Measure that metric of correctness systematically against existing systems you deem relevant.
> 3. Demonstrate that Julia does more poorly.
> 
> The absence of rigor here ensures this debate will degenerate into pure emotion and provide no benefit to anyone involved.

1. Number of overflow scenarios where the language returns either an error, or a value that not being true allows the user either to stop the calculation or handle the overflow appropriately (e.g. Inf)
2. This should be straightforward.
3. This one should be straightforward too I think.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 9:21pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/83 "2021-10-12T21:21:57Z")

</div>

> [@StefanKarpinski](#):
>
> Or if (for some bizarre reason) someone is computing a drug dosage using `Int` values, and the correct dosage is larger than 2^64, then it could accidentally give someone a negative dosage.

That’s exactly how it works; there you have the pharmacometrician doing 2^64 and giving negative doses to patients. Thank you for taking it seriously Stefan.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 9:25pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/84 "2021-10-12T21:25:45Z")

</div>

> [@mbauman](#):
>
> Compliance really isn’t just about the tool itself — it’s more about how you use it.

That’s very true… However some tools will have it easier, in theory we could use an assembly language to design a clinical trial but that’s not going to happen.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2021, 9:47pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/85 "2021-10-12T21:47:48Z")

</div>

I’m a chemist, and a lecturer, and have many friends working in industry. I have seen all kinds of absurd concentrations coming from calculations, most times those were done by hand. Never the error came from the overflow of a number in a calculator. Almost always they come from human error in input data, particularly with units.

I do think that a package, or an environment that forces a user to stick to certain rules and checks values and units for the user would be of great importance. But I really cannot think of integer overflow being one of the important checks (though it could be there as well), it feels like a problem not much connected to actual research and development problems of any product. Missing a dose by a factor of 10 can also kill a patient.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 9:49pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/86 "2021-10-12T21:49:08Z")

</div>

> [@lungben](#):
>
> The question is where there is a risk of one in a million (or larger) for an integer overflow.  
> …  
> Edit: @viraltux may I ask to what industry you refer to?

I work now in pharma but I’ve also work in the aerospace industry and they also have their own strict compliance protocols to attend.

As @mbauman mentioned is not really about the tool, if we build a Julia piece of analytics with very thorough checks in every input and in every calculation we could potentially demonstrate that no error can take place in that constraint environment as long as every package that our analytics uses guarantees the same.

However, let’s try now to place ourselves in the position of a Pharmacometrician, would we want to care mainly about the mathematics of our model knowing that the tools we’re using have all kind of numerical checks in place, or we rather worry not just about our models, but about low level computation problems?

An extreme case would be choosing between assembly and R. What language would we choose as Pharmacometricians? The one we are at risk at every operation or the one that has ever possible check in place and allows for us to focus in the formulas?

And if we choose assembly and something goes wrong, how will we answer the question “Why didn’t you use R instead?”

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 9:58pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/87 "2021-10-12T21:58:08Z")

</div>

> [@lmiq](#):
>
> But I really cannot think of integer overflow being one of the important checks, it feels like a problem not much connected to actual research and development problems of any product.

If you’re into research absurd concentrations are not a problem, in research nothing is and we use whatever works best.

> [@lmiq](#):
>
> Missing a dose by a factor of 10 can also kill a patient.

In the unfortunate case a patient dies we want to make sure we can explain our choices; speed of a language won’t make it.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2021, 10:03pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/88 "2021-10-12T22:03:56Z")

</div>

Well, I would challenge you to find a single example, of the many thousand cases of bad research and development of a drug, where the problem was the precision of the arithmetics of the language used in a program. I think you are trying to push an hypothetical problem to an extreme that does not exist.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 12, 2021, 10:04pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/89 "2021-10-12T22:04:50Z")

</div>

At this point, I think it’s worth putting on my other hat and say that both Pumas-AI and Julia Computing take compliance needs very seriously. I’m spending a large portion of my day working towards 21 CFR Part 11 and Annex 11 and SOC 2 and other GxP processes. Modular integer arithmetic is only one small part here and while you may disagree with this choice there are many other features of Julia that lend itself _very well_ to these standards — first and foremost its reproducibility. If you’re interested in a compliant environment I encourage you to email [compliance@pumas.ai](mailto:compliance@pumas.ai) or [compliance@juliacomputing.com](mailto:compliance@juliacomputing.com).

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 10:05pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/90 "2021-10-12T22:05:26Z")

</div>

> [@lmiq](#):
>
> Well, I would challenge you to find a single example, of the many thousand cases of bad research and development of a drug, where the problem was the precision of the arithmetics of the language used in a program. I think you are trying to push an hypothetical problem to an extreme that does not exist.

Maybe we won’t find any because they use SAS and R?

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [October 12, 2021, 10:08pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/91 "2021-10-12T22:08:56Z")

</div>

> [@mbauman](#):
>
> Modular integer arithmetic is only one small part here and while you may disagree with this choice there are many other features of Julia that lend itself _very well_ to these standards

As a matter of fact I don’t disagree with the choice because I don’t want to lose Julia speed either, that’s why all the suggestions about --check-overflow options, so we have the best of both worlds.

> [@mbauman](#):
>
> If you’re interested in a compliant environment I encourage you to email [compliance@pumas.ai](mailto:compliance@pumas.ai) or [compliance@juliacomputing.com](mailto:compliance@juliacomputing.com).

Thank you, good to know!

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 12, 2021, 10:09pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/92 "2021-10-12T22:09:14Z")

</div>

How is any of this different from “it’s possible to call a non-stiff ODE solver on a stiff equation and get the wrong estimate of the concentration in 1hr” or “it’s possible to call an MCMC sampler and have it fail to converge” or “it’s possible to enter 1/(1+ exp(-x)) and have the user call this for a large negative x and the formula will give a wrong answer”

Doing things that are known to be numerically bad will be bad… It’s true in ANY language. R won’t keep you from getting the wrong answer if you apply function(x){1/(1+exp(-x))} to a sufficiently negative value (searching for a proper example).

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [October 12, 2021, 10:29pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/93 "2021-10-12T22:29:08Z")

</div>

Ok, this is easier to give clarity on:

1. R’s integers may or may not overflow – user code may or may not overflow depending on whether it calls overflow-safe operations like + or overflow-unsafe operations like reading CSV files. (R can silently produce garbage if you read in data generated by a system that uses 64-bit integers; Julia will not corrupt such data.)
2. Julia’s integers always overflow.
3. R and Julia have almost the exact same floating point semantics for overflow. The only difference is that R uses `NaN` in a strange way that produces different results depending on compiler settings.

So I think the score is either even or slightly in Julia’s favor. But you seem to have a different understanding, which I’d like you to flesh out in some greater detail.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 12, 2021, 10:30pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/94 "2021-10-12T22:30:25Z")

</div>

> [@viraltux](#):
>
> That’s exactly how it works; there you have the pharmacometrician doing 2^64 and giving negative doses to patients. Thank you for taking it seriously Stefan.

I am taking it quite seriously and this is a real error analysis based on modular arithmetic. Suppose someone were to write code that used `Int` values to compute a drug dosage. This is admittedly somewhat implausible, but lets suppose they do, since that’s where all of this started: with the supposition that someone might be using integer arithmetic to compute drug dosages and that might lead to fatally incorrect answers. Then the answer they get would be correct modulo 2^64, with representatives of the equivalence classes taken from the set of values `-2^63:2^63-1`. That means that if the true answer they should have gotten is in that range, then they will get the exact right answer. If the answer is out of that range, then they’ll get an incorrect answer (still correct in modular arithmetic). Thus, it’s impossible for modular integer behavior to result in an overdose, only an underdose. Of course an underdose could be fatal too, but if you know that valid range of doses are always in the range `-2^63:2^63-1` then the answer cannot be incorrect, even if some intervening operations overflow or underflow. This is actually a much better guarantee than either floating-point or saturating arithmetic (like Matlab uses for integers) can provide—both of those can be arbitrarily wrong even when a correct answer would be in a very modest range.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 12, 2021, 10:35pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/95 "2021-10-12T22:35:03Z")

</div>

> [@StefanKarpinski](#):
>
> impossible for modular integer behavior to result in an overdose, only an underdose

and unless the technician is willing to grind the patient up and run them through a distillation column it’s unlikely they will act on a wrong result giving a negative dose

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [October 12, 2021, 10:35pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/96 "2021-10-12T22:35:18Z")

</div>

> [@viraltux](#):
>
> Maybe we won’t find any because they use SAS and R?

I love how you subtly imply that correlation may have something to do with causation 😛  
We all know that frivolous usage of integer/fp arithmetic may lead to mathematically wrong answers (in any language), so I really don’t understand what’s the point. I was still on board when @tim.holy summarized the story. I’m lost by now in somehow eristic discussion that followed…

Anyway, my take away from the discussion is:

1. `--check-overflow` flag would be nice to have, is possible, but requires real work = commitment of time, effort and resources of someone skillful and/or an engagement of an industrial partner.
2. Rust has different operators when overflow is intended. I somehow like this solution. Maybe a small point to be remembered for julia 2.0 (which is still way below the horizon 😉 )

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 12, 2021, 10:41pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/97 "2021-10-12T22:41:51Z")

</div>

For those interested, John Regehr has [written](https://blog.regehr.org/archives/1384) about fast overflow checking, concluding:

> In summary, signed integer overflow checks in LLVM are fast but they get in the way of the optimizers, which haven’t yet been systematically taught to see through them or eliminate them. There’s plenty of low-hanging fruit, and hopefully we can get the overhead down to the point where people can turn on overflow checking in production codes without thinking too hard about the tradeoffs.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [October 12, 2021, 10:42pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/98 "2021-10-12T22:42:46Z")

</div>

From a curious bystander: how compliant is Julia with regard to [ISO/IEC 10967 - Wikipedia](https://en.wikipedia.org/wiki/ISO/IEC_10967)?

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [October 13, 2021, 3:01am UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/99 "2021-10-13T03:01:28Z")

</div>

> [@lmiq](#):
>
> NASA has landed on the moon using probably Fortran with 32bits arithmetic

Wow you are so young and optimistic—it was more like [15 bits using assembly](https://en.wikipedia.org/wiki/Apollo_Guidance_Computer)! To their credit, NASA also lost a Mars orbiter due to units mismatch.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 13, 2021, 3:10am UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/100 "2021-10-13T03:10:03Z")

</div>

Also, an Ariane 5 famously crashed due to 16 bit integer overflow.

---

<div class="post-metadata">

**Author:** ![czylabsonasa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/czylabsonasa/32/8663_2.png) [@czylabsonasa](https://discourse.julialang.org/u/czylabsonasa)\
**Post date:** [October 13, 2021, 5:09am UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/101 "2021-10-13T05:09:39Z")

</div>

> [@viraltux](#):
>
> Well, let’s look at it in a different way, what mathematically correct calculation would you type in R that would return something wrong?

One of the drawbacks of the default double approach is:

```R
> x = 2^60 + 100; y = 2^60; sqrt(x-y)
[1] 0
> 

```

[Previous page](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627.md?page=4)

[Next page](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627.md?page=6)
