News for hackers. Stuff that matters.
Re:How expensive is integer-overflow trapping in C++? by masklinn on Thursday September 24, 2020 @05:39AM (#24576907)
> Is it just about signed numbers or not?

No. You can plug an `UInt8(255)` in an online playground and try to increment it, it’ll crash unless you use an overflowing operator.

> Also, what other languages are there that are "like Swift"?

In debug mode, Rust will panic on overflow (regardless of signing). You can also enable overflow-checks in release mode.

Re:How expensive is integer-overflow trapping in C++? by bregma on Thursday September 24, 2020 @07:01AM (#24577351)
Huh. That would make classic LCG PRNGs pretty useless. I guess it's "safer" in that it prevents rare dangerous edge cases at the expense of common everyday safe operations, but that's a tradeoff.
Re:How expensive is integer-overflow trapping in C++? by twic on Thursday September 24, 2020 @07:09AM (#24577390)
For your LCG you would use an explicitly wrapping type: https://doc.rust-lang.org/std/num/struct.Wrapping.html
Re:How expensive is integer-overflow trapping in C++? by OskarS on Thursday September 24, 2020 @07:14AM (#24577429)
If you want the wrapping behaviour (like with an LCG), you can prepend the operator with an ampersand(i.e &+ instead of +).
Re:How expensive is integer-overflow trapping in C++? by masklinn on Thursday September 24, 2020 @11:07AM (#24579300)
As hinted in my comment, Swift provides overflowing operators. You would implement your PRNG using those.
Re:How expensive is integer-overflow trapping in C++? by zeotroph on Thursday September 24, 2020 @05:48AM (#24576955)
So is Swift effectively adding a jo / jump on overflow instruction after each normal integer operation?
Re:How expensive is integer-overflow trapping in C++? by saeta on Thursday September 24, 2020 @10:15AM (#24578795)
`+` is checked for overflow by default (can be overridden with compiler flags), and `&+` is overflowing addition and does auto-vectorize equivalently to C++.

I'd be quite interested in seeing a benchmark comparing checked and unchecked addition that doesn't get auto-vectorized to understand the performance in practice for non-numerical computing.

Re:How expensive is integer-overflow trapping in C++? by quietbritishjim on Thursday September 24, 2020 @07:12AM (#24577410)
Thanks for answering about unsigned numbers.

I don't believe Rust is anything like Swift. Do you think that by "like Swift" it just meant "languages where an overflow will result in the program aborting its execution"? If so then that was a pretty useless tautology. (I had assumed it meant something like "like Java" meaning any language that uses the JVM.)

Re:How expensive is integer-overflow trapping in C++? by masklinn on Thursday September 24, 2020 @11:10AM (#24579329)
> I don't believe Rust is anything like Swift. Do you think that by "like Swift" it just meant "languages where an overflow will result in the program aborting its execution"?

That's literally the sole and entire subject of the article, what else would it mean?

> If so then that was a pretty useless tautology.

The wording is odd but it's a useful classification because it's an extremely uncommon behaviour.

> I had assumed it meant something like "like Java" meaning any language that uses the JVM.

That… doesn't make any sense.

Re:How expensive is integer-overflow trapping in C++? by quietbritishjim on Thursday September 24, 2020 @05:33PM (#24583945)
I don't see what your confusion is with my confusion!

Something like "this thing is true about those languages where it's true" would be a totally worthless thing to say: it communicates zero actual information. It doesn't even say there actually is any one language (apart from Swift itself) that satisfies it. Why would the article include an empty statement like that? Even if it's conceivable that it does, surely it's reasonable for me to ask if there's an alternative interpretation that actually had some useful meaning?

> what else would it mean?

It could be that "languages like Swift" refers to a collection of related languages which are pretty obvious if you're familiar with them regardless of this overflow stuff. That is actually the more obvious interpretation to me, because saying that they all share the same overflow behaviour would be meaningful non-trivial information.

I think that's a reasonable thing to think given that "language like X" makes sense for a lot of languages. Maybe Java wasn't the best example, but if someone said "lisp-like" languages or "JavaScript-like languages" you might have an idea what they mean. As I said, I'm not familiar with Swift so for all I know "Swift-like" could make sense too. But it raises the question: what languages would that phrase include? That's why I asked.

Re:How expensive is integer-overflow trapping in C++? by quietbritishjim on Friday September 25, 2020 @04:55AM (#24587933)
After all that mumbo-jumbo I now see your point (not that you're still reading...). I was reading it literally but it was just very badly written. It can be fixed up like this to have the meaning you suggested:

> In some programming languages, like Swift, an overflow will result in the program aborting its execution.