Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Safety of casting from 'long' to 'int' Date: Wed, 13 May 2026 11:32:53 -0700 Organization: A noiseless patient Spider Lines: 28 Message-ID: <86qznfjh2i.fsf@linuxsc.com> References: <10su8cn$am9i$1@dont-email.me> <10tigio$1l93l$2@dont-email.me> <10tj2hk$bbf$1@reader1.panix.com> <10tk4sg$2l19a$2@dont-email.me> <10tloub$32n$1@reader1.panix.com> <10tlvaa$1l93l$16@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Wed, 13 May 2026 18:32:54 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3026765"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sqwhYvTQNGK5WR5DWg4Ya8yR0ppyGrMc="; posting-host="ace8b6eaa328dcf44f89a3207699c4cb" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:/kQQArg6OIX8+LDrSZjFF83cZ1Y= sha1:X1f3C+ddDyyoi4Jc56rAkIPfqV8= sha256:PNKgeJXh8VCrNuQ/fgifnyAhe3vTi4i+mp/6UF8h9iI= sha1:mZSXRdskiL2RGp8YDcteyqQtJ4w= Xref: csiph.com comp.lang.c:398893 Janis Papanagnou writes: > On 2026-05-09 00:43, Dan Cross wrote: > >>> [...] >> >> Sure. This was a bit of a contrived example, but you ask a good >> question: how often might one want write code like that? >> >> In short, I don't know, but I can think of any number of hash >> functions, checksums, etc, that may be implemented using 16-bit >> arithmetic, and I can well see programmers wanting to take >> advantage of the modular semantics afforded by using unsigned >> types to do so. Every day? Probably not. But often enough. > > I mentioned it before but it may have got lost in the lots text > typically exchanged here; for hash functions a modulus based on > powers of two has *bad* _distribution properties_, so it's not > a sensible example or plausible rationale to vindicate modular > arithmetic for the few special cases (m=8, 16, 32, 64, etc.). To me this sounds like folklore. A well-designed hash function (and it isn't hard to write one or find one) will have good distribution properties with any modulus. In many cases there are good reasons to prefer a power-of-two modulus. So I think the right takeaway here is make sure you have a good hash function, and don't settle for some ad hoc thing just thrown together.