JS数字如何在内部表示? [英] How are JS numbers represented internally??

查看:60
本文介绍了JS数字如何在内部表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一些问题需要了解JS数字的代表情况

内部。


以此代码作为一个奇怪的例子:


var largest = Number.MAX_VALUE;

var smaller = Number.MAX_VALUE - 1;


警报(最大>较小);

警报(最大==较小);


此输出false然后真实不是真实的那么假正如我所料!


这里发生了什么?这是与精度有关吗?


我正在寻找的是可以用

javascript表示的最大可能整数,但我希望它在非指数形式,即

123456789012345678901234567890 NOT 1.234e + 123.


Thx

Hi

I''m have some problem understanding how JS numbers are represented
internally.

Take this code for an example of weirdness:

var biggest = Number.MAX_VALUE;
var smaller = Number.MAX_VALUE - 1;

alert(biggest > smaller);
alert(biggest == smaller);

This outputs "false" then "true" not "true" then "false" as I''d expect!

What''s going on here? Is this to do with precision?

What I''m looking for is the largest possible integer representable by
javascript, but I want it in non-exponential form, i.e.
123456789012345678901234567890 NOT 1.234e+123.

Thx

推荐答案

bo******@gmx.net 写道:
bo******@gmx.net writes:
我在理解如何在内部表示JS数字时遇到一些问题。


它们被指定为64位IEEE浮点数。

使用此代码作为一个奇怪的例子:

var largest = Number.MAX_VALUE;
var小= Number.MAX_VALUE - 1;

警报(最大>较小);
警报(最大==较小);

这输出假然后真实不是真实的那么假正如我所料!

这里发生了什么?这与精确度有关吗?


是的。第一个不能用64位
浮点数表示的整数是2 ^ 52 + 1。这是因为数字是

,表示为52位尾数(+ 1符号位)和10位指数

(+ 1符号位)。这样你最多可以有52个有效位,

和2 ^ 52 + 1是二进制

10000000000000000000000000000000000000000000000000 001

需要53位详细说明。

我正在寻找的是
javascript可表示的最大可能整数,但我希望它是非指数形式,即
123456789012345678901234567890 NOT 1.234e +123。
I''m have some problem understanding how JS numbers are represented
internally.
They are specified to work as 64 bit IEEE floating point numbers.
Take this code for an example of weirdness:

var biggest = Number.MAX_VALUE;
var smaller = Number.MAX_VALUE - 1;

alert(biggest > smaller);
alert(biggest == smaller);

This outputs "false" then "true" not "true" then "false" as I''d expect!

What''s going on here? Is this to do with precision?
Yes. The first integer that cannot be represented by a 64 bit
floating point number is 2^52+1. This is because the number is
represented as 52 bits of mantissa (+ 1 sign bit) and 10 bit exponent
(+ 1 sign bit). You can at most have 52 significant bits in this way,
and 2^52+1 is binary
10000000000000000000000000000000000000000000000000 001
which needs 53 bits of precission.
What I''m looking for is the largest possible integer representable by
javascript, but I want it in non-e0ponential form, i.e.
123456789012345678901234567890 NOT 1.234e+123.




数字是(2 ^ 52-1)* 2 ^(2 ^ 10-52)。这个数字是Javascript

通常输出为1.7976931348623157e + 308(这不是确切的,

但确实建议您需要309个十进制数字来写它:)


用二进制文件很容易做到:521后跟9720。

十进制它的:


17976931348623155085612432838450624023434343715745 93359244048724485818457545561143884706399431262203 21960804027157371570809852884964511743044087662767 60090959433192772823707887618876057953256376869865 40648252621157710157914639830148577040081234194593 86245141723703148097529108423358883457665451722744 025579520


(看出来的换行符:)


/ L

-

Lasse Reichstein Nielsen - lr * @ hotpop。 com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只是降低了s pirit divine。''



The number is (2^52-1)*2^(2^10-52). It is this number that Javascript
typically outputs as 1.7976931348623157e+308 (which is not exact,
but does suggest that you need 309 decimal digits to write it :)

It''s easy to do in binary: 52 "1"''s followed by 972 "0"''s.
In decimal it''s:

17976931348623155085612432838450624023434343715745 93359244048724485818457545561143884706399431262203 21960804027157371570809852884964511743044087662767 60090959433192772823707887618876057953256376869865 40648252621157710157914639830148577040081234194593 86245141723703148097529108423358883457665451722744 025579520

(look out for line breaks :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


> >我正在寻找的是可以通过
> > What I''m looking for is the largest possible integer representable by
javascript表示的最大可能整数,但我想以非指数形式表示,即
123456789012345678901234567890 NOT 1.234 e + 123。
javascript, but I want it in non-e0ponential form, i.e.
123456789012345678901234567890 NOT 1.234e+123.



数字是(2 ^ 52-1)* 2 ^(2 ^ 10-52)。这个数字Javascript
通常输出为1.7976931348623157e + 308(这不是确切的,但确实建议你需要309个十进制数字来写它:)

它用二进制文件很容易做到:521之后是9720。
以小数形式表示:

17976931348623155085612432838450624023434343715745 93359244048724485818457545561143884706399431262203 21960804027157371570809852884964511743044087662767 60090959433192772823707887618876057953256376869865 40648252621157710157914639830148577040081234194593 86245141723703148097529108423358883457665451722744 025579520

(注意换行符:):



The number is (2^52-1)*2^(2^10-52). It is this number that Javascript
typically outputs as 1.7976931348623157e+308 (which is not exact,
but does suggest that you need 309 decimal digits to write it :)

It''s easy to do in binary: 52 "1"''s followed by 972 "0"''s.
In decimal it''s:

17976931348623155085612432838450624023434343715745 93359244048724485818457545561143884706399431262203 21960804027157371570809852884964511743044087662767 60090959433192772823707887618876057953256376869865 40648252621157710157914639830148577040081234194593 86245141723703148097529108423358883457665451722744 025579520

(look out for line breaks :)




谢谢,这太棒了!


您是否知道如何在javascript中输出上述数字(或任意

数字)作为字符串?


Number.MAX_VALUE.toString()只是给了我指数形式。


我想它已经有了与操作二进制数

直接相关并使用按位移位将其转换为十进制形式和

迭代(??),但我不知道在哪里到开始(不习惯

直接使用二进制数)。你能指点我正确的方向吗?谢谢!



Thank you, that''s great!

Do you know of a way to output the above number (or any arbitrary
number) in javascript as a string?

Number.MAX_VALUE.toString() just gives me the exponential form.

I guess it''s got something to do with manipulating the binary number
directly and converting it into decimal form using bitwise shifts and
iteration (??), but I have no clue as to where to start (not used to
working directly with binary numbers). Could you point me in the right
direction? Thanks!


bo ****** @ gmx .net 在3/17/2006 8:00 PM说:
bo******@gmx.net said the following on 3/17/2006 8:00 PM:
我在找什么for是可用javascript表示的最大可能整数,但我希望它以非指数形式出现,即
123456789012345678901234567890 NOT 1.234e + 123。数字是(2 ^ 52-1)* 2 ^(2 ^ 10-52)。这个数字Javascript
通常输出为1.7976931348623157e + 308(这不是确切的,但确实建议你需要309个十进制数字来写它:)

它用二进制文件很容易做到:521之后是9720。
以小数形式表示:

17976931348623155085612432838450624023434343715745 93359244048724485818457545561143884706399431262203 21960804027157371570809852884964511743044087662767 60090959433192772823707887618876057953256376869865 40648252621157710157914639830148577040081234194593 86245141723703148097529108423358883457665451722744 025579520

(注意换行符:)
What I''m looking for is the largest possible integer representable by
javascript, but I want it in non-e0ponential form, i.e.
123456789012345678901234567890 NOT 1.234e+123. The number is (2^52-1)*2^(2^10-52). It is this number that Javascript
typically outputs as 1.7976931348623157e+308 (which is not exact,
but does suggest that you need 309 decimal digits to write it :)

It''s easy to do in binary: 52 "1"''s followed by 972 "0"''s.
In decimal it''s:

17976931348623155085612432838450624023434343715745 93359244048724485818457545561143884706399431262203 21960804027157371570809852884964511743044087662767 60090959433192772823707887618876057953256376869865 40648252621157710157914639830148577040081234194593 86245141723703148097529108423358883457665451722744 025579520

(look out for line breaks :)



谢谢,这太棒了!

你知道一种在javascript中输出上述数字(或任意
数字)作为字符串的方法吗?



Thank you, that''s great!

Do you know of a way to output the above number (or any arbitrary
number) in javascript as a string?




var

maxValue =" 1797693134862315508561243283845062402343 434371574593359244048724485818457545 56114388470639 94312622032196080402715737157080985288496451174304 40876627676009095943319277282370788761887605795325 63768698654064825262115771015791463983014857704008 12341945938624514172370314809752910842335888345766 5451722744025579520英寸;


现在,它是一个字符串:)

Number.MAX_VALUE.toString()只是给我的指数形式。


由于它具有精确的能力。

我想它与直接操纵二进制数有关。使用按位移位和
迭代(??)将其转换为十进制形式,但我不知道从哪里开始(不习惯直接使用二进制数)。


与此无关。

你能指出我正确的方向吗?谢谢!



var
maxValue="1797693134862315508561243283845062402343 43437157459335924404872448581845754556114388470639 94312622032196080402715737157080985288496451174304 40876627676009095943319277282370788761887605795325 63768698654064825262115771015791463983014857704008 12341945938624514172370314809752910842335888345766 5451722744025579520";

Now, its a string :)

Number.MAX_VALUE.toString() just gives me the exponential form.
Due to it''s precision abilities.
I guess it''s got something to do with manipulating the binary number
directly and converting it into decimal form using bitwise shifts and
iteration (??), but I have no clue as to where to start (not used to
working directly with binary numbers).
Has nothing to do with that.
Could you point me in the right direction? Thanks!




见上文。

-

兰迪

comp。 lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /



See above.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


这篇关于JS数字如何在内部表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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