Mathematica 内部数字格式和精度 [英] Mathematica internal number formats and precision

查看:129
本文介绍了Mathematica 内部数字格式和精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题相关,数字格式到底发生了什么?

Tangentially related to this question, what exactly is happening here with the number formatting?

In[1]  := InputForm @ 3.12987*10^-270
Out[1] := 3.12987`*^-270

In[2]  := InputForm @ 3.12987*10^-271
Out[2] := 3.1298700000000003`*^-271

如果你使用 *10.^ 作为乘数,过渡就是你天真地期望的地方:

If you use *10.^ as the multiplier the transition is where you would naively expect it to be:

In[3]  := InputForm @ 3.12987*10.^-16
Out[3] := 3.12987`*^-16

In[4]  := InputForm @ 3.12987*10.^-17
Out[4] := 3.1298700000000004`*^-17

*^ 使过渡更进一步,尽管机器精度开始下降:

whereas *^ takes the transition a bit further, albeit it is the machine precision that starts flaking out:

In[5]  := InputForm @ 3.12987*^-308
Out[5] := 3.12987`*^-308

In[6]  := InputForm @ 3.12987*10.^-309
Out[6] := 3.12987`15.954589770191008*^-309

基地很晚才开始瓦解

In[7]  := InputForm @ 3.12987*^-595
Out[7] := 3.12987`15.954589770191005*^-595

In[8]  := InputForm @ 3.12987*^-596
Out[8] := 3.1298699999999999999999999999999999999999`15.954589770191005*^-596

我假设这些转换与 Mathematica 内部保存其数字的格式有关,但有没有人知道或愿意冒险进行有根据的猜测,如何?

I am assuming these transitions relate to the format in which Mathematica internally keeps it's numbers, but does anyone know, or care to hazard an educated guess at, how?

推荐答案

如果我理解正确,您会想知道 InputForm 何时会显示超过 6 位数字.如果是这样,它会偶然发生,每当需要更多数字来最好"表示评估后获得的数字时.由于评估涉及显式乘以 10^(一些幂),并且由于十进制输入不需要(在这种情况下不是)可以用二进制精确表示,因此您可能会与预期的有所不同.

If I understand correctly you are wondering as to when the InputForm will show more than 6 digits. If so, it happens haphazardly, whenever more digits are required to "best" represent the number obtained after evaluation. Since the evaluation involves explicit multiplication by 10^(some power), and since the decimal input need not be (and in this case is not) exactly representable in binary, you can get small differences from what you expect.

In[26]:= Table[3.12987*10^-j, {j, 10, 25}] // InputForm

Out[26]//InputForm=
{3.12987*^-10,
 3.12987*^-11, 
 3.12987*^-12, 
 3.12987*^-13, 
 3.12987*^-14, 
 3.12987*^-15, 
 3.12987*^-16, 
 3.1298700000000004*^-17, 
 3.1298700000000002*^-18, 
 3.12987*^-19, 
 3.12987*^-20, 
 3.1298699999999995*^-21, 
 3.1298700000000003*^-22, 
 3.1298700000000004*^-23, 
 3.1298700000000002*^-24, 
 3.1298699999999995*^-25}

至于 *^ 输入语法,这实际上是一个解析(实际上是词法)构造.没有计算明确的 10 的精确幂.构造了一个浮点值,它在二进制到十进制允许的范围内尽可能忠实于您的输入.InputForm 将显示与输入数字时使用的数字一样多的数字,因为这确实是与所创建的相应二进制值最接近的十进制数.

As for the *^ input syntax, that's effectively a parsing (actually lexical) construct. No explicit exact power of 10 is computed. A floating point value is constructed and it is faithful as possible, to the extent allowed by binary-to-decimal, to your input. The InputForm will show as many digits as were used in inputting the number, because that is indeed the closest decimal to the corresponding binary value that got created.

当您超越机器浮点数的限制时,您将获得任意精度的模拟.它不再是 machinePrecision 而实际上是 $MachinePrecision(这是 Mathematica 中机器浮点数的 bignum 类比).

When you surpass the limitations of machine floating point numbers, you get an arbitrary precision analog. It no longer is machinePrecision but actually is $MachinePrecision (that's the bignum analog to machine floats in Mathematica).

您在 InputForm 中看到的 3.12987*^-596(以 9 为结尾的十进制数)是由 Mathematica 涉及使用保护位的内部表示引起的.如果只有 53 个尾数位,类似于机器双精度,那么最接近的十进制表示将是预期的六位数字.

What you see in InputForm for 3.12987*^-596 (a decimal ending with a slew of 9's) is, I believe, caused by Mathematica's internal representation involving usage of guard bits. Were there only 53 mantissa bits, analogous to a machine double, then the closest decimal representation would be the expected six digits.

丹尼尔·利希布劳Wolfram 研究

Daniel Lichtblau Wolfram Research

这篇关于Mathematica 内部数字格式和精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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