为什么Matlab和Mathematica之间存在显着的双精度差异? [英] why is there significant double precision difference between Matlab and Mathematica?

查看:317
本文介绍了为什么Matlab和Mathematica之间存在显着的双精度差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中创建了一个随机双精度值,方法是

I created a random double precision value in Matlab by

x = rand(1,1);

然后显示x的所有可能位数

then display all possible digits of x by

vpa(x,100)

并获取:

0.2238119394911369 7971853298440692014992237091064453125

我将x保存到.mat文件,并将其导入Mathematica,然后将其转换:

I save x to a .mat file, and import it into Mathematica, and then convert it:

y = N[FromDigits[RealDigits[x]],100]

并获取:

0.2238119394911369 0000

然后返回Matlab并使用(将所有Mathematica数字复制并粘贴到Matlab):

Then go back to Matlab and use (copy and paste all the Mathematica digits to Matlab):

 vpa(0.22381193949113690000,100)

并获取:

0.22381193949113689 64518061375201796181499958038330078125

为什么同一个双精度变量之间存在显着差异?

Why there is significant difference between the same double precision variable?

在Mathematica和Matlab之间交换数据时如何缩小差距?

How to bridge the gap when exchanging data between Mathematica and Matlab?

推荐答案

您可以使用ReadList而不是Import来解决此问题.我在下面添加了一些演示步骤来探索显示的舍入和相等性.请注意最终测试d == e?在Mathematica 7中为False,在Mathematica 9中为True(带有所有期望的数字).因此,似乎版本9已在Import中添加了一些精度.该演示使用一个演示文件.

You can fix this problem by using ReadList instead of Import. I have added some demo steps below to explore displayed rounding and equality. Note the final test d == e? is False in Mathematica 7 but True in Mathematica 9, (with all the expected digits). So it looks like some precision has been added to Import by version 9. The demo uses a demo file.

demo.dat的内容:

Contents of demo.dat:

0.22381193949113697971853298440692014992237091064453125
"0.22381193949113697971853298440692014992237091064453125"

探索:-

a = Import["demo.dat"]
b = ReadList["demo.dat"]
a[[1, 1]] == a[[2, 1]]
b[[1]] == b[[2]]
a[[1, 1]] == b[[1]]
a[[1, 1]] == ToExpression@b[[2]]
b[[1]] // FullForm
c = First@StringSplit[ToString@FullForm@b[[1]], "`"]
b[[2]]
ToExpression /@ {c, b[[2]]}
d = N[FromDigits[RealDigits[a[[1, 1]]]], 100]
e = N[FromDigits[RealDigits[b[[1]]]], 100]
d == e

这篇关于为什么Matlab和Mathematica之间存在显着的双精度差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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