number of digits in a number

B

bejiz

Hi,
Do you know a way to tell to the compiler that if
there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this:
" ".
But if I have numbers of 3 digits, I want spaces
like this: " ".
I don't know how to get the numbers of digits in
a number (integer or float).
Thanks.
 
V

Victor Bazarov

bejiz said:
Hi,
Do you know a way to tell to the compiler that if
there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this:
" ".
But if I have numbers of 3 digits, I want spaces
like this: " ".
I don't know how to get the numbers of digits in
a number (integer or float).

Number of digits in 'x' (assuming 'x' is unsigned) :

int ndigits = x > 0 ? int(log10(x)) + 1 : 1;

As to preceding number of spaces, see 'setw' stream manipulator.

V
 
M

Mark P

Victor said:
Number of digits in 'x' (assuming 'x' is unsigned) :

int ndigits = x > 0 ? int(log10(x)) + 1 : 1;

As to preceding number of spaces, see 'setw' stream manipulator.

V

Maybe the standard guarantees otherwise, but this seems potentially
susceptible to numerical inaccuracies. What happens if log10( 100)
returns 1.99999999987?
 
V

Victor Bazarov

Mark said:
Maybe the standard guarantees otherwise, but this seems potentially
susceptible to numerical inaccuracies. What happens if log10( 100)
returns 1.99999999987?

Right. I should have added 0.5:

int ndigits = x > 0 ? int(log10(x + 0.5)) + 1 : 1;

V
 
M

Mark P

Victor said:
Right. I should have added 0.5:

int ndigits = x > 0 ? int(log10(x + 0.5)) + 1 : 1;

V

That works, and also spares you the indignity of the ternary operator.
 
R

robertwessel2

Right. I should have added 0.5:

int ndigits = x > 0 ? int(log10(x + 0.5)) + 1 : 1;


Now it won't work for 99.7, 999.7, 9999.7, etc. It's also not clear
that this does what you want for numbers less than 1.

I suggest you give this up, you will not be able to make this work
reliably, since the result of log10() is almost never exact, and you'd
have to prove that the result is correct around all the powers of 10.
While you might be able to verify that for any given implementation of
C++, you can't make the case in general.

While using the log() operator is likely to be overly expensive in any
case, you *could* use that to pick an approximation (IOW log10(x)),
and then test if that needs to be adjusted up or down one, perhaps
with a table lookup. Something like:


double powers10[] = {1., 10., 100., 1000., 10000., 100000.,
1000000....};

nd = log10(x); //needs range check

if (x < powers10[nd]) nd--;
else if (x >=powers10[nd+1) nd++;
 
J

James Kanze

Now it won't work for 99.7, 999.7, 9999.7, etc. It's also not clear
that this does what you want for numbers less than 1.

It's not clear at all what he wants to begin with; Victor's
guess is as good as anything else. But if he wants to know how
many digits are present, the most obvious solution is:

std::eek:strinstream s ;
// add whatever format flags are wanted...
s << value ;
return s.str().size() ;

It probably won't be as fast as the solution with log10, but it
does guarantee that the number you get corresponds exactly to
the number of characters ostream will generate.
I suggest you give this up, you will not be able to make this
work reliably, since the result of log10() is almost never
exact, and you'd have to prove that the result is correct
around all the powers of 10. While you might be able to
verify that for any given implementation of C++, you can't
make the case in general.

You could certainly do it for IEEE, at least for numbers in a
"normal" range. (As for the extremes... what should
log10(1e300) return? You're not really passing it 1e300 to
begin with.)
 
R

robertwessel2

You could certainly do it for IEEE, at least for numbers in a
"normal" range. (As for the extremes... what should
log10(1e300) return? You're not really passing it 1e300 to
begin with.)


No, you actually can't. There isn't actually a formal accuracy
standard for the complex transcendental functions, and the accepted
"One ULP*" rule (which, IIRC, is suggested by the IEEE standard too))
leaves enough slack that you can certainly *not* guarantee that all
implementations will produce identical results. Contract that with
the basic arithmetic operations, where all operations have an exact
result in IEEE-754 terms (and mind you that many compilers, even on
reasonably IEEE compliant hardware, do *not* generate code that
produces IEEE compliant results, even for basic operations. Consider
the default operand widening performed/allowed by most x87 code
generators, so your problem is even worse (and an inappropriately
lengthened intermediate result may be what's actually passed to the
log function).


*Except when that's too hard and it's the "almost always one ULP, two
in a few cases we can't fix" rule.
 
V

Victor Bazarov

Now it won't work for 99.7, 999.7, 9999.7, etc. It's also not clear
that this does what you want for numbers less than 1.

But there is no way to tell how many digits those numbers have! The
original solution was proposed for the *unsigned* 'x', IOW the one
that does NOT have a fractional part.
I suggest you give this up, [..]

I suggest you reread the original post and think before you shoot next
time.

V
 
B

bejiz

Thanks for your answers. I wanted to know the number of digits so that
it would be possible to line up the columns of a matrix and choose an
appropriate size for the spaces. I now think I'll use cout.width() for
the spaces and the booleans with log10 for guessing the size. I think
it is possible finding the number of digits of a float by multiplying
it by a huge power of 10 and then removing the zeros dividing by 10
until the modulo is different from zero and counting the number of
digits in the final number.
 
P

Pete Becker

Thanks for your answers. I wanted to know the number of digits so that
it would be possible to line up the columns of a matrix and choose an
appropriate size for the spaces. I now think I'll use cout.width() for
the spaces and the booleans with log10 for guessing the size. I think
it is possible finding the number of digits of a float by multiplying
it by a huge power of 10 and then removing the zeros dividing by 10
until the modulo is different from zero and counting the number of
digits in the final number.

I suspect you've misunderstood what basic_ostream::width does. It sets
the width of the field, i.e. the number of characters for each column.
After you set that, the stream inserter converts the value into text
and adds as many spaces as are needed to pad the text to the required
number of characters. You don't, in general, need to guess the size of
any value.
 
V

Victor Bazarov

bejiz said:
Thanks for your answers. I wanted to know the number of digits so that
it would be possible to line up the columns of a matrix and choose an
appropriate size for the spaces. I now think I'll use cout.width() for
the spaces and the booleans with log10 for guessing the size. I think
it is possible finding the number of digits of a float by multiplying
it by a huge power of 10 and then removing the zeros dividing by 10
until the modulo is different from zero and counting the number of
digits in the final number.

I think you're overthinking it. The maximum number of significant
digits in 'float' is 'std::numeric_limits<float>::digits10()', print
them *all* using 'scientific' notation (%g format in fprintf).

V
 
R

robertwessel2

Now it won't work for 99.7, 999.7, 9999.7, etc. It's also not clear
that this does what you want for numbers less than 1.

But there is no way to tell how many digits those numbers have! The
original solution was proposed for the *unsigned* 'x', IOW the one
that does NOT have a fractional part.
I suggest you give this up, [..]

I suggest you reread the original post and think before you shoot next
time.


The original poster specified "integer or float." And the question
was clearly how many digits there were to the left of the decimal
point, since the OP was wanting to line up the numbers. And the
"number of digits (to the left of the decimal point)" in the case of
999.7 is clearly 3.
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top