// Tertiary
t = a > 0? a : a + 6.28
// vs fmod
m = fmod(a + 6.28, 6.28)
Is there a general difference between the two in performance?
Of course profiling is best @NathanOlive, yet as a general guide, consider optimization potential.
A compiler will typically optimize over the entire range of the type of a
, not [-3.14, 3.14]. t
, a simple calculation, is readily optimize-able.
Further, depending on FLT_EVAL_METHOD, In C, m
calculation is forced into double
and certainly a function call. More restrictions mean less optimization possibilities. t
may use an optimal FP width.
Recommend a > 0 ? a : a + 6.28
as a general preferred approach.
Given 2 expressions that do the same thing
But they do not do the same thing over domain [-3.14, 3.14]
About 1/4 of all double
are in the range [0...1.0]. m
usage of a + 6.28
will lose at least 3 to all bits of precision with that addition. Advantage: t
.
Ranges differ:
The range of t
is [0, 6.28]
The range of m
is [0, 6.28), not [0, 6.28]
Consideration about the higher goal
It is apparent code is attempting trigonometric range reduction. To do this well is harder than the basic sine. cosine, tangent calculation itself. See ARGUMENT REDUCTION FOR HUGE ARGUMENTS:
Good to the Last Bit.
If code is starting with degrees rather than radians, consider the advantages of range reduction in degrees first.
Bigger picture
Depending on how a
is derived or t
, m
are used, even better performance ideas are possible. So if performance is truly a concern, the surrounding code is needed, else we are incorrectly micro-optimizing.