Date: Jul 24, 2013 9:00 PM
Author: Ki
Subject: Re: precision of floating number in matlab and c++

"James Tursa" wrote in message <ksg3lr$1kp$1@newscl01ah.mathworks.com>...
> "WK" wrote in message <ksfnfv$68s$1@newscl01ah.mathworks.com>...
> > Hi all,
> > I am porting a matlab code to c++ but I find out that there is minor different between the floating number precision in matlab and c++. My matlab code is running in 64-bit windows8 as follow
> >
> > data = -3:0.2:3;
> > sprintf('%.30f\n', bin_series)
> >
> > which shows
> >
> > ------------------------------------------------------------------------
> > -3.000000000000000000000000000000
> > -2.799999999999999800000000000000
> > -2.600000000000000100000000000000
> > -2.399999999999999900000000000000
> > -2.200000000000000200000000000000
> > -2.000000000000000000000000000000
> > -1.799999999999999800000000000000
> > -1.599999999999999900000000000000
> > -1.399999999999999900000000000000
> > -1.200000000000000000000000000000
> > -1.000000000000000000000000000000
> > -0.799999999999999820000000000000
> > -0.599999999999999640000000000000
> > -0.399999999999999910000000000000
> > -0.199999999999999730000000000000
> > 0.000000000000000000000000000000
> > 0.199999999999999730000000000000
> > 0.399999999999999910000000000000
> > 0.599999999999999640000000000000
> > 0.799999999999999820000000000000
> > 1.000000000000000000000000000000
> > 1.200000000000000000000000000000
> > 1.399999999999999900000000000000
> > 1.599999999999999900000000000000
> > 1.799999999999999800000000000000
> > 2.000000000000000000000000000000
> > 2.200000000000000200000000000000
> > 2.399999999999999900000000000000
> > 2.600000000000000100000000000000
> > 2.799999999999999800000000000000
> > 3.000000000000000000000000000000
> > ------------------------------------------------------------------------
> > and in C++ (64bit, linux) with compile intel icpc
> >
> > double max=3, min=-3, s=0.2;
> > unsigned int num = floor((max-min)/s)+1;
> > double data[num];
> > for (unsigned int n=0; n<num; n++)
> > {
> > data[n] = min + (double)n*s;
> > cout << setprecision(30) << data[n] << endl;
> > }
> >
> > which shows
> > ------------------------------------------------------------------------
> > -3
> > -2.79999999999999982236431605997
> > -2.60000000000000008881784197001
> > -2.39999999999999991118215802999
> > -2.20000000000000017763568394003
> > -2
> > -1.79999999999999982236431605997
> > -1.59999999999999986677323704498
> > -1.39999999999999991118215802999
> > -1.19999999999999995559107901499
> > -1
> > -0.799999999999999822364316059975
> > -0.59999999999999964472863211995
> > -0.399999999999999911182158029987
> > -0.199999999999999733546474089962
> > 0
> > 0.200000000000000177635683940025
> > 0.40000000000000035527136788005
> > 0.600000000000000088817841970013
> > 0.800000000000000266453525910038
> > 1
> > 1.20000000000000017763568394003
> > 1.40000000000000035527136788005
> > 1.60000000000000053290705182008
> > 1.8000000000000007105427357601
> > 2
> > 2.20000000000000017763568394003
> > 2.40000000000000035527136788005
> > 2.60000000000000053290705182008
> > 2.8000000000000007105427357601
> > 3
> > ------------------------------------------------------------------------
> > It seems that they are the same; however, in my algorithm, I need to use that array to search some boundary. For example, I need to find out all number greater than or eqal to 2.8. But in the c++ code, it returns the correct numbers, but in matlab some number will be miss. In my algorithm, I need to use the return numbers for some calculating a need seed and put it back to the program and iterate the same process for 10 millions time. I find that initially, the results given by matlab and c++ are almost the same, but after long time running, the difference accumulated to a big number. I am not asking the method to improve the precision of the algorithm. I just want to know why the number generated in two language looks that different?

>
> The sprintf function in Windows does not print all of the trailing digits beyond the eps of the number, hence all the zeros at the end. To see how different the results really are you can pass the C++ results back to MATLAB (or vice-versa) and then compare them. You can also use this utility on the MATLAB side to print all the digits:
>
> http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str
>
> James Tursa


Thank James. I think instead of talking how to render the number. Let's consider the a concrete example due to the floating number representation. I am searching some elements from a matrix under some condition

bi = 0.2;
range = -4:bi :4;
NUM= length(range);
jn = -1:0.1:1;
sol = -4:4;
[a, b] = meshgrid(jn, sol);
data = a+b;
for k = 1:NUM
lb = (range(k) - 0.5*bi);
up = (range(k) + 0.5*bi);
found{k} = find( (data>=lb) & (data<up) );
end

I write a simple C++ code to create the data matrix (I check that those two data are exact the same), also bi and range are identical in the C++ code to the matlab one. But for searching, I did

// c++ array with index start at 0
// here I am not showing the full code but just the kernal part
std::vector<int> found[NUM];
for (int k=0; k<NUM; k++)
{
lb = (range[k] - 0.5*bi);
up = (range[k] + 0.5*bi);
for (int col=0; col<MAX_COL_NUM; col++)
{
for (int row=0; row<MAX_ROW_NUM; row++)
{
if ((data[row][col]>=lb) && (data[row][col]<up))
{
found[k].push_back(col*9 + row + 1);
}
}
}
}

By comparing the found, you will see some of the numbers are not matching. I compare the data carefully and see that because when it calculate the bounds (lb and up), matlab and c++ give a very slight different numbers (e.g. for C++ some numbers is supposed to be 1.500000000000002 but matlab will give 1.499999999999999999999) so it won't return the same number as expected. I just wonder why is that? Both c++ and matlab use double precision and why it gives that difference? Is it anything to do with 64bit or not? The matlab I am using is 64bit but the compiler (g++) is running in the server and I don't know if it is 32 bit or 64bit (I think it should be 64bit too).