This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: Accuracy of floating-point elementary math functions

Tool/software: TI C/C++ Compiler

Hello,

Lately, I am calculating Discrete Fourier Transform of a measured signal in which several elementary functions are used such as trigonometric functions. 

As the sample size increases, the accuracy becomes more sensitive to round-off and trigonometric fcn errors, especially if some inexpensive DFT methods are used in case low number of DFT bins are needed.

I just want to verify the accuracy of elementary math functions such as trigonometric functions. 

I checked the .c files of those mathematical functions in C2000 compiler library folder and I saw that they were taken from a book written 40 years ago. Perhaps, they are not the best methods available now.

Do you have any data on accuracy of those functions?

Thanks.

  • Don't knock old floating point routines! IEEE754 was adopted in 1985. My HP71B is probably as good as any modern computer!

    That being said, What processor and word size are you using?

    As they say in Kernigham and Plauger, "Floating point is like working with sand piles, Every time you move one you lose a little sand and pick up a little dirt."

  • I am using 28069 and float32.

    In fact I read the paper below which emphasizes the accuracy of sine and cosine values in DFT calculations and says device's own values are generally erroneous. Then, I just wanted dig into it. The routines that C2000 compiler uses seems to apply a few order Taylor series after reduction, perhaps 3rd order although I am not sure of exact order. The reference book that C2000 routines uses is "Software Manual for the Elementary Functions" by Cody and Waite from 1980.

    The paper: JAMES C. SCHATZMAN - ACCURACY OF THE DISCRETE FOURIER TRANSFORM AND THE FAST FOURIER TRANSFORM


    I used Goertzel and direct DFT for comparison. In fact, for N=5000 and fundamental frequency (k=1), they show quite different values. I knew Goertzel would give erroneous result, but maybe it should not show that much error for 5000 samples. So, I want to verify the accuracy of the trigonometric fcn of C2000 compilers.

    Gortzel uses cos(2*pi*k*N) as filter coefficient which is equal to cos(2*pi*0.0002) for for N=5000 and fundamental frequency (k=1). Here I am talking about very small errors that accumulates, maybe one decimal digit. Errors divergent from the best possible representation in float32 of the elementary math functions...
  • What version of the C2000 compiler are you using?
  • This is a friendly reminder to let us know the compiler version.

    Thanks and regards,

    -George
  • Code Composer Studio

    Version: 6.1.1.00022

    After the post, I did some quick check, it seems they are working good with 7 digit precision.

    I have found a good note about this topic. Probably, TI uses similar method with 7.3 digit precision in this note.

    nnp.ucsd.edu/.../trig_approximations.pdf

    It appears the Goertzel algorithm causes the error that I mentioned in fist post because of the too low signal frequency and accumulation of float32 round-off effect.

    Direct DFT calculation improved the result.

  • Thank you.  But we need to know the version of the compiler, and not CCS.  Please see this article for more explanation.

    Thanks and regards,

    -George

  • Compiler: TI v6.4.6
  • The C2000 compiler version 6.4.x RTS library supports sin with a polynomial approximation of an interval about the origin. cos is first translated to sin, then evaluated the same way. Yes, both of these algorithms are derived from the Cody & Waite book, 1980 edition.

    The current release of the C2000 compiler (18.1.x.LTS) uses the same algorithm if the compiler is configured to use FPU32 support, but it uses a different polynomial approximation with higher accuracy when FPU32 support is not enabled.

    We hope to improve the speed and accuracy, and the tradeoffs between them, over time for all architectures. However, at this time, there are no specific plans on the roadmap. If our implementation of sin and cos does not meet your needs, you can drop in your own implementation, perhaps one available on the internet.

    As to the accuracy of the C&W algorithm, decimal digits can be misleading. We measure it in terms of ULP, which is related to float epsilon (see en.wikipedia.org/.../Unit_in_the_last_place). One decimal digit of error would be somewhere in the vicinity of 3.3 ULP. According to a quick Monte Carlo test, I see that the existing C2000 algorithm gets just under 1 ULP of error in the worst case for 32-bit sin. The ideal is under 0.5 ULP, but under 1 ULP is pretty good for a trig function.

    If you're having trouble with the accuracy of the C2000's library, could you give a more specific example? A specific input value with a large error in the result would be the most helpful.