Print the float in C, avoiding allowing the variational parameter to double

How can I print (i.e. stdout) a float in C without if it will be passed to double when passed to printf?

The problem here is that variable functions in C allow all float parameters to double, which leads to two unnecessary conversions. For example, if you enable -Wdouble-promotion in GCC and compile

float f = 0.f; printf("%f", f); 

You'll get

 warning: implicit conversion from 'float' to 'double' when passing argument to function 

I have relatively little processing power to play with (ARM Cortex-M3 at 72 MHz), and I definitely have a bottleneck in the output of ASCII floating point data. Since the architecture lacks a hardware FPU to begin with, converting between single and double precision does not help.

Is there a way to print float in direct C more efficiently?

+7
source share
4 answers

Avoiding progress will not save you, since the internal double (or more likely long double ) printf arithmetic will consume at least 1000x as much time. Accurate printing of floating point values ​​is not easy .

If you still don't need accuracy, and you just need to quickly print out the approximate values, you can flip your own print cycle. While your values ​​are not too large to match the integer type, first convert and print the non-fractional part as an integer, and then subtract that off and the cycle multiplied by 10, and cross out the integer part to print the fractional part one digit at a time (buffer in line to improve performance).

Or you could just do something like:

 printf("%d.%.6d", (int)x, (int)((x-(int)x)*1000000)); 
+7
source

Unfortunately, printf does not support simple float: s support.

This means that you will need to write your own print function. If you don't want the full expressive power of printf , you can easily convert the floating point value to an integral part and the part representing the number of decimal places, and print both values ​​using integers.

If, on the other hand, you just want to get rid of the warning, you can explicitly point the float to double .

+5
source

I think this does not matter - printf is already such a mysterious problem that these conversions should not matter. Converting floating point time to double should be much less than converting any number to ascii (you should / could profile your code to get the final answer). The only remaining solution would be to write your own custom output procedure, which converts float-> ascii, and then uses puts (or similar).

+2
source
  • First approach: use ftoa instead of printf. Profile.

  • To increase the flexibility of output, I would go to the source code of your stdlib compiler, perhaps in some derivative of gcc, find the printf implementation and copy the appropriate code to convert double β†’ ascii. Rewrite it in float -> ascii.

  • Then manually change one or two inappropriate call sites to a new (non-invariant) version and profile.

  • If it solves your problem, you might consider rewriting your own printf based on the version from stdlib in which you pass float * instead of float. This should get rid of automatic advertising.

0
source

All Articles