非常奇怪的操作结果 [英] very strange operation result

查看:81
本文介绍了非常奇怪的操作结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以在javascript 中试试这个:


A)警报(1.9 * 50); = 95

B)警报(2.9 * 50); = 145

C)警报(3.9 * 50); = 195

D)警报(4.9 * 50); = 245.00000000000003

E)警报(5.9 * 50); = 295


为什么D)= 245.00000000000003?


谢谢,

毛利人。

You can try this in javascript:

A) alert( 1.9 * 50 ); = 95
B) alert( 2.9 * 50 ); = 145
C) alert( 3.9 * 50 ); = 195
D) alert( 4.9 * 50 ); = 245.00000000000003
E) alert( 5.9 * 50 ); = 295

Why D) = 245.00000000000003 ?

Thanks,
Maori.

推荐答案

2月8日,4:20 * pm,毛利人写道:
On Feb 8, 4:20*pm, maori wrote:

你可以在javascript中试试这个


A)警报(1.9 * 50); * * = * 95

B)警报(2.9 * 50); * * = * 145

C)警报(3.9 * 50); * * = * 195

D)警报(4.9 * 50); * * = * 245.00000000000003

E)警报(5.9 * 50); * * = * 295


为什么D)* = 245.00000000000003?
You can try this in javascript:

A) alert( 1.9 * 50 ); * *= * 95
B) alert( 2.9 * 50 ); * *= * 145
C) alert( 3.9 * 50 ); * *= * 195
D) alert( 4.9 * 50 ); * *= * 245.00000000000003
E) alert( 5.9 * 50 ); * *= * 295

Why D) *= 245.00000000000003 ?



Javascript'唯一的数字数据类型是IEEE双精度

浮点数。这种类型的数字可以精确地表示许多数字

,而其他数字只能近似。您已经遇到了

其中一个只能近似的数字。


这不应该是一个意想不到的功能,因为它在系统中很常见

代表数字。例如,1 * / b
除以3的十进制表示不太可能是精确的(因为它将无限长
),而相同的数字可以精确地表示在基础3为

0.1。

Javascript''s only numeric data type is an IEEE double precision
floating point number. That type of number can represent many numbers
precisely, and others it can only approximate. You have encountered
one of the numbers that can only be approximated.

This should not be an unexpected feature as it is common in systems
for representing numbers. For example, a decimal representation of 1
divided by 3 is unlikely to be precise (because it would be infinitely
long), while the same number can be precisely represented in base 3 as
0.1.


而且,我如何在javascript中处理这个问题呢?

亨利 < rc ******* @raindrop.co.ukescribióenel mensaje

news:b9 ******************** ************** @ d4g2000p rg.googlegroups.com ...

2月8日下午4:20,毛利人写道:
And, how can I deal with this in javascript, please?
"Henry" <rc*******@raindrop.co.ukescribió en el mensaje
news:b9**********************************@d4g2000p rg.googlegroups.com...
On Feb 8, 4:20 pm, maori wrote:

你可以在javascript 中试试这个:


A)alert(1.9 * 50); = 95

B)警报(2.9 * 50); = 145

C)警报(3.9 * 50); = 195

D)警报(4.9 * 50); = 245.00000000000003

E)警报(5.9 * 50); = 295


为什么D)= 245.00000000000003?
You can try this in javascript:

A) alert( 1.9 * 50 ); = 95
B) alert( 2.9 * 50 ); = 145
C) alert( 3.9 * 50 ); = 195
D) alert( 4.9 * 50 ); = 245.00000000000003
E) alert( 5.9 * 50 ); = 295

Why D) = 245.00000000000003 ?



Javascript'唯一的数字数据类型是IEEE双精度

浮点数。这种类型的数字可以精确地表示许多数字

,而其他数字只能近似。您已经遇到了

其中一个只能近似的数字。


这不应该是一个意想不到的功能,因为它在系统中很常见

代表数字。例如,1 * / b
除以3的十进制表示不太可能是精确的(因为它将无限长
),而相同的数字可以精确地表示在基数3为

0.1。

Javascript''s only numeric data type is an IEEE double precision
floating point number. That type of number can represent many numbers
precisely, and others it can only approximate. You have encountered
one of the numbers that can only be approximated.

This should not be an unexpected feature as it is common in systems
for representing numbers. For example, a decimal representation of 1
divided by 3 is unlikely to be precise (because it would be infinitely
long), while the same number can be precisely represented in base 3 as
0.1.


timothytoe于2008年2月8日在comp.lang.javascript中写道
timothytoe wrote on 08 feb 2008 in comp.lang.javascript:

你发现的并不让我感到惊讶。我已经看过这样的事情了。几十年。我唯一能记住的系统没有那个问题

是Atari 8位,它打开了二进制编码的十进制位,在

为6502的Atari BASIC。
What you found did not surprise me. I''ve seen that kind of things for
decades. The only system I can remember that didn''t have that problem
was the Atari 8-bit, which turned on the binary coded decimal bit in
the 6502 for Atari BASIC.



Signetics / Philips 2650微处理器的中央数据基础,

据传是由一位名叫William Gates的年轻人写的,

提供BCD作为它的标准数学引擎。


还有更多:


< http ://en.wikipedia.org/wiki/Binary-coded_decimal>


它是用于金融编程的,在代码中大多数无线电时钟在

欧洲从法兰克福长波发射器和许多其他

applocations接收。


-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)


Central Data Basic for the Signetics/Philips 2650 microprosessor,
rumoured to have been written by a youth called William Gates,
usded BCD as it''s standard Math engine.

And there were more:

<http://en.wikipedia.org/wiki/Binary-coded_decimal>

It is stil used in financial programming, in the code most radio clocks in
Europe receive from the Frankfurt long wave transmitter, and many other
applocations.

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


这篇关于非常奇怪的操作结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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