| Deutsch English Français Italiano |
|
<666d7d79$0$3747335$882e4bbb@reader.netnews.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news-out.netnews.com!postmaster.netnews.com!us8.netnews.com!not-for-mail X-Trace: DXC=jO\E33ebZ7B9E:6N@`X^6NHWonT5<]0TMQ;nb^V>PUfF5[gZBW6J?LL>8J_kK>kdRID]>mPc=jiABYG5D_7nn;`HMba][>SC2CGf;]EoU=id5F=R<hY6_C9TH X-Complaints-To: support@blocknews.net Date: Sat, 15 Jun 2024 07:39:40 -0400 MIME-Version: 1.0 User-Agent: Betterbird (Windows) From: DFS <nospam@dfs.com> Subject: Re: "undefined behavior"? Newsgroups: comp.lang.c References: <666a095a$0$952$882e4bbb@reader.netnews.com> <v4d4hm$1rjc5$1@dont-email.me> <8734ph7qe5.fsf@nosuchdomain.example.com> <666a226d$0$951$882e4bbb@reader.netnews.com> <v4erpi$29e2g$2@dont-email.me> <666b0451$0$953$882e4bbb@reader.netnews.com> <v4hu1b$2ve93$3@dont-email.me> <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> <v4j7ak$3aorq$1@dont-email.me> Content-Language: en-US In-Reply-To: <v4j7ak$3aorq$1@dont-email.me> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 41 Message-ID: <666d7d79$0$3747335$882e4bbb@reader.netnews.com> NNTP-Posting-Host: 127.0.0.1 X-Trace: 1718451577 reader.netnews.com 3747335 127.0.0.1:59067 Bytes: 2806 On 6/15/2024 1:03 AM, Janis Papanagnou wrote: > On 15.06.2024 06:45, DFS wrote: >> On 6/14/2024 11:56 PM, Keith Thompson wrote: >>> DFS <nospam@dfs.com> 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? Python bytecode $ python3 -m dis file.py i = N//2 1068 LOAD_NAME 12 (N) 1070 LOAD_CONST 10 (2) 1072 BINARY_FLOOR_DIVIDE 1074 STORE_NAME 10 (i) i = int(N/2) 1068 LOAD_NAME 11 (int) 1070 LOAD_NAME 12 (N) 1072 LOAD_CONST 10 (2) 1074 BINARY_TRUE_DIVIDE 1076 CALL_FUNCTION 1 1078 STORE_NAME 10 (i) Fewer ops is better, so I'll go with your suggestion. Good catch.