Why are there individual keywords for different multiples of π

I don’t understand why there are individual keywords for different multiples of π. Why use TWO_PI when you can just use 2*PI? Doesn’t it allow for more compact and readable code? I would love it if someone could explain this to me.

Welcome to the forum.

The discussion title doesn’t have anything to do with the question asked so I will answer that :grin:

There are 2π radians in a circle so usingTWO_PI would suggest there is some connection with the geometry of a circle for instance ang = ang % TWO_PI where ang is any positive angle in radians would normalise the angle to the range ≥0 and <2π radians.

You could do the same with ang = ang % (2 * TWO_PI) but I think the first is clearer in this instance. Using 2 * PI does not have the same semantic feel also TWO_PI saves a multiplication operation every time it is used.

Ultimately the programmer has a choice between TWO_PI or 2 * PI depending on their own personal preference.

Finally you might ask yourself why have the constant PI rather than using 3.14159265359 the answer is obvious.

Java and Processing have many useful constants to prevent errors and make the code more readable & understandable.

I fixed the title :smiling_face:

We could add that there are even more constants for multiples and fractions of PI too.

As mentioned in the reference:

  • HALF_PI: a mathematical constant with the value 1.57079632679489661923
  • PI: a mathematical constant with the value 3.14159265358979323846
  • QUARTER_PI: a mathematical constant with the value 0.7853982
  • TAU: An alias for TWO_PI
  • TWO_PI: a mathematical constant with the value 6.28318530717958647693

We can also use its alias TAU instead: :hollow_red_circle:

There are others, like HALF_PI, QUARTER_PI and even THIRD_PI:

When they’re clustered together, like 5 - THIRD_PI * RAD_TO_DEG, Java can replace literals and final variables operations as 1 “compile-time constant” value:
https://www.Baeldung.com/java-compile-time-constants