Path: ...!feed.opticnetworks.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Sat, 15 Jun 2024 09:37:06 +0100 Organization: A noiseless patient Spider Lines: 58 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; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 15 Jun 2024 10:37:07 +0200 (CEST) Injection-Info: dont-email.me; posting-host="70a9fc796b84cb08329413872ec51cfa"; logging-data="3564154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+W9qmWk/m0mD2R1/YppcRJ" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:D94YijDuZRsrOFgLizEnBlt9zQ0= In-Reply-To: <666d1c61$0$953$882e4bbb@reader.netnews.com> Content-Language: en-GB Bytes: 3452 On 15/06/2024 05:45, DFS wrote: > On 6/14/2024 11:56 PM, Keith Thompson wrote: >> DFS writes: >>> On 6/14/2024 9:39 PM, Keith Thompson wrote: >>>> DFS writes: >>>> [...] >>>>> During conversion, I got a Python error I don't remember seeing in >>>>> the past: >>>>> >>>>> "TypeError: list indices must be integers or slices, not float" >>>>> >>>>> because division returns a float, and some of the array addressing was >>>>> like this: nums[i/2]. >>>> [...] >>>> C's "/" operator yields a result with the type of the operands >>>> (after >>>> promotion to a common type). >>>> Python's "/" operator yields a floating-point result.  For C-style >>>> integer division, Python uses "//".  (Python 2 is more C-like.) >>> >>> I was surprised python did that, since every division used in the >>> array addressing results in an integer. >>> >>> 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) > > > >> But if you want help with your Python code, comp.lang.python is the >> place to ask. > > Thanks for your help, but David Brown is a Python developer and I'll ask > him python questions here whenever I care to. Yeah do that. Set up a private corner of comp.lang.c where David Brown has a sideline answering questions about Python from only one poster. Nobody else is allowed to answer. Sounds ridiculous, yes? > In the recent past you were involved in discussions on perl, Fortran and > awk, among other off-topics. > > Rules for thee but not for me? >