MATLAB和C余弦函数的不同 [英] matlab and c differ with cos function

查看:161
本文介绍了MATLAB和C余弦函数的不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab和c中的相同程序来实现的程序,结果不同。

I have a program implemented in matlab and the same program in c, and the results differ.

我有点摸不着头脑了余弦函数不返回完全相同的结果。

I am bit puzzled that the cos function does not return the exact same result.

我用的是同一台电脑,英特尔酷睿2双核和8字节双数据类型在这两种情况下。

I use the same computer, Intel Core 2 Duo, and 8 bytes double data type in both cases.

为什么结果不同?

下面是测试:

c:
double a = 2.89308776595231886830;
double b = cos(a);
printf("a = %.50f\n", a);
printf("b = %.50f\n", b);
printf("sizeof(a): %ld\n", sizeof(a));
printf("sizeof(b): %ld\n", sizeof(b));

a = 2.89308776595231886830106304842047393321990966796875
b = -0.96928123535654842068964853751822374761104583740234
sizeof(a): 8
sizeof(b): 8



matlab:
a = 2.89308776595231886830
b = cos(a);
fprintf('a = %.50f\n', a);
fprintf('b = %.50f\n', b);
whos('a')
whos('b')

a = 2.89308776595231886830106304842047393321990966796875
b = -0.96928123535654830966734607500256970524787902832031
  Name      Size            Bytes  Class     Attributes
  a         1x1                 8  double              

  Name      Size            Bytes  Class     Attributes
  b         1x1                 8  double  


So, b differ a bit (very slightly, but enough to make my debuging task difficult)

b = -0.96928123535654842068964853751822374761104583740234  c
b = -0.96928123535654830966734607500256970524787902832031  matlab


我用的是同一台电脑,英特尔酷睿2双核和8字节双数据类型。


I use the same computer, Intel Core 2 Duo, and 8 bytes double data type.

为什么结果不同?

确实MATLAB不使用cos函数内置硬件英特尔?

does matlab do not use the cos function hardware built-in in Intel?

有没有哪怕慢一点使用MATLAB和C(用精确的结果)同样的余弦函数,这样我可以有把握地比较我的Matlab与C计划的结果的简单方法?

Is there a simple way to use the same cos function in matlab and c (with exact results), even if a bit slower, so that I can safely compare the results of my matlab and c program?

更新:

非常感谢您的回答!

因此​​,正如你所指出的那样,cos函数的Matlab与C不同。
太棒了!我以为他们是使用内置的英特尔微处理器的余弦函数。

So, as you have pointed out, the cos function for matlab and c differ. That's amazing! I thought they were using the cos function built-in in the Intel microprocessor.

的matlab的COS版本等于(至少对于这个试验)到MATLAB中的一个。
你可以从MATLAB也尝试:B = java.lang.Math.cos(一)

The cos version of matlab is equal (at least for this test) to the one of matlab. you can try from matlab also: b=java.lang.Math.cos(a)

然后,我做了一个小MEX功能使用COS C版本从MATLAB中,并能正常工作;这使我调试我的程序(同MATLAB和C实现),并看到它们的不同点是什么,这是这个职位的目的。

Then, I did a small MEX function to use the cos c version from within matlab, and it works fine; This allows me to debug the my program (the same one implemented in matlab and c) and see at what point they differ, which was the purpose of this post.

的唯一一件事就是调用MEXÇCOS从MATLAB版本是太缓慢。

The only thing is that calling the MEX c cos version from matlab is way too slow.

我现在想从C调用Java余弦函数(因为它是从MATLAB一样的),看看是否能去得更快。

I am now trying to call the Java cos function from c (as it is the same from matlab), see if that goes faster.

推荐答案

使用脚本在<一个href=\"http://www.mathworks.com/matlabcentral/fileexchange/1777-from-double-to-string\">http://www.mathworks.com/matlabcentral/fileexchange/1777-from-double-to-string

两个数之间的差别只在最后位:

the difference between the two numbers is only in the last bit:

octave:1> bc = -0.96928123535654842068964853751822374761104583740234;
octave:2> bm = -0.96928123535654830966734607500256970524787902832031;
octave:3> num2bin(bc)
ans = -.11111000001000101101000010100110011110111001110001011*2^+0
octave:4> num2bin(bm)
ans = -.11111000001000101101000010100110011110111001110001010*2^+0

其中之一必须是接近正确的答案,假设给定值是准确的。

>> be = vpa('cos(2.89308776595231886830)',50)                 
be =
-.96928123535654836529707365425580405084360377470583
>> bc = -0.96928123535654842068964853751822374761104583740234;
>> bm = -0.96928123535654830966734607500256970524787902832031;
>> abs(bc-be)                                                 
ans =
.5539257488326242e-16
>> abs(bm-be)                                                 
ans =
.5562972757925323e-16

所以,C库结果更准确。

So, the C library result is more accurate.

对于你的问题的目的,但是,你不应该期望获得MATLAB和相同的答案为准C库与你联系。

For the purposes of your question, however, you should not expect to get the same answer in matlab and whichever C library you linked with.

这篇关于MATLAB和C余弦函数的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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