Pages: 1 [2]   Go Down
Author Topic: Base prefix notation  (Read 1510 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 33
Posts: 1447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The leading zero and 0x notation is only used for integers, so the presence of a decimal point changes the game so to speak.

While the months are not part of the language, they are part of the time.h suite of the standard library - which is defined byt he standard. K&R are very careful to point out that the months number represents the "months since January" (italics in original). Harbison and Steele point out the same distinction - which probably came from unix.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess I just don't get the hostility to zero-origin indexing.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess I just don't get the hostility to zero-origin indexing.

It is not hostility to zero-origin indexing so much as hostility to a gotcha. Programming is hard enough without gotchas.

As for zero-origin indexing: following your logic, why aren't the days within each month numbered starting from zero?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
following your logic,
Read my sig  smiley-wink
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 33
Posts: 1447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because they are already numbered.

Note that the days of the week (tm_wday) are "days since sunday". (0-6)
Days of the year (tm_yday) are "days since Jan 1". (0-364/365)

Actually the day of the month one is the wired one since it is the only one not stated as x since y.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1848
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

0bnnnnnnnn is C/C++

No it isn't.  It is a Gnu extension.

If you want to be "proper", use 0bnnnnnnnn.
If you want to be proper, use hexidecimal (0x followed by 0..9, a..f, or A..F) or octal (0 followed by 0..7).
Logged

0
Online Online
Shannon Member
****
Karma: 206
Posts: 12184
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As for a leading zero for octal: I doubt that a worse piece of design in programming language syntax exists anywhere. Except maybe for those programming languages which number the months of the year from 0 to 11 (such as C and ECMAScript).


Let's see: I believe that the relevant rule in many programming languages (including the Arduino programming environment) goes like this:
Numbers with only decimal digits (0 through 9) are decimal.
Except if they begin with 0, in which case they are octal.
Except if they contain a decimal point, in which case they are decimal after all.
Please correct me if I am wrong.

except if they contain a decimal point or an exponent, I think.   02e4 = 20000.0
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As for zero-origin indexing: following your logic, why aren't the days within each month numbered starting from zero?
Because they are already numbered.
As are the months of the year.

Really, who understands month "1" to be February but a programmer, and then only in the context of source code?
Woe to that programmer if s/he uses that month numbering in a user interface...
« Last Edit: May 03, 2013, 10:12:53 pm by odometer » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As for zero-origin indexing: following your logic, why aren't the days within each month numbered starting from zero?
Because they are already numbered.
As are the months of the year.

Really, who understands month "1" to be February but a programmer, and then only in the context of source code?
Woe to that programmer if s/he uses that month numbering in a user interface...
That's why 8 out of 10 cats prefer strftime()...
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Netherlands
Offline Offline
Full Member
***
Karma: 3
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
int c3=076; <--- This is troublesome
int c4=098; <--- Ahhhh!!!!!! (actually throws a compiler error!)

I don't know why it always makes me crack up, but I love this comment in the 2nd edition of K&R:
Quote
Everyone's favorite trivial change: 8 and 9 are not octal digits

So pre-ANSI, your 098 might not have thrown an error, but I have no idea what it would do.

Those old compilers (from the mid 70s of the previous century) just considered, say, 099 to be equal to 8*9+9 == 81.

kind regards,

Jos
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would guess, that if you start looking at date calculations, you would find that the 0 based method is more natural, and in fact there are a lot of (day-1) calculations in the code.

0 based lets you use modulo in calculations easily.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would guess, that if you start looking at date calculations, you would find that the 0 based method is more natural, and in fact there are a lot of (day-1) calculations in the code.

0 based lets you use modulo in calculations easily.

If you want to go that route, I recommend you think of the year as running from the beginning of March to the end of February. This makes dealing with leap years a lot simpler (if, in some ways, less intuitive).
If we reckon the human date "March 1" (of any year) as machine date 0, then machine date 31 will always mean April 1; machine date 61 will always mean May 1; Halloween and Christmas Day will come every year without fail on machine dates 244 and 299, respectively; and your girlfriend will wallop you if you don't special-case machine date 350.
Logged

Pages: 1 [2]   Go Up
Jump to: