4 ms·
> 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 overf
by masklinn 6y ago
> 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.
- zeotroph 6y agoSo is Swift effectively adding a jo / jump on overflow instruction after each normal integer operation?
- saeta 6y ago`+` 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.
- bregma 6y agoHuh. 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.
- twic 6y agoFor your LCG you would use an explicitly wrapping type: https://doc.rust-lang.org/std/num/struct.Wrapping.html https://doc.rust-lang.org/std/num/struct.Wrapping.html
- OskarS 6y agoIf you want the wrapping behaviour (like with an LCG), you can prepend the operator with an ampersand(i.e &+ instead of +).
- masklinn 6y agoAs hinted in my comment, Swift provides overflowing operators. You would implement your PRNG using those.
- quietbritishjim 6y agoThanks 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.)
- masklinn 6y ago> 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.
- quietbritishjim 6y agoI 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.