Hi guys,

Just encountered a floating point accuracy issue. Anyone here knows why? Thanks a lot..,

I wrote a simple code using VDSP5 (newest update) and ran on ADSP-21479:

float x, y, z; |

for (x = 0; x<2*PI; x = x+ (1e-4)) | ||

{ | ||

y = sinf(x); | ||

z = cosf(x); | ||

MsgLen = sprintf (PrintMsg, "%f, %f, %f \r\n",x,y,z); | ||

UARTSendMsg(PrintMsg,MsgLen); | ||

delayms(50); | ||

} |

Then I logged the data from UART. According to the code, x should be:

0, 0.0001, 0.0002, ...,

However what I got is:

0, 0.0001, 0.0002,..., 0.063200, 0.063301, 0.063401,..., 0.122202, ...,

the small error grows to some funny numbers like 0.432879,. which should never be seen.

The same code in Matlab, calculation result is correct.

we calculated some cosd() array in 21479 and found some funny numeric noise.., and then did this experiment.

Hi,

There are limitations with Float values as to the level of accuracy that can be represented in 32-bits (the default for 21479). It is impossible to store an exact value for a floating point number; the processor merely stores an approximation (as a HEX value) that is extrapolated to a floating point representation depending on the number of bits provided for precision.

For a 32-bit float we expect precision in the region of 7 significant digits. For 64 bit floats we expect precision of between 13 and 14 significant digits.

To get higher precision you should use the long-double data type, which provides 64-bit floating point arithmetic and libraries. Again, this is discussed in the SHARC Compiler and Libraries Manual under:

Compiler->Compiler Command-Line Interface->Data Type and Data Type Sizes->Floating-Point Data Types

Note that printing of Long Doubles is only supported by projects built with the -double-size-64 switch, which can be enabled by selecting "64-bit (IEEE Double Precision)" under Project Options: Compile: Processor. It can them be printed using the %LF or %Le modifiers. Printf will only provide 6 (sometimes 7) significant digits of precision.

You will see evidence of greater precision (and the expected value for 'x') if you use the memory/expressions/locals window.

The difference in MATLAB is, I believe, that it uses 128 bits for the storage of floats as it has the freedom to do so with little consequence on a Windows machine (emulated 128-bit arithmetic would be slow on a DSP).

Regards,

Craig.