1

I'm actually trying to print a float in c using atmel std and instead of printing the value , it gives me just "f". Anyone knows why it's like this?

code :

  float dist = 2.0*3.14*0.25*count;

  printf("counter : %d\n",count);
  printf("dist : %f\n", dist);

so, counter's actually a volatile long that updates after a button click on my hardware, consider it's working, the print of it's correct, but the print of dist's not correct, it gives me this:

dist : f
counter : 12
dist : f
counter : 12
dist : f
counter : 12
dist : f
counter : 12
dist : f
counter : 12
dist : f

Even if i try printing 0.25 like:

printf("%f\n",0.25);

it gives me the output "f" here 's the compiler output

Tested this, but it didn't worked:

    char b[10];

    uint32_t dist = b_radious*counter*M_PI*2.0f;

    dist = (dist + 1) >> 1;

    unsigned units = dist/1000;
    unsigned fraction = dist - (dist*1000);

    sprintf(b, "%4u.%03u", units, fraction);
gnites
  • 13
  • 5

1 Answers1

5

In order for printf to support floating-point output, it would need to include a rather substantial amount (thousands of bytes) of code which would be totally useless for many applications that use printf but don't use it to output floating-point numbers. Some tool chains will attempt to automatically link in a full-featured version of printf if code outputs floating-point numbers, and a minimal one if it doesn't, but some tool chains require that the user manually select which version of printf to use.

Note that if code space is of any concern, using constructs like:

/* Output a number from 0.000 to 9999.000 */
uint32_t xi = x*2000.0f;
xi = (xi+1) >> 1; // Round value without extra floating-point math
unsigned units = xi/1000;
unsigned frac = xi - (units*1000);
sprintf(out, "%4u.%03u", units, frac);

and avoiding the use of floating-point formats with printf may avoid the need to use the larger printf library, while using far less code space than that library would consume.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • check the edit to see what i've tested, but it didn't worked – gnites May 16 '20 at 19:25
  • output: nÇ@ Ÿ@nÇ@ Ÿ@ – gnites May 16 '20 at 19:27
  • I assume you're then outputting `b` somehow? If you want to use `printf` instead of `sprintf`, get rid of the `out` argument. As an embedded programmer, I'm more prone to use `sprintf` rather than `printf` so as to allow data to be output to e.g. an LCD display or other device that wouldn't really work with `printf`. – supercat May 16 '20 at 19:30
  • yes, i'm using it to print on a lcd, i'm just using printf to debug since when i put the sprintf and use my function to draw it on the lcd, the RTT stops working. – gnites May 16 '20 at 19:37
  • If you want to output values with three significant figures, you need to multiply by 2000.0f, not 2.0f. What happens if you try to do something like `printf("%4u.%03u", 1234,56);`? How about `printf("%4d.%03d", 1234,56);` Different lightweight versions of `printf` include different features. If you're trying to use a typical text based LCD, a useful trick is to output integers right to left, by repeatedly computing `x%10` and `x/10`, inserting a decimal point after outputting some number of digits. – supercat May 16 '20 at 19:42
  • dude you really helped me, thanks a lot. it worked!! – gnites May 16 '20 at 19:43
  • @gnites: So what did you end up doing? – supercat May 16 '20 at 19:44
  • @gnites I notice that your floating point values posted seem to be a crude value for π (which might as well be `22 / 7` and `0.25` which could be `/4`. It's often possible to avoid floating point altogether. – Weather Vane May 16 '20 at 19:46
  • used sprintf, i checked that a flag in rtt was always setting 0 after doing the print in lcd, so I changed the flag to !flag and used your method to print on lcd . Only isue now's that lcd screen have some noise in the right side. – gnites May 16 '20 at 19:48