Discussion about math, puzzles, games and fun. Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ π -¹ ² ³ °

You are not logged in.

- Topics: Active | Unanswered

**Hannibal lecter****Member**- Registered: 2016-02-11
- Posts: 347

Hi,

int number 123456;

int firstDigit;

int totalDigits = (int) Math.log10(number);

firstDigit = (int) (number/ (int) Math.pow(10,totalDigits));

System.out.println("First Digit is: " + firstDigit);

how could log10 of a number like 123456 is 5?

I type that in google and it's 5.09151220163 not 5, I know we used casting so it discard the fractional parts to get 5, but my question is not about the Java it's about log10 why log10 always get the total digits with fractional parts always!

like log10 of 532 is 2.7259116323 which is 2 digits that is working! but how mathematically is that

how can Math.log10(12345) calculate the number of digits which is 5 correct!?

only that first part of the code is really ambiguous

Wisdom is a tree which grows in the heart and fruits on the tongue

Offline

**Bob****Administrator**- Registered: 2010-06-20
- Posts: 9,534

hi

It's all due to the definition of log base 10

If

So let's look at some integer cases first:

x = 1 y = 10

x = 2 y = 100

x = 3 y = 1000

and

x = 0 y = 1

x = -1 y = 0.1

x = -2 y = 0.01

x = -3 y = 0.001

Now for a number like eg. 53

and so on.

So numbers between 1 and 10 have a log that is between 0 and 1

numbers between 10 and 100 have a log that is between 1 and 2

numbers between 100 and 1000 have a log that is between 2 and 3

For your example 123456 = 1.23456 x 10^5 so the log will be 5.something

Bob

some back ground here: https://www.mathsisfun.com/algebra/logarithms.html

Children are not defined by school ...........The Fonz

You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei

Sometimes I deliberately make mistakes, just to test you! …………….Bob

Offline