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.

F28035 sprintf doesn't work for floats

Hi,

I'm using a F28035 with the CLA to perform floating point math.

I can't seem to get sprintf to work for floats. I read that you have to increase the stack size for this to potentially work.

I'm using the CLA example as a starting point and the stack size in that project was set to 0x50. I tried increasing the size, but nothing happened. Is there any other settings I need to change?

I read somewhere that printf doesn't work because it's too complicated. It seems to me that it is quite pointless to have a MCU/DSP which can't even handle a basic RS232 out for debugging purposes.

Thanks.

  • Yup KC1, I did notice the same thing few weeks ago when I needed sprintf for float to string conversion. It doesn't work! Also how do I limit my float's decimal numbers like %.2f in C (without using a math library)?

    I would like one of my TI buddies to address this issue, why such limitation?

    Regards,

    Gautam

  • Ok, after some Googling around I found a solution.

    Btw, sprintf did work for a normal project, but when I started from a CLA example, it didn't work.

    I use the following code: (adapted from: http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i) (Funny how it is from stackoverflow and I suspect this is a similar issue)

    I have removed the exponent part.

    Hope it helps!

    #include <math.h>

    #include <string.h> // For printf

    #include <stdio.h>

    static double PRECISION = 0.001;

    static int MAX_NUMBER_STRING_SIZE = 32; // this can be smaller I think

    char print_str[30]; // this can be smaller I think

    char text_1[30];


    char * dtoa(char *s, double n) {

      // handle special cases

      if (isnan(n)) {

        strcpy(s, "nan");

      } else if (isinf(n)) {

        strcpy(s, "inf");

      } else if (n == 0.0) {

        strcpy(s, "0");

      } else {

        int digit, m, m1;

        char *c = s;

        int neg = (n < 0);

        if (neg)

          n = -n;

        // calculate magnitude

         m = log10(n);

        if (neg)

          *(c++) = '-';

        else

          // *(c++) = '+'; // To have a ‘+’ sign

        if (m < 1.0) {

          m = 0;

        }

        // convert the number

        while (n > PRECISION || m >= 0) {

          double weight = pow(10.0, m);

          if (weight > 0 && !isinf(weight)) {

            digit = floor(n / weight);

            n -= (digit * weight);

            *(c++) = '0' + digit;

          }

          if (m == 0 && n > 0)

            *(c++) = '.';

          m--;

        }

        *(c) = '\0';

      }

      return s;

    }

    int main(void){
      float num = 1.214;

      sprintf(text_1, “%s\n\r\0”, dtoa(print_str, num);
      scia_msg(text_1);

    }

  • Hi,

     

    sprintf(text_1, “%s\n\r\0”, dtoa(print_str, num);

    KC1, the above method is for the ones who don't have libraries to play with. I was expecting the sprintf to function in this way:

    double a = 0.0000005l;
    char aa[50];
    sprintf(aa,a);
    sci_msg(aa);

    It didn't work. Later found out that CCS supports the following pattern:

    sprintf(char *_string, const char *_format, ...)

    So, chose some other way to achieve the same!

    Regards,

    Gautam

     

  • Hi,

    Yes it's for people who don't have standard C lib functions. But after using a CLA example project as a template, the sprintf function stopped working for converting floats to strings.

    I googled around and saw that other people had similar issues and they had suspected it to be a stack problem. But it wasn't clear how to fix it. (Increasing stack size didn't work for me)

    So I looked for a more efficient method and found the above code and it did the job for me.

    Thanks.

  • Cool, no problem. Thanks for sharing!

    Regards,

    Gautam