-1

Using Java btw. But Usually people seem to do this sort of thing with.

int numDigits = (int)(log10(num)+1); //can explicitly floor, or casting to int will do that

but log10(0) = -INF, which means my length is being set to the largest negative integer value.

I suppose I could make a condition

if (numDigits is negative)
    numDigits = 1 //not 0 because I'm counting 0 as 1 digit.

This is being used to implement a natural number constructor, just so people have context to my problem.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Aerlusch
  • 517
  • 3
  • 6
  • 11
  • 2
    What is the question? You could also do `numDigits= (num==0?1:(int)(log10(num)+1))` – amit Nov 07 '12 at 15:09
  • Can't you simply parse int to string and get its length? – Francis P Nov 07 '12 at 15:11
  • @FrancisP: It is a terrible solution if this is done in a tight loop, it will take much longer I believe to construct the string then a simple log – amit Nov 07 '12 at 15:12
  • @amit what exactly does the (num==0?1:(int)... part do? – Aerlusch Nov 07 '12 at 15:14
  • @amit: See here: http://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int – Francis P Nov 07 '12 at 15:14
  • @Aerlusch: It is the trenary operator, you need to take only the special case of `num == 0`, if it is - no need to even take the log - the answer is 1. – amit Nov 07 '12 at 15:15
  • this needs to also ignore leading zeros, so if the incoming type is int then 000 is the same as 0, correct? – Aerlusch Nov 07 '12 at 15:16
  • I think the 0 case has to be handled separately using log, both because of the -INF problem, and because it is the only case in which a leading zero is counted as a digit. If this is in a performance critical situation I would measure both this calculation and Integer.toString(num).length(). It is not obvious to me that the log10 calculation will necessarily be faster, and the string approach requires no special cases. – Patricia Shanahan Nov 07 '12 at 15:16
  • Test against http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 methods that don't require a round trip to float and back. This may be a stupid question, but what is a "natural number constructor"? – A. Webb Nov 07 '12 at 15:29
  • @A.Webb a constructor for a Natural number class. It basically represents a natural number and the size of the number is only limited by the memory of the JVM. When declaring a new object for my class you initialize it with a non-negative integer value. This value is defaulted to 0 if nothing is passed, or it can read it na integer, String, or an Object of my natural number class. I was just having issues when 0 was passed in as an integer. – Aerlusch Nov 07 '12 at 16:07
  • @Aerlush So, you are reimplementing Bignum? – A. Webb Nov 07 '12 at 16:41

2 Answers2

0

The code you listed would mean that -10 has a numDigits value of 1 which I believe is incorrect.

How about converting the number to string and getting the string length?

Ivan Klaric
  • 413
  • 1
  • 4
  • 12
  • `log10(10) + 1 = 1+1 = 2` :| – amit Nov 07 '12 at 15:13
  • Its an implementation of a natural number class. It requires input to be a positive integer, or a string representing a positive integer, or can take be initialised with an object of its own type. Anyways the requires prevents negative numbers from being an issue. – Aerlusch Nov 07 '12 at 15:20
0

If you have a particular optimization problem, then go for the log solution:

int length = (number ==0) ? 1 : (int)Math.log10(number) + 1;

Else, I recommand you use the String.valueOf solution, since it is much more readable and easier to understand for future developpers/maintainers:

int length = String.valueOf(number).length();
Francis P
  • 13,377
  • 3
  • 27
  • 51