257
you are viewing a single comment's thread
view the rest of the comments
[-] TootSweet@lemmy.world 15 points 11 months ago* (last edited 11 months ago)
for (int y = MIN_INT; y <= MAX_INT; y++) {
        if (y == x + 1) {
                x = y;
        }
}

(Not sure there's a way to prevent Lemmy from escaping my left angle bracket. I definitely didn't type ampersand-el-tee-semicolon. You'll just have to squint and pretend. I'm using the default lemmy-ui frontend.)

[-] xthexder@l.sw0.com 2 points 11 months ago

y <= MAX_INT will never be false, since the loop will overflow and wrap around to MIN_INT

(You can escape code with `backticks`, and regular markdown rules)

[-] mormegil@programming.dev 4 points 11 months ago

It will not "overflow". Signed integer overflow is undefined behavior. The compiler could remove the whole loop or do anything else imaginable (or not).

[-] TootSweet@lemmy.world 1 points 11 months ago

TIL!

I wonder how many languages out there do define what happens on integer overflow.

[-] BatmanAoD@programming.dev 1 points 11 months ago

Languages with dynamic typing and implicit large-integer types, such as Python and Ruby, generally just convert to that large-integer type.

I figured Java would probably define the behavior in the JVM, but based on a quick web search it sounds like it probably doesn't by default, but does provide library methods to add or subtract safely.

Rust guarantees a panic by default, but provides library methods for wrapping, saturating, and unchecked (i.e. unsafely opting back in to undefined behavior).

load more comments (3 replies)
load more comments (4 replies)
this post was submitted on 21 Oct 2023
257 points (94.8% liked)

Programmer Humor

19313 readers
280 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS