number of diggits in a integer

Started by
30 comments, last by Conner McCloud 17 years, 6 months ago
Quote:Original post by Plasmadog
Ever read The Daily WTF? It's amazing how often the "convert to string" approach is taken in real-world applications by programmers who should know better.


I was just going to say that. It's funny how the first solution that comes to mind is usually a WTF one.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Advertisement
but seriously.. im a newb.. why? :D
Quote:Original post by Nanook
but seriously.. im a newb.. why? :D


Because the problem you are trying to solve is just basic maths. There is a mathematical solution.
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
Quote:Original post by Nanook
but seriously.. im a newb.. why? :D


Because it's equivalent to printing out the number, putting the print out on a wooden table, taking a picture, scanning the picture and then using an image recognition program to find the number of digits. :-)

Seriously, though, if there is a simple formula to calculate the number of digits, there is no need to get wooden tables involved. In this case, not only the string-based solution is much slower, the professor probably wouldn't accept it because by using strings you ignore properties of numbers. Knowing these properties will help you in programming.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
You could cast the integer (344) to double (344.0), use log10() to get the base-10 logarithm (2.536), cast back to integer (2) and add 1 (3).

Edit: but have a special case check for 0
Quote:Original post by Palidine
Quote:Original post by Nice Coder
for(int i = 0; x > 9; x /= 10, i++);

Would that work?


i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me


Ok so to fix it:

for(int i = 1; x > 9; x /= 10; i++);

There... now "i" is the number of digits, but assuming that x is a positive number.

...and it IS accessible outside the scope of the loop. Why do you think it's not?
Alternatively, you can do:


int numberOfDigits = 1;
if(theNumber > 0)
numberOfDigits += (int)log(myNumber);
};

This will give number of digits. :)
Quote:Original post by superdeveloper
Quote:Original post by Palidine
Quote:Original post by Nice Coder
for(int i = 0; x > 9; x /= 10, i++);

Would that work?


i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me


Ok so to fix it:

for(int i = 1; x > 9; x /= 10; i++);

There... now "i" is the number of digits, but assuming that x is a positive number.

...and it IS accessible outside the scope of the loop. Why do you think it's not?


Only if you are using VC++ 6.
Quote:Original post by superdeveloper
...and it IS accessible outside the scope of the loop. Why do you think it's not?


It won't be, if the compiler is fully standards-compliant. It shouldn't be relied upon.
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
I don't see where the i fits into that solution . . . except as a counter, but still. My solution is nearly the same, just slightly different.

digits = 0
for(count = 10; count<goal_num; count *= 10, digits++);

When count is greater than the goal number it'll stop, so digits will equal the number of digits. The only problem, I guess, is for negative numbers.

This topic is closed to new replies.

Advertisement