Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine number of leading zeros in a floating point number

How can I calculate how many zeros come after the decimal point but before the first non-zero in a floating point number. Examples:

0 -> 0
1 -> 0
1.0 -> 0
1.1 -> 0
1.01 -> 1
1.00003456 ->4

Intuitively I assume there is a math function that provides this, or at least does the main part. But I can neither recall nor figure out which one.

I know it can be done by first converting the number to a string, as long as the number isn't in scientific notation, but I want a pure math solution.

In my case I don't need something that works for negative numbers if that's a complication.

I'd like to know what the general ways to do it are, irrespective of language.

But if there is a pretty standard math function for this, I would also like to know if JavaScript has this function.

As a sidenote, I wonder if this calculation is related to the method for determining how many digits are required for the decimal representation of an integer.

like image 241
hippietrail Avatar asked Sep 07 '25 20:09

hippietrail


2 Answers

Let x be a non-whole number that can be written as n digits of the whole part, then the decimal point, then m zeroes, then the rest of the fractional part.

x = [a1a2...an] . [0102...0m][b1b2...bm]

This means that the fractional part of x is larger than or equal to 10–m, and smaller than 10–m+1.

In other words, the decimal logarithm of the fractional part of x is larger than or equal to –m, and smaller than –m+1.

Which, in turn, means that the whole part of the decimal logarithm of the fractional part of x equals –m.

function numZeroesAfterPoint(x) {
  if (x % 1 == 0) {
    return 0;
  } else {
    return -1 - Math.floor(Math.log10(x % 1));
  }
}

console.log(numZeroesAfterPoint(0));
console.log(numZeroesAfterPoint(1));
console.log(numZeroesAfterPoint(1.0));
console.log(numZeroesAfterPoint(1.1));
console.log(numZeroesAfterPoint(1.01));
console.log(numZeroesAfterPoint(1.00003456));

As a sidenote, I wonder if this calculation is related to the method for determining how many digits are required for the decimal representation of an integer.

In the same manner, a positive integer x takes n decimal digits to represent it if and only if n - 1 <= log10(x) < n.

So the number of digits in the decimal representation of x is floor(log10(x)) + 1.

That said, I wouldn't recommend using this method of determining the number of digits in practice. log10 is not guaranteed to give the exact value of the logarithm (not even as exact as IEEE 754 permits), which may lead to incorrect results in some edge cases.

like image 120
Anton Avatar answered Sep 10 '25 02:09

Anton


You can do it with a simple while loop:

function CountZeros(Num) {

    var Dec = Num % 1;
    var Counter = -1;

    while ((Dec < 1) && (Dec > 0)) {
        Dec = Dec * 10;
        Counter++;
    }
    Counter = Math.max(0, Counter); // In case there were no numbers at all after the decimal point.

    console.log("There is: " + Counter + " zeros");
}

Then just pass the number you want to check into the function:

CountZeros(1.0034);
like image 43
Amit Sheen Avatar answered Sep 10 '25 00:09

Amit Sheen