TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Numbering should start at zero (1982)

107 点作者 checkyoursudo大约 2 个月前

38 条评论

nivertech大约 2 个月前
<p><pre><code> Numbering should start at π (2025) (umars.edu) </code></pre> Seriously, it all depends on whether u&#x27;re counting the items themselves (1-based) or the spaces btwn them (0-based). The former uses natural numbers, while the latter uses non-negative integers<p>For instance, when dealing with memory words, do u address the word itself or its starting location (the first byte)?<p>The same consideration applies to coordinate systems: r u positioning urself at the center of a pixel or at the pixel&#x27;s origin?<p>Almost 2 decades ago I designed an hierarchical IDs system (similar to OIDs[1]). First I made it 0-based for each index in the path. After a while I understood that I need to be able to represent invalid&#x2F;dummy IDs also, so I used -1 for that. It was ugly - so I made it 1-based, &amp; any 0 index would&#x27;ve made the entire ID - invalid<p>---<p>1. <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Object_identifier" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Object_identifier</a>
评论 #43436332 未加载
评论 #43439718 未加载
评论 #43435969 未加载
评论 #43437687 未加载
评论 #43440443 未加载
评论 #43436340 未加载
评论 #43436435 未加载
评论 #43436091 未加载
评论 #43436651 未加载
pansa2大约 2 个月前
&quot;Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.&quot; — Stan Kelly-Bootle
评论 #43437299 未加载
评论 #43436857 未加载
评论 #43436843 未加载
Aransentin大约 2 个月前
Perhaps ideally we&#x27;d change English to count the &quot;first&quot; entry in a sequence as the &quot;zeroth&quot; item, but the path dependency and the effort required to do that is rather large to say the least.<p>At least we&#x27;re not stuck with the Roman &quot;inclusive counting&quot; system that included one extra number in ranges* so that e.g. weeks have &quot;8&quot; days and Sunday is two days before Monday since Monday is itself included in the count.<p>* <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Counting#Inclusive_counting" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Counting#Inclusive_counting</a>
评论 #43436316 未加载
评论 #43436361 未加载
评论 #43440366 未加载
评论 #43445249 未加载
评论 #43439584 未加载
评论 #43438143 未加载
评论 #43440129 未加载
roenxi大约 2 个月前
The 1980s were not a particularly enlightened time for programming language design; and Dijkstra&#x27;s opinions seem to carry extra weight mainly because his name has a certain shock and awe factor.<p>It isn&#x27;t usual for me to agree with the mathematical convention for notations, but the 1st element of a sequence being denoted with a &quot;1&quot; just seems obviously superior. I&#x27;m sure there is a culture that counts their first finger as 0 and I expect they&#x27;re mocked mercilessly for it by all their neighbours. I&#x27;ve been programming for too long to appreciate it myself, but always assumed it traces back to memory offsets in an array rather than any principled stance because 0-counting sequences represents a crazy choice.
评论 #43434869 未加载
评论 #43435025 未加载
评论 #43436879 未加载
评论 #43436632 未加载
评论 #43435246 未加载
评论 #43434737 未加载
评论 #43435016 未加载
silotis大约 2 个月前
Pretty much any algorithm that involves mul&#x2F;div&#x2F;mod operations on array indexes will naturally use 0-based indexes (i.e. if using 1-based indexes they will have to be converted to&#x2F;from 0-based to make the math work).<p>To me this is a far more compelling argument for 0-based indexes than anything I&#x27;ve seen in favor of 1-based indexes.
评论 #43436909 未加载
arnsholt大约 2 个月前
Both are fine, IMO. In a context where array indexing is pointer plus offset, zero indexing makes a lot of sense, but in a higher level language either is fine. I worked in SmallTalk for a while, which is one indexed, and sometimes it made things easier and sometimes it was a bit inconvenient. It evens out in the end. Besides, in a high level language, manually futzing around with indexing is frequently a code smell; I feel you generally want to use higher level constructs in most cases.
noneeeed大约 2 个月前
I&#x27;ve always appreciated Ada&#x27;s approach to arrays. You can create array types and specify both the type of the values and of the index. If zero based makes sense for your use, use that, if something else makes sense use that.<p>e.g.<p><pre><code> type Index is range 1 .. 5; type My_Int_Array is array (Index) of My_Int; </code></pre> It made life pretty nice when working in SPARK if you defined suitable range types for indexes. The proof steps were generally much easier and frequently automatically handled.
评论 #43439783 未加载
评论 #43439558 未加载
评论 #43438845 未加载
评论 #43438986 未加载
myfonj大约 2 个月前
I found it devastating that there are no distinct agreed-upon words denoting zero- and one-based addressing. Initially I thought that the word &quot;index&quot; clearly denotes zero-base, and for one-base there is &quot;order&quot;, &quot;position&quot;, &quot;rank&quot; or some other word, but after rather painful and humiliating research I stood corrected. (&quot;Index&quot; is really used in both meanings, and without prior knowledge of the context, there is really no way to tell what base it refers to.)<p>So to be clear, we have to tediously specify &quot;z e r o - b a s e d &quot; or &quot;o n e - b a s e d&quot; every single time to avoid confusion. (Is there a chance for creating some new, terser consensus here?)
评论 #43435051 未加载
评论 #43434861 未加载
评论 #43435030 未加载
评论 #43434892 未加载
评论 #43436618 未加载
评论 #43435387 未加载
评论 #43439804 未加载
评论 #43442293 未加载
ufo大约 2 个月前
This article is one my pet peeves. It always shows up in discussions as &quot;proof&quot; that 0 indexing is superior, but it hides under the carpet all the cases the where it is not. For instance, backwards iteration needs a &quot;-1&quot; and breaks with unsigned ints.<p><pre><code> for (i=N-1; i&gt;=0; i--) </code></pre> I like the argument that 0-based is better for offsets and 1-based is better for indexes: <a href="https:&#x2F;&#x2F;hisham.hm&#x2F;2021&#x2F;01&#x2F;18&#x2F;again-on-0-based-vs-1-based-indexing&#x2F;" rel="nofollow">https:&#x2F;&#x2F;hisham.hm&#x2F;2021&#x2F;01&#x2F;18&#x2F;again-on-0-based-vs-1-based-ind...</a>
评论 #43448005 未加载
norir大约 2 个月前
Always beware the word should. I agree with Dijkstra&#x27;s logic in the context that he presents it, but there are other contexts where I don&#x27;t think it applies.<p>Personally, I find that in compiler writing, which is the only programming I do these days, the only things I use indexes for are line numbers and character offsets into strings. Calling the first character the zeroth character is ridiculous to me, so I just store a leading 0 byte in all strings and then can use one based indexing with no performance hit. Alternatively, since I am the compiler writer, I could just internally store the pointer to the string - 1 to avoid the &lt; 1 byte per string average overhead (I also machine word align strings so often the leading zero doesn&#x27;t affect the string size in memory).<p>If you are often directly working with array indices, you are likely doing low level programming. It is worth asking if the task at hand requires that, or if you would be better off using higher level constructs and&#x2F;or a higher level language. Low level details ideally should not leak into the application level.
评论 #43438451 未加载
krukah大约 2 个月前
Whenever I explain to someone when or why to use 0-indexing, I like to say:<p>Start from 0 if you are counting <i>boundaries</i> (fenceposts, memory addresses)<p>Start from 1 if you are counting <i>spaces</i> (pages in a book, ordinals)<p>Floors are a case where both make intuitive sense, which is maybe how we ended up with European vs American floor numbering.
评论 #43437158 未加载
yazantapuz大约 2 个月前
I like my numbering to start like my tape measure: at zero.
评论 #43436144 未加载
评论 #43440542 未加载
评论 #43443716 未加载
umanwizard大约 2 个月前
1-based numbering is nonsense. How many years old are you when you’re born?<p>I notice almost all defenses of 1-based indexing are purely based on arbitrary cultural factors or historical conventions, e.g. “that’s how it’s done in math”, rather than logical arguments.
评论 #43435949 未加载
评论 #43436666 未加载
评论 #43436016 未加载
评论 #43436118 未加载
评论 #43436291 未加载
评论 #43440560 未加载
sunflowerfly大约 2 个月前
We taught our toddler to count from zero. Their kindergarten teacher was not amused.
zkmon大约 2 个月前
The question arises when people get confused between a cut and span. These two are opposite concepts, and they make up a continuum, and they define each other.<p>So, it depends on what you understand as &quot;numbering&quot;. If it is about counting objects, the word &quot;first object&quot; refers to existence of non-zero number of objects. This shows why the first one can&#x27;t be called as zero, as zero is not equal to non-zero.<p>If the numbering is about continuous scale such as tape measure, then the graduations can start with zero. But still the first meter refers to one meter, not zero meters.<p>It looks silly when people have their book chapters numbering to begin with zero. They have no clue whether the chapter refers to a span or a cut. Sure, they can call the top surface of their book cover as zero, though. But still they can&#x27;t number a page as zero.<p>The use of zero index for memory location comes from possible magnetic states of array of bits. Each such state is a kind of a cut, not a span. It&#x27;s like a graduation on the tape measure, or mile stone on the side of the road. So it can start with zero.<p>So, if you are counting markers or separators, with zero magnitude, you can start at zero. And when you count spans or things of non-zero magnitude, you start at one. If you count apples, start at one. If you count spaces between apples start at zero.
calibas大约 2 个月前
I see people bringing up arrays, and an array index is represented by a number, you can do math on it, but it&#x27;s not a regular number for counting a sequence of items. It&#x27;s a unique reference to a location in the memory, and it&#x27;s dangerous to treat an array index like it&#x27;s just any old number.<p>Behold, the really stupid things you can do in Javascript:<p><pre><code> let myArr = []; let index = 0; myArr[--index] = 5; console.log(myArr.length); &#x2F;&#x2F; 0 console.log(myArr[index]); &#x2F;&#x2F; 5</code></pre>
评论 #43437121 未加载
barotalomey大约 2 个月前
1 or 0-based index...<p>I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write correct lua code on the first try.<p>I suppose 1-based index is more logical, but decades of programming languages choosing 0-based index is hard to ignore.
评论 #43435266 未加载
评论 #43435718 未加载
评论 #43434975 未加载
评论 #43434640 未加载
评论 #43434969 未加载
评论 #43435077 未加载
评论 #43435602 未加载
nikolayasdf123大约 2 个月前
sets is the most intuitive reason for me<p>[0,1,2) + [2,3,4) = [0,1,2,3,4)<p>meanwhile<p>[0,1,2] + [2,3,4] = [0,1,2,2,3,4] — this double counting is just ugly
bazoom42大约 2 个月前
It is easy to miss that his argument boils down to that zero-based is “nicer” in a specific select case. The paper is written in the style of a mathematical proof but hinges on a completely subjective opinion.
phkahler大约 2 个月前
&gt;&gt; when starting with subscript 1, the subscript range 1 ≤ i &lt; N+1; starting with 0, however, gives the nicer range 0 ≤ i &lt; N.<p>What about the range 0 &lt; i ≤ N which starts with 1? Why only use ≤ on the lower end of the range? This zero-based vs one-based tends to come up in programming and mathematics, and both are used in both areas. Isn&#x27;t it obvious that there is no universally correct way to index things?
评论 #43440921 未加载
jmount大约 2 个月前
I love the uncertain history of both 0 and 1: <a href="https:&#x2F;&#x2F;win-vector.com&#x2F;2020&#x2F;09&#x2F;18&#x2F;clearly-the-author-does-not-know-what-the-natural-numbers-are&#x2F;" rel="nofollow">https:&#x2F;&#x2F;win-vector.com&#x2F;2020&#x2F;09&#x2F;18&#x2F;clearly-the-author-does-no...</a>
sim7c00大约 2 个月前
love this :D<p>&quot;The above has been triggered by a recent incident, when, in an emotional outburst, one of my mathematical colleagues at the University —not a computing scientist— accused a number of younger computing scientists of &quot;pedantry&quot; because —as they do by habit— they started numbering at zero. &quot;<p>:&#x27;)
AndrewSwift大约 2 个月前
In France, street-level is the 0th floor, and the one above is the first floor.<p>You see zero in elevators all the time.
评论 #43438019 未加载
tsoukase大约 2 个月前
Zero means nothing (not that it has no importance :-) but that it symbolises the void). So the symbol 0 could be also a single space or any other predetermined. So, it is not a number and should not be used like one (pun intended)
effdee大约 2 个月前
Let&#x27;s also add a link to the handwritten version for good taste:<p><a href="https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;~EWD&#x2F;ewd08xx&#x2F;EWD831.PDF" rel="nofollow">https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;~EWD&#x2F;ewd08xx&#x2F;EWD831.PDF</a>
tim333大约 2 个月前
Perhaps we can extend this to everyday language? Taylor Swift had a number zero hit, ones company, two&#x27;s a crowd, I won the race and came in at number zero and so on?
1970-01-01大约 2 个月前
Numbers are a joke. I count from A-Z and then Aa Ab Ac ...<p>I have Ar apples for sale.<p>Only $A.J each!
kibwen大约 2 个月前
I appreciate Dijkstra&#x27;s arguments, but the fact remains that no non-technical user is ever going to jibe with a zero-indexed system, no matter the technical merits.<p>Languages aimed at casual audiences (e.g. scripting languages like Lua) should maybe just provide two different ways of indexing into arrays: an `offset` method that&#x27;s zero-indexed, and an `item` method that&#x27;s one-indexed. Let users pick, in a way that&#x27;s mildly less confusing than languages that let you override the behavior of the indexing operator (an operator which really doesn&#x27;t particularly need to exist in a world where iterators are commonplace).
评论 #43435654 未加载
评论 #43435644 未加载
anigbrowl大约 2 个月前
&#x27;start counting on your zeroth finger&#x27; &#x27;can I have none apples please&#x27;<p>- statements dreamed up by the utterly deranged.<p><i>They have played us for absolute fools</i>.
djmips大约 2 个月前
Where I live and maybe where you live our ages are zero based although no one seems to like me calling their baby zero years old.
评论 #43440947 未加载
pmarreck大约 2 个月前
If it&#x27;s about collections of things, use collection iterators (`each`, etc.) and avoid the problem entirely.
frhack大约 2 个月前
Starting from zero saves memory. If I have a variable used as an index for an array of 256 elements, starting from 0 allows me to store it in a single byte. If I start from 1, I need two bytes, effectively doubling the memory usage—an unnecessary 100% increase. Now, multiply this inefficiency across every instance where programs encounter a similar situation.
评论 #43437777 未加载
golol大约 2 个月前
Consider n real numbers a_0, ..., a_{n-1}. That&#x27;s not very elegant.
评论 #43436700 未加载
评论 #43444418 未加载
评论 #43436216 未加载
geenat大约 2 个月前
Hard ask considering there&#x27;s effectively 2 Americas: only the scientific one using scaleable units like mg&#x2F;g&#x2F;kg, cm&#x2F;m&#x2F;km- everyone else using randomized trash... ft, mile, yard, inch, pound....
neves大约 2 个月前
I&#x27;ll upvote this Djisktra note every time it appears. :-)<p>It settles the discussion of array numbering. F*ck off Visual Basic, MS Javascript, and all the languages that said you should start with 1.
评论 #43440611 未加载
ks2048大约 2 个月前
Swift uses 0-based indexes, but offers, IMHO, a nice choice for specifying ranges,<p><pre><code> Array(1...5) == [1,2,3,4,5] Array(1..&lt;5) == [1,2,3,4]</code></pre>
shrubble大约 2 个月前
Explains why he didn&#x27;t like APL...
评论 #43440640 未加载
BeetleB大约 2 个月前
I know I&#x27;ll get downvoted to Hell for this, but I have a mental list of traits poor programmer&#x27;s have, and one of them is &quot;Excessively complains about 1 based indexing&quot;.