JavaScript在处理整数时确实会出错 [英] JavaScript does make errors when dealing just with integers

查看:45
本文介绍了JavaScript在处理整数时确实会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与JavaScript字段中的一个权限相反:

JavaScript在处理整数时确实会出错。


此权限(Douglas Crockford) 。)说:

浮点整数运算[正如JS使用的]是精确的


嗯,我可以用这个程序证明这是不正确的:
http://mynichecomputing.com/digitallearning/yourOwn.htm


这个程序只使用整数,但在使用时会增加或计算

。这里是如何做实验和看到:


不添加小于1的任意十进制数(我相信

我使用.9),

这个程序的计数会很短(另外)。

所以,从程序中省略.9所以程序依赖于

pure exact

JavaScript添加。现在,作为第2步:为库存评分设置程序

通过使用

程序设置的一些示例答案

为库存描述(请参阅网页上的链接我

给了你

代码,用于指示) - 项目可能依赖于



一个等级和几个项目得分每个等级up(设置为每个

)。

那么:使用该评分系统

在几组客户端答案中运行(你也可以自己制作样品。

如果你从代码中省略了我的+ .9,那么你会发现数量很快。


您会发现计数很短。这对投票机器来说是灾难性的。

解决方案

lorlarz< lo **** *@gmail.comwrites:


与JavaScript字段中的一个权限相反:

JavaScript在处理时遇到错误整数。


这个权威(道格拉斯克罗克福德。)说:

浮点整数算术[正如JS所用]是确切的


好​​吧,我可以用这个程序证明这是不正确的:
http://mynichecomputing.com/digitallearning/yourOwn.htm


这是一个只使用整数的程序,但它的简称

新增或计算

使用时。这里是如何做实验和看到:


不添加小于1的任意十进制数(我相信

我使用.9),

这个程序的计数会很短(另外)。

所以,从程序中省略.9所以程序依赖于

pure exact

JavaScript添加。现在,作为第2步:为库存评分设置程序

通过使用

程序设置的一些示例答案

为库存描述(请参阅网页上的链接我

给了你

代码,用于指示) - 项目可能依赖于



一个等级和几个项目得分每个等级up(设置为每个

)。

那么:使用该评分系统

在几组客户端答案中运行(你也可以自己制作样品。

如果你从代码中省略了我的+ .9,那么你会发现数量很快。


您会发现计数很短。这对投票机器来说是个灾难。



万一它逃脱了你:0.9不是一个整数,所以你不是使用只是整数的
。我们已经做到了这一点。


参见:

http://docs.sun.com/source/806-3568/ncg_goldberg.html


-

Joost Diepenmaat |博客: http://joost.zeekat.nl/ |工作: http://zeekat.nl/

lorlarz写道:


与JavaScript字段中的一个权限相反:

JavaScript在处理时确实会出错用整数。

我用.9),



有趣的整数。


< blockquote> 8月19日下午2:04 * pm,Joost Diepenmaat< jo ... @ zeekat.nlwrote:


>

万一它逃脱了你:0.9不是一个整数,所以你不是使用只是整数的
。我们已经做到了这一点。



.9是一个需要更正的因素,而不是计划中的方式

**失败**。


.9是必须添加的,以使纯整数算术

停止制作

错误并且做得很短。在实验中,你把它拿出来。


一旦你删除.9,所有都是整数,数学应该是

确切的,

但事实并非如此。


Contrary to what one authority in the JavaScript field says:
JavaScript does make errors when dealing with just with integers.

This authority (Douglas Crockford.) says:
"integer arithmetic in floating point [as JS uses] is exact"

Well, I can prove this is incorrect with this program:
http://mynichecomputing.com/digitallearning/yourOwn.htm

This a program that uses only integers, yet comes up short in its
addition or count
when used. HERE IS HOW TO DO THE EXPERIMENT AND SEE:

Without adding on an arbitrary decimal number less than one (I believe
I use .9) ,
this program will come up short in its count(addition).
So, omit that .9 from the program so the program is relying on the
pure exact
JavaScript addition. NOW, as a step 2: Set up the program
for "inventory scoring" by using some sample answers set using the
procedure
described for doing so for an inventory (see link on the web page I
gave you
the code for, for the directions) -- where an item may count on more
than
one scale and several items score each scale up (set up to do some of
each).
THEN: Use that scoring system
in several runs on sets of client answers (samples you also make
yourself)
and you will find the count coming up SHORT if you omit my + .9 from
the code.

You will find the count short. This would be disasterous in a voting
machine.

解决方案

lorlarz <lo*****@gmail.comwrites:

Contrary to what one authority in the JavaScript field says:
JavaScript does make errors when dealing with just with integers.

This authority (Douglas Crockford.) says:
"integer arithmetic in floating point [as JS uses] is exact"

Well, I can prove this is incorrect with this program:
http://mynichecomputing.com/digitallearning/yourOwn.htm

This a program that uses only integers, yet comes up short in its
addition or count
when used. HERE IS HOW TO DO THE EXPERIMENT AND SEE:

Without adding on an arbitrary decimal number less than one (I believe
I use .9) ,
this program will come up short in its count(addition).
So, omit that .9 from the program so the program is relying on the
pure exact
JavaScript addition. NOW, as a step 2: Set up the program
for "inventory scoring" by using some sample answers set using the
procedure
described for doing so for an inventory (see link on the web page I
gave you
the code for, for the directions) -- where an item may count on more
than
one scale and several items score each scale up (set up to do some of
each).
THEN: Use that scoring system
in several runs on sets of client answers (samples you also make
yourself)
and you will find the count coming up SHORT if you omit my + .9 from
the code.

You will find the count short. This would be disasterous in a voting
machine.


Just in case it escaped you: 0.9 is NOT an integer, so you are NOT
working with "just integers". We''ve done this to death.

See also:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


lorlarz wrote:

Contrary to what one authority in the JavaScript field says:
JavaScript does make errors when dealing with just with integers.
I use .9) ,

Funny looking integer.


On Aug 19, 2:04*pm, Joost Diepenmaat <jo...@zeekat.nlwrote:

>
Just in case it escaped you: 0.9 is NOT an integer, so you are NOT
working with "just integers". We''ve done this to death.

.9 IS A NEEDED CORRECTION FACTOR AND NOT IN THE PROGRAM THE WAY IT
**FAILS**.

The .9 is what has to be added to make the pure integer arithmetic
stop making
errors and coming up short. In the experiment, you take it OUT.

Once you remove the .9, all are integers and the math is supposed to
be exact,
and it is not.


这篇关于JavaScript在处理整数时确实会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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