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:
- Replaces all
double's withtypedef double xnec2c_double_t - Provide a
./configure --enable-long-doubleoption inconfigure.acthat adjusts the typedef tolong double. - Not break
printf's and related format specifier-based functions (likesprintf, 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:
- I'd rather not switch to C++.
- This question is not about autoconf.
- I am the xnec2c maintainer.
Convert the floating point argument to
long doubleand use matchinglong doublespecifiers so the same format can be used in all cases.Follow the
PRI...example from<inttypes.h>and create your own specifier as in#define SPEC_DOUBLE "Lf". Note no"%".Create a helper function to convert your special floating point into a string.
And now for something completely different, consider Formatted print without the need to specify type matching specifiers.