We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am working with the C8051F580 using a custom board.
I use UART0 to communicate with TERATERM. 115200 bps
I am using printf to send characters to TERATERM
for decimal printing works fine: printf ("%d",(samples[i]*5)/100);
but if I change it to float it displays "???" : printf ("%3.6f",(samples[i]*5)/100);
"samples" is unsigned int
any suggestion will be appreciated
Thank you.
Fausto Bartra
Try: printf ("%3.6f", (samples[i] * 5.0) / 100.0);
or, depending on the expected range of "samples": printf ("%3.6f", (samples[i] * 5) / 100.0);
Rene:
this works now, thank you.
it is funny that you need to have "5 & 100" written as "5.0 & 100.0" for the compiler to work.
Thanks very much
Fausto
That's not so much "funny" as it is the way C works, and always did: operations on integers yield integers, so if it's floating-point output you wanted, you have to transition to floating-point at some point in the computation.It may well be a generally better plan to not involve floats at all, though, and instead allow the output to come without that decimal separator:
printf("%9u", samples[i]*5);
because surely, whatever is at the receiving end of that output has an easier time converting to floating-point data formats than a small, FPU-less 8051 derivative.