Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

precision differences in matlab and c++

I am trying to make equivalence tests on an algorithm written in C++ and in Matlab. The algorithm contains some kind of a loop in time and runs more than 1000 times. It has arithmetic operations and some math functions.

I feed the initial inputs to both platforms by hand (like a=1.767, b=6.65, ...) and when i check the hexadecimal representations of those inputs they are the same. So no problem for inputs. And get the outputs of c++ to matlab by a text file with 16 decimal digits. (i use "setprecision(32)" statement)

But here comes the problem; although after the 614'th step of both code, all the results are exactly the same, on the step of 615 I get a difference about 2.xxx..xxe-19? And after this step the error becomes larger and larger, and at the end of the runs it is about 5.xx..xxe-14.

0x3ff1 3e42 a211 6cca--->[C++ function]--->0x3ff4 7619 7005 5a42

0x3ff1 3e42 a211 6cca--->[MATLAB function]--->ans

ans - 0x3ff4 7619 7005 5a42

= 2.xxx..xxe-19

I searched how matlab behaves the numbers and found really interesting things like "denormalized mantissa". While realmin is about e-308, by denormalizing the mantissa matlab has the smallest real number about e-324. Further matlab holds many more digits for "pi" or "exp(1)" than that of c++.

On the other hand, matlab help says that whatever the format it displays, matlab uses the double precision internally.

So,I'd really appreciate if someone explains what the exact reason is for these differences? How can we make equivalence tests on matlab and c++?

like image 496
crbah Avatar asked Jun 22 '12 07:06

crbah


People also ask

Is MATLAB single or double precision?

Matlab defaults to double precision, but single precision is sufficient for many computational problems. In addition, single precision uses half the memory, and is generally twice as fast.

What is MATLAB default precision?

By default, MATLAB® uses 16 digits of precision. For higher precision, use vpa . The default precision for vpa is 32 digits.

How do you do precision in MATLAB?

For higher precision, use the vpa function in Symbolic Math Toolbox™. vpa provides variable precision which can be increased without limit. When you choose variable-precision arithmetic, by default, vpa uses 32 significant decimal digits of precision. For details, see Choose Numeric or Symbolic Arithmetic.

What is single precision MATLAB?

Single-precision variables in MATLAB® are stored as 4-byte (32-bit) floating-point values of data type (class) single . For example: y = single(10); whos y. Name Size Bytes Class Attributes y 1x1 4 single. For more information on floating-point values, see Floating-Point Numbers.


2 Answers

There is one thing in x86 CPU about floating points numbers. Internally, the floating point unit uses registers that are 10 bytes, i.e. 80 bits. Furthermore, the CPU has a setting that tells whether the floating point calculations should be made with 32 bits (float), 64 bits (double) or 80 bits precision. Less precision meaning faster executed floating point operations. (The 32 bits mode used to be popular for video games, where speed takes over precision).

From this I remember I tracked a bug in a calculation library (dll) that given the same input did not gave the same result whether it was started from a test C++ executable, or from MatLab.. Furthermore, this did not happen in Debug mode, only in Release!

The final conclusion was that MatLab did set the CPU floating point precision to 80 bits, whereas our test executable did not (and leave the default 64 bits precision). Furthermore, this calculation mismatch did not happen Debug mode because all the variables were written to memory into 64 bits double variables, and reloaded from there afterward, nullifying the additional 16 bits. In Release mode, some variables were optimized out (not written to memory), and all calculations were done with floating point registers only, on 80 bits, keeping the additional 16 bits non-zero value.

Don't know if this helps, but maybe worth knowing.

like image 175
Didier Trosset Avatar answered Oct 03 '22 09:10

Didier Trosset


A similar discussion occurred before, the conclusion was that IEEE 754 tolerates error in the last bit for transcendental functions (cos, sin, exp, etc..). So you can't expect exactly same results between MATLAB and C (not even same C code compiled in different compilers).

like image 34
Amro Avatar answered Oct 03 '22 07:10

Amro