了解浮点变量 [英] understanding floating point variables

查看:33
本文介绍了了解浮点变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点问题,反正我看不懂.

There is some problem, i can't understand anyway.

请看这段代码

<script type="text/javascript">
function math(x)
{
 var y;
 y = x*10;
 alert(y);
}

</script>
<input type="button" onclick="math(0.011)">

<小时>

点击按钮后必须提醒什么?我认为 0.11,但不,它会提醒0.10999999999999999请解释这种行为.提前致谢


What must be alerted after i click on button? i think 0.11, but no, it alerts 0.10999999999999999 explain please this behavior. thanks in advance

推荐答案

这是因为你正在处理浮点,这是浮点数学的预期行为.

this is because you are dealing with floating point, and this is the expected behavior of floating point math.

你需要做的是格式化那个数字.

what you need to do is format that number.

看这个java解释如果您想知道为什么会发生这种情况,也适用于此.

see this java explanation which also applies here if you want to know why this is happening.

在 javascript 中,所有数字都表示为 64 位浮点数,因此您经常会遇到这种情况.

in javascript all numbers are represented as 64bit floats, so you will run into this sort of thing often.

该文章的快速概述是浮点尝试表示一系列值更大然后适合 64 位,因此会有一些不精确的表示,这就是你所看到的.

the quick overview of that article is that floating point tries to represent a range of values larger then would fit in 64bits, therefor there is going to be some imprecise representation, and this is what you are seeing.

这篇关于了解浮点变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆