tanhf (example 3.4)

Percentage Accurate: 52.7% → 100.0%
Time: 7.2s
Alternatives: 7
Speedup: 17.9×

Specification

?
\[\begin{array}{l} \\ \frac{1 - \cos x}{\sin x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (sin x)))
double code(double x) {
	return (1.0 - cos(x)) / sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / sin(x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / Math.sin(x);
}
def code(x):
	return (1.0 - math.cos(x)) / math.sin(x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / sin(x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / sin(x);
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - \cos x}{\sin x}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 52.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1 - \cos x}{\sin x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (sin x)))
double code(double x) {
	return (1.0 - cos(x)) / sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / sin(x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / Math.sin(x);
}
def code(x):
	return (1.0 - math.cos(x)) / math.sin(x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / sin(x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / sin(x);
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - \cos x}{\sin x}
\end{array}

Alternative 1: 100.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \tan \left(0.5 \cdot x\right) \end{array} \]
(FPCore (x) :precision binary64 (tan (* 0.5 x)))
double code(double x) {
	return tan((0.5 * x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = tan((0.5d0 * x))
end function
public static double code(double x) {
	return Math.tan((0.5 * x));
}
def code(x):
	return math.tan((0.5 * x))
function code(x)
	return tan(Float64(0.5 * x))
end
function tmp = code(x)
	tmp = tan((0.5 * x));
end
code[x_] := N[Tan[N[(0.5 * x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan \left(0.5 \cdot x\right)
\end{array}
Derivation
  1. Initial program 47.6%

    \[\frac{1 - \cos x}{\sin x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{1 - \cos x}{\sin x}} \]
    2. lift--.f64N/A

      \[\leadsto \frac{\color{blue}{1 - \cos x}}{\sin x} \]
    3. lift-cos.f64N/A

      \[\leadsto \frac{1 - \color{blue}{\cos x}}{\sin x} \]
    4. lift-sin.f64N/A

      \[\leadsto \frac{1 - \cos x}{\color{blue}{\sin x}} \]
    5. hang-p0-tanN/A

      \[\leadsto \color{blue}{\tan \left(\frac{x}{2}\right)} \]
    6. lower-tan.f64N/A

      \[\leadsto \color{blue}{\tan \left(\frac{x}{2}\right)} \]
    7. clear-numN/A

      \[\leadsto \tan \color{blue}{\left(\frac{1}{\frac{2}{x}}\right)} \]
    8. associate-/r/N/A

      \[\leadsto \tan \color{blue}{\left(\frac{1}{2} \cdot x\right)} \]
    9. metadata-evalN/A

      \[\leadsto \tan \left(\color{blue}{\frac{1}{2}} \cdot x\right) \]
    10. lower-*.f64100.0

      \[\leadsto \tan \color{blue}{\left(0.5 \cdot x\right)} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\tan \left(0.5 \cdot x\right)} \]
  5. Add Preprocessing

Alternative 2: 53.0% accurate, 4.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 10.2:\\ \;\;\;\;\left(\frac{x \cdot x}{\mathsf{fma}\left(\mathsf{fma}\left(-0.002857142857142857, x \cdot x, -2.4\right), x \cdot x, 24\right)} + 0.5\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x 10.2)
   (*
    (+
     (/ (* x x) (fma (fma -0.002857142857142857 (* x x) -2.4) (* x x) 24.0))
     0.5)
    x)
   1.0))
double code(double x) {
	double tmp;
	if (x <= 10.2) {
		tmp = (((x * x) / fma(fma(-0.002857142857142857, (x * x), -2.4), (x * x), 24.0)) + 0.5) * x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (x <= 10.2)
		tmp = Float64(Float64(Float64(Float64(x * x) / fma(fma(-0.002857142857142857, Float64(x * x), -2.4), Float64(x * x), 24.0)) + 0.5) * x);
	else
		tmp = 1.0;
	end
	return tmp
end
code[x_] := If[LessEqual[x, 10.2], N[(N[(N[(N[(x * x), $MachinePrecision] / N[(N[(-0.002857142857142857 * N[(x * x), $MachinePrecision] + -2.4), $MachinePrecision] * N[(x * x), $MachinePrecision] + 24.0), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 10.2:\\
\;\;\;\;\left(\frac{x \cdot x}{\mathsf{fma}\left(\mathsf{fma}\left(-0.002857142857142857, x \cdot x, -2.4\right), x \cdot x, 24\right)} + 0.5\right) \cdot x\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 10.199999999999999

    1. Initial program 32.0%

      \[\frac{1 - \cos x}{\sin x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right)\right) \cdot x} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right)\right) \cdot x} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right) + \frac{1}{2}\right)} \cdot x \]
      4. *-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right) \cdot {x}^{2}} + \frac{1}{2}\right) \cdot x \]
      5. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right), {x}^{2}, \frac{1}{2}\right)} \cdot x \]
      6. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right) + \frac{1}{24}}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right) \cdot {x}^{2}} + \frac{1}{24}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
      8. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}, {x}^{2}, \frac{1}{24}\right)}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
      9. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\frac{17}{40320} \cdot {x}^{2} + \frac{1}{240}}, {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
      10. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{17}{40320}, {x}^{2}, \frac{1}{240}\right)}, {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
      11. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, \color{blue}{x \cdot x}, \frac{1}{240}\right), {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
      12. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, \color{blue}{x \cdot x}, \frac{1}{240}\right), {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
      13. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, x \cdot x, \frac{1}{240}\right), \color{blue}{x \cdot x}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
      14. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, x \cdot x, \frac{1}{240}\right), \color{blue}{x \cdot x}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
      15. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, x \cdot x, \frac{1}{240}\right), x \cdot x, \frac{1}{24}\right), \color{blue}{x \cdot x}, \frac{1}{2}\right) \cdot x \]
      16. lower-*.f6472.1

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.00042162698412698415, x \cdot x, 0.004166666666666667\right), x \cdot x, 0.041666666666666664\right), \color{blue}{x \cdot x}, 0.5\right) \cdot x \]
    5. Applied rewrites72.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.00042162698412698415, x \cdot x, 0.004166666666666667\right), x \cdot x, 0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x} \]
    6. Step-by-step derivation
      1. Applied rewrites72.1%

        \[\leadsto \mathsf{fma}\left(\frac{1}{\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.00042162698412698415, x \cdot x, 0.004166666666666667\right), x \cdot x, 0.041666666666666664\right)}}, x \cdot x, 0.5\right) \cdot x \]
      2. Taylor expanded in x around 0

        \[\leadsto \mathsf{fma}\left(\frac{1}{24 + {x}^{2} \cdot \left(\frac{-1}{350} \cdot {x}^{2} - \frac{12}{5}\right)}, x \cdot x, \frac{1}{2}\right) \cdot x \]
      3. Step-by-step derivation
        1. Applied rewrites72.2%

          \[\leadsto \mathsf{fma}\left(\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(-0.002857142857142857, x \cdot x, -2.4\right), x \cdot x, 24\right)}, x \cdot x, 0.5\right) \cdot x \]
        2. Step-by-step derivation
          1. Applied rewrites72.2%

            \[\leadsto \left(\frac{x \cdot x}{\mathsf{fma}\left(\mathsf{fma}\left(-0.002857142857142857, x \cdot x, -2.4\right), x \cdot x, 24\right)} + 0.5\right) \cdot x \]

          if 10.199999999999999 < x

          1. Initial program 98.6%

            \[\frac{1 - \cos x}{\sin x} \]
          2. Add Preprocessing
          3. Applied rewrites11.5%

            \[\leadsto \color{blue}{{1}^{-0.5}} \]
          4. Step-by-step derivation
            1. lift-pow.f64N/A

              \[\leadsto \color{blue}{{1}^{\frac{-1}{2}}} \]
            2. pow-base-111.5

              \[\leadsto \color{blue}{1} \]
          5. Applied rewrites11.5%

            \[\leadsto \color{blue}{1} \]
        3. Recombined 2 regimes into one program.
        4. Add Preprocessing

        Alternative 3: 53.0% accurate, 5.1× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 14:\\ \;\;\;\;\left(\frac{x \cdot x}{\mathsf{fma}\left(x \cdot x, -2.4, 24\right)} + 0.5\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
        (FPCore (x)
         :precision binary64
         (if (<= x 14.0) (* (+ (/ (* x x) (fma (* x x) -2.4 24.0)) 0.5) x) 1.0))
        double code(double x) {
        	double tmp;
        	if (x <= 14.0) {
        		tmp = (((x * x) / fma((x * x), -2.4, 24.0)) + 0.5) * x;
        	} else {
        		tmp = 1.0;
        	}
        	return tmp;
        }
        
        function code(x)
        	tmp = 0.0
        	if (x <= 14.0)
        		tmp = Float64(Float64(Float64(Float64(x * x) / fma(Float64(x * x), -2.4, 24.0)) + 0.5) * x);
        	else
        		tmp = 1.0;
        	end
        	return tmp
        end
        
        code[x_] := If[LessEqual[x, 14.0], N[(N[(N[(N[(x * x), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] * -2.4 + 24.0), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;x \leq 14:\\
        \;\;\;\;\left(\frac{x \cdot x}{\mathsf{fma}\left(x \cdot x, -2.4, 24\right)} + 0.5\right) \cdot x\\
        
        \mathbf{else}:\\
        \;\;\;\;1\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if x < 14

          1. Initial program 32.0%

            \[\frac{1 - \cos x}{\sin x} \]
          2. Add Preprocessing
          3. Taylor expanded in x around 0

            \[\leadsto \color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right)\right)} \]
          4. Step-by-step derivation
            1. *-commutativeN/A

              \[\leadsto \color{blue}{\left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right)\right) \cdot x} \]
            2. lower-*.f64N/A

              \[\leadsto \color{blue}{\left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right)\right) \cdot x} \]
            3. +-commutativeN/A

              \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right) + \frac{1}{2}\right)} \cdot x \]
            4. *-commutativeN/A

              \[\leadsto \left(\color{blue}{\left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right)\right) \cdot {x}^{2}} + \frac{1}{2}\right) \cdot x \]
            5. lower-fma.f64N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{24} + {x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right), {x}^{2}, \frac{1}{2}\right)} \cdot x \]
            6. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot \left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right) + \frac{1}{24}}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
            7. *-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}\right) \cdot {x}^{2}} + \frac{1}{24}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
            8. lower-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{1}{240} + \frac{17}{40320} \cdot {x}^{2}, {x}^{2}, \frac{1}{24}\right)}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
            9. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\frac{17}{40320} \cdot {x}^{2} + \frac{1}{240}}, {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
            10. lower-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{17}{40320}, {x}^{2}, \frac{1}{240}\right)}, {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
            11. unpow2N/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, \color{blue}{x \cdot x}, \frac{1}{240}\right), {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
            12. lower-*.f64N/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, \color{blue}{x \cdot x}, \frac{1}{240}\right), {x}^{2}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
            13. unpow2N/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, x \cdot x, \frac{1}{240}\right), \color{blue}{x \cdot x}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
            14. lower-*.f64N/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, x \cdot x, \frac{1}{240}\right), \color{blue}{x \cdot x}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
            15. unpow2N/A

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{17}{40320}, x \cdot x, \frac{1}{240}\right), x \cdot x, \frac{1}{24}\right), \color{blue}{x \cdot x}, \frac{1}{2}\right) \cdot x \]
            16. lower-*.f6472.1

              \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.00042162698412698415, x \cdot x, 0.004166666666666667\right), x \cdot x, 0.041666666666666664\right), \color{blue}{x \cdot x}, 0.5\right) \cdot x \]
          5. Applied rewrites72.1%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.00042162698412698415, x \cdot x, 0.004166666666666667\right), x \cdot x, 0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x} \]
          6. Step-by-step derivation
            1. Applied rewrites72.1%

              \[\leadsto \mathsf{fma}\left(\frac{1}{\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.00042162698412698415, x \cdot x, 0.004166666666666667\right), x \cdot x, 0.041666666666666664\right)}}, x \cdot x, 0.5\right) \cdot x \]
            2. Taylor expanded in x around 0

              \[\leadsto \mathsf{fma}\left(\frac{1}{24 + \frac{-12}{5} \cdot {x}^{2}}, x \cdot x, \frac{1}{2}\right) \cdot x \]
            3. Step-by-step derivation
              1. Applied rewrites72.1%

                \[\leadsto \mathsf{fma}\left(\frac{1}{\mathsf{fma}\left(-2.4, x \cdot x, 24\right)}, x \cdot x, 0.5\right) \cdot x \]
              2. Step-by-step derivation
                1. Applied rewrites72.1%

                  \[\leadsto \left(\frac{x \cdot x}{\mathsf{fma}\left(x \cdot x, -2.4, 24\right)} + 0.5\right) \cdot x \]

                if 14 < x

                1. Initial program 98.6%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Applied rewrites11.5%

                  \[\leadsto \color{blue}{{1}^{-0.5}} \]
                4. Step-by-step derivation
                  1. lift-pow.f64N/A

                    \[\leadsto \color{blue}{{1}^{\frac{-1}{2}}} \]
                  2. pow-base-111.5

                    \[\leadsto \color{blue}{1} \]
                5. Applied rewrites11.5%

                  \[\leadsto \color{blue}{1} \]
              3. Recombined 2 regimes into one program.
              4. Add Preprocessing

              Alternative 4: 53.0% accurate, 6.3× speedup?

              \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 3.2:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.004166666666666667, x \cdot x, 0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
              (FPCore (x)
               :precision binary64
               (if (<= x 3.2)
                 (*
                  (fma (fma 0.004166666666666667 (* x x) 0.041666666666666664) (* x x) 0.5)
                  x)
                 1.0))
              double code(double x) {
              	double tmp;
              	if (x <= 3.2) {
              		tmp = fma(fma(0.004166666666666667, (x * x), 0.041666666666666664), (x * x), 0.5) * x;
              	} else {
              		tmp = 1.0;
              	}
              	return tmp;
              }
              
              function code(x)
              	tmp = 0.0
              	if (x <= 3.2)
              		tmp = Float64(fma(fma(0.004166666666666667, Float64(x * x), 0.041666666666666664), Float64(x * x), 0.5) * x);
              	else
              		tmp = 1.0;
              	end
              	return tmp
              end
              
              code[x_] := If[LessEqual[x, 3.2], N[(N[(N[(0.004166666666666667 * N[(x * x), $MachinePrecision] + 0.041666666666666664), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
              
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              \mathbf{if}\;x \leq 3.2:\\
              \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.004166666666666667, x \cdot x, 0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x\\
              
              \mathbf{else}:\\
              \;\;\;\;1\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if x < 3.2000000000000002

                1. Initial program 32.0%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{240} \cdot {x}^{2}\right)\right)} \]
                4. Step-by-step derivation
                  1. *-commutativeN/A

                    \[\leadsto \color{blue}{\left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{240} \cdot {x}^{2}\right)\right) \cdot x} \]
                  2. lower-*.f64N/A

                    \[\leadsto \color{blue}{\left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{240} \cdot {x}^{2}\right)\right) \cdot x} \]
                  3. +-commutativeN/A

                    \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{24} + \frac{1}{240} \cdot {x}^{2}\right) + \frac{1}{2}\right)} \cdot x \]
                  4. *-commutativeN/A

                    \[\leadsto \left(\color{blue}{\left(\frac{1}{24} + \frac{1}{240} \cdot {x}^{2}\right) \cdot {x}^{2}} + \frac{1}{2}\right) \cdot x \]
                  5. lower-fma.f64N/A

                    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{24} + \frac{1}{240} \cdot {x}^{2}, {x}^{2}, \frac{1}{2}\right)} \cdot x \]
                  6. +-commutativeN/A

                    \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{1}{240} \cdot {x}^{2} + \frac{1}{24}}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
                  7. lower-fma.f64N/A

                    \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{1}{240}, {x}^{2}, \frac{1}{24}\right)}, {x}^{2}, \frac{1}{2}\right) \cdot x \]
                  8. unpow2N/A

                    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{240}, \color{blue}{x \cdot x}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
                  9. lower-*.f64N/A

                    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{240}, \color{blue}{x \cdot x}, \frac{1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x \]
                  10. unpow2N/A

                    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{240}, x \cdot x, \frac{1}{24}\right), \color{blue}{x \cdot x}, \frac{1}{2}\right) \cdot x \]
                  11. lower-*.f6472.1

                    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(0.004166666666666667, x \cdot x, 0.041666666666666664\right), \color{blue}{x \cdot x}, 0.5\right) \cdot x \]
                5. Applied rewrites72.1%

                  \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.004166666666666667, x \cdot x, 0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x} \]

                if 3.2000000000000002 < x

                1. Initial program 98.6%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Applied rewrites11.5%

                  \[\leadsto \color{blue}{{1}^{-0.5}} \]
                4. Step-by-step derivation
                  1. lift-pow.f64N/A

                    \[\leadsto \color{blue}{{1}^{\frac{-1}{2}}} \]
                  2. pow-base-111.5

                    \[\leadsto \color{blue}{1} \]
                5. Applied rewrites11.5%

                  \[\leadsto \color{blue}{1} \]
              3. Recombined 2 regimes into one program.
              4. Add Preprocessing

              Alternative 5: 52.9% accurate, 9.3× speedup?

              \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 3.2:\\ \;\;\;\;\mathsf{fma}\left(x \cdot x, 0.041666666666666664, 0.5\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
              (FPCore (x)
               :precision binary64
               (if (<= x 3.2) (* (fma (* x x) 0.041666666666666664 0.5) x) 1.0))
              double code(double x) {
              	double tmp;
              	if (x <= 3.2) {
              		tmp = fma((x * x), 0.041666666666666664, 0.5) * x;
              	} else {
              		tmp = 1.0;
              	}
              	return tmp;
              }
              
              function code(x)
              	tmp = 0.0
              	if (x <= 3.2)
              		tmp = Float64(fma(Float64(x * x), 0.041666666666666664, 0.5) * x);
              	else
              		tmp = 1.0;
              	end
              	return tmp
              end
              
              code[x_] := If[LessEqual[x, 3.2], N[(N[(N[(x * x), $MachinePrecision] * 0.041666666666666664 + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
              
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              \mathbf{if}\;x \leq 3.2:\\
              \;\;\;\;\mathsf{fma}\left(x \cdot x, 0.041666666666666664, 0.5\right) \cdot x\\
              
              \mathbf{else}:\\
              \;\;\;\;1\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if x < 3.2000000000000002

                1. Initial program 32.0%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{x \cdot \left(\frac{1}{2} + \frac{1}{24} \cdot {x}^{2}\right)} \]
                4. Step-by-step derivation
                  1. *-commutativeN/A

                    \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{1}{24} \cdot {x}^{2}\right) \cdot x} \]
                  2. lower-*.f64N/A

                    \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{1}{24} \cdot {x}^{2}\right) \cdot x} \]
                  3. +-commutativeN/A

                    \[\leadsto \color{blue}{\left(\frac{1}{24} \cdot {x}^{2} + \frac{1}{2}\right)} \cdot x \]
                  4. *-commutativeN/A

                    \[\leadsto \left(\color{blue}{{x}^{2} \cdot \frac{1}{24}} + \frac{1}{2}\right) \cdot x \]
                  5. lower-fma.f64N/A

                    \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{24}, \frac{1}{2}\right)} \cdot x \]
                  6. unpow2N/A

                    \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{24}, \frac{1}{2}\right) \cdot x \]
                  7. lower-*.f6472.1

                    \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.041666666666666664, 0.5\right) \cdot x \]
                5. Applied rewrites72.1%

                  \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 0.041666666666666664, 0.5\right) \cdot x} \]

                if 3.2000000000000002 < x

                1. Initial program 98.6%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Applied rewrites11.5%

                  \[\leadsto \color{blue}{{1}^{-0.5}} \]
                4. Step-by-step derivation
                  1. lift-pow.f64N/A

                    \[\leadsto \color{blue}{{1}^{\frac{-1}{2}}} \]
                  2. pow-base-111.5

                    \[\leadsto \color{blue}{1} \]
                5. Applied rewrites11.5%

                  \[\leadsto \color{blue}{1} \]
              3. Recombined 2 regimes into one program.
              4. Add Preprocessing

              Alternative 6: 52.9% accurate, 17.9× speedup?

              \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 1.4:\\ \;\;\;\;0.5 \cdot x\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
              (FPCore (x) :precision binary64 (if (<= x 1.4) (* 0.5 x) 1.0))
              double code(double x) {
              	double tmp;
              	if (x <= 1.4) {
              		tmp = 0.5 * x;
              	} else {
              		tmp = 1.0;
              	}
              	return tmp;
              }
              
              real(8) function code(x)
                  real(8), intent (in) :: x
                  real(8) :: tmp
                  if (x <= 1.4d0) then
                      tmp = 0.5d0 * x
                  else
                      tmp = 1.0d0
                  end if
                  code = tmp
              end function
              
              public static double code(double x) {
              	double tmp;
              	if (x <= 1.4) {
              		tmp = 0.5 * x;
              	} else {
              		tmp = 1.0;
              	}
              	return tmp;
              }
              
              def code(x):
              	tmp = 0
              	if x <= 1.4:
              		tmp = 0.5 * x
              	else:
              		tmp = 1.0
              	return tmp
              
              function code(x)
              	tmp = 0.0
              	if (x <= 1.4)
              		tmp = Float64(0.5 * x);
              	else
              		tmp = 1.0;
              	end
              	return tmp
              end
              
              function tmp_2 = code(x)
              	tmp = 0.0;
              	if (x <= 1.4)
              		tmp = 0.5 * x;
              	else
              		tmp = 1.0;
              	end
              	tmp_2 = tmp;
              end
              
              code[x_] := If[LessEqual[x, 1.4], N[(0.5 * x), $MachinePrecision], 1.0]
              
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              \mathbf{if}\;x \leq 1.4:\\
              \;\;\;\;0.5 \cdot x\\
              
              \mathbf{else}:\\
              \;\;\;\;1\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if x < 1.3999999999999999

                1. Initial program 31.7%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{\frac{1}{2} \cdot x} \]
                4. Step-by-step derivation
                  1. lower-*.f6472.6

                    \[\leadsto \color{blue}{0.5 \cdot x} \]
                5. Applied rewrites72.6%

                  \[\leadsto \color{blue}{0.5 \cdot x} \]

                if 1.3999999999999999 < x

                1. Initial program 98.6%

                  \[\frac{1 - \cos x}{\sin x} \]
                2. Add Preprocessing
                3. Applied rewrites11.7%

                  \[\leadsto \color{blue}{{1}^{-0.5}} \]
                4. Step-by-step derivation
                  1. lift-pow.f64N/A

                    \[\leadsto \color{blue}{{1}^{\frac{-1}{2}}} \]
                  2. pow-base-111.7

                    \[\leadsto \color{blue}{1} \]
                5. Applied rewrites11.7%

                  \[\leadsto \color{blue}{1} \]
              3. Recombined 2 regimes into one program.
              4. Add Preprocessing

              Alternative 7: 7.0% accurate, 215.0× speedup?

              \[\begin{array}{l} \\ 1 \end{array} \]
              (FPCore (x) :precision binary64 1.0)
              double code(double x) {
              	return 1.0;
              }
              
              real(8) function code(x)
                  real(8), intent (in) :: x
                  code = 1.0d0
              end function
              
              public static double code(double x) {
              	return 1.0;
              }
              
              def code(x):
              	return 1.0
              
              function code(x)
              	return 1.0
              end
              
              function tmp = code(x)
              	tmp = 1.0;
              end
              
              code[x_] := 1.0
              
              \begin{array}{l}
              
              \\
              1
              \end{array}
              
              Derivation
              1. Initial program 47.6%

                \[\frac{1 - \cos x}{\sin x} \]
              2. Add Preprocessing
              3. Applied rewrites7.1%

                \[\leadsto \color{blue}{{1}^{-0.5}} \]
              4. Step-by-step derivation
                1. lift-pow.f64N/A

                  \[\leadsto \color{blue}{{1}^{\frac{-1}{2}}} \]
                2. pow-base-17.1

                  \[\leadsto \color{blue}{1} \]
              5. Applied rewrites7.1%

                \[\leadsto \color{blue}{1} \]
              6. Add Preprocessing

              Developer Target 1: 100.0% accurate, 1.9× speedup?

              \[\begin{array}{l} \\ \tan \left(\frac{x}{2}\right) \end{array} \]
              (FPCore (x) :precision binary64 (tan (/ x 2.0)))
              double code(double x) {
              	return tan((x / 2.0));
              }
              
              real(8) function code(x)
                  real(8), intent (in) :: x
                  code = tan((x / 2.0d0))
              end function
              
              public static double code(double x) {
              	return Math.tan((x / 2.0));
              }
              
              def code(x):
              	return math.tan((x / 2.0))
              
              function code(x)
              	return tan(Float64(x / 2.0))
              end
              
              function tmp = code(x)
              	tmp = tan((x / 2.0));
              end
              
              code[x_] := N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              \tan \left(\frac{x}{2}\right)
              \end{array}
              

              Reproduce

              ?
              herbie shell --seed 2024308 
              (FPCore (x)
                :name "tanhf (example 3.4)"
                :precision binary64
              
                :alt
                (! :herbie-platform default (tan (/ x 2)))
              
                (/ (- 1.0 (cos x)) (sin x)))