Deutsch   English   Français   Italiano  
<666d0f4f$0$979$882e4bbb@reader.netnews.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news-out.netnews.com!postmaster.netnews.com!us14.netnews.com!not-for-mail
X-Trace: DXC=cALT6`R?fdaRM5U<P6WHSdHWonT5<]0TmQ;nb^V>PUff=AnO\FUBY[`nF54O@^\1?d_og>=_9<RdiZXMZ9Rc<mgenk9`jfQW@>mdDioXmM8L1aOXeKkS2`?ji
X-Complaints-To: support@blocknews.net
Date: Fri, 14 Jun 2024 23:49:37 -0400
MIME-Version: 1.0
User-Agent: Betterbird (Windows)
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>
Content-Language: en-US
From: DFS <nospam@dfs.com>
In-Reply-To: <87r0cz3rx5.fsf@nosuchdomain.example.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 26
Message-ID: <666d0f4f$0$979$882e4bbb@reader.netnews.com>
NNTP-Posting-Host: 127.0.0.1
X-Trace: 1718423375 reader.netnews.com 979 127.0.0.1:40229
Bytes: 1994

On 6/14/2024 9:39 PM, Keith Thompson wrote:
> DFS <nospam@dfs.com> 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.

Thanks