0
#ifdef DOUBLE_PRECISION_COMPUTE
    #define MAKE_PRECISE(N) ...
#endif
// if not defined, leave as it is. MAKE_PRECISE(N) (N)

Can above macro (replaced "..." with the right redifiniton) do the below operation?

double PI=MAKE_PRECISE(3.14159265359f);

becomes

double PI=3.14159265359;

double area= MAKE_PRECISE(3.14159265359f)*r*r;

becomes

double area= 3.14159265359*r*r;
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
  • Do you have C++14 available? – AndyG Jul 14 '17 at 14:09
  • is `MAKE_DOUBLE` and `ADD_PRECISION` supposed to be the same symbol? – 463035818_is_not_an_ai Jul 14 '17 at 14:09
  • Just made up those names, yes same intention. Edited, now all are MAKE_DOUBLE. I need to drop the f at the and. No, I don't have C++14 available yet but I can try later. Now I'm in C++11 – huseyin tugrul buyukisik Jul 14 '17 at 14:10
  • `static constexpr auto PI = 3.14159265358979323846264338327950288` should work I guess?? Apple defines it as `#define M_PI 3.14159265358979323846264338327950288` in Objective-C. Any particular reason you need it to be prefixed as a float? I guess I'm wondering what the point of `MAKE_PRECISE` is. – Brandon Jul 14 '17 at 14:17
  • 1
    _"Edited, now all are MAKE_DOUBLE."_ Except, no, they're not; they're all `MAKE_PRECISE`. Your attention to detail is invisible. – Lightness Races in Orbit Jul 14 '17 at 14:37
  • @Brandon [This question and answers](https://stackoverflow.com/q/5026570/2096401) gives some of the effects of using `f` (to do with whether or not arguments get promoted to `double` or not). Not quite sure either why it would matter or need to be selectable, but... – TripeHound Jul 14 '17 at 15:14

2 Answers2

6

No.

There is no way to transform a token using preprocessor directives.

Instead, correct the literals by removing the f in your source code.

The result will anyway be far easier to read than wrapping everything in some MAKE_PRECISE macro that is unclear as to what's going on.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

As noted, you cannot strip the f, but you can conditionally add it:

#ifdef DOUBLE_PRECISION_COMPUTE
#   define MAKE_PRECISE(N) (N)
#else
#   define MAKE_PRECISE(N) (N##f)
#endif

which will turn:

double area = MAKE_PRECISE(3.14159265359)*r*r;

into either:

double area = (3.14159265359)*r*r;   // with DOUBLE_PRECISION_COMPUTE defined

or

double area = (3.14159265359f)*r*r;  // with DOUBLE_PRECISION_COMPUTE UNdefined
TripeHound
  • 2,721
  • 23
  • 37