cfloating-pointprintfdoubleformat-string

How do you support both doubles and long doubles without changing format specifiers?


I was able to compile xnec2c with long double's by simply search-replacing double with long double. It calculates correctly, except anywhere that a format specifier is used because it wants %Lf... and of course GCC is barking quite a bit about -Wformat= after hacking it in with this one-liner:

perl -p -i -e 's/_Complex/complex/g; s/gdouble/double/g; s/([ \t(]|^)double/$1long double/g' src/*.[ch]

Ultimately the goal is to:

  1. Replaces all double's with typedef double xnec2c_double_t
  2. Provide a ./configure --enable-long-double option in configure.ac that adjusts the typedef to long double.
  3. Not break printf's and related format specifier-based functions (like sprintf, etc).

Question:

What are the best practices for making format specifiers work with compile-time selection of double vs long double by changing the typedef?

I could go put #ifdef's around every print, or #define SPEC_DOUBLE "%Lf" and string-concatenate as printf("foo=" SPEC_DOUBLE "\n", foo) but that seems almost as hackish as adding #ifdef's around every printf. A my_smart_printf macro could be an option if there is an elegant way to do it, but I've not come up with one.

Is there a "good" way to do this?

Nota bene:


Solution

  • How do you support both doubles and long doubles without changing format specifiers in C?