473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

number of digits in a number

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.

Sep 26 '07 #1
12 2977
bejiz wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '07 #2
Victor Bazarov wrote:
bejiz wrote:
>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
Maybe the standard guarantees otherwise, but this seems potentially
susceptible to numerical inaccuracies. What happens if log10( 100)
returns 1.99999999987?
Sep 26 '07 #3
Mark P wrote:
Victor Bazarov wrote:
>bejiz wrote:
>>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

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #4
Victor Bazarov wrote:
Mark P wrote:
>Victor Bazarov wrote:
>>bejiz wrote:
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
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
That works, and also spares you the indignity of the ternary operator.
Sep 27 '07 #5
On Sep 26, 7:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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++;

Sep 27 '07 #6
On Sep 27, 7:00 am, "robertwess...@yahoo.com"
<robertwess...@yahoo.comwrote:
On Sep 26, 7:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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.
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::ostrinstream 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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 27 '07 #7
On Sep 27, 5:09 am, James Kanze <james.ka...@gmail.comwrote:
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.

Sep 27 '07 #8
ro***********@yahoo.com wrote:
On Sep 26, 7:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #9
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.

Sep 27 '07 #10
On 2007-09-27 16:55:08 -0400, bejiz <br**********@wanadoo.frsaid:
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 27 '07 #11
bejiz wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #12
On Sep 27, 7:32 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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.

Sep 27 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
10
by: guidosh | last post by:
Hello, I'm trying to write a printf statement that sets the field width when printing a number. I'm using this: printf("%*", fieldwidth, num_to_print); However, I can't figure out how to...
6
by: Jovo Mirkovic | last post by:
Hi, I have to make a program which will generate 100,000 different ID numbers (for example 2345-9341-0903-3432-3432 ...) It must be really different, I meen it can not be a similar (like...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.