Article View: comp.arch
Article #1098Re: reassociation and undefined behaviour
From: David Brown
Date: Sat, 05 Dec 2026 12:07
Date: Sat, 05 Dec 2026 12:07
54 lines
4551 bytes
4551 bytes
On 04/12/2026 20:29, EricP wrote: > On 2026-Dec-04 13:08, Anton Ertl wrote: >> EricP <ThatWouldBeTelling@thevillage.com> writes: >>> Why is signed twos's complement wrapping mathematically absurd? >> >> It's not. The question is if the programmer wants it. The existence >> of programs that behave with modular arithmetic as intended and don't >> behave as intended with other overflow behaviour shows that there are >> cases where programmers want it. And it has led to compiler >> maintainers adding -fwrapv as a way to telling that property of a >> program to the compiler. >> >>> I'm not a mathy but is it mathematically not just modulo arithmetic >>> with a relabelling of some values? >> >> There is no relabeling, just labeling. With the modulus m, any number >> x is congruent with all numbers x+nm, for any integer n; they form a >> congruence class, which is an equivalence class; i.e., all these >> numbers are equivalent with respect to the modulus m. >> >> You can label the congruence class of all these numbers with any of >> the numbers in the congruence class. One way to label the m >> congruence classes is with 0..m-1. But you can also label them with, >> e.g., (for even m) -m/2..m/2-1. Or in any other way. >> >> - anton > > Yes. But as 0 is a special number, because you can divide any number > by any other number except 0, and as David said it was mathematically > absurd, I was wondering if there was something mathematically different > about wrapping from negative to positive symmetrically around zero > vs asymmetrically from zero to positive. My intuition said there should > be no difference but I'm not a mathy so was asking. Wrapping signed integers like this are fully defined and work as you expect. The "mathematical absurdity" is not in the algebraic construct itself - it is in the idea that it is a good model for what people normally want from integer types in a programming language. As Anton said (and I mentioned too), there are /some/ occasions where wrapping signed integer arithmetic is useful, but these are vanishingly rare and usually better handled with wrapping /unsigned/ integer types, or by some kind of checked arithmetic. > > The reason I ask is because in my hobby language design I have 4 integer > data types, signed and unsigned linear sint and uint with overflow checks, > and signed and unsigned modulo smod and umod. If there is something > wrong with the concept of an "smod" data type I wanted to know. > But it sounds like smod is mathematically ok, though perhaps morally > objectionable to some apple counters of the world. > Later I added signed and unsigned saturating ssat and usat integer types. > >
Technical Details
Message-ID:
<11f0upp$2okjb$4@dont-email.me>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11cpqih$acf5$1@dont-email.me> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> <11epd07$362ph$1@dont-email.me> <1796263648-5857@newsgrouper.org> <11ergn7$6utu$1@dont-email.me> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-5857@newsgrouper.org> <11etu0b$1aih9$1@dont-email.me> <TlCQS.44884$7vt4.19168@fx07.iad> <2026Dec4.190839@mips.complang.tuwien.ac.at> <ewEQS.416856$Lt64.284338@fx41.iad>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour Date: Sat, 5 Dec 2026 13:07:21 +0100 Organization: A noiseless patient Spider Lines: 54 Message-ID: <11f0upp$2okjb$4@dont-email.me> References: <11cpqih$acf5$1@dont-email.me> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> <11epd07$362ph$1@dont-email.me> <1796263648-5857@newsgrouper.org> <11ergn7$6utu$1@dont-email.me> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-5857@newsgrouper.org> <11etu0b$1aih9$1@dont-email.me> <TlCQS.44884$7vt4.19168@fx07.iad> <2026Dec4.190839@mips.complang.tuwien.ac.at> <ewEQS.416856$Lt64.284338@fx41.iad> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 05 Dec 2026 12:07:22 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2904683"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FLaAvgoiOtbiCXvzgEkdWArDaJxhf6Wk="; posting-host="e6744434c7b28607c9aa71bc7804445d" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:u8Wz1wFquMCZssYctDjp0enQigM= sha256:xrFsJxv4247PbcNoeD5eRZ7nKFDc5tAhV5ofptgS/VA= sha1:kPb1TlJA/noaSQv/ZtFFg+HrZ70= sha256:ZCbUCfva9w4qaNj2TI7MA01Tfae7arjjAzU77LJvKuI= In-Reply-To: <ewEQS.416856$Lt64.284338@fx41.iad> Content-Language: en-GB