Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does math.h define pi, pi/2, 2/pi but not 2*pi? [closed]

Tags:

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:

  1. Avoiding rounding errors.
  2. Avoiding runtime divisions which might be more expensive.
like image 553
hanno Avatar asked Nov 17 '12 00:11

hanno


People also ask

What is the difference between cmath and math H?

[cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math. h] defines symbols in the global namespace, and may also define symbols in the std namespace. if you include the former and use an unqualified symbol, it may compile with one compiler but not with another.

How is math H used in PI in C?

Using Constants in C: One is by using using the preprocessor directive '#define' to make 'PI' equal to 3.142857. The other uses the key work 'const' to define a double called 'pi' equal to 22.0/7.0.


1 Answers

Is 2*M_PI so hard to write?

Seriously though, once upon a time, when people worried about simple compilers that might not do constant folding and division was prohibitively expensive, it actually made sense to have a constant PI/2 rather than risk a runtime division. In our modern world, one would probably just define M_PI and call it a day, but the other variants live on for backwards compatibility.

like image 99
Stephen Canon Avatar answered Sep 28 '22 05:09

Stephen Canon