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);
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.