Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-10042

Decimal to binary conversion in strtof and snprintf loses precision

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: Low Low

      The attached try1.c has these lines ...

       

          printf("2 strtof tests\n");
      
          fval = strtof("15.000", NULL);
          printf("expect 15.000, get %f\n", (double) fval);
      
          fval = strtof("15.000000000000000", NULL);
          printf("expect 15.000000000000000, get %f\n", (double) fval);    
      
          printf("\n1 snprintf test\n");
      
      snprintf(buffer, LENGTH, "%.6f", 123456790519087104.0f);
          printf("expect 123456790519087104, get %s\n", buffer);

      Build it ...

      % armcl -@options.txt try1.c -z -o try1.out -l lnk.cmd
      <Linking>
      

      Execute it to see this output ...

      2 strtof tests
      expect 15.000, get 15.000000
      expect 15.000000000000000, get 15.000003
      
      1 snprintf test
      expect 123456790519087104, get 123456790519087076.187
      

      The first strof test is good.  The second is bad.  The snprintf test is bad.

      Please see the related forum thread for more context.

       

       

       

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: