为什么这个数字会增加一个? [英] Why does this number get increased by one?

查看:83
本文介绍了为什么这个数字会增加一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre class =snippet-code-js lang-js prettyprint-override> console.log(10209761399365907);



为什么这段代码输出的数字大一(10209761399365908而不是10209761399365907)?



这仅针对此特定数字发生。例如,使用10155071933693662,我得到正确的值(10155071933693662)。



我需要知道有关该特定号码的事情吗?我想出的唯一解决方法是将值作为字符串传递。

解决方案

您的数值太大而无法准确表示作为64位浮点数。你得到的值是近似值。 JavaScript本身只有64位浮点数;在C或C ++中适合 long 的值可能太大而无法准确表示,因为64位中的一些用于指数在浮点表示。 (折衷的是,对于浮点数,可以使用 更大或更小的数值幅度的值的近似值,而不是简单的整数表示。)



如果将值作为数字操作并不重要,请将其设为字符串:

  console.log( 10209761399365907); 

或正如我最初提出的问题所建议的那样:

 < input type =buttonvalue =Invitaonclick =Invite('@ us.FacebookID')/> 

其中FacebookID是有问题的大号。



有一个常量(在较新的JavaScript环境中),名为 Number.MAX_SAFE_INTEGER 。这告诉你你可以表示的数字有多大,以便精确地表示该值;也就是说,64位浮点尾数将包含整个值的显式位。



可以精确表示较大的偶数整数,取决于它们的二进制表示的低端有多少个零位。因此, 1152921504606846800 可以准确表示,因为它在二进制表示的低端有7位全零。但是, 1152921504606846801 1152921504606846800 的形式出现,因为低 1 有点无处可去。 (未表示的位被假定为零。)



附录:这是提出的问题的(几乎)原始版本,用于解释上面可能令人困惑的示例代码:






我遇到的Javascript问题非常奇怪。
我有一些来自我的服务器的ID,当我按下按钮时,参数被传递给一个方法来执行请求。



这是代码:

 < script type =text / javascript> 
函数邀请(id)
{
alert(id);
}
< / script>

另一方面,我有:



(CSHTML语法)

 < input type =buttonvalue =Invitaonclick =邀请(@us) .FacebookID)/> 

成为:

 < input type =buttonvalue =Invitaonclick =Invite(10209761399365907)> 

但是当我点击按钮时......方法接收的参数数量由ONE增加! / p>



这只发生在一个特定的ID上!
例如,使用10155071933693662我得到了正确的值(10155071933693662)



我需要知道有关该特定数字的信息吗?所有这一切都很奇怪,我想出的唯一解决方法是将值作为字符串传递!


console.log(10209761399365907);

Why does this code output a number greater by one (10209761399365908 instead of 10209761399365907)?

This is happening only for this specific number. For instance with 10155071933693662 I get the correct value (10155071933693662).

Is there something I need to know about that specific number? The only workaround I figured out is to pass the value as a string.

解决方案

Your numeric value is too large to be represented exactly as a 64-bit floating point number. The value you get is an approximation. JavaScript natively only has 64-bit floating point numbers; values that do fit in a long in C or C++ may be too large to be represented exactly because some of the 64 bits are used for an exponent in floating point representation. (The tradeoff is that with a floating point it's possible to work with approximations for values with much larger or smaller numeric magnitude than is possible with simple integer representation.)

If it's not important to manipulate the value as a number, make it a string:

console.log("10209761399365907");

or as I suggested for the question as it originally appeared:

<input type="button" value="Invita" onclick="Invite('@us.FacebookID')" />

where that "FacebookID" was the big number in question.

There's a constant (in newer JavaScript environments) called Number.MAX_SAFE_INTEGER. That tells you how big a number you can represent such that the value will be represented exactly; that is, that the 64-bit floating point mantissa will contain explicit bits for the entire value.

Larger even integers can be represented exactly, depending on how many zero bits are at the low end of their binary representation. Thus, 1152921504606846800 can be exactly represented because it's got 7 bits of all zeros at the low end of the binary representation. However, 1152921504606846801 comes out as 1152921504606846800 because that low 1 bit has nowhere to go. (Unrepresented bits are assumed to be zero.)

Addendum: Here's the (almost) original version of the question as posed, to explain the possibly confusing example code above:


I'm facing a problem with Javascript that is quite weird. I have some ids coming from my server and when I press a button the parameter is passed to a method to perform a request.

This is the code:

<script type="text/javascript">
function Invite(id)
{
    alert(id);
}
</script>

On the other hand, I have:

(CSHTML syntax)

 <input type="button" value="Invita" onclick="Invite(@us.FacebookID)" />

That became:

<input type="button" value="Invita" onclick="Invite(10209761399365907)">

But when I click the button... the number the method receives the parameter INCREASED by ONE!

And this is happening ONLY for one specific ID! For instance, with 10155071933693662 I get the correct value (10155071933693662)

Is there something I need to know about that specific number? All this is really strange and the only workaround I figured out is to pass the value as a string!

这篇关于为什么这个数字会增加一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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