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?

"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

"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).

"WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

> "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).

>

The matlab code I shown here is to retrieve the indices of the desired elements based on some condition. I will use those elements as input for a nonlinear iterative equation. If it return different elements, since it is nonlinear, very soon the results will diverse. So it is important to get the same number. I just don't know why they will give the different number if the precision is the same.

"WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

> 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).

>

How do you build range on the C++ side? It appears you are doing it differently from the algorithm that MATLAB uses to build -4:bi :4.

James Tursa

"James Tursa" wrote in message <ksqev5$lk3$1@newscl01ah.mathworks.com>...

> "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

> > 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).

> >

>

> How do you build range on the C++ side? It appears you are doing it differently from the algorithm that MATLAB uses to build -4:bi :4.

>

> James Tursa

In C++, I use a loop to generate -4:bi:4

double bi=0.2;

int NUM = round(8.0/bi)+1;

for (int k=0; k<NUM; k++) range[k] = -4 + (double)k*bi;

I check the result, it gives the same sequence as matlab does (I only check up to the 4 decimal places). Well, it these two sequences are different, so how matlab generating that? and why they are different if both use double precision?

"WK" wrote in message <ksqn5p$96t$1@newscl01ah.mathworks.com>...

> "James Tursa" wrote in message <ksqev5$lk3$1@newscl01ah.mathworks.com>...

> > "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

... SNIP ...

> I check the result, it gives the same sequence as matlab does (I only check up to the 4 decimal places). Well, it these two sequences are different, so how matlab generating that? and why they are different if both use double precision?

You really need to check to more than 4 decimal places.

You would probably get a SLIGHTLY different sequence if you used linspace instead of the colon operator or a for loop (as you did with C++), for example.

Take a look at the at the references in the MATLAB FAQ:

<<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>>

for a more detailed discussion of floating point numbers.

Also, (and this is out of my comfort zone) as I recall the c/c++ specification for floating point precision is machine/complier independent. That is, it does not specify whether double precision is 16, 32, or 64 bits. It simply requires that:

double precision >= single precision

As I said, I may not be up to date on this topic, but that is my recollection.

"someone" <someone@somewhere.net> wrote in message

news:ksrc4v$sk1$1@newscl01ah.mathworks.com...

> "WK" wrote in message <ksqn5p$96t$1@newscl01ah.mathworks.com>...

>> "James Tursa" wrote in message <ksqev5$lk3$1@newscl01ah.mathworks.com>...

>> > "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

>

> ... SNIP ...

>

>> I check the result, it gives the same sequence as matlab does (I only

>> check up to the 4 decimal places). Well, it these two sequences are

>> different, so how matlab generating that? and why they are different if

>> both use double precision?

>

> You really need to check to more than 4 decimal places.

>

> You would probably get a SLIGHTLY different sequence if you used linspace

> instead of the colon operator or a for loop (as you did with C++), for

> example.

>

> Take a look at the at the references in the MATLAB FAQ:

>

> <<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>>

>

> for a more detailed discussion of floating point numbers.

>

> Also, (and this is out of my comfort zone) as I recall the c/c++

> specification for floating point precision is machine/complier

> independent. That is, it does not specify whether double precision is 16,

> 32, or 64 bits. It simply requires that:

> double precision >= single precision

> As I said, I may not be up to date on this topic, but that is my

> recollection.

If the OP wants C++ code that generates the same vector as the COLON

operator in MATLAB, he or she may be interested in MATLAB Coder.

http://www.mathworks.com/products/matlab-coder/

The code it generates is not nearly as simple or straightforward as the OP's

code. But computing a:d:b isn't as simple or straightforward as it may

appear at first, as the Cleve's Corner article linked in the FAQ article

states.

--

Steve Lord

slord@mathworks.com

To contact Technical Support use the Contact Us link on

http://www.mathworks.com

"someone" wrote in message <ksrc4v$sk1$1@newscl01ah.mathworks.com>...

> "WK" wrote in message <ksqn5p$96t$1@newscl01ah.mathworks.com>...

> > "James Tursa" wrote in message <ksqev5$lk3$1@newscl01ah.mathworks.com>...

> > > "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

>

> ... SNIP ...

>

> > I check the result, it gives the same sequence as matlab does (I only check up to the 4 decimal places). Well, it these two sequences are different, so how matlab generating that? and why they are different if both use double precision?

>

> You really need to check to more than 4 decimal places.

>

> You would probably get a SLIGHTLY different sequence if you used linspace instead of the colon operator or a for loop (as you did with C++), for example.

>

> Take a look at the at the references in the MATLAB FAQ:

>

> <<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>>

>

> for a more detailed discussion of floating point numbers.

>

> Also, (and this is out of my comfort zone) as I recall the c/c++ specification for floating point precision is machine/complier independent. That is, it does not specify whether double precision is 16, 32, or 64 bits. It simply requires that:

> double precision >= single precision

> As I said, I may not be up to date on this topic, but that is my recollection.

The compiler I am using is running in 64bit linux system and it uses 64bits to for double precision. The MATLAB I am running is in 64bit windows 8 and also 64bit precision for double. But I am wondering it the floating point representation in matlab is of different standard. I try

sprintf('%.100f', 1/3)

it shows 0.3333333333333333100000000000000000000000000000000000000000000000000000000000000000000000000000000000

in c++, I try

cout << setprecision(100) << 1.0/3.0 << endl;

it shows

0.333333333333333314829616256247390992939472198486328125

"WK" wrote in message <ksptpo$fos$1@newscl01ah.mathworks.com>...

> "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

> > "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).

> >

>

> The matlab code I shown here is to retrieve the indices of the desired elements based on some condition. I will use those elements as input for a nonlinear iterative equation. If it return different elements, since it is nonlinear, very soon the results will diverse. So it is important to get the same number. I just don't know why they will give the different number if the precision is the same.

I download the num2exact package and use it to show all digits of the results generated by matlab. I shows all numbers in range and jn vectors and use loop to create that in c++. It shows the following number

1) range

MATLAB:

-4

-3.79999999999999982236431605997495353221893310546875

-3.600000000000000088817841970012523233890533447265625

-3.399999999999999911182158029987476766109466552734375

-3.20000000000000017763568394002504646778106689453125

-3

-2.79999999999999982236431605997495353221893310546875

-2.5999999999999996447286321199499070644378662109375

-2.399999999999999911182158029987476766109466552734375

-2.20000000000000017763568394002504646778106689453125

-2

-1.79999999999999982236431605997495353221893310546875

-1.5999999999999996447286321199499070644378662109375

-1.399999999999999911182158029987476766109466552734375

-1.199999999999999733546474089962430298328399658203125

-1

-0.79999999999999982236431605997495353221893310546875

-0.5999999999999996447286321199499070644378662109375

-0.399999999999999911182158029987476766109466552734375

-0.199999999999999733546474089962430298328399658203125

0

0.199999999999999733546474089962430298328399658203125

0.399999999999999911182158029987476766109466552734375

0.5999999999999996447286321199499070644378662109375

0.79999999999999982236431605997495353221893310546875

1

1.199999999999999733546474089962430298328399658203125

1.399999999999999911182158029987476766109466552734375

1.5999999999999996447286321199499070644378662109375

1.79999999999999982236431605997495353221893310546875

2

2.20000000000000017763568394002504646778106689453125

2.399999999999999911182158029987476766109466552734375

2.5999999999999996447286321199499070644378662109375

2.79999999999999982236431605997495353221893310546875

3

3.20000000000000017763568394002504646778106689453125

3.399999999999999911182158029987476766109466552734375

3.600000000000000088817841970012523233890533447265625

3.79999999999999982236431605997495353221893310546875

4

CPP:

-4

-3.79999999999999982236431605997495353221893310546875

-3.600000000000000088817841970012523233890533447265625

-3.399999999999999911182158029987476766109466552734375

-3.20000000000000017763568394002504646778106689453125

-3

-2.79999999999999982236431605997495353221893310546875

-2.5999999999999996447286321199499070644378662109375

-2.399999999999999911182158029987476766109466552734375

-2.20000000000000017763568394002504646778106689453125

-2

-1.79999999999999982236431605997495353221893310546875

-1.5999999999999996447286321199499070644378662109375

-1.399999999999999911182158029987476766109466552734375

-1.199999999999999733546474089962430298328399658203125

-1

-0.79999999999999982236431605997495353221893310546875

-0.5999999999999996447286321199499070644378662109375

-0.399999999999999911182158029987476766109466552734375

-0.199999999999999733546474089962430298328399658203125

0

0.20000000000000017763568394002504646778106689453125

0.4000000000000003552713678800500929355621337890625

0.60000000000000053290705182007513940334320068359375

0.800000000000000710542735760100185871124267578125

1

1.20000000000000017763568394002504646778106689453125

1.4000000000000003552713678800500929355621337890625

1.60000000000000053290705182007513940334320068359375

1.800000000000000710542735760100185871124267578125

2

2.20000000000000017763568394002504646778106689453125

2.4000000000000003552713678800500929355621337890625

2.60000000000000053290705182007513940334320068359375

2.800000000000000710542735760100185871124267578125

3

3.20000000000000017763568394002504646778106689453125

3.4000000000000003552713678800500929355621337890625

3.60000000000000053290705182007513940334320068359375

3.800000000000000710542735760100185871124267578125

4

2) and for jn vector:

MATLAB:

-1

-0.90000000000000002220446049250313080847263336181640625

-0.8000000000000000444089209850062616169452667236328125

-0.6999999999999999555910790149937383830547332763671875

-0.59999999999999997779553950749686919152736663818359375

-0.5

-0.399999999999999911182158029987476766109466552734375

-0.29999999999999993338661852249060757458209991455078125

-0.1999999999999999555910790149937383830547332763671875

-9.999999999999997779553950749686919152736663818359375e-2

0

9.999999999999997779553950749686919152736663818359375e-2

0.1999999999999999555910790149937383830547332763671875

0.29999999999999993338661852249060757458209991455078125

0.399999999999999911182158029987476766109466552734375

0.5

0.59999999999999997779553950749686919152736663818359375

0.6999999999999999555910790149937383830547332763671875

0.8000000000000000444089209850062616169452667236328125

0.90000000000000002220446049250313080847263336181640625

1

CPP:

-1

-0.90000000000000002220446049250313080847263336181640625

-0.8000000000000000444089209850062616169452667236328125

-0.6999999999999999555910790149937383830547332763671875

-0.59999999999999997779553950749686919152736663818359375

-0.5

-0.399999999999999911182158029987476766109466552734375

-0.29999999999999993338661852249060757458209991455078125

-0.1999999999999999555910790149937383830547332763671875

-0.09999999999999997779553950749686919152736663818359375

0

0.100000000000000088817841970012523233890533447265625

0.20000000000000017763568394002504646778106689453125

0.3000000000000000444089209850062616169452667236328125

0.4000000000000001332267629550187848508358001708984375

0.5

0.600000000000000088817841970012523233890533447265625

0.70000000000000017763568394002504646778106689453125

0.8000000000000000444089209850062616169452667236328125

0.9000000000000001332267629550187848508358001708984375

1

Interestingly, all negative numbers matching pretty well but the positive number not that good. I check that both matlab and my intel c++ compile use ANSI/IEEE 754 standard for floating point and both use 64bits for double precision, just don't understand why they will render such different numbers.

"WK" wrote in message <ksrj3v$ilr$1@newscl01ah.mathworks.com>...

> "WK" wrote in message <ksptpo$fos$1@newscl01ah.mathworks.com>...

> > "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

> > > "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).

> > >

> >

> > The matlab code I shown here is to retrieve the indices of the desired elements based on some condition. I will use those elements as input for a nonlinear iterative equation. If it return different elements, since it is nonlinear, very soon the results will diverse. So it is important to get the same number. I just don't know why they will give the different number if the precision is the same.

>

> I download the num2exact package and use it to show all digits of the results generated by matlab. I shows all numbers in range and jn vectors and use loop to create that in c++. It shows the following number

>

> 1) range

> MATLAB:

> -4

> -3.79999999999999982236431605997495353221893310546875

> -3.600000000000000088817841970012523233890533447265625

> -3.399999999999999911182158029987476766109466552734375

> -3.20000000000000017763568394002504646778106689453125

> -3

> -2.79999999999999982236431605997495353221893310546875

> -2.5999999999999996447286321199499070644378662109375

> -2.399999999999999911182158029987476766109466552734375

> -2.20000000000000017763568394002504646778106689453125

> -2

> -1.79999999999999982236431605997495353221893310546875

> -1.5999999999999996447286321199499070644378662109375

> -1.399999999999999911182158029987476766109466552734375

> -1.199999999999999733546474089962430298328399658203125

> -1

> -0.79999999999999982236431605997495353221893310546875

> -0.5999999999999996447286321199499070644378662109375

> -0.399999999999999911182158029987476766109466552734375

> -0.199999999999999733546474089962430298328399658203125

> 0

> 0.199999999999999733546474089962430298328399658203125

> 0.399999999999999911182158029987476766109466552734375

> 0.5999999999999996447286321199499070644378662109375

> 0.79999999999999982236431605997495353221893310546875

> 1

> 1.199999999999999733546474089962430298328399658203125

> 1.399999999999999911182158029987476766109466552734375

> 1.5999999999999996447286321199499070644378662109375

> 1.79999999999999982236431605997495353221893310546875

> 2

> 2.20000000000000017763568394002504646778106689453125

> 2.399999999999999911182158029987476766109466552734375

> 2.5999999999999996447286321199499070644378662109375

> 2.79999999999999982236431605997495353221893310546875

> 3

> 3.20000000000000017763568394002504646778106689453125

> 3.399999999999999911182158029987476766109466552734375

> 3.600000000000000088817841970012523233890533447265625

> 3.79999999999999982236431605997495353221893310546875

> 4

>

> CPP:

> -4

> -3.79999999999999982236431605997495353221893310546875

> -3.600000000000000088817841970012523233890533447265625

> -3.399999999999999911182158029987476766109466552734375

> -3.20000000000000017763568394002504646778106689453125

> -3

> -2.79999999999999982236431605997495353221893310546875

> -2.5999999999999996447286321199499070644378662109375

> -2.399999999999999911182158029987476766109466552734375

> -2.20000000000000017763568394002504646778106689453125

> -2

> -1.79999999999999982236431605997495353221893310546875

> -1.5999999999999996447286321199499070644378662109375

> -1.399999999999999911182158029987476766109466552734375

> -1.199999999999999733546474089962430298328399658203125

> -1

> -0.79999999999999982236431605997495353221893310546875

> -0.5999999999999996447286321199499070644378662109375

> -0.399999999999999911182158029987476766109466552734375

> -0.199999999999999733546474089962430298328399658203125

> 0

> 0.20000000000000017763568394002504646778106689453125

> 0.4000000000000003552713678800500929355621337890625

> 0.60000000000000053290705182007513940334320068359375

> 0.800000000000000710542735760100185871124267578125

> 1

> 1.20000000000000017763568394002504646778106689453125

> 1.4000000000000003552713678800500929355621337890625

> 1.60000000000000053290705182007513940334320068359375

> 1.800000000000000710542735760100185871124267578125

> 2

> 2.20000000000000017763568394002504646778106689453125

> 2.4000000000000003552713678800500929355621337890625

> 2.60000000000000053290705182007513940334320068359375

> 2.800000000000000710542735760100185871124267578125

> 3

> 3.20000000000000017763568394002504646778106689453125

> 3.4000000000000003552713678800500929355621337890625

> 3.60000000000000053290705182007513940334320068359375

> 3.800000000000000710542735760100185871124267578125

> 4

>

> 2) and for jn vector:

> MATLAB:

> -1

> -0.90000000000000002220446049250313080847263336181640625

> -0.8000000000000000444089209850062616169452667236328125

> -0.6999999999999999555910790149937383830547332763671875

> -0.59999999999999997779553950749686919152736663818359375

> -0.5

> -0.399999999999999911182158029987476766109466552734375

> -0.29999999999999993338661852249060757458209991455078125

> -0.1999999999999999555910790149937383830547332763671875

> -9.999999999999997779553950749686919152736663818359375e-2

> 0

> 9.999999999999997779553950749686919152736663818359375e-2

> 0.1999999999999999555910790149937383830547332763671875

> 0.29999999999999993338661852249060757458209991455078125

> 0.399999999999999911182158029987476766109466552734375

> 0.5

> 0.59999999999999997779553950749686919152736663818359375

> 0.6999999999999999555910790149937383830547332763671875

> 0.8000000000000000444089209850062616169452667236328125

> 0.90000000000000002220446049250313080847263336181640625

> 1

>

> CPP:

> -1

> -0.90000000000000002220446049250313080847263336181640625

> -0.8000000000000000444089209850062616169452667236328125

> -0.6999999999999999555910790149937383830547332763671875

> -0.59999999999999997779553950749686919152736663818359375

> -0.5

> -0.399999999999999911182158029987476766109466552734375

> -0.29999999999999993338661852249060757458209991455078125

> -0.1999999999999999555910790149937383830547332763671875

> -0.09999999999999997779553950749686919152736663818359375

> 0

> 0.100000000000000088817841970012523233890533447265625

> 0.20000000000000017763568394002504646778106689453125

> 0.3000000000000000444089209850062616169452667236328125

> 0.4000000000000001332267629550187848508358001708984375

> 0.5

> 0.600000000000000088817841970012523233890533447265625

> 0.70000000000000017763568394002504646778106689453125

> 0.8000000000000000444089209850062616169452667236328125

> 0.9000000000000001332267629550187848508358001708984375

> 1

>

>

> Interestingly, all negative numbers matching pretty well but the positive number not that good. I check that both matlab and my intel c++ compile use ANSI/IEEE 754 standard for floating point and both use 64bits for double precision, just don't understand why they will render such different numbers.

Um ... since the negative numbers match up, I modify my c++ code to generate the vector in this way

for (int k=0; k<NUM; k++)

{

range[k] = -4 + (double)k*bi;

if (k>=round(NUM/2)) range[k] = range[2*(int)round(NUM/2) - k];

}

With this trick, the results match up well. But still don't know why I have to do this way.

"WK" wrote in message <ksrpk3$74c$1@newscl01ah.mathworks.com>...

> "WK" wrote in message <ksrj3v$ilr$1@newscl01ah.mathworks.com>...

> > "WK" wrote in message <ksptpo$fos$1@newscl01ah.mathworks.com>...

> > > "WK" wrote in message <ksptao$eoq$1@newscl01ah.mathworks.com>...

> > > > "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

> > > > > >

... SNIP ...

>

> With this trick, the results match up well. But still don't know why I have to do this way.

Don't get me wrong, this is an interesting discussion.

But one has to wonder that if an algorithm (or code or whatever) is that sensitive to these small differences, then maybe the algorithm needs to be modified or maybe you should look for some other work-around.

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

*No tags are associated with this thread.*

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Got questions?

Get answers.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreDiscover what MATLAB ^{®} can do for your career.

Opportunities for recent engineering grads.

Apply TodayThe newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

**MATLAB Central**

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

**One Account**

Your MATLAB Central account is tied to your MathWorks Account for easy access.

**Use the Email Address of Your Choice**

The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

**Spam Control**

Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

**Tagging**

Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

**Watch lists**

Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

- Use a newsreader through your school, employer, or internet service provider
- Pay for newsgroup access from a commercial provider
- Use Google Groups
- Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup
- Run your own server. For typical instructions, see: http://www.slyck.com/ng.php?page=2

Choose your country to get translated content where available and see local events and offers. Based on your location, we recommend you select **United States** from the following list:

- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)