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.

std:strtold gives wrong result

Other Parts Discussed in Thread: TMS320F2812

The problem is when I use std:strtold  to get a double precision value from string.

I use Code Composer 3.3 Version 3.3.82.13, and CGT v 5.2.9 and BIOS 5.33.03. and the target is TMS320F2812 / 28335.

In the following code snippets ( without BIOS) I have 3 different string inputs, but only the first one give correct result.

#include <stdio.h>
#include <cstring>
#include <cstdlib>

 

int main()
{
 long double dValueOk = 0.0L;
 long double dValueFailed = 0.0L;
 long double dValueFailed_2 = 0.0L;
 char szInputOk[] ={"3.123456789123456"};
 char szInputFailed[] ={"3.1234567891234567"};
 char szInputFailed_2[] ={"3.1234e-12"};

 dValueOk = std::strtold(szInputOk, NULL); // correct result = 3.123456789123456
 dValueFailed = std::strtold(szInputFailed, NULL); // failed result = 3.123456703989202
 dValueFailed_2 = std::strtold(szInputFailed_2, NULL); // failed result = 3.123399914867293e-012
 return 0;
}

  • That's a bug in the compiler which affects strtold.  The problem is that the constant "1e16" is rounded to single-precision, which is a lot less precise than double-precision.  I've filed SDSCM00038954 to track this issue.

  • Thanks for the fast response. How soon do you think this bug will be fixed?

  • I don't know.  Feel free to track the bug with the SDOWP link in my sig below.

    Thanks and regards,

    -George

  • The bug is in the rts source file strtold.c.  This source is available in the rtssrc.zip file that is included in the compiler installation, in the lib directory.  The strtold uses a global const array that is initialized to powers of ten:

    static const _DATA_ACCESS long double powerof10[]  = { 1.e1, 1.e2, 1.e4, 1.e8,
                                                           1.e16, 1.e32, 1.e64L,
                                                           1.e128L, 1.e256L};

    This code is not initializing all the literals to long doubles as is necessary for C28x.  Thus some of these literals are first converted to double, then long double, losing precision.  The fix, see attached file, is to add the correct suffix for all the values in this list.  As a workaround, you could include the correct source in your project, such that this file is used rather than the object file in the rts library.

    A compiler update with the bug fix would be available in the next patch 5.2.10.  That is currently schedule for 1-28-2011.

  • Thanks for fast response of this problem