This is a really simple question: Why are there predefined constants for pi, pi/2, pi/4, 1/pi and 2/pi but not for 2*pi? Is there a deeper reason behind it?
This question is not about the whole pi vs tau debate. I am wondering if there is a technical reason for implementing certain constants but not others. I can think of two possibilities:
- Avoiding rounding errors.
- Avoiding runtime divisions which might be more expensive.
This is just my guess.
I suppose that these constants are related to the implementations of different functions in the math library:
M_PI,M_PI_2, andM_PI_4show up quite often but there’s no2.0 * M_PI. So to Hanno’s original question, I think MvanGeest is right — 2π is just not that useful, at least in implementinglibm.Now about
M_PI_2andM_PI_4, their existences are well justified. The documentation of the GNU C library suggests that “these constants come from the Unix98 standard and were also available in 4.4BSD”. Compilers were not that smart back at that time. TypingM_PI/4instead ofM_PI_4may cause an unnecessary division. Although modern compilers can optimize that away (gcc uses mpfr since 2008 so even rounding is done correctly), using numeric constants is still a more portable way to write high performance code.