Representing Big Numbers in Source Code for Readability

Representing big numbers in source code for readability?

Here's a macro that would do it, tested on both MSVC and GCC. No reliance on Boost...

#define NUM(...) NUM_(__VA_ARGS__, , , , , , , , , , )
#define NUM_(...) NUM_MSVCHACK((__VA_ARGS__))
#define NUM_MSVCHACK(numlist_) NUM__ numlist_
#define NUM__(a1_, a2_, a3_, a4_, a5_, a6_, a7_, a8_, ...) a1_##a2_##a3_##a4_##a5_##a6_##a7_##a8_

Use it like:

int y = NUM(1,2,3,4,5,6,7,8);
int x = NUM(100,460,694);

Produces:

int y = 12345678;
int x = 100460694;

How to represent large numbers nicely in source code for Java?

You can use underscores instead of spaces, which will get close to what you want.

int i = 65_232;

This is assuming that you're using at least Java 7.

The compiler treats it as though the underscores weren't there, so you can place them (almost) anywhere you want inside of numeric literals, which gives you quite a bit of freedom in how you format your source code.

int XD = 0__0;

Is there a way to write a large number in C++ source code with spaces to make it more readable?

Try digit separator:

int i = 1'000'000'000;

This feature is introduced since C++14. It uses single quote (') as digit separator.


Also see:

  • Why was the space character not chosen for C++14 digit separators?
  • Generalizing Overloading for C++2000 (April's joke by the father of C++ himself)

Improve readability of very large constants in C++

how about

const long long VERY_LARGE_NUMBER =  (long long) 300 * 1000 * 1000 * 1000;

Why write 1,000,000,000 as 1000*1000*1000 in C?

One reason to declare constants in a multiplicative way is to improve readability, while the run-time performance is not affected.
Also, to indicate that the writer was thinking in a multiplicative manner about the number.

Consider this:

double memoryBytes = 1024 * 1024 * 1024;

It's clearly better than:

double memoryBytes = 1073741824;

as the latter doesn't look, at first glance, the third power of 1024.

As Amin Negm-Awad mentioned, the ^ operator is the binary XOR. Many languages lack the built-in, compile-time exponentiation operator, hence the multiplication.

Nicely formatting numbers in C++

As of C++14, you can use ' as a digit group separator:

auto one_m = 1'000'000;

Previous versions of C++ did not support this natively. There were two major workarounds:

  • Using user-defined literals in C++11; this would allow you to write code as follows:

    auto x = "1_000_000"_i;

    (Writing this as a constexpr would be trickier – but is definitely possible.)

  • Using a straightforward macro, which would allow the following code:

      auto x = NUM(1,000,000);

How to separate digits in numeric constants (i.e. 10,000) in C or C++ code

The only way to do this is in C++14, is with single quotes, like this. Unfortunately, the only problem with this is that syntax highlighting often gets messed up with the notation below, and you can see that in my example as well:

int i = 1'000'000;



Working Example


According to http://en.cppreference.com/w/cpp/language/user_literal:

In the integer and floating-point digit sequences, optional separators ' are allowed between any two digits and are ignored (since C++14)

Implement a source code that turns numbers into text (C++)

Everything below twenty is a special case. However, instead of having a huge if chain, you should have a data structure containing "zero", "one", "two", etc and index into the data structure. (Vector, map or array for example. If you don't know how to use any of these, I suggest you learn because data structures are VERY useful in all programming languages and you will want to learn them).

Above twenty, we have to start coding more generically. We have to split creating the word version into parts:

1) Getting the units column. You can use number % 10 to get only the units, because % 10 gets the remainder after dividing by 10. You can use the units number to index into your zero, one, two etc data structure from earlier and get what to print.

2) Getting the tens column. Similar idea - (number / 10) % 10. Now index tens column into a data structure like "", "ten", "twenty", "thirty", ... at 0, 1, 2, 3...

3) ... And so on for every higher column.



Related Topics



Leave a reply



Submit