指数限制对于C和Matlab

huangapple go评论57阅读模式
英文:

Exponential limits for C and Matlab

问题

在计算机中,浮点数的数学运算实际上是分别处理基数和指数部分,然后将它们组合在一起。我们在计算基础教材中学到了这一点。然而,我发现在C程序和MATLAB中,限制值(limits)是不同的。下面是一个例子:

C程序:

#include <stdio.h>

int main()
{
    float c1, c2, c3;
    
    c1 = 1.0e-20;
    c2 = 1.0e-30;
    
    c3 = c1 * c2;
    printf("%e,%e,%e\n",c1,c2,c3);

    return 0;
}

运行结果为:

1.000000e-20,1.000000e-30,0.000000e+00

MATLAB程序:

c1 = 1.0e-20;
c2 = 1.0e-30;
    
c3 = c1 * c2;
fprintf("%e,%e,%e\n",c1,c2,c3);

运行结果为:

1.000000e-20,1.000000e-30,1.000000e-50

显然,MATLAB给出了正确的乘法结果,而C给出了错误的结果。有人可以解释为什么会发生这种情况吗?

在我的项目中,我的计算涉及在C语言中进行这种小数计算。由于Matlab可以正确执行,您能否提供建议,我如何在C中也能正确执行这种计算?

英文:

In computers, the math operation of floating numbers is actually processed using the base number and the exponential number separately, and then combine them together. We learn this in our computation fundamental textbooks. However, I find the limits in C program and MATLAB are quite different. Here is an example:

C program:

#include &lt;stdio.h&gt;

int main()
{
    float c1, c2, c3;
    
    c1 = 1.0e-20;
    c2 = 1.0e-30;
    
    c3 = c1 * c2;
    printf(&quot;%e,%e,%e\n&quot;,c1,c2,c3);

    return 0;
}

The running result is:

1.000000e-20,1.000000e-30,0.000000e+00

MATLAB program:

c1 = 1.0e-20;
c2 = 1.0e-30;
    
c3 = c1 * c2;
fprintf(&quot;%e,%e,%e\n&quot;,c1,c2,c3);

The running result is:

1.000000e-20,1.000000e-30,1.000000e-50

It is obvious that MATLAB gives the right multiplication result, whereas C gives a wrong result. Could anyone answer why this happens?

In my project, my computation involves such small number computations in C language. Since Matlab can do it correctly, can you give a suggestion how I can also do this in C?

答案1

得分: 4

在大多数编程语言中,您将至少可以访问两种浮点数类型:单精度和双精度。

在C语言中,float是单精度浮点数,double是双精度浮点数。

MATLAB默认使用double,但也具有用于单精度浮点数的single

如果您在MATLAB程序中使用single值,您将执行与C程序相同的计算,得到相同的结果:

c1 = single(1.0e-20);
c2 = single(1.0e-30);
c3 = c1 * c2;
fprintf("%e,%e,%e\n",c1,c2,c3);

输出为:

1.000000e-20,1.000000e-30,0.000000e+00

同样,您可以更改C程序,使用double而不是float,以完全复制MATLAB的结果。

英文:

In most languages, you'll have access to at least two floating-point types: single-precision and double-precision.

In C, float is a single-precision float, double is a double-precision float.

MATLAB uses double by default, but also has single for single-precision floats.

If you use single values in your MATLAB program, you'll do the same computation that your C program does, with the same result:

c1 = single(1.0e-20);
c2 = single(1.0e-30);
c3 = c1 * c2;
fprintf(&quot;%e,%e,%e\n&quot;,c1,c2,c3);

The output is:

1.000000e-20,1.000000e-30,0.000000e+00

Likewise, you can change your C program to use double instead of float, to reproduce the MATLAB results exactly.

huangapple
  • 本文由 发表于 2023年6月2日 05:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定