-
Max Digits
Hi,
I am writing a C program that will allow me to determine the maximum number of digits that a value can have to be within the range 0..m.
for example:
if m=23
max number of digits =1
Actual range 0..9
if m=379
max number of digits =2
Actual range 0..99
if m=453638923
max number of digits =8
Actual range 0..99999999
where 9 is a digit not a value...
firstly I dont want code but could someone explain the above theory as I just dont get it....
-
log10(23) = 1.xxx
log10(379) = 2.xxx
-
I dont understand. Isn't the value 20 within the range of 0...23? 20 has 2 digits.
What you need to do to find out the max digits is to find the number of digits your 'm' has. I really dont know how to explain theory, but I can give you an example...
12345 / 10 = 1234.5
12345 / 100 = 123.45
12345 / 1000 = 12.345
.
.
.
remember that if both values are ints, then the fractional portion of the values will get truncated (cut off)
-
Salem made use of the logarithm function, it is in some way the inverse of the power function. Example:
10^2 = 100
log10 (100) = 2
Note that the base is 10, that's why it goes OK. For numbers with base 10, the total number of digits can be calculated as:
nr_of_digits = (int) log10 (N) + 1