large numbers

M

mike7411

I was just wondering if there is anything in C++ similar to commas for
making large numbers more readable.

For instance, if you want to write one hundred million, the standard
way is 100000000, and it's
not immediately obvious how many zeros are there. I'd like to be able
to do something like:

long x = 100,000,000;

Any help is appreciated.
 
V

Victor Bazarov

I was just wondering if there is anything in C++ similar to commas for
making large numbers more readable.

No, unfortunately nothing. There was some talk about using the underscore
as the separator, but I don't think it reached the Committee. Ask about
the form '100_000_000' in 'comp.std.c++' to see whether it has ever been
suggested.

V
 
R

Robert Bauck Hamar

I was just wondering if there is anything in C++ similar to commas for
making large numbers more readable.

For instance, if you want to write one hundred million, the standard
way is 100000000, and it's
not immediately obvious how many zeros are there. I'd like to be able
to do something like:

long x = 100,000,000;

Any help is appreciated.

template <long M, unsigned E>
struct power {
static long value = M * power<E-1>::value;
};
template <long M>
struct power<M, 0> {
static long value = 1;
};

....
long x = power<10, 8>::value;

or

#define e8 100##000##000
long x = e8;
#undef e8
 
R

Rolf Magnus

I was just wondering if there is anything in C++ similar to commas for
making large numbers more readable.

For instance, if you want to write one hundred million, the standard
way is 100000000, and it's not immediately obvious how many zeros are
there. I'd like to be able to do something like:

long x = 100,000,000;

Any help is appreciated.

Why not just:

long x = 100 * 1000 * 1000;

Or even

static const long million = 1000 * 1000;
long x = 100 * million;
 
V

Victor Bazarov

Rolf said:
Why not just:

long x = 100 * 1000 * 1000;

Are you sure it's guaranteed to work and not overflow or something of
that nature? Both 100 and 1000 are below the minimal 'int' limit, but
'100 * 1000' is beyond it. The compiler may be smart enough to make
sure the result is of the "next bigger type", but I can't find any
requirement to that effect in the Standard.
Or even

static const long million = 1000 * 1000;
long x = 100 * million;

That's better, since 'million' has a particular type.

V
 
R

Rolf Magnus

Victor said:
Are you sure it's guaranteed to work and not overflow or something of
that nature?

Now that you mention it...
Both 100 and 1000 are below the minimal 'int' limit, but
'100 * 1000' is beyond it. The compiler may be smart enough to make
sure the result is of the "next bigger type", but I can't find any
requirement to that effect in the Standard.

Yes, you are right.
That's better, since 'million' has a particular type.

I think it can still overflow though, because 1000 has type int, so the
result of the multiplication will also be be int. The conversion to long
happens afterwards. So better make it:

static const long million = 1000L * 1000L;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top