0

I have a periodic function with fmod() like this (in C):

float f(float x) {
    x = fmod(x, 1);
    if (x < 0.5f) return 1 - x;
    else return x
}

If I need a formal math formula for this function, what is the prettiest way?

Do people use fmod or mod functions in formulas like this? It looks strange.

$$ f(x) = \left\{ \begin{array}{**lr**} 1-\mathrm{fmod} (x,1), & 0 \leq \mathrm{fmod} (x,1) < 0.5 \\ \mathrm{fmod}(x,1), & 0.5 \leq \mathrm{fmod} (x,1) < 1 \end{array} \right. $$

landings
  • 121
  • 1
    If you can solve for $\text{fmod}(x,1)=0,0.5,1$ then you can change the expression. (Even if there is a solution, the expression may not be so 'pretty' as you wish). Otherwise, it's impossible to re-express the equation. – Eric Monlye Jul 20 '20 at 08:20
  • Thanks. Actually fmod() is the real number version of 'divide and find remainder' operation. So fmod(x, c) produces periodic effect with cycle c. I wonder if there's a way to express this operation. (I don't know if mathematicians use $n \ \rm{mod} \ m$ for integers) – landings Jul 20 '20 at 08:40
  • 1
    Ah, I see. How about $f(x)=\max{\lfloor x\rfloor,1-\lfloor x\rfloor}$? – Eric Monlye Jul 20 '20 at 09:30
  • 1
    @EricMonlye Actually, you want $\max{x-\lfloor x\rfloor,\lceil x\rceil -x}$ – Hagen von Eitzen Jul 20 '20 at 11:05
  • @HagenvonEitzen You are right. I mistook the $\lfloor\rfloor$ for the decimal part. – Eric Monlye Jul 20 '20 at 11:09
  • 1
    That being said, $x\bmod 1$ for fmod(x,1) is certainly understood. – Hagen von Eitzen Jul 20 '20 at 11:19

0 Answers0