为什么我收到“没有可显示的有效数字”在这个例子中?
V 8.0.4,Windows 7。
当我试图看看如果我能让 M 在硬件单精度中运行时是否可以获得更快的计算(我不知道即使这是否可能,我正在尝试)我注意到当我碰巧使 Precision 略小于 double,但比 single 更好时,AbsoluteTiming 返回 0,用于计时 Pause
:
这是一个示例:
我重新启动内核,然后键入
r=AbsoluteTiming[Pause[2]]
{2.00111440000,Null}
Accuracy[r]
Out[26]= 11.438897913739035
Now I set M to double
$MinPrecision=$MachinePrecision;
$MaxPrecision=$MachinePrecision;
r=AbsoluteTiming[Pause[2]]
Out[32]= {2.001114400000000,Null}
Accuracy[r]
Out[33]= 15.653317853034773
没问题。
但当我这样做时
prec=Log[10,2]*29;
$MaxPrecision=prec;
$MinPrecision=prec;
r=AbsoluteTiming[Pause[2]]
Out[41]= {0.,Null}
,你看,它是零。
Accuracy[r]
Out[42]= 307.6526555685888
但我一直试图看看它到底在哪里从零翻转到返回实际秒数,并在一次尝试中得到了这个
我知道 M 在内部使用任意精度数:
http://reference.wolfram.com/mathematica/tutorial/ArbitraryPrecisionNumbers.html
从上面的链接中可以看出:
In doing calculations that degrade precision, it is possible
to end up with numbers that have no significant digits at all
我的问题是:有人可以解释这个例子中的这种行为吗?为什么测量 Pause[]
的时间需要比单精度更高的精度?测量 AbsoluteTime
至少需要两倍的计算量,具体涉及哪些计算?帮助说:
AbsoluteTiming is always accurate down to a granularity of $TimeUnit
seconds, but on many systems is much more accurate.
实际上
In[22]:= $TimeUnit//N
Out[22]= 0.001
我的主要目的只是看看我是否可以让 M 使用硬件单精度浮点运行计算,只是看看它是否会运行得更快。我在一本书中读到,单精度可以是双精度的两倍,当我注意到这一点时,我正试图在我拥有的东西上测试这一点。
我目前在顶部运行所有内容,
$MinPrecision = $MachinePrecision;
$MaxPrecision = $MachinePrecision;
以便 M 使用硬件双倍运行。
谢谢, 附注我之前检查过控制台,没有发现语法错误。
V 8.0.4, windows 7.
As I was trying to see if I can get faster computation if I can get M to run in hardware single Precision (I do not know even if this is possible, I was trying things) I noticed that AbsoluteTiming returns 0 for just timing a Pause
when I happen to make the Precision little less than double, but better than single:
Here is an example:
I restart the kernel, and type
r=AbsoluteTiming[Pause[2]]
{2.00111440000,Null}
Accuracy[r]
Out[26]= 11.438897913739035
Now I set M to double
$MinPrecision=$MachinePrecision;
$MaxPrecision=$MachinePrecision;
r=AbsoluteTiming[Pause[2]]
Out[32]= {2.001114400000000,Null}
Accuracy[r]
Out[33]= 15.653317853034773
No problem.
But when I do
prec=Log[10,2]*29;
$MaxPrecision=prec;
$MinPrecision=prec;
r=AbsoluteTiming[Pause[2]]
Out[41]= {0.,Null}
You see, it is zero.
Accuracy[r]
Out[42]= 307.6526555685888
But I kept trying to see at where exactly it flips from zero to returning the actual seconds, and got this during one try
I know M uses Arbitrary Precision Numbers internally:
http://reference.wolfram.com/mathematica/tutorial/ArbitraryPrecisionNumbers.html
From the above link it says:
In doing calculations that degrade precision, it is possible
to end up with numbers that have no significant digits at all
My question here is: Could someone explain this behaviour in this example? Why would measuring the timing of just a Pause[]
require much more precision than single precision? What exactly are the computation involved that it requires at least double to measure the AbsoluteTime
? Help says:
AbsoluteTiming is always accurate down to a granularity of $TimeUnit
seconds, but on many systems is much more accurate.
and
In[22]:= $TimeUnit//N
Out[22]= 0.001
Actually my main purpose was just to see if I can make M run the computation using hardware single Precision floating points, just to see if it will run faster. I read in a book that single Precision can be 2 times as fast as double, and was just trying to test that on something I have when I noticed this.
I currently run everything with the following at the top
$MinPrecision = $MachinePrecision;
$MaxPrecision = $MachinePrecision;
So that M runs using hardware double.
Thanks,
ps. and I checked this time the console before, I see no syntax errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为设置
$MaxPrecision
和/或$MachinePrecision
不会产生您想要的效果。这是一个例子。在我的机器上,第一次计算需要百分之八秒,而第二次计算需要近三秒。现实情况是,第一次计算是用机器算术完成的,而第二次计算是用软件算术完成的,两者恰好具有相同的数值精度。
我认为在 Mathematica 8 中强制执行机器算术的最简单方法是使用
Compile
并将"CatchMachineOverflow"
和"CatchMachineUnderflow"
设置为 <代码>假;或者更好的是,只需将“RuntimeOptions”设置为“Speed”即可。另外,我认为您的
AbsoluteTiming
命令得到 0 的原因是您将精度设置得太低而无法表示结果。First, I don't think that setting
$MaxPrecision
and/or$MachinePrecision
will have the effect that you desire. Here's an example.On my machine, the first computation takes 8 hundreths of a second, while the second computation takes nearly three seconds. The reality is that the first computation is done in machine arithmetic, while the second is done in software arithmetic, that happens to have the same numerical precision.
I think that the easiest way to force machine arithmetic in Mathematica 8 is to use
Compile
with the"CatchMachineOverflow"
and"CatchMachineUnderflow"
set toFalse
; or better yet just set"RuntimeOptions"
to"Speed"
.Also, I think the reason you are getting 0 for your
AbsoluteTiming
command is that you have set the precision too low to represent the result.