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 <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;
}
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("%e,%e,%e\n",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?
doubleinstead offloatin the C program.floatand 64-bitdoublevalues that are using IEEE-754 format, so you might want to read about that.floatin C; always usedouble.floatto represent. The biggest number you can represent in afloatis about 1e38, and the smallest is about 1e-45.doublewill typically give you a useful range of 1e-307 to 1e307 (or down to 1e-324 if you include the subnormals), but if you need better than that, you may need to look into specialized libraries.