# 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:** 10\
**Page:** 10

<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:** [October 15, 2021, 3:14pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/183 "2021-10-15T15:14:20Z")

</div>

> [@dlakelan](#):
>
> But this isn’t just about performance. It’s also about semantics. If you want a signed twos complement integer you write 37 in Julia, if you want a 64 bit IEEE Float you write 37.0 the two things **mean different things** and are **supposed to**.

Nit: I would say that `37` and `37.0` represent the same real number — that is why they are `==` — but that operations like `+` are defined differently (though they agree until the precision is overflowed) for different subsets of the reals described by different types.

That being said, it’s relatively rare that you actually _want_ the 2s-complement overflow behavior, so if it weren’t for performance considerations it would be reasonable for the default `+` to _not_ do silent 2s-complement wraparound, and have some other operator/function for the rare case where you want this.

The fact that 2s-complement — or often, _undefined_ behavior on integer overflow! — is the _default_ in most compiled languages really is a performance tradeoff, I think.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [October 15, 2021, 3:18pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/184 "2021-10-15T15:18:41Z")

</div>

> [@stevengj](#):
>
> Yes, the original quote was a unfortunately bit incendiary.

I considered not adding the preamble in my post because it distracts from the concrete point about closed source languages (there are real-world examples, eg regarding Mathematica and Bessel functions). This is a very public forum, so it’s important to refute damaging statements that are incorrect. But, that had already been done explicitly in other comments, and my preamble added nothing.

I agree that being sympathetic to the extent possible is the most productive choice. At times, we all find some design incongruous or stupid only because we don’t understand the bigger picture. That @viraltux expresses a strongly held opinion with non-negligible currency seems to have been somewhat productive. But, it is difficult to have a productive discussion when people fail repeatedly, even after being directed to a number of resources, to exercise some well-founded epistemic humility.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [October 15, 2021, 3:43pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/185 "2021-10-15T15:43:05Z")

</div>

You made a very sweeping statement, that is demonstrably false in my experience with healthcare IT:

> [@dlakelan](#):
>
> I’d go farther to say that it’s **unreasonable** to let anyone do anything of any consequence if they don’t understand computer arithmetic. Hence the whole “it’s not safe for regulated uses” thing is really a red herring. What’s not safe is to let unqualified people do the work.

That is what I was responding to.

> [@dlakelan](#):
>
> Evidently though they weren’t doing any significant math. Like for example Pharmacokinetics models or linear regression or molecular dynamics or agent based modeling of interacting plant species or solutions of PDEs on irregular regions or developing new hashing functions for IPv6 addresses and port number combos to minimize congestion on Ethernet switch LAG groups.

I never said that M/MUMPS was a great language for those sorts of use cases - however, for many decades, it was the best language available for many sorts of healthcare applications, and one that didn’t make it difficult for the domain experts to write code that was safe, and _saved_ lives.

These days, if I were writing such applications, I’d definitely use Julia, because then I have the power of using IEEE decimal floating point for the cases where that does makes sense, as well as Int64, Int128, Float64, Rational, or _whatever_ makes sense in other cases, all in a very performant, easy to understand, high level language.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [October 15, 2021, 3:53pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/186 "2021-10-15T15:53:25Z")

</div>

> [@dlakelan](#):
>
> This is like the polar opposite of Julia. Everything is semantically a string, vs everything is semantically exactly whatever type you told Julia to make it.

In M/MUMPS and their successors such as Caché ObjectScript, values are strings, it is the _operations_ which determine whether the values are used as a numeric value or not. `"123" + "1"` returns 124, because the + operator (as well as -, \*, /, \*\*) treat the operands as numbers. Internally, things are optimized, so that you don’t spend all of your time converting back and forth between string and numeric representations when trying to do arithmetic.  
It is totally the opposite of the way Julia (and most other languages) work, true! (but that doesn’t make it an invalid approach, either)

---

<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 15, 2021, 3:57pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/187 "2021-10-15T15:57:15Z")

</div>

Doesn’t this waste a bit of energy?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [October 15, 2021, 4:08pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/188 "2021-10-15T16:08:28Z")

</div>

> [@stevengj](#):
>
> Parsing decimal strings to `Float64` and converting back to strings is actually lossless in Julia — as long as your decimal has \< 14 digits, which should be true for any decimal that is worth representing exactly — because the float-printing algorithm ([Ryu](https://github.com/ulfjack/ryu), formerly Grisu) prints the _shortest_ decimal that rounds to the same binary-float value. See [this discussion](https://discourse.julialang.org/t/sum-of-float64-vector-gives-slightly-incorrect-answer/8577/20).

If that were really the case, why even bother with anything but Float64, which is what R, Matlab, JavaScript, and other languages seem to have done? What’s the point of Int128, BigInt, Dec128, etc. then?  
14 exact digits is _not_ always enough.  
It still doesn’t deal with the case where simple scaling by powers of 10, or adding up values (add .1 10 times) gives results that are unexpected to people used to decimal arithmetic (and can be incorrect if you are trying to deal with things like currencies and dosages).

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [October 15, 2021, 4:19pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/189 "2021-10-15T16:19:52Z")

</div>

> [@goerch](#):
>
> Doesn’t this waste a bit of energy?

If you are talking about the approach MUMPS took, back in the 60s, it was quite efficient (and still was, until the early 90s when IEEE 754 hardware became more widely available - the Intel x486 wasn’t released until mid 1989). Numeric values could be packed into delimited strings, and operated on easily. Most operations were fairly simple arithmetic, usually addition and subtraction by values with the same scale (exponent), scaling by powers of 10 just meant addition/subtraction of the exponent value.  
Back then, without any standard binary floating point hardware, with often only software libraries for binary floating point, decimal arithmetic was actually frequently faster than trying to use binary floating point (and worrying about each software library or hardware implementation being different, and giving different answers).

---

<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 15, 2021, 4:50pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/190 "2021-10-15T16:50:04Z")

</div>

> [@ScottPJones](#):
>
> (but that doesn’t make it an invalid approach, either)

Absolutely, it’s a potentially valid approach for the domain of problems M/MUMPS was designed for. But Julia was _designed_ to have 2 be different from 2.0 because people CARE about the bits and Julia is a language for people who care about such things. For example suppose you’re designing an ASIC that automatically load-balances packets between different ports on a 100Gbps ethernet switch. You want to prototype your algorithm. It works on 48 bit hardware masks that represent a 32 bit IP address and a 16 bit port number (or a 128 bit IPv6 address that has been xored down to a 32 bit field plus a 16 bit port)… if you choose M/MUMPS, R, or Octave to do this prototyping you will be in a WORLD of hurt. Julia will let you do this. If you need to calculate some kind of polynomial ring modulus over these 48 bit fields… julia will do it happily. R won’t without a lot of hassle. Saying that ASIC designers are just SOL and floats are where it’s at doesn’t seem to be what Julia is about.

---

<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 15, 2021, 5:02pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/191 "2021-10-15T17:02:21Z")

</div>

Hey all, this has been a lively discussion, but I fear that it’s long past feeling like a bit of a pile-on (albeit a very civil one 👏), with @viraltux being the sole person arguing on one side and all the rest of us on the other side, which just doesn’t feel like a nice dynamic. Some very good points have been made:

- @viraltux has pointed out, quite rightly, that integer overflow can lead to program errors, which can be a concern and that it would be nice to be able to do something like `--check-overflow` to make sure that integer overflow errors aren’t happening in programs.
- It looks like a very limited version of this checking is probably going to happen by default in a future Julia version for `Int^Int`. This is nice because `^` is one of the operators that is most likely to cause an integer overflow and for which it is unlikely that the user actually wanted native integer modular behavior.
- Implementing `--check-overflow` for other arithmetic operations is more difficult because Julia has been ambiguous about whether `Int+Int` and similar operations are intended to do wrapping arithmetic or not. Before we could usefully introduce overflow checks, we’d need `+ₘ` etc. operators that are explicitly wrapping.

The rest of the discussion is much more subjective and reasonable people can disagree on how dangerous integer overflow is versus floating point error. But it seems like enough points have been made all around, so lest this seem like too much of a pile-on—and it may be too late, with apologies to @viraltux—let’s call it a day.

---

<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 15, 2021, 5:02pm UTC](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627/192 "2021-10-15T17:02:34Z")

</div>

Closed on October 15, 2021, 5:02pm UTC



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