?

Average Error: 30.3 → 0.9
Time: 18.1s
Precision: binary64
Cost: 46728

?

\[\frac{1 - \cos x}{\sin x} \]
\[\begin{array}{l} t_0 := \frac{1 - \cos x}{\sin x}\\ \mathbf{if}\;t_0 \leq -0.05:\\ \;\;\;\;\frac{4}{\frac{4}{t_0}}\\ \mathbf{elif}\;t_0 \leq 10^{-6}:\\ \;\;\;\;0.041666666666666664 \cdot {x}^{3} + \left(\left(0.5 \cdot x + 0.004166666666666667 \cdot {x}^{5}\right) + 0.00042162698412698415 \cdot {x}^{7}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (sin x)))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (/ (- 1.0 (cos x)) (sin x))))
   (if (<= t_0 -0.05)
     (/ 4.0 (/ 4.0 t_0))
     (if (<= t_0 1e-6)
       (+
        (* 0.041666666666666664 (pow x 3.0))
        (+
         (+ (* 0.5 x) (* 0.004166666666666667 (pow x 5.0)))
         (* 0.00042162698412698415 (pow x 7.0))))
       t_0))))
double code(double x) {
	return (1.0 - cos(x)) / sin(x);
}
double code(double x) {
	double t_0 = (1.0 - cos(x)) / sin(x);
	double tmp;
	if (t_0 <= -0.05) {
		tmp = 4.0 / (4.0 / t_0);
	} else if (t_0 <= 1e-6) {
		tmp = (0.041666666666666664 * pow(x, 3.0)) + (((0.5 * x) + (0.004166666666666667 * pow(x, 5.0))) + (0.00042162698412698415 * pow(x, 7.0)));
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / sin(x)
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (1.0d0 - cos(x)) / sin(x)
    if (t_0 <= (-0.05d0)) then
        tmp = 4.0d0 / (4.0d0 / t_0)
    else if (t_0 <= 1d-6) then
        tmp = (0.041666666666666664d0 * (x ** 3.0d0)) + (((0.5d0 * x) + (0.004166666666666667d0 * (x ** 5.0d0))) + (0.00042162698412698415d0 * (x ** 7.0d0)))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / Math.sin(x);
}
public static double code(double x) {
	double t_0 = (1.0 - Math.cos(x)) / Math.sin(x);
	double tmp;
	if (t_0 <= -0.05) {
		tmp = 4.0 / (4.0 / t_0);
	} else if (t_0 <= 1e-6) {
		tmp = (0.041666666666666664 * Math.pow(x, 3.0)) + (((0.5 * x) + (0.004166666666666667 * Math.pow(x, 5.0))) + (0.00042162698412698415 * Math.pow(x, 7.0)));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x):
	return (1.0 - math.cos(x)) / math.sin(x)
def code(x):
	t_0 = (1.0 - math.cos(x)) / math.sin(x)
	tmp = 0
	if t_0 <= -0.05:
		tmp = 4.0 / (4.0 / t_0)
	elif t_0 <= 1e-6:
		tmp = (0.041666666666666664 * math.pow(x, 3.0)) + (((0.5 * x) + (0.004166666666666667 * math.pow(x, 5.0))) + (0.00042162698412698415 * math.pow(x, 7.0)))
	else:
		tmp = t_0
	return tmp
function code(x)
	return Float64(Float64(1.0 - cos(x)) / sin(x))
end
function code(x)
	t_0 = Float64(Float64(1.0 - cos(x)) / sin(x))
	tmp = 0.0
	if (t_0 <= -0.05)
		tmp = Float64(4.0 / Float64(4.0 / t_0));
	elseif (t_0 <= 1e-6)
		tmp = Float64(Float64(0.041666666666666664 * (x ^ 3.0)) + Float64(Float64(Float64(0.5 * x) + Float64(0.004166666666666667 * (x ^ 5.0))) + Float64(0.00042162698412698415 * (x ^ 7.0))));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / sin(x);
end
function tmp_2 = code(x)
	t_0 = (1.0 - cos(x)) / sin(x);
	tmp = 0.0;
	if (t_0 <= -0.05)
		tmp = 4.0 / (4.0 / t_0);
	elseif (t_0 <= 1e-6)
		tmp = (0.041666666666666664 * (x ^ 3.0)) + (((0.5 * x) + (0.004166666666666667 * (x ^ 5.0))) + (0.00042162698412698415 * (x ^ 7.0)));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.05], N[(4.0 / N[(4.0 / t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 1e-6], N[(N[(0.041666666666666664 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(0.5 * x), $MachinePrecision] + N[(0.004166666666666667 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.00042162698412698415 * N[Power[x, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
t_0 := \frac{1 - \cos x}{\sin x}\\
\mathbf{if}\;t_0 \leq -0.05:\\
\;\;\;\;\frac{4}{\frac{4}{t_0}}\\

\mathbf{elif}\;t_0 \leq 10^{-6}:\\
\;\;\;\;0.041666666666666664 \cdot {x}^{3} + \left(\left(0.5 \cdot x + 0.004166666666666667 \cdot {x}^{5}\right) + 0.00042162698412698415 \cdot {x}^{7}\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original30.3
Target0.0
Herbie0.9
\[\tan \left(\frac{x}{2}\right) \]

Derivation?

  1. Split input into 3 regimes
  2. if (/.f64 (-.f64 1 (cos.f64 x)) (sin.f64 x)) < -0.050000000000000003

    1. Initial program 0.6

      \[\frac{1 - \cos x}{\sin x} \]
    2. Applied egg-rr0.7

      \[\leadsto \color{blue}{\frac{1}{\sin x} \cdot \left(1 - \cos x\right)} \]
    3. Applied egg-rr0.7

      \[\leadsto \color{blue}{\frac{4}{\frac{4}{\frac{1 - \cos x}{\sin x}}}} \]

    if -0.050000000000000003 < (/.f64 (-.f64 1 (cos.f64 x)) (sin.f64 x)) < 9.99999999999999955e-7

    1. Initial program 59.3

      \[\frac{1 - \cos x}{\sin x} \]
    2. Applied egg-rr59.3

      \[\leadsto \color{blue}{\frac{1}{\sin x} \cdot \frac{1}{\frac{1}{1 - \cos x}}} \]
    3. Taylor expanded in x around 0 0.9

      \[\leadsto \color{blue}{0.5 \cdot x + \left(0.004166666666666667 \cdot {x}^{5} + \left(0.00042162698412698415 \cdot {x}^{7} + 0.041666666666666664 \cdot {x}^{3}\right)\right)} \]
    4. Simplified0.9

      \[\leadsto \color{blue}{0.041666666666666664 \cdot {x}^{3} + \left(\left(0.5 \cdot x + 0.004166666666666667 \cdot {x}^{5}\right) + 0.00042162698412698415 \cdot {x}^{7}\right)} \]
      Proof

      [Start]0.9

      \[ 0.5 \cdot x + \left(0.004166666666666667 \cdot {x}^{5} + \left(0.00042162698412698415 \cdot {x}^{7} + 0.041666666666666664 \cdot {x}^{3}\right)\right) \]

      rational.json-simplify-41 [=>]0.9

      \[ 0.5 \cdot x + \color{blue}{\left(0.00042162698412698415 \cdot {x}^{7} + \left(0.041666666666666664 \cdot {x}^{3} + 0.004166666666666667 \cdot {x}^{5}\right)\right)} \]

      rational.json-simplify-1 [<=]0.9

      \[ 0.5 \cdot x + \left(0.00042162698412698415 \cdot {x}^{7} + \color{blue}{\left(0.004166666666666667 \cdot {x}^{5} + 0.041666666666666664 \cdot {x}^{3}\right)}\right) \]

      rational.json-simplify-41 [=>]0.9

      \[ \color{blue}{0.00042162698412698415 \cdot {x}^{7} + \left(\left(0.004166666666666667 \cdot {x}^{5} + 0.041666666666666664 \cdot {x}^{3}\right) + 0.5 \cdot x\right)} \]

      rational.json-simplify-1 [<=]0.9

      \[ 0.00042162698412698415 \cdot {x}^{7} + \color{blue}{\left(0.5 \cdot x + \left(0.004166666666666667 \cdot {x}^{5} + 0.041666666666666664 \cdot {x}^{3}\right)\right)} \]

      rational.json-simplify-1 [=>]0.9

      \[ 0.00042162698412698415 \cdot {x}^{7} + \left(0.5 \cdot x + \color{blue}{\left(0.041666666666666664 \cdot {x}^{3} + 0.004166666666666667 \cdot {x}^{5}\right)}\right) \]

      rational.json-simplify-41 [=>]0.9

      \[ 0.00042162698412698415 \cdot {x}^{7} + \color{blue}{\left(0.041666666666666664 \cdot {x}^{3} + \left(0.004166666666666667 \cdot {x}^{5} + 0.5 \cdot x\right)\right)} \]

      rational.json-simplify-41 [=>]0.9

      \[ \color{blue}{0.041666666666666664 \cdot {x}^{3} + \left(\left(0.004166666666666667 \cdot {x}^{5} + 0.5 \cdot x\right) + 0.00042162698412698415 \cdot {x}^{7}\right)} \]

      rational.json-simplify-1 [=>]0.9

      \[ 0.041666666666666664 \cdot {x}^{3} + \left(\color{blue}{\left(0.5 \cdot x + 0.004166666666666667 \cdot {x}^{5}\right)} + 0.00042162698412698415 \cdot {x}^{7}\right) \]

    if 9.99999999999999955e-7 < (/.f64 (-.f64 1 (cos.f64 x)) (sin.f64 x))

    1. Initial program 1.1

      \[\frac{1 - \cos x}{\sin x} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \leq -0.05:\\ \;\;\;\;\frac{4}{\frac{4}{\frac{1 - \cos x}{\sin x}}}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \leq 10^{-6}:\\ \;\;\;\;0.041666666666666664 \cdot {x}^{3} + \left(\left(0.5 \cdot x + 0.004166666666666667 \cdot {x}^{5}\right) + 0.00042162698412698415 \cdot {x}^{7}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - \cos x}{\sin x}\\ \end{array} \]

Alternatives

Alternative 1
Error0.6
Cost39496
\[\begin{array}{l} t_0 := \frac{1 - \cos x}{\sin x}\\ \mathbf{if}\;t_0 \leq -0.01:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 10^{-6}:\\ \;\;\;\;0.5 \cdot x + 0.041666666666666664 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error0.6
Cost39496
\[\begin{array}{l} t_0 := \frac{1 - \cos x}{\sin x}\\ \mathbf{if}\;t_0 \leq -0.01:\\ \;\;\;\;\frac{4}{\frac{4}{t_0}}\\ \mathbf{elif}\;t_0 \leq 10^{-6}:\\ \;\;\;\;0.5 \cdot x + 0.041666666666666664 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error0.5
Cost13512
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.0045:\\ \;\;\;\;\frac{t_0}{\sin x}\\ \mathbf{elif}\;x \leq 0.0041:\\ \;\;\;\;0.5 \cdot x + 0.041666666666666664 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} \cdot t_0\\ \end{array} \]
Alternative 4
Error30.9
Cost576
\[\frac{1}{\frac{2}{x} - 0.16666666666666666 \cdot x} \]
Alternative 5
Error31.1
Cost192
\[0.5 \cdot x \]

Error

Reproduce?

herbie shell --seed 2023064 
(FPCore (x)
  :name "tanhf (example 3.4)"
  :precision binary64

  :herbie-target
  (tan (/ x 2.0))

  (/ (- 1.0 (cos x)) (sin x)))