Date: Jul 25, 2013 11:51 AM
Author: Ki
Subject: Re: precision of floating number in matlab and c++

"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