Saturday, February 2, 2013

Digit Separators in C++

 

Problem

Here is the statement of the problem as described by Lawrence Crowl

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3499.html#Problem

  • Pronounce 7237498123.
  • Compare 237498123 with 237499123 for equality.
  • Decide whether 237499123 or 20249472 is larger.

The paper then goes on to describe a proposal the C++ standard to add digit separators to C++.  In this blog post, we will look at what we can do for this problem in C++ as it is currently.

Doing this involves (ab)using the preprocessor ## operator which concatenates 2 tokens. Using this yields two different options. We will use the number 1234567 as an example for these 2 options.

Option 1 – Self-contained but not terse

   1: #define n 1 ## 234 ## 567
   2:  
   3: int j = n;
   4:  
   5: #undef n

This option has the advantage that anyone that knows C would be able to figure out what is going on without looking at any other code. The disadvantage is that this is definitely not terse.


Option 2 – Terse but requires macro


Given the macro below



   1: #define NUM_HELPER(a,b,c,d,e,f,g,...) a##b##c##d##e##f##g
   2:  
   3: #define NUM(...) NUM_HELPER(__VA_ARGS__,,,,,,,,,)

We can write the example number 1234567 as below



   1: int j = NUM( 1,234,567 );

The advantage is that this option is terse. It also has the same format that is commonly used outside of programming. The disadvantage is that the code looks pretty confusing unless you know what NUM does. The other disadvantage is that the macro also pollutes the namespace.


While both of these approaches are inferior to C++ native support for digit separators, the two options described above work with C++ now.


Please let me know what you think and which option you like better.


- John Bandela

2 comments:

  1. I'd prefer a user-defined literal, something like:

    "1_000_000_000"_i;

    or

    "1,000,000,000"_i;

    Of course, it'd be nicer if you didn't need the quotes, but such is life.

    In case you care about how to implement that: http://stackoverflow.com/a/14220849/179910

    ReplyDelete
  2. To create your programing assignments and learn programing just visit this website. http://clanguageprogrames.blogspot.com/

    ReplyDelete