?

Average Error: 30.3 → 0.8
Time: 16.0s
Precision: binary64
Cost: 46276

?

\[\frac{1 - \cos x}{\sin x} \]
\[\begin{array}{l} t_0 := 1 - \cos x\\ t_1 := \frac{t_0}{\sin x}\\ \mathbf{if}\;t_1 \leq -0.02:\\ \;\;\;\;\frac{\cos x}{\sin x \cdot -8} - \frac{\left(\cos x \cdot 0.75 + \cos x\right) - 2}{2 \cdot \sin x}\\ \mathbf{elif}\;t_1 \leq 10^{-7}:\\ \;\;\;\;0.5 \cdot x + \left(0.041666666666666664 \cdot {x}^{3} + 0.004166666666666667 \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} \cdot 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))) (t_1 (/ t_0 (sin x))))
   (if (<= t_1 -0.02)
     (-
      (/ (cos x) (* (sin x) -8.0))
      (/ (- (+ (* (cos x) 0.75) (cos x)) 2.0) (* 2.0 (sin x))))
     (if (<= t_1 1e-7)
       (+
        (* 0.5 x)
        (+
         (* 0.041666666666666664 (pow x 3.0))
         (* 0.004166666666666667 (pow x 5.0))))
       (* (/ 1.0 (sin x)) t_0)))))
double code(double x) {
	return (1.0 - cos(x)) / sin(x);
}
double code(double x) {
	double t_0 = 1.0 - cos(x);
	double t_1 = t_0 / sin(x);
	double tmp;
	if (t_1 <= -0.02) {
		tmp = (cos(x) / (sin(x) * -8.0)) - ((((cos(x) * 0.75) + cos(x)) - 2.0) / (2.0 * sin(x)));
	} else if (t_1 <= 1e-7) {
		tmp = (0.5 * x) + ((0.041666666666666664 * pow(x, 3.0)) + (0.004166666666666667 * pow(x, 5.0)));
	} else {
		tmp = (1.0 / sin(x)) * 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) :: t_1
    real(8) :: tmp
    t_0 = 1.0d0 - cos(x)
    t_1 = t_0 / sin(x)
    if (t_1 <= (-0.02d0)) then
        tmp = (cos(x) / (sin(x) * (-8.0d0))) - ((((cos(x) * 0.75d0) + cos(x)) - 2.0d0) / (2.0d0 * sin(x)))
    else if (t_1 <= 1d-7) then
        tmp = (0.5d0 * x) + ((0.041666666666666664d0 * (x ** 3.0d0)) + (0.004166666666666667d0 * (x ** 5.0d0)))
    else
        tmp = (1.0d0 / sin(x)) * 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);
	double t_1 = t_0 / Math.sin(x);
	double tmp;
	if (t_1 <= -0.02) {
		tmp = (Math.cos(x) / (Math.sin(x) * -8.0)) - ((((Math.cos(x) * 0.75) + Math.cos(x)) - 2.0) / (2.0 * Math.sin(x)));
	} else if (t_1 <= 1e-7) {
		tmp = (0.5 * x) + ((0.041666666666666664 * Math.pow(x, 3.0)) + (0.004166666666666667 * Math.pow(x, 5.0)));
	} else {
		tmp = (1.0 / Math.sin(x)) * 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)
	t_1 = t_0 / math.sin(x)
	tmp = 0
	if t_1 <= -0.02:
		tmp = (math.cos(x) / (math.sin(x) * -8.0)) - ((((math.cos(x) * 0.75) + math.cos(x)) - 2.0) / (2.0 * math.sin(x)))
	elif t_1 <= 1e-7:
		tmp = (0.5 * x) + ((0.041666666666666664 * math.pow(x, 3.0)) + (0.004166666666666667 * math.pow(x, 5.0)))
	else:
		tmp = (1.0 / math.sin(x)) * t_0
	return tmp
function code(x)
	return Float64(Float64(1.0 - cos(x)) / sin(x))
end
function code(x)
	t_0 = Float64(1.0 - cos(x))
	t_1 = Float64(t_0 / sin(x))
	tmp = 0.0
	if (t_1 <= -0.02)
		tmp = Float64(Float64(cos(x) / Float64(sin(x) * -8.0)) - Float64(Float64(Float64(Float64(cos(x) * 0.75) + cos(x)) - 2.0) / Float64(2.0 * sin(x))));
	elseif (t_1 <= 1e-7)
		tmp = Float64(Float64(0.5 * x) + Float64(Float64(0.041666666666666664 * (x ^ 3.0)) + Float64(0.004166666666666667 * (x ^ 5.0))));
	else
		tmp = Float64(Float64(1.0 / sin(x)) * 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);
	t_1 = t_0 / sin(x);
	tmp = 0.0;
	if (t_1 <= -0.02)
		tmp = (cos(x) / (sin(x) * -8.0)) - ((((cos(x) * 0.75) + cos(x)) - 2.0) / (2.0 * sin(x)));
	elseif (t_1 <= 1e-7)
		tmp = (0.5 * x) + ((0.041666666666666664 * (x ^ 3.0)) + (0.004166666666666667 * (x ^ 5.0)));
	else
		tmp = (1.0 / sin(x)) * 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[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 / N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.02], N[(N[(N[Cos[x], $MachinePrecision] / N[(N[Sin[x], $MachinePrecision] * -8.0), $MachinePrecision]), $MachinePrecision] - N[(N[(N[(N[(N[Cos[x], $MachinePrecision] * 0.75), $MachinePrecision] + N[Cos[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision] / N[(2.0 * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 1e-7], N[(N[(0.5 * x), $MachinePrecision] + N[(N[(0.041666666666666664 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(0.004166666666666667 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / N[Sin[x], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]]]]]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
t_0 := 1 - \cos x\\
t_1 := \frac{t_0}{\sin x}\\
\mathbf{if}\;t_1 \leq -0.02:\\
\;\;\;\;\frac{\cos x}{\sin x \cdot -8} - \frac{\left(\cos x \cdot 0.75 + \cos x\right) - 2}{2 \cdot \sin x}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{1}{\sin x} \cdot 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.8
\[\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.0200000000000000004

    1. Initial program 0.8

      \[\frac{1 - \cos x}{\sin x} \]
    2. Applied egg-rr1.0

      \[\leadsto \frac{\color{blue}{\left(1 - \frac{\cos x}{2}\right) + \left(-\frac{\cos x}{2}\right)}}{\sin x} \]
    3. Applied egg-rr1.1

      \[\leadsto \frac{\color{blue}{\frac{\cos x}{-4} - \left(\left(\cos x \cdot 0.5 + -1\right) - \frac{\cos x}{-4}\right)}}{\sin x} \]
    4. Simplified0.9

      \[\leadsto \frac{\color{blue}{\frac{\cos x}{-4} - \left(0.5 \cdot \cos x - \left(1 + \frac{\cos x}{-4}\right)\right)}}{\sin x} \]
      Proof

      [Start]1.1

      \[ \frac{\frac{\cos x}{-4} - \left(\left(\cos x \cdot 0.5 + -1\right) - \frac{\cos x}{-4}\right)}{\sin x} \]

      rational_best-simplify-19 [=>]1.1

      \[ \frac{\frac{\cos x}{-4} - \left(\color{blue}{\left(\cos x \cdot 0.5 - 1\right)} - \frac{\cos x}{-4}\right)}{\sin x} \]

      rational_best-simplify-52 [=>]0.9

      \[ \frac{\frac{\cos x}{-4} - \color{blue}{\left(\cos x \cdot 0.5 - \left(\frac{\cos x}{-4} + 1\right)\right)}}{\sin x} \]

      rational_best-simplify-1 [=>]0.9

      \[ \frac{\frac{\cos x}{-4} - \left(\color{blue}{0.5 \cdot \cos x} - \left(\frac{\cos x}{-4} + 1\right)\right)}{\sin x} \]

      rational_best-simplify-3 [<=]0.9

      \[ \frac{\frac{\cos x}{-4} - \left(0.5 \cdot \cos x - \color{blue}{\left(1 + \frac{\cos x}{-4}\right)}\right)}{\sin x} \]
    5. Applied egg-rr0.9

      \[\leadsto \color{blue}{\frac{\cos x}{\sin x \cdot -8} - \left(\frac{\cos x + -1}{\sin x \cdot 2} + \frac{\cos x \cdot 0.75 + -1}{\sin x \cdot 2}\right)} \]
    6. Simplified0.9

      \[\leadsto \color{blue}{\frac{\cos x}{\sin x \cdot -8} - \frac{\left(\cos x \cdot 0.75 + \cos x\right) - 2}{2 \cdot \sin x}} \]
      Proof

      [Start]0.9

      \[ \frac{\cos x}{\sin x \cdot -8} - \left(\frac{\cos x + -1}{\sin x \cdot 2} + \frac{\cos x \cdot 0.75 + -1}{\sin x \cdot 2}\right) \]

      rational_best-simplify-64 [=>]0.9

      \[ \frac{\cos x}{\sin x \cdot -8} - \color{blue}{\frac{\left(\cos x + -1\right) + \left(\cos x \cdot 0.75 + -1\right)}{\sin x \cdot 2}} \]

      rational_best-simplify-3 [<=]0.9

      \[ \frac{\cos x}{\sin x \cdot -8} - \frac{\color{blue}{\left(\cos x \cdot 0.75 + -1\right) + \left(\cos x + -1\right)}}{\sin x \cdot 2} \]

      rational_best-simplify-38 [=>]0.9

      \[ \frac{\cos x}{\sin x \cdot -8} - \frac{\color{blue}{\left(\cos x \cdot 0.75 + \cos x\right) - 2}}{\sin x \cdot 2} \]

      rational_best-simplify-1 [=>]0.9

      \[ \frac{\cos x}{\sin x \cdot -8} - \frac{\left(\cos x \cdot 0.75 + \cos x\right) - 2}{\color{blue}{2 \cdot \sin x}} \]

    if -0.0200000000000000004 < (/.f64 (-.f64 1 (cos.f64 x)) (sin.f64 x)) < 9.9999999999999995e-8

    1. Initial program 59.9

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

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

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

      [Start]0.4

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

      rational_best-simplify-3 [=>]0.4

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

    if 9.9999999999999995e-8 < (/.f64 (-.f64 1 (cos.f64 x)) (sin.f64 x))

    1. Initial program 1.4

      \[\frac{1 - \cos x}{\sin x} \]
    2. Applied egg-rr1.5

      \[\leadsto \color{blue}{\frac{1}{\sin x} \cdot \left(1 - \cos x\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.8

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

Alternatives

Alternative 1
Error0.8
Cost40008
\[\begin{array}{l} t_0 := 1 - \cos x\\ t_1 := \frac{t_0}{\sin x}\\ \mathbf{if}\;t_1 \leq -0.02:\\ \;\;\;\;\left(1 - \frac{\cos x + -1}{\sin x}\right) + -1\\ \mathbf{elif}\;t_1 \leq 10^{-7}:\\ \;\;\;\;0.5 \cdot x + \left(0.041666666666666664 \cdot {x}^{3} + 0.004166666666666667 \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} \cdot t_0\\ \end{array} \]
Alternative 2
Error0.8
Cost39624
\[\begin{array}{l} t_0 := 1 - \cos x\\ t_1 := \frac{t_0}{\sin x}\\ \mathbf{if}\;t_1 \leq -0.02:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_1 \leq 10^{-7}:\\ \;\;\;\;0.5 \cdot x + 0.041666666666666664 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} \cdot t_0\\ \end{array} \]
Alternative 3
Error0.8
Cost39624
\[\begin{array}{l} t_0 := 1 - \cos x\\ t_1 := \frac{t_0}{\sin x}\\ \mathbf{if}\;t_1 \leq -0.02:\\ \;\;\;\;\left(1 - \frac{\cos x + -1}{\sin x}\right) + -1\\ \mathbf{elif}\;t_1 \leq 10^{-7}:\\ \;\;\;\;0.5 \cdot x + 0.041666666666666664 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} \cdot t_0\\ \end{array} \]
Alternative 4
Error0.5
Cost13384
\[\begin{array}{l} t_0 := \frac{1 - \cos x}{\sin x}\\ \mathbf{if}\;x \leq -0.004:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.0045:\\ \;\;\;\;0.5 \cdot x + 0.041666666666666664 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error25.9
Cost6592
\[0.5 \cdot \sin x \]
Alternative 6
Error31.1
Cost704
\[0.5 \cdot \frac{1}{\frac{1}{x} + x \cdot 0.16666666666666666} \]
Alternative 7
Error31.2
Cost192
\[0.5 \cdot x \]

Error

Reproduce?

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

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

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