# Tag Archives: Logarithm

## How to determine the number of digits in a number

Mathematically the number of digits in given integer > 0 can be determined with logarithm.

The number of digits in an integer > 0 is

Trunc(Logk(number)) + 1

Trunc comes from the Pascal programming language and means truncation. ”number” is 10 base system number of k base system number.

The idea is to examine how many powers of base system k there is in the number, that is how many times the given number can be divided with the base number until we get number < 1. With this our friend logarithm helps us to determine that. As a reminder, logarithm is reverse operation to power.

Examples:

In 10 base system in number 12345678 there is 7 powers of 10: Trunc(Log1012345678) = 7, the number of digits is 7 + 1 = 8.

The hexdecimal system number 99916 = 245710, Log16 2457 = 2.8156…, the number of digits is 2 + 1 = 3.

Hex number FF16 = 25510, Log16 255 = 1.9985…, the number of digits is 1 + 1 = 2.

Binary number (k = 2) 100000002 = 128, Log2 128 = 7, the number of digits is 7 + 1 = 8.

Binary number 101000012 = 16110, Trunc(Log2 161) = Trunc(7.33091…) = 7, the number of digits is 7 + 1 = 8. Image courtesy of Stuart Miles at FreeDigitalPhotos.net

Via programming the number of digits can be determined by dividing the number by the base system number k until the number being divided is < 1. Now the number of digits is the number of divisions.

Below is a C programming language program that determines the number digits of 10 base system number that can be positive or negative decimal number:

```#include <stdio.h>
#include <math.h>

int detDigits(double);

void main(void) {

double decimalnumber;
int number_of_digits;

printf("Give a decimal number: ");
scanf("%lf", &decimalnumber);

number_of_digits = detDigits(decimalnumber);

printf("The number of digits is %d.",number_of_digits);

}

int detDigits(double dn) {

int digits_integer_part, digits_decimal_part;
int worknumber;

if (dn == 0) return 1;

if (dn < 0) dn = -dn;

digits_integer_part = ((int)log10(dn)) + 1;
dn = dn – (int)dn;

worknumber = (int) dn;

while (worknumber % 10 != 0) {
dn = dn * 10;
worknumber = (int)dn;
}

/* Variable "worknumber" now holds the number of digits
of decimal part of the given number + 1 */

digits_decimal_part = worknumber – 1;

return digits_integer_part + digits_decimal_part;
}```

Another way of determining the number of the digits is of course converting the decimal number to string and by examining the number of characters.

## k base logarithm of negative real number

Many years ago when I was studying for the second year in the university of Jyväskylä in one sleepless night I somehow invented how to determine logarithm of negative real number, although I had only basic knowledge of complex numbers. And yes, the logarithm of negative real number is, of course, a complex number.

Now to the formula…

Let x ∈ ℜ and x < 0 and k > 1. Now

I was able to prove this formula in less than 30 minutes, about one A4 paper of proof. Next morning I showed my proof to one person at staff of department of mathematics at university of Jyväskylä (Finland). He didn’t find anything wrong in my proof. A tricky part was a situation where there was two variables in one equation in my proof, but the other varibale had as coefficient sin π that equals 0, so the other variable was eliminated from the equation. I don’t have the proof anymore and I don’t really remember any relevant details of my proof other than I used Euler’s formula (*) in some part of the proof that I had become familiar with in course of differential equations.

(*) As far I can see, the formula probably was the following:

ex + iy = ex(cos y + i sin y)