为什么用processing.js从value中减去-0.2会把一个整数变成无理数?
Why would subtracting -0.2 from value with processing.js turn an integer into an irrational number?
我正在为一个随时间衰减的对象循环if语句,每次取值并减去-0.2
。第一次通过后,数字会变得像0.9653677422
一样奇怪,而不是保持0.2 (4.2, 4.0, 3.8, 3.6...etc)
的倍数。
初始值为7
。
的例子:
if (this.health > 0){
this.health = this.health - 0.2;
}else{
return;
}
为什么从processing.js的值中减去-0.2
会把整数变成无理数?
编辑* * *通过adonike的答案,我了解到小数不能用浮点二进制数100%精确地表示。通过将所有数字保持为整数(将初始值乘以10并减去值),可以保留相同的衰减率(每个循环1/35)而不会失去精度。数字变为70
表示初始值,2
表示衰减率(减去数字)&一切顺利。
也可以用35
表示初始值,用1
表示减去后得到的相同结果。
要进一步了解这一点,请参见:为什么十进制数字不能精确地用二进制表示?以及Kevin Workman的回答中提供的链接。
这个问题的代码如下所示:
var Tile = function(x, y, sourceImg) {
this.x = x;
this.y = y;
this.width = 25;
this.health = 35;
this.healthMax = this.health;
this.decayRate = 1;
this.daysOld = 0;
};
和
Tile.prototype.drawPlantTiles = function() {
if (this.health > this.healthMax/2){
this.occupied = true;
fill(168, 118, 25);
stroke(163, 109, 21);
rect(this.x, this.y, this.width, this.width,0);
strokeWeight(1);
noStroke();
image(plantImg1, this.x, this.y, this.width, this.width);
this.health = this.health - this.decayRate;
this.daysOld++;
} else if (this.health > this.healthMax/4 && this.health <= this.healthMax/2){
this.occupied = true;
fill(168, 118, 25);
stroke(163, 109, 21);
rect(this.x, this.y, this.width, this.width,0);
strokeWeight(1);
noStroke();
image(plantImg2, this.x, this.y, this.width, this.width);
this.health = this.health - this.decayRate;
this.daysOld++;
} else if (this.health > 0 && this.health <= this.healthMax/4){
this.occupied = true;
fill(168, 118, 25);
stroke(163, 109, 21);
rect(this.x, this.y, this.width, this.width,0);
strokeWeight(1);
noStroke();
image(plantImg3, this.x, this.y, this.width, this.width);
this.health = this.health - this.decayRate;
this.daysOld++;
}else{
fill(168, 118, 25);
stroke(163, 109, 21);
rect(this.x, this.y, this.width, this.width,0);
strokeWeight(1);
noStroke();
this.state = false;
return;
}
};
注意:"this."healthMax"阈值仍然需要重新设计,以删除所有小数/保持100%的精度
另一个答案是正确的,但我想提供更多的细节。
每个计算机科学家都应该知道的浮点运算这里是一个简短的,JavaScript特定的版本:每个JavaScript开发人员应该知道关于浮点
文章列出了一些库,它们是解决这个问题的方法:
- sinful.js
- math.js
- BigDecimal.js
- 您也可以使用现有的
toPrecision()
和toFixed()
函数
参见:
-
如何处理JavaScript中的浮点数精度?
-
浮点数学坏了吗?
这是因为浮点运算不是100%准确的。没有办法用二进制形式表示小数
在javascript中,所有数字都被视为64位浮点数。
为了帮助您的特殊情况(使用0.2或-0.2的增量),您可以在进行增量之前乘以10,操作然后除以10:
if (this.health > 0){
this.health = (this.health * 10 - 2) / 10;
}else{
return;
}
相关文章:
- 我想在AngularJS应用程序中创建一个输入数字框,用户不应该在该框上键入十进制数字.(一个整数输入框)
- Javascript-当存在's是一个整数标记
- 如何拆分一个整数和产生的所有数字来创建一个新数字
- 找到字符串值,但需要一个整数
- 使用javascript向上取整到下一个整数,即使值为1.0000000001
- 只使用一个整数表示颜色-Javascript
- 在字符串名称后附加一个整数
- JavaScript:将一个整数与一长串字符串关联起来
- 如何传递一个整数来创建新的变量名
- 将浮点数向上舍入到 javascript 中的下一个整数
- node.js - 发送一个整数作为 POST 请求中的输入参数
- 在类名的末尾添加一个整数
- 正则表达式仅检查一个整数
- 正则表达式,用于将字符串中的最后一个整数与 php 和 javascript 匹配
- 使用 node-soap 传递一个整数数组
- 求一个整数数组的最高子集,其总和加起来就是给定的目标
- 如何将一个整数数组作为参数从javascript传递到python
- 在Javascript中将rgba值转换为一个整数
- 我需要将一个整数除以12,如果结果是浮点值,则用javascript将其加1
- 为什么在控制台上记录对象引用时会显示一个整数