Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Zero-based numbering

From Wikipedia, the free encyclopedia
Counting from "0" instead of "1" first

Zero-based numbering is a way ofnumbering in which the initial element of asequence is assigned theindex 0, rather than the index 1 as is typical in everyday non-mathematical or non-programming circumstances. Under zero-based numbering, the initial element is sometimes termed thezeroth element,[1] rather than thefirst element;zeroth is acoined word for theordinal number zero. In some cases, an object or value that does not (originally) belong to a given sequence, but which could be naturally placed before its initial element, may be termed the zeroth element. There is no wide agreement regarding the correctness of using zero as an ordinal (nor regarding the use of the termzeroth), as it creates ambiguity for all subsequent elements of the sequence when lacking context.

Numbering sequences starting at 0 is quite common in mathematics notation, in particular incombinatorics, though programming languages for mathematics usually index from 1.[2][3][4] Incomputer science,array indices usually start at 0 in modern programming languages, so computer programmers might usezeroth in situations where others might usefirst, and so forth. In some mathematical contexts, zero-based numbering can be used without confusion, when ordinal forms have well established meaning with an obvious candidate to come beforefirst; for instance, azeroth derivative of a function is the function itself, obtained bydifferentiating zero times. Such usage corresponds to naming an element not properly belonging to the sequence but preceding it: the zeroth derivative is not really a derivative at all. However, just as thefirst derivative precedes thesecond derivative, so also does thezeroth derivative (or the original function itself) precede thefirst derivative.

Computer programming

[edit]

Origin

[edit]

Martin Richards, creator of theBCPL language (a precursor ofC), designed arrays initiating at 0 as the natural position to start accessing the array contents in the language, since the value of apointerp used as an address accesses the positionp + 0 in memory.[5][6] BCPL was first compiled for theIBM 7094; the language introduced norun-timeindirection lookups, so the indirection optimization provided by these arrays was done at compile time.[6] The optimization was nevertheless important.[6][7]

In 1982Edsger W. Dijkstra in his pertinent noteWhy numbering should start at zero[8] argued that arrays subscripts should start at zero as that's the mostnatural number. Discussing possible designs of array ranges by enclosing them in a chained inequality, combining sharp and standard inequalities to four possibilities, demonstrating that to his conviction zero-based arrays are best represented by non-overlapping index ranges, which start at zero, alluding toopen, half-open and closed intervals as with the real numbers. Dijkstra's criteria for preferring this convention are in detail that it represents empty sequences in a more natural way(ai <a ?) than closed "intervals" (ai ≤ (a − 1) ?), and that with half-open "intervals" of naturals, the length of a sub-sequence equals the upper minus the lower bound (ai <b gives(ba) possible values fori, witha,b,i all integers).

Usage in programming languages

[edit]
See also:Comparison of programming languages (array) § Array dimensions

This usage follows from design choices embedded in many influentialprogramming languages, includingC,Java, andLisp. In these three, sequence types (C arrays, Java arrays and lists, and Lisp lists and vectors) are indexed beginning with the zero subscript. Particularly in C, where arrays are closely tied topointer arithmetic, this makes for a simpler implementation: the subscript refers to an offset from the starting position of an array, so the first element has an offset of zero.

Referencing memory by an address and an offset is represented directly incomputer hardware on virtually all computer architectures, so this design detail in C makes compilation easier, at the cost of some human factors. In this context using "zeroth" as an ordinal is not strictly correct, but a widespread habit in this profession. Some programming languages, such asFortran orCOBOL, have array subscripts starting with one, because they were meant ashigh-level programming languages, and as such they had to have a correspondence to the usualordinal numbers which predate theinvention of the zero by a long time. And some programming languages, e.g.,Ada,ALGOL 60,PL/I, allow an arbitrary lower bound for each index.

Pascal allows the range of an array to be of any ordinal type (including enumerated types) and Ada allows any discrete subtype.APL allows setting the index origin to 0 or 1 during runtime programmatically.[9][10] Some recent languages, such asLua andVisual Basic, have adopted the same convention for the same reason.

Zero is the lowest unsigned integer value, one of the most fundamental types in programming and hardware design. In computer science,zero is thus often used as the base case for many kinds of numericalrecursion. Proofs and other sorts of mathematical reasoning in computer science often begin with zero. For these reasons, in computer science it is not unusual to number from zero rather than one.

If an array is used to represent a cycle, it is convenient to obtain the index with amodulo function, which can result in zero.

Numerical properties

[edit]

With zero-based numbering, a range can be expressed as the half-openinterval,[0,n), as opposed to the closed interval,[1,n]. Empty ranges, which often occur in algorithms, are tricky to express with a closed interval without resorting to obtuse conventions like[1, 0]. Because of this property, zero-based indexing potentially reducesoff-by-one andfencepost errors.[8] On the other hand, the repeat countn is calculated in advance, making the use of counting from 0 ton − 1 (inclusive) less intuitive. Some authors prefer one-based indexing, as it corresponds more closely to how entities are indexed in other contexts.[11]

Another property of this convention is in the use ofmodular arithmetic as implemented in modern computers. Usually, themodulo function maps any integer moduloN to one of the numbers0, 1, 2, ...,N − 1, whereN ≥ 1. Because of this, many formulas in algorithms (such as that for calculating hash table indices) can be elegantly expressed in code using the modulo operation when array indices start at zero.

Pointer operations can also be expressed more elegantly on a zero-based index due to the underlying address/offset logic mentioned above. To illustrate, supposea is thememory address of the first element of an array, andi is the index of the desired element. To compute the address of the desired element, if the index numbers count from 1, the desired address is computed by this expression:

a+s×(i1),{\displaystyle a+s\times (i-1),}

wheres is the size of each element. In contrast, if the index numbers count from 0, the expression becomes

a+s×i.{\displaystyle a+s\times i.}

This simpler expression is more efficient to compute atrun time.

However, a language wishing to index arrays from 1 could adopt the convention that every array address is represented bya′ =as; that is, rather than using the address of the first array element, such a language would use the address of a fictitious element located immediately before the first actual element. The indexing expression for a 1-based index would then be

a+s×i.{\displaystyle a'+s\times i.}

Hence, the efficiency benefit at run time of zero-based indexing is not inherent, but is an artifact of the decision to represent an array with the address of its first element rather than the address of the fictitious zeroth element. However, the address of that fictitious element could very well be the address of some other item in memory not related to the array.

Superficially, the fictitious element doesn't scale well to multidimensional arrays. Indexing multidimensional arrays from zero makes a naive (contiguous) conversion to a linear address space (systematically varying one index after the other) look simpler than when indexing from one. For instance, when mapping the three-dimensional arrayA[P][N][M] to a linear arrayL[M⋅N⋅P], both withM ⋅ N ⋅ P elements, the indexr in the linear array to access a specific element withL[r] = A[z][y][x] in zero-based indexing, i.e.[0 ≤x <P],[0 ≤y <N],[0 ≤z <M], and[0 ≤r <M ⋅ N ⋅ P], is calculated by

r=zMN+yM+x.{\displaystyle r=z\cdot M\cdot N+y\cdot M+x.}

Organizing all arrays with 1-based indices ([1 ≤x′P],[1 ≤y′N],[1 ≤z′M],[1 ≤r′M ⋅ N ⋅ P]), and assuming an analogous arrangement of the elements, gives

r=(z1)MN+(y1)M+(x0){\displaystyle r'=(z'-1)\cdot M\cdot N+(y'-1)\cdot M+(x'-0)}

to access the same element, which arguably looks more complicated. Of course,r′ =r + 1, since[z =z′ – 1],[y =y′ – 1], and[x =x′ – 1]. A simple and everyday-life example ispositional notation, which the invention of the zero made possible. In positional notation, tens, hundreds, thousands and all other digits start with zero, only units start at one.[12]

This situation can lead to some confusion in terminology. In a zero-based indexing scheme, the first element is "element number zero"; likewise, the twelfth element is "element number eleven". Therefore, an analogy from the ordinal numbers to the quantity of objects numbered appears; the highest index ofn objects will ben − 1, and it refers to thenth element. For this reason, the first element is sometimes referred to as thezeroth element, in an attempt to avoid confusion.

Science

[edit]

Inmathematics, many sequences of numbers or ofpolynomials are indexed by nonnegative integers, for example, theBernoulli numbers and theBell numbers.

In bothmechanics andstatistics, the zerothmoment is defined, representing total mass in the case of physicaldensity, or total probability, i.e. one, for aprobability distribution.

Thezeroth law of thermodynamics was formulated after the first, second, and third laws, but considered more fundamental, thus its name.

In biology, an organism is said to have zero-order intentionality if it shows "no intention of anything at all". This would include a situation where the organism's genetically predetermined phenotype results in a fitness benefit to itself, because it did not "intend" to express its genes.[13] In the similar sense, a computer may be considered from this perspective a zero-order intentional entity, as it does not "intend" to express the code of the programs it runs.[14]

In biological or medical experiments, the first day of an experiment is often numbered as day 0.[15]

Patient zero (orindex case) is the initialpatient in thepopulation sample of anepidemiological investigation.

Other fields

[edit]

Theyear zero does not exist in the widely usedGregorian calendar or in its predecessor, theJulian calendar. Under those systems, the year1 BC is followed byAD 1. However, there is a year zero inastronomical year numbering (where it coincides with the Julian year 1 BC) and inISO 8601:2004 (where it coincides with the Gregorian year 1 BC), as well as in allBuddhist andHindu calendars.

In many countries, theground floor in buildings is considered as floor number 0 rather than as the "1st floor", the naming convention usually found in the United States of America. This makes a consistent set with underground floors marked with negative numbers.

While the ordinal of 0 mostly finds use in communities directly connected to mathematics, physics, and computer science, there are also instances in classical music. The composerAnton Bruckner regarded his earlySymphony in D minor to be unworthy of including in the canon of his works, and he wrotegilt nicht ("doesn't count") on the score and a circle with a crossbar, intending it to mean "invalid". But posthumously, this work came to be known asSymphony No. 0 in D minor, even though it was actually written afterSymphony No. 1 in C minor. There is an even earlierSymphony in F minor of Bruckner's, which is sometimes calledNo. 00. The Russian composerAlfred Schnittke also wrote aSymphony No. 0.

In some universities, including Oxford and Cambridge, "week 0" or occasionally "noughth week" refers to the week before the first week of lectures in a term. In Australia, some universities refer to this as "O week", which serves as a pun on "orientation week". As a parallel, the introductory weeks at university educations inSweden are generally callednollning (zeroing).

TheUnited States Air Force starts basic training each Wednesday, and the first week (of eight) is considered to begin with the following Sunday. The four days before that Sunday are often referred to as "zero week".

24-hour clocks and the international standardISO 8601 use 0 to denote the first (zeroth) hour of the day, consistent with using the 0 to denote the first (zeroth) minute of the hour and the first (zeroth) second of the minute. Also, the12-hour clocks used inJapan use 0 to denote the hour immediately after midnight and noon in contrast to 12 used elsewhere, in order to avoid confusionwhether 12 a.m. and 12 p.m. represent noon or midnight.

Robert Crumb's drawings for the first issue ofZap Comix were stolen, so he drew a whole new issue, which was published as issue 1. Later he re-inked his photocopies of the stolen artwork and published it as issue 0.

TheBrussels ring road in Belgium is numbered R0. It was built after the ring road aroundAntwerp, but Brussels (being the capital city) was deemed deserving of a more basic number. Similarly the (unfinished) orbital motorway aroundBudapest in Hungary is calledM0.

Zero is sometimes usedin street addresses, especially in schemes where even numbers are one side of the street and odd numbers on the other. A case in point isChrist Church onHarvard Square, whose address is 0 Garden Street.

Formerly inFormula One, when a defending world champion did not compete in the following season, the number 1 was not assigned to any driver, but one driver of the world champion team would carry the number 0, and the other, number 2. This did happen both in 1993 and 1994 withDamon Hill carrying the number 0 in both seasons, as defending championNigel Mansell quit after 1992, and defending championAlain Prost quit after 1993. However, in 2014 the series moved to drivers carrying career-long personalised numbers, instead of team-allocated numbers, other than the defending champion still having the option to carry number 1. Therefore 0 is no longer used in this scenario. It is not clear if it is available as a driver's chosen number, or whether they must be between 2 and 99, but it has not been used to date under this system.

Some team sports allow 0 to be chosen as a player'suniform number (in addition to the typical range of 1-99). The NFL voted to allow this from 2023 onwards.

A chronological prequel of a series may be numbered as 0, such asRing 0: Birthday orZork Zero.

TheSwiss Federal Railways number certain classes of rolling stock from zero, for example,Re 460 000 to 118.

In the realm of fiction,Isaac Asimov eventually added a Zeroth Law to hisThree Laws of Robotics, essentially making them four laws.

A standardroulette wheel contains the number 0 as well as 1-36. It appears in green, so is classed as neither a "red" nor "black" number for betting purposes. The card gameUno has number cards running from 0 to 9 along with special cards, within each coloured suit.

TheFour Essential Freedoms of Free Software are numbered starting from zero. This is for historical reasons: the list originally had only three freedoms, and when the fourth was added it was placed in the zeroth position as it was considered more basic.

See also

[edit]

References

[edit]

Citations

[edit]
  1. ^M. Seed, Graham (1965).An Introduction to Object-Oriented Programming in C++ with Applications in Computer Graphics (2nd ed.). British Library: Springer. p. 391.ISBN 1852334509. Retrieved11 February 2020.
  2. ^Steve Eddins and Loren Shure."Matrix Indexing in MATLAB". Retrieved23 February 2021.
  3. ^"How to : Get Elements of Lists". Wolfram. Retrieved23 February 2021.
  4. ^"Indexing Arrays, Matrices, and Vectors". Maplesoft. Retrieved23 February 2021.
  5. ^Martin Richards (1967).The BCPL Reference Manual(PDF). Massachusetts Institute of Technology. p. 11. Archived fromthe original(PDF) on 2013-01-20. Retrieved2014-01-28.
  6. ^abcMike Hoye."Citation Needed". Retrieved28 January 2014.
  7. ^Tom Van Vleck (1995)."The IBM 7094 and CTSS". Retrieved28 January 2014.
  8. ^abDijkstra, Edsger Wybe (May 2, 2008)."Why numbering should start at zero (EWD 831)".E. W. Dijkstra Archive.University of Texas at Austin. Retrieved2011-03-16.
  9. ^Brown, Jim (December 1978). "In Defense of Index Origin 0".ACM SIGAPL APL Quote Quad.9 (2): 7.doi:10.1145/586050.586053.S2CID 40187000.
  10. ^Hui, Roger."Is Index Origin 0 a Hindrance?".jsoftware.com. JSoftware. Retrieved19 January 2015.
  11. ^Programming Microsoft® Visual C# 2005 by Donis Marshall.
  12. ^Sal Khan.Math 1st Grade / Place Value / Number grid. Khan Academy. RetrievedJuly 28, 2018.Youtube title: Number grid / Counting / Early Math / Khan Academy.
  13. ^Byrne, Richard W."The Thinking Ape: Evolutionary Origins of Intelligence". Archived from the original on January 22, 2013. Retrieved2010-05-18.
  14. ^Dunbar, Robin."The Human Story – A new history of mankind's Evolution". Archived fromthe original on 2010-08-27. Retrieved2010-05-18.
  15. ^Towle, Albert (1989).Modern Biology. Holt McDougal. p. 35.ISBN 9780030139277.

Sources

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Zero-based_numbering&oldid=1324529296"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp