![](/img/trans.png)
[英]Determine if a floating point number is a multiple of another floating point number?
[英]Determine number of leading zeros in a floating point number
如何计算小数点后但在浮点数中第一个非零之前有多少个零。 例子:
0 -> 0
1 -> 0
1.0 -> 0
1.1 -> 0
1.01 -> 1
1.00003456 ->4
直觉上我假设有一个数学函数提供这个,或者至少是主要部分。 但我既不记得也不知道哪一个。
我知道可以通过首先将数字转换为字符串来完成,只要数字不是科学记数法,但我想要一个纯数学解决方案。
在我的情况下,如果这是一个复杂的问题,我不需要一些适用于负数的东西。
无论语言如何,我想知道一般的方法是什么。
但如果有一个非常标准的数学函数,我也想知道JavaScript是否具有此功能。
作为旁注,我想知道这个计算是否与确定整数的十进制表示所需的位数的方法有关。
设x
是一个非整数,可以写成整个部分的n
数字,然后是小数点,然后是m
,然后是小数部分的其余部分。
x = [a 1 a 2 ... a n ] 。 [0 1 0 2 ... 0 m ] [b 1 b 2 ... b m ]
这意味着x
的小数部分大于或等于10- m ,小于10- 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));
作为旁注,我想知道这个计算是否与确定整数的十进制表示所需的位数的方法有关。
以相同的方式,当且仅当n - 1 <= log10(x) < n
,正整数x
需要n
十进制数来表示它。
因此x
的十进制表示中的位数是floor(log10(x)) + 1
。
也就是说,我不建议使用这种方法来确定实际的位数。 log10
不能保证给出对数的精确值(甚至不像IEEE 754允许的那样精确),这可能会导致某些边缘情况下的结果不正确。
你可以用一个简单的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);
我的方法是使用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); }
你可以用toFixed()
方法做到这一点,但是我的代码中只有一个缺陷,你需要指定点之后的数字长度.
这是因为该方法的使用方式。
注意:
toFixed()
方法的最大长度为20,因此请勿输入超过20个数字.
正如文档中所述
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.