Path: ...!feed.opticnetworks.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Sat, 15 Jun 2024 07:03:16 +0200 Organization: A noiseless patient Spider Lines: 23 Message-ID: References: <666a095a$0$952$882e4bbb@reader.netnews.com> <8734ph7qe5.fsf@nosuchdomain.example.com> <666a226d$0$951$882e4bbb@reader.netnews.com> <666b0451$0$953$882e4bbb@reader.netnews.com> <666ccccb$0$973$882e4bbb@reader.netnews.com> <87r0cz3rx5.fsf@nosuchdomain.example.com> <666d0f4f$0$979$882e4bbb@reader.netnews.com> <87msnm505i.fsf@nosuchdomain.example.com> <666d1c61$0$953$882e4bbb@reader.netnews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Sat, 15 Jun 2024 07:03:16 +0200 (CEST) Injection-Info: dont-email.me; posting-host="daa1712469358176e82fff82b0d57740"; logging-data="3498874"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+o4ojPQflNRuWACSC2Opi/" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:cR1iiTnd2v673WciChT0tUS2cjg= X-Enigmail-Draft-Status: N1110 In-Reply-To: <666d1c61$0$953$882e4bbb@reader.netnews.com> Bytes: 2413 On 15.06.2024 06:45, DFS wrote: > On 6/14/2024 11:56 PM, Keith Thompson wrote: >> DFS writes: >>> >>> After casting i to an int before any array addressing, // works. >> >> I'm surprised you needed to convert i to an int. I would think that >> just replacing nums[i/2] by nums[i//2] would do the trick, >> as long as i always has an int value (note Python's dynamic typing). >> If i is acquiring a float value, that's probably a bug, given the name. > > I spotted the issue. Just prior to using i for array addressing I said: > i = N/2. > > The fix is set i = int(N/2) Given what Keith suggested, and assuming N is an integer, wouldn't it be more sensible to use the int division operator '//' and just write i = N // 2 ? I mean, why do a float division on integer operands and then again coerce the result to int again? Janis