Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

I prefer my indexes to start at:

by Arunbear (Prior)
on Sep 01, 2022 at 09:56 UTC ( [id://11146600]=poll: print w/replies, xml ) Need Help??

Vote on this poll

0
[bar] 115/89%
1
[bar] 9/7%
Other value
[bar] 5/4%
129 total votes
Replies are listed 'Best First'.
Re: I prefer my indexes to start at:
by Tux (Canon) on Sep 01, 2022 at 14:50 UTC

    0 unless spreadsheets. Using 0 in any spreadsheet will confuse the hell out of any programmer dealing with Excel or LibreOffice where everything starts with 1.

    That said, 0 is thusly available for metadata :). You can accuse me of misuse or even abuse of 0 in these evil data structures.


    Enjoy, Have FUN! H.Merijn
      > 0 unless spreadsheets.

      I think you can make the same exception for SQL tables, since ROW_NUMBER() starts with 1.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      Since sheet row 2 is usually the first row of data in a spreadsheet, thinking of the header as data row(0) makes sense as it's followed by the first data row of data(1).
      my $name = "Eric Beavers\n"; my $email = "elb"; my $website = "carryingstones.com\n"; print $name, $email, '@', $website;

        Assumption is the mother of all FU's

        The danger in your remark is the word usually. Been there done that. And even if the data you are dealing with initially had a header row, there is no guarantee that the next time you get that data from external sources will have (the same) header line or that they either left it out, changed the order, spelling, casting or even inserted line(s) before the header to add "comments" or "instructions". Excel is likely one of the most misused/abused end-user tools in the (ICT) world. I *never* assumer anything in Excel, not even correct encoding or default (date) formatting.


        Enjoy, Have FUN! H.Merijn
Re: I prefer my indexes to start at:
by BillKSmith (Monsignor) on Sep 01, 2022 at 12:28 UTC
    I voted for '0', but only as a default. I do want the option to change it. Mathematicians usually publish algorithms using '1'. Implementing them using '0' can be challenging.
    Bill

      Since coding in c, I got used to start with zero ... and kept it. If there are algorithms (or data interfaces) that are more convenient starting with 1, I usually keep the data on index zero empty or use it for default values, as wasting one additional element is no issue nowadays ....

      So long, Rata

      I can't say that I've ever read a published pure maths paper - if I did it was so long ago I have forgotten.

      OTOH, every time I have seen the Fibonacci Sequence defined it has been like this:

      • F0 = 0
      • F1 = 1
      • Fn = Fn-1 + Fn-2 (for n > 1)

      Perhaps it is the exception? I voted for zero anyway. Cardinal, not ordinal. But you are right that having the choice is good.


      🦛

Re: I prefer my indexes to start at:
by GrandFather (Saint) on Sep 06, 2022 at 23:17 UTC

    If it starts at 1 it's an item number, not an item index (which starts at 0). Adopting index/number as a distinguishing nomenclature for the two cases has saved a lot of confusion at $work (ignoring the convention on the other hand has caused a lot of confusion and wasted time).

    Any other base number gets the appellation "mistake", although there are some places where a non 0 or 1 base can help clean unusual code up considerably.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Definitely a step in the right direction, but it does not solve the problem. Should we reference array elements by 'item number' or 'index number'? Clearly the choice depends on the application.
      Bill
Re: I prefer my indexes to start at:
by choroba (Cardinal) on Sep 01, 2022 at 13:54 UTC
    Zero, unless I need to use XPath...

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: I prefer my indexes to start at:
by dbuckhal (Chaplain) on Sep 04, 2022 at 03:53 UTC

    Was taking a speech class in college... Just to get the speech out of the way, I would hurry to put my name on the board in the #1 spot. I came to class one day and noticed someone already put their name at the #1 spot, so I wrote my name in the #0 spot and went first.

    so, there was that...

      Then someone comes in, thinks to be smart and uses spot #-1 and then - to their surprise - they get seated *last*


      Enjoy, Have FUN! H.Merijn
Re: I prefer my indexes to start at:
by bliako (Monsignor) on Sep 09, 2022 at 08:55 UTC

    I have just noticed that my keyboard's horizontal row of numbers starts from 1. I don't remember seeing any keyboard with a row of: 0-1-2...-9. But my keyboard's keypad has zero next to 1 (at the bottom as a huge button).

    Wasn't it at some stage, with x86 assembly at least, that a comparison with zero (jump-zero instruction) was faster than comparing with any arbitrary integer? This could have had (aeons ago) an effect in looping through an (edit: zero-index)-array (and preferring the backwards loop) ?

    bw, bliako

      Wasn't it at some stage, with x86 assembly at least, that a comparison with zero (jump-zero instruction) was faster than comparing with any arbitrary integer? This could have had (aeons ago) an effect in looping through an (edit: zero-index)-array (and preferring the backwards loop) ?

      It actually depends on the machine implementation. A forward loop from 0 to N-1 may end up with assembler code similar to this pseudo-code:

      mov r1,0 // sometimes, xor r1,r1 needs less program space and/ +or time mov r2,N loop: // do something with r1 inc r1 // may or may not set flags, depending on the impleme +ntation cmp r1,r2 // often imlemented as sub r1,r2 but without actually + writing the result back to the register, just setting flags jne loop // if implemented as sub r1,r2, jne (jump if not rqua +l) is equal to jnz (jump if not zero)

      If the machine allows compare with a constant, one register may be sufficient:

      xor r1,r1 loop: // do something with r1 inc r1 cmp r1,N jne loop

      The same loop running backwards from N down to 1 may be more efficient IF the decrement operation sets the zero flag:

      mov r1,N loop: // do something with r1 dec r1 // must set/clear zero flag depending on value of r1 jnz loop

      On the Z80, there is a special instruction DJNZ (decrement B and jump if non-zero) for exactly this purpose. It does not even affect the flags, it's all handled internally. The 8051 also has a DJNZ that can work on memory or a choosen register. The x86 processors have LOOP/LOOPE/LOOPNE. LOOP is like DJNZ using CX, ECX or RCX, LOOPE and LOOPNE additionally check the zero flag.

      x86 processors have the REP/REPZ/REPNZ prefixes for a similar purpose. It repeats the follwing string instruction until CX/ECX/RCX is zero. REPZ also aborts if the zero flag is cleared, REPNZ also aborts if the zero flag is set. String operations are INS (read from I/O port to memory), MOVS (copy memory, i.e. memcpy, strcpy), OUTS (write from memory to I/O port), LODS (read from memory to accumulator), STOS (write accumulator to memory, i.e. memset), CMPS (compare memory, i.e. strcmp, memcmp), SCAS (compare memory to accumulator, i.e. strchr, strlen).

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      AFAIR there always was a dedicated command for comparing with zero - for all flavors of "comparison" - because it's quite easy to implement and happens sufficiently often (e.g. when decrementing down).

      And this command would also be shorter in length, since it doesn't need to read a constant from memory to compare with. And shorter length will also add up to the speed gain.

      Now even if you used the longer command to compare with 0, a good assembler could try to optimize (think constant folding) a "comparison to a constant 0" to the shorter and faster "zero-compare"

      I learned several assemblers, mainly MOS 6502 and Motorola 68000, but that was too long ago to tell you now where I've experienced this and give the correct mnemonics.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: I prefer my indexes to start at:
by Anonymous Monk on Sep 06, 2022 at 06:39 UTC
    I'm just wondering what some of those other values are.

      Sub-poll!

      • 0E0
      • -1
      • 2147483647
      • -2147483647
      • π or e or sqrt(2)
      • i
      • -Inf
      • NaN

      🦛

        Apparently, the totalOrder predicate from IEEE 754-2008 specifies that -NaN < -Inf < negative finite numbers < -0 < +0 < positive finite numbers < +Inf < +NaN.
        So if floating point values are allowed, then I think that "-Nan" would be the obvious starting point ... coz you can't not get no lower than that ;-)

        (Hopefully, indexes have to be real integer values.)

        Cheers,
        Rob
        • π or e or sqrt(2)

        Close: my answer would have been the first index is Φ ≈ 1.618, second is e ≈ 2.718, third is π ≈ 3.14, and so on, picking irrational (and preferably transcendental) numbers whose integer portion is the next counting number. (I had forgotten that Φ, like √2, is irrational but algebraic, and had started to write that as "picking transcendental numbers" before having a long think about it; still, I prefer Φ since it has a predefined symbol.)

        Ooh, another fun set: ln(2) ≈ 0.693, √2 ≈ 1.414, 2**(√2) ≈ 2.665, ... I'm sure someone could come up with a whole series of those whose integer portions are increasing, but all the terms only combine operators and 2. ;-)

Re: I prefer my indexes to start at:
by mandarin (Hermit) on Sep 02, 2022 at 13:14 UTC
    No sane person starts counting in "real life" at 0, so why should my indexes do so? I stongly suspect that C's idiocy of starting at 0 is the number one cause for off-by-one errors and resulting segfaults, buffer overflows, etc.

      Pointer arithmetic and calculation of memory addresses, esp. for arrays, becomes easier when starting index is zero.

      Depends whether you need your first fence post at "zero feet from the wall". ;-)

        Are you attaching your fencing directly to the wall? If not, you need a post there to support it.

      Isn't it more efficient in binary?

      Example for 4 bit:

      0000 = 0

      0001 = 1

      0010 = 2

      -...

      And starting at 1 would waste the 0000 state. I think this is why starting at 0 is the more natural way to count in basic electronics in my opinion, which is how I think it made its way into C.

      Nowadays it is almost obsolete, but changing so old standards can often be hard to become used to for expierienced programmers, and those mistakes mostly just happen to novice ones, who are not the ones designing new languages.

      Hope I could make you see another side of this debate.

      Greetings, Ninto

View List Of Past Polls


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found