News for hackers. Stuff that matters.
Re:C++14 by kibwen on Friday November 02, 2012 @09:58AM (#4732596)
"digit separators to make for instance binary literals readable 0b1_0010_0010_0000_1000"

Nice. I first saw this in Rust, and it seemed like such an obviously good idea (allowing underscores in numeric literals) that I've wondered why more languages don't do it. Now I'm curious where it originated.

Re:C++14 by rootbear on Friday November 02, 2012 @12:34PM (#4733431)
I am a big fan of adding digit separators where ever possible. I'm tired of staring at ls listings of terabyte filesystems and trying to figure out what order of magnitude the numbers are. And yes, I know about ls -lh.

I would like to see a universal format agreed on by everyone, so that numbers with separators (call them punctuated numbers) output by one program could be used as input for another, as is often done in Unix pipelines.

The languages that I know of that have punctuated numbers all use underscores. However, having thought about it a lot, I don't think this is a good choice. First, the underscore is often used, as in C, as an additional character in identifiers, but the semantics are different. An underscore in an identifier is significant, in a number it is not:

1_234_567 == 1234567, but

my_long_identifier != mylongidentifier

Also, the underscore is too "big". In a fixed width font, this is irrelevant, but in a variable width font, I think it makes numbers look ugly. The separator should be narrow. In some locales, the apostrophe is used:

1'234'567

I like the thinner character. I have at least one calculator that uses this convention in the display. Alas, this is probably unworkable in computers, since the apostrophe is used commonly for quoting. One option that might work is the back quote:

1`234`567

The back quote isn't used as much as the apostrophe, but it still might cause problems. On US keyboards, it's conveniently located right next to the 1.

Finally, I'd like to see both period and comma accepted as the decimal separator

1`234`567.89 == 1`234`567,89

but there are some obvious problems with that.

My bottom line is that, before C++, or any other language, adds underscores as thousands separators because that's what everyone else did, we should think more deeply about what's needed in a universal format for punctuated numbers.

Re:C++14 by Symmetry on Friday November 02, 2012 @12:55PM (#4733566)
Far better to use a character that's supposed to be part of an identifier like _ than a character that could have some other syntactical use in various languages. The fact that a number begins with a numeric character lets you distinguish it from other identifiers.
Re:C++14 by evincarofautumn on Friday November 02, 2012 @01:58PM (#4733901)
Digit separation might make long numbers more readable, but now they’re also more locale-dependent. Same goes for commas versus periods. Regardless, I think the grave accent is a poor choice here. Whitespace would be better, much like adjacent string literals in C. A compiler could warn by default about numeric literals crossing line boundaries in free-form languages, to catch missing punctuation. This also has the advantage of precedent in scientific literature.

Unfortunately it doesn’t work in languages without some redundancy, like many in the vector family. Is this a four-vector of one-digit numbers or a one-vector of a four-digit number?

    [1 2 3 4]
Re:C++14 by kps on Friday November 02, 2012 @10:38AM (#4732785)
There's a Boost header with macros that provide this for C and C++.

  int i = BOOST_BINARY( 1 0010 0010 0000 1000 );
Re:C++14 by ruggeri on Friday November 02, 2012 @01:26PM (#4733722)
Upvoted, but I cry at the thought of all that extra typing.
Re:C++14 by mahmud on Friday November 02, 2012 @10:01AM (#4732618)
It's very common in ML languages: SML and Ocaml both support it.
Re:C++14 by alex-g on Friday November 02, 2012 @11:04AM (#4732948)
This seems to me to be far more useful than user-defined literals (the conflicting feature). At least, I can think of many programs where digit separators would be nice - but for user-defined literals, I don't see them as providing much. We could already make invocations like "pixels(200)" and being able to say "200_pixels" doesn't add much to that, except complicating the parser.
Re:C++14 by anonymouz on Friday November 02, 2012 @11:14AM (#4732996)
The problem with this argument is that user-defined literals already exist in C++11, and use of the underscore character for a different purpose in a future standard has to be in such a way that it can be unambiguously distinguished from the user-defined literal usage. Backward-compatibility to the current standard has to be preserved.
Re:C++14 by alex-g on Friday November 02, 2012 @11:15AM (#4733007)
Oh, absolutely, I just think it's a shame.
Re:C++14 by mpyne on Friday November 02, 2012 @10:01AM (#4732614)
Perl has had it for awhile, although there were probably languages that had it beforehand (awk or ADA maybe?)
Re:C++14 by chmike on Friday November 02, 2012 @10:28AM (#4732729)
I saw it first in D. Doesn't mean it was invented there ;)
Re:C++14 by ksherlock on Friday November 02, 2012 @10:01AM (#4732615)
That was in use in some assemblers back in the 80s and probably earlier.
Re:C++14 by jordibunster on Friday November 02, 2012 @10:00AM (#4732608)
Not sure about the origin, but Ruby has them too.
Re:C++14 by jlarocco on Friday November 02, 2012 @10:03AM (#4732629)
Not sure of the origin either, but Ada has had that since 1983.
Re:C++14 by _delirium on Friday November 02, 2012 @12:00PM (#4733220)
Aha, interesting. I was doing some quick scanning on Google-Books and other places to try to find the earliest usage, using variants of "numeric literal" and "underscore" as search terms, and couldn't find anything pre-1990s. Now I find the Ada stuff, and it wasn't coming up because all the '80s Ada documentation/discussion refers to them as underline characters, rather than underscore.

(Incidentally, one of the perils of trying to draw historical conclusions from text searches, or data sets like Google Ngrams: you could get completely wrong results if you miss a shift in spelling or word usage.)

Re:C++14 by positr0n on Friday November 02, 2012 @10:11AM (#4732660)
Verilog has that feature too
Re:C++14 by zem on Friday November 02, 2012 @01:44PM (#4733828)
ruby was the first place i saw it used, but it's probably older than that.
Re:C++14 by JoeAltmaier on Friday November 02, 2012 @10:10AM (#4732658)
But not much more readable than, say Hex e.g. 0x12204.
Re:C++14 by kibwen on Friday November 02, 2012 @10:33AM (#4732755)
Indeed, but it's also useful in order to detect at a glance the order of magnitude of a literal. For instance, if I come across 10_000_000 in some code, I don't need to interrupt my train of thought to meticulously count the zeroes; I instantly know that's ten million. I couldn't say the same if it was instead written as 100000000.
Re:C++14 by easytiger on Friday November 02, 2012 @11:26AM (#4733051)
puny human
Re:C++14 by tptacek on Friday November 02, 2012 @10:33AM (#4732757)
It is if you're representing a flag word.
Re:C++14 by fauigerzigerk on Friday November 02, 2012 @01:46PM (#4733834)
Java has it.
Re:C++14 by kibwen on Friday November 02, 2012 @02:13PM (#4733978)
Whoa, you're right. Looks like it's a new feature in Java 7.

http://docs.oracle.com/javase/7/docs/technotes/guides/langua...

Re:C++14 by elrzn on Friday November 02, 2012 @10:02AM (#4732626)
I think I saw it first in Perl.