www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why don't underscores in numbers enforce proper formatting?

reply "ixid" <nuaccount gmail.com> writes:
The underscores in values such as 1_000_000 aid readability but 
DMD doesn't see anything wrong with any placement of underscores 
as long as they follow a number. Is there any reason to allow 
uses like 1_00_000, which are typos or exceedingly lazy 
modifications of value, and not enforce digits to form sets of 
three after the first underscore?
Mar 06 2013
next sibling parent reply "simendsjo" <simendsjo gmail.com> writes:
On Wednesday, 6 March 2013 at 21:06:43 UTC, ixid wrote:
 The underscores in values such as 1_000_000 aid readability but 
 DMD doesn't see anything wrong with any placement of 
 underscores as long as they follow a number. Is there any 
 reason to allow uses like 1_00_000, which are typos or 
 exceedingly lazy modifications of value, and not enforce digits 
 to form sets of three after the first underscore?
If someone uses different semantics on their numbers (like ISBN), having underscores in different places will help readability as the underscores can show the semantics of the number.
Mar 06 2013
parent "simendsjo" <simendsjo gmail.com> writes:
On Wednesday, 6 March 2013 at 21:15:07 UTC, simendsjo wrote:
 On Wednesday, 6 March 2013 at 21:06:43 UTC, ixid wrote:
 The underscores in values such as 1_000_000 aid readability 
 but DMD doesn't see anything wrong with any placement of 
 underscores as long as they follow a number. Is there any 
 reason to allow uses like 1_00_000, which are typos or 
 exceedingly lazy modifications of value, and not enforce 
 digits to form sets of three after the first underscore?
If someone uses different semantics on their numbers (like ISBN), having underscores in different places will help readability as the underscores can show the semantics of the number.
Norwegian SSNs have this structure for instance: ddmmyyxxxcc where ddmmyy is day/month/year of birth, xxx is an auto increment number (that also shows somewhat the year of birth) and cc is control digits created from xxx where the last digit also indicates gender. This can be grouped in different ways: ddmmyy_xxx_cc, ddmmyy_xxx_c_c etc. based on what you find the most readable. Forcing this to be d_dmmy_yxx_xcc will clearly be suboptimal. Of course, hardcoded SSNs in code is a bad example :)
Mar 06 2013
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 6 March 2013 at 21:06:43 UTC, ixid wrote:
 and not enforce digits to form sets of three after the first 
 underscore?
binary: 1000_1010 or binary: 11001001_11110000 hex: 10_10_40_ae or hex: dead_beef Decimal ones might do it too, because you can use a decimal literal for other things, like std.conv.octal. Which is a specific case where I don't think you'd split other than on three, it is still something I could imagine.
Mar 06 2013
prev sibling parent reply =?iso-8859-15?Q?Simen_Kj=E6r=E5s?= <simen.kjaras gmail.com> writes:
On Wed, 06 Mar 2013 22:06:42 +0100, ixid <nuaccount gmail.com> wrote:

 The underscores in values such as 1_000_000 aid readability but DMD  
 doesn't see anything wrong with any placement of underscores as long as  
 they follow a number. Is there any reason to allow uses like 1_00_000,  
 which are typos or exceedingly lazy modifications of value, and not  
 enforce digits to form sets of three after the first underscore?
In addition to the examples given by Simen and Adam, China and Japan place digits in groups of 4 (10 0000 0000), while India uses groups of 2 (10 00 00 00). -- Simen
Mar 06 2013
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Thursday, 7 March 2013 at 06:31:39 UTC, Simen Kjærås wrote:
 On Wed, 06 Mar 2013 22:06:42 +0100, ixid <nuaccount gmail.com> 
 wrote:

 The underscores in values such as 1_000_000 aid readability 
 but DMD doesn't see anything wrong with any placement of 
 underscores as long as they follow a number. Is there any 
 reason to allow uses like 1_00_000, which are typos or 
 exceedingly lazy modifications of value, and not enforce 
 digits to form sets of three after the first underscore?
In addition to the examples given by Simen and Adam, China and Japan place digits in groups of 4 (10 0000 0000), while India uses groups of 2 (10 00 00 00).
Seems enforcing underscores to a particular number would be a bad thing. Unless you could specify the width of them so it would do a side check to confirm it fits a particular format, but it would then clutter the code perhaps?? Hmmm.. However using underscores it's less likely to make silly mistakes since you mentally can count and tell the groups are in 2's, 3's, 4's or 5's fairly easily.
Mar 07 2013