代码之家  ›  专栏  ›  技术社区  ›  hippietrail

确定浮点数中前导零的数量

  •  3
  • hippietrail  · 技术社区  · 7 年前

    如何计算浮点数中小数点后第一个非零之前有多少个零。示例:

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

    我知道可以先把数字转换成字符串,只要数字不是用科学记数法,但我想要一个纯数学的解决方案。

    在我的情况下,如果负数很复杂,我不需要对负数有效的东西。

    我想知道做这件事的一般方法是什么,不考虑语言。

    作为旁注,我想知道这个计算是否与确定整数的十进制表示需要多少位的方法有关。

    4 回复  |  直到 7 年前
        1
  •  4
  •   Anton    7 年前

    允许 x 是一个非整数,可以写成 n m 零,然后是分数部分的其余部分。

    x=[a n . 1. 2. ...0 m b m

    这意味着 m m+1 .

    换句话说,小数部分的十进制对数 x –m –m+1 .

    反过来,这意味着小数对数的整部分是 x 等于 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));

    以同样的方式,正整数 x n 表示它的十进制数字当且仅当 n - 1 <= log10(x) < n .

    因此,十进制表示中的位数 floor(log10(x)) + 1 .

    也就是说,我不建议在实践中使用这种确定位数的方法。 log10

        2
  •  2
  •   Amit Sheen    7 年前

    while 回路:

    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");
    }
    

    然后只需将要检查的数字传递到函数中:

    CountZeros(1.0034);
    
        3
  •  2
  •   Matt Newelski    7 年前

    while() 比较 .floor(n) 具有的值 n.toFixed(x) x 直到两者不相等:

    console.log(getZeros(0));           //0
    console.log(getZeros(1));           //0
    console.log(getZeros(1.0));         //0
    console.log(getZeros(1.1));         //0
    console.log(getZeros(1.01));        //1
    console.log(getZeros(1.00003456));  //4
    
    function getZeros(num) {
        var x = 0;
        if(num % 1 === 0) return x;
        while(Math.floor(num)==num.toFixed(x)) {x++;}
        return(x-1);
    }
        4
  •  0
  •   Ahmed Can Unbay    7 年前

    你可以用 toFixed() .

    注:

    最大长度 toFixed() . 如前所述 in the docs

    var num = 12.0003400;
    
    var lengthAfterThePoint = 7;
    var l = num.toFixed(lengthAfterThePoint);
    var pointFound = false;
    var totalZeros = 0;
    
    for(var i = 0; i < l.length; i++){
      if(pointFound == false){
        if(l[i] == '.'){
          pointFound = true;
        }
      }else{
        if(l[i] != 0){
          break;
        }else{
          totalZeros++;
        }
      }
    }
    console.log(totalZeros);
    

    这是我的额外答案,在这个函数中,程序计算所有的零,直到最后一个

    var num = 12.034000005608000;
    
    var lengthAfterThePoint = 15;
    var l = num.toFixed(lengthAfterThePoint);
    var pointFound = false;
    var theArr = [];
    
    for(var i = 0; i < l.length; i++){
      if(pointFound == false){
        if(l[i] == '.'){
          pointFound = true;
        }
      }else{
        theArr.push(l[i]);
      }
    }
    
    
    var firstNumFound = false;
    var totalZeros = 0;
    
    for(var j = 0; j < theArr.length; j++){
      if(firstNumFound == false){
        if(theArr[j] != 0){
          firstNumFound = true;
          totalZeros = totalZeros + j;
        }
      }else{
        if(theArr[j] == 0){
          totalZeros++;
        }
      }
    }
    
    
    var totalZerosLeft = 0;
    for (var k = theArr.length; k > 0; k--) {
      if(theArr[k -1] == 0){
        totalZerosLeft++;
      }else{
        break;
      }
    }
    
    console.log(totalZeros - totalZerosLeft);