Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Topic: precision of floating number in matlab and c++
Replies: 11   Last Post: Jul 25, 2013 5:28 PM

 Messages: [ Previous | Next ]
 Ki Posts: 16 Registered: 5/20/12
precision of floating number in matlab and c++
Posted: Jul 21, 2013 12:19 AM

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?

Date Subject Author
7/21/13 Ki
7/21/13 James Tursa
7/24/13 Ki
7/24/13 Ki
7/25/13 Ki
7/25/13 Ki
7/25/13 Curious
7/25/13 James Tursa
7/25/13 Ki
7/25/13 Curious
7/25/13 Steven Lord
7/25/13 Ki