VandenBroeck and Keller, Equation (6)

Percentage Accurate: 76.5% → 99.3%
Time: 16.9s
Alternatives: 7
Speedup: 3.7×

Specification

?
\[\begin{array}{l} \\ \pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right) \end{array} \]
(FPCore (F l)
 :precision binary64
 (- (* PI l) (* (/ 1.0 (* F F)) (tan (* PI l)))))
double code(double F, double l) {
	return (((double) M_PI) * l) - ((1.0 / (F * F)) * tan((((double) M_PI) * l)));
}
public static double code(double F, double l) {
	return (Math.PI * l) - ((1.0 / (F * F)) * Math.tan((Math.PI * l)));
}
def code(F, l):
	return (math.pi * l) - ((1.0 / (F * F)) * math.tan((math.pi * l)))
function code(F, l)
	return Float64(Float64(pi * l) - Float64(Float64(1.0 / Float64(F * F)) * tan(Float64(pi * l))))
end
function tmp = code(F, l)
	tmp = (pi * l) - ((1.0 / (F * F)) * tan((pi * l)));
end
code[F_, l_] := N[(N[(Pi * l), $MachinePrecision] - N[(N[(1.0 / N[(F * F), $MachinePrecision]), $MachinePrecision] * N[Tan[N[(Pi * l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right)
\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: 76.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right) \end{array} \]
(FPCore (F l)
 :precision binary64
 (- (* PI l) (* (/ 1.0 (* F F)) (tan (* PI l)))))
double code(double F, double l) {
	return (((double) M_PI) * l) - ((1.0 / (F * F)) * tan((((double) M_PI) * l)));
}
public static double code(double F, double l) {
	return (Math.PI * l) - ((1.0 / (F * F)) * Math.tan((Math.PI * l)));
}
def code(F, l):
	return (math.pi * l) - ((1.0 / (F * F)) * math.tan((math.pi * l)))
function code(F, l)
	return Float64(Float64(pi * l) - Float64(Float64(1.0 / Float64(F * F)) * tan(Float64(pi * l))))
end
function tmp = code(F, l)
	tmp = (pi * l) - ((1.0 / (F * F)) * tan((pi * l)));
end
code[F_, l_] := N[(N[(Pi * l), $MachinePrecision] - N[(N[(1.0 / N[(F * F), $MachinePrecision]), $MachinePrecision] * N[Tan[N[(Pi * l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right)
\end{array}

Alternative 1: 99.3% accurate, 0.9× speedup?

\[\begin{array}{l} l\_m = \left|\ell\right| \\ l\_s = \mathsf{copysign}\left(1, \ell\right) \\ l\_s \cdot \begin{array}{l} \mathbf{if}\;\pi \cdot l\_m \leq 2 \cdot 10^{+14}:\\ \;\;\;\;\pi \cdot l\_m - \frac{\frac{\tan \left(\pi \cdot l\_m\right)}{F}}{F}\\ \mathbf{else}:\\ \;\;\;\;\pi \cdot l\_m\\ \end{array} \end{array} \]
l\_m = (fabs.f64 l)
l\_s = (copysign.f64 #s(literal 1 binary64) l)
(FPCore (l_s F l_m)
 :precision binary64
 (*
  l_s
  (if (<= (* PI l_m) 2e+14)
    (- (* PI l_m) (/ (/ (tan (* PI l_m)) F) F))
    (* PI l_m))))
l\_m = fabs(l);
l\_s = copysign(1.0, l);
double code(double l_s, double F, double l_m) {
	double tmp;
	if ((((double) M_PI) * l_m) <= 2e+14) {
		tmp = (((double) M_PI) * l_m) - ((tan((((double) M_PI) * l_m)) / F) / F);
	} else {
		tmp = ((double) M_PI) * l_m;
	}
	return l_s * tmp;
}
l\_m = Math.abs(l);
l\_s = Math.copySign(1.0, l);
public static double code(double l_s, double F, double l_m) {
	double tmp;
	if ((Math.PI * l_m) <= 2e+14) {
		tmp = (Math.PI * l_m) - ((Math.tan((Math.PI * l_m)) / F) / F);
	} else {
		tmp = Math.PI * l_m;
	}
	return l_s * tmp;
}
l\_m = math.fabs(l)
l\_s = math.copysign(1.0, l)
def code(l_s, F, l_m):
	tmp = 0
	if (math.pi * l_m) <= 2e+14:
		tmp = (math.pi * l_m) - ((math.tan((math.pi * l_m)) / F) / F)
	else:
		tmp = math.pi * l_m
	return l_s * tmp
l\_m = abs(l)
l\_s = copysign(1.0, l)
function code(l_s, F, l_m)
	tmp = 0.0
	if (Float64(pi * l_m) <= 2e+14)
		tmp = Float64(Float64(pi * l_m) - Float64(Float64(tan(Float64(pi * l_m)) / F) / F));
	else
		tmp = Float64(pi * l_m);
	end
	return Float64(l_s * tmp)
end
l\_m = abs(l);
l\_s = sign(l) * abs(1.0);
function tmp_2 = code(l_s, F, l_m)
	tmp = 0.0;
	if ((pi * l_m) <= 2e+14)
		tmp = (pi * l_m) - ((tan((pi * l_m)) / F) / F);
	else
		tmp = pi * l_m;
	end
	tmp_2 = l_s * tmp;
end
l\_m = N[Abs[l], $MachinePrecision]
l\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[l]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[l$95$s_, F_, l$95$m_] := N[(l$95$s * If[LessEqual[N[(Pi * l$95$m), $MachinePrecision], 2e+14], N[(N[(Pi * l$95$m), $MachinePrecision] - N[(N[(N[Tan[N[(Pi * l$95$m), $MachinePrecision]], $MachinePrecision] / F), $MachinePrecision] / F), $MachinePrecision]), $MachinePrecision], N[(Pi * l$95$m), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
l\_m = \left|\ell\right|
\\
l\_s = \mathsf{copysign}\left(1, \ell\right)

\\
l\_s \cdot \begin{array}{l}
\mathbf{if}\;\pi \cdot l\_m \leq 2 \cdot 10^{+14}:\\
\;\;\;\;\pi \cdot l\_m - \frac{\frac{\tan \left(\pi \cdot l\_m\right)}{F}}{F}\\

\mathbf{else}:\\
\;\;\;\;\pi \cdot l\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (PI.f64) l) < 2e14

    1. Initial program 85.6%

      \[\pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \frac{1}{\color{blue}{F \cdot F}} \cdot \tan \left(\mathsf{PI}\left(\right) \cdot \ell\right) \]
      2. lift-PI.f64N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\color{blue}{\mathsf{PI}\left(\right)} \cdot \ell\right) \]
      3. lift-*.f64N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \ell\right)} \]
      4. lift-tan.f64N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \frac{1}{F \cdot F} \cdot \color{blue}{\tan \left(\mathsf{PI}\left(\right) \cdot \ell\right)} \]
      5. associate-*l/N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \color{blue}{\frac{1 \cdot \tan \left(\mathsf{PI}\left(\right) \cdot \ell\right)}{F \cdot F}} \]
      6. *-lft-identityN/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \frac{\color{blue}{\tan \left(\mathsf{PI}\left(\right) \cdot \ell\right)}}{F \cdot F} \]
      7. lift-*.f64N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \frac{\tan \left(\mathsf{PI}\left(\right) \cdot \ell\right)}{\color{blue}{F \cdot F}} \]
      8. associate-/r*N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \color{blue}{\frac{\frac{\tan \left(\mathsf{PI}\left(\right) \cdot \ell\right)}{F}}{F}} \]
      9. lower-/.f64N/A

        \[\leadsto \mathsf{PI}\left(\right) \cdot \ell - \color{blue}{\frac{\frac{\tan \left(\mathsf{PI}\left(\right) \cdot \ell\right)}{F}}{F}} \]
      10. lower-/.f6498.7

        \[\leadsto \pi \cdot \ell - \frac{\color{blue}{\frac{\tan \left(\pi \cdot \ell\right)}{F}}}{F} \]
    4. Applied egg-rr98.7%

      \[\leadsto \pi \cdot \ell - \color{blue}{\frac{\frac{\tan \left(\pi \cdot \ell\right)}{F}}{F}} \]

    if 2e14 < (*.f64 (PI.f64) l)

    1. Initial program 49.2%

      \[\pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right) \]
    2. Add Preprocessing
    3. Taylor expanded in l around inf

      \[\leadsto \color{blue}{\ell \cdot \mathsf{PI}\left(\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{\ell \cdot \mathsf{PI}\left(\right)} \]
      2. lower-PI.f6499.8

        \[\leadsto \ell \cdot \color{blue}{\pi} \]
    5. Simplified99.8%

      \[\leadsto \color{blue}{\ell \cdot \pi} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\pi \cdot \ell \leq 2 \cdot 10^{+14}:\\ \;\;\;\;\pi \cdot \ell - \frac{\frac{\tan \left(\pi \cdot \ell\right)}{F}}{F}\\ \mathbf{else}:\\ \;\;\;\;\pi \cdot \ell\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 83.3% accurate, 0.4× speedup?

\[\begin{array}{l} l\_m = \left|\ell\right| \\ l\_s = \mathsf{copysign}\left(1, \ell\right) \\ \begin{array}{l} t_0 := \pi \cdot l\_m + \tan \left(\pi \cdot l\_m\right) \cdot \frac{-1}{F \cdot F}\\ l\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -5 \cdot 10^{+171}:\\ \;\;\;\;\pi \cdot l\_m\\ \mathbf{elif}\;t\_0 \leq -2 \cdot 10^{-292}:\\ \;\;\;\;l\_m \cdot \left(-\frac{\pi}{F \cdot F}\right)\\ \mathbf{else}:\\ \;\;\;\;\pi \cdot l\_m\\ \end{array} \end{array} \end{array} \]
l\_m = (fabs.f64 l)
l\_s = (copysign.f64 #s(literal 1 binary64) l)
(FPCore (l_s F l_m)
 :precision binary64
 (let* ((t_0 (+ (* PI l_m) (* (tan (* PI l_m)) (/ -1.0 (* F F))))))
   (*
    l_s
    (if (<= t_0 -5e+171)
      (* PI l_m)
      (if (<= t_0 -2e-292) (* l_m (- (/ PI (* F F)))) (* PI l_m))))))
l\_m = fabs(l);
l\_s = copysign(1.0, l);
double code(double l_s, double F, double l_m) {
	double t_0 = (((double) M_PI) * l_m) + (tan((((double) M_PI) * l_m)) * (-1.0 / (F * F)));
	double tmp;
	if (t_0 <= -5e+171) {
		tmp = ((double) M_PI) * l_m;
	} else if (t_0 <= -2e-292) {
		tmp = l_m * -(((double) M_PI) / (F * F));
	} else {
		tmp = ((double) M_PI) * l_m;
	}
	return l_s * tmp;
}
l\_m = Math.abs(l);
l\_s = Math.copySign(1.0, l);
public static double code(double l_s, double F, double l_m) {
	double t_0 = (Math.PI * l_m) + (Math.tan((Math.PI * l_m)) * (-1.0 / (F * F)));
	double tmp;
	if (t_0 <= -5e+171) {
		tmp = Math.PI * l_m;
	} else if (t_0 <= -2e-292) {
		tmp = l_m * -(Math.PI / (F * F));
	} else {
		tmp = Math.PI * l_m;
	}
	return l_s * tmp;
}
l\_m = math.fabs(l)
l\_s = math.copysign(1.0, l)
def code(l_s, F, l_m):
	t_0 = (math.pi * l_m) + (math.tan((math.pi * l_m)) * (-1.0 / (F * F)))
	tmp = 0
	if t_0 <= -5e+171:
		tmp = math.pi * l_m
	elif t_0 <= -2e-292:
		tmp = l_m * -(math.pi / (F * F))
	else:
		tmp = math.pi * l_m
	return l_s * tmp
l\_m = abs(l)
l\_s = copysign(1.0, l)
function code(l_s, F, l_m)
	t_0 = Float64(Float64(pi * l_m) + Float64(tan(Float64(pi * l_m)) * Float64(-1.0 / Float64(F * F))))
	tmp = 0.0
	if (t_0 <= -5e+171)
		tmp = Float64(pi * l_m);
	elseif (t_0 <= -2e-292)
		tmp = Float64(l_m * Float64(-Float64(pi / Float64(F * F))));
	else
		tmp = Float64(pi * l_m);
	end
	return Float64(l_s * tmp)
end
l\_m = abs(l);
l\_s = sign(l) * abs(1.0);
function tmp_2 = code(l_s, F, l_m)
	t_0 = (pi * l_m) + (tan((pi * l_m)) * (-1.0 / (F * F)));
	tmp = 0.0;
	if (t_0 <= -5e+171)
		tmp = pi * l_m;
	elseif (t_0 <= -2e-292)
		tmp = l_m * -(pi / (F * F));
	else
		tmp = pi * l_m;
	end
	tmp_2 = l_s * tmp;
end
l\_m = N[Abs[l], $MachinePrecision]
l\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[l]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[l$95$s_, F_, l$95$m_] := Block[{t$95$0 = N[(N[(Pi * l$95$m), $MachinePrecision] + N[(N[Tan[N[(Pi * l$95$m), $MachinePrecision]], $MachinePrecision] * N[(-1.0 / N[(F * F), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(l$95$s * If[LessEqual[t$95$0, -5e+171], N[(Pi * l$95$m), $MachinePrecision], If[LessEqual[t$95$0, -2e-292], N[(l$95$m * (-N[(Pi / N[(F * F), $MachinePrecision]), $MachinePrecision])), $MachinePrecision], N[(Pi * l$95$m), $MachinePrecision]]]), $MachinePrecision]]
\begin{array}{l}
l\_m = \left|\ell\right|
\\
l\_s = \mathsf{copysign}\left(1, \ell\right)

\\
\begin{array}{l}
t_0 := \pi \cdot l\_m + \tan \left(\pi \cdot l\_m\right) \cdot \frac{-1}{F \cdot F}\\
l\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \leq -5 \cdot 10^{+171}:\\
\;\;\;\;\pi \cdot l\_m\\

\mathbf{elif}\;t\_0 \leq -2 \cdot 10^{-292}:\\
\;\;\;\;l\_m \cdot \left(-\frac{\pi}{F \cdot F}\right)\\

\mathbf{else}:\\
\;\;\;\;\pi \cdot l\_m\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 (PI.f64) l) (*.f64 (/.f64 #s(literal 1 binary64) (*.f64 F F)) (tan.f64 (*.f64 (PI.f64) l)))) < -5.0000000000000004e171 or -2.0000000000000001e-292 < (-.f64 (*.f64 (PI.f64) l) (*.f64 (/.f64 #s(literal 1 binary64) (*.f64 F F)) (tan.f64 (*.f64 (PI.f64) l))))

    1. Initial program 74.3%

      \[\pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right) \]
    2. Add Preprocessing
    3. Taylor expanded in l around inf

      \[\leadsto \color{blue}{\ell \cdot \mathsf{PI}\left(\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{\ell \cdot \mathsf{PI}\left(\right)} \]
      2. lower-PI.f6482.5

        \[\leadsto \ell \cdot \color{blue}{\pi} \]
    5. Simplified82.5%

      \[\leadsto \color{blue}{\ell \cdot \pi} \]

    if -5.0000000000000004e171 < (-.f64 (*.f64 (PI.f64) l) (*.f64 (/.f64 #s(literal 1 binary64) (*.f64 F F)) (tan.f64 (*.f64 (PI.f64) l)))) < -2.0000000000000001e-292

    1. Initial program 92.6%

      \[\pi \cdot \ell - \frac{1}{F \cdot F} \cdot \tan \left(\pi \cdot \ell\right) \]
    2. Add Preprocessing
    3. Taylor expanded in l around 0

      \[\leadsto \color{blue}{\ell \cdot \left(\mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{\ell \cdot \left(\mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right)} \]
      2. lower--.f64N/A

        \[\leadsto \ell \cdot \color{blue}{\left(\mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right)} \]
      3. lower-PI.f64N/A

        \[\leadsto \ell \cdot \left(\color{blue}{\mathsf{PI}\left(\right)} - \frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right) \]
      4. lower-/.f64N/A

        \[\leadsto \ell \cdot \left(\mathsf{PI}\left(\right) - \color{blue}{\frac{\mathsf{PI}\left(\right)}{{F}^{2}}}\right) \]
      5. lower-PI.f64N/A

        \[\leadsto \ell \cdot \left(\mathsf{PI}\left(\right) - \frac{\color{blue}{\mathsf{PI}\left(\right)}}{{F}^{2}}\right) \]
      6. unpow2N/A

        \[\leadsto \ell \cdot \left(\mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{\color{blue}{F \cdot F}}\right) \]
      7. lower-*.f6491.5

        \[\leadsto \ell \cdot \left(\pi - \frac{\pi}{\color{blue}{F \cdot F}}\right) \]
    5. Simplified91.5%

      \[\leadsto \color{blue}{\ell \cdot \left(\pi - \frac{\pi}{F \cdot F}\right)} \]
    6. Taylor expanded in F around 0

      \[\leadsto \ell \cdot \color{blue}{\left(-1 \cdot \frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right)} \]
    7. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \ell \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right)\right)} \]
      2. lower-neg.f64N/A

        \[\leadsto \ell \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{\mathsf{PI}\left(\right)}{{F}^{2}}\right)\right)} \]
      3. lower-/.f64N/A

        \[\leadsto \ell \cdot \left(\mathsf{neg}\left(\color{blue}{\frac{\mathsf{PI}\left(\right)}{{F}^{2}}}\right)\right) \]
      4. lower-PI.f64N/A

        \[\leadsto \ell \cdot \left(\mathsf{neg}\left(\frac{\color{blue}{\mathsf{PI}\left(\right)}}{{F}^{2}}\right)\right) \]
      5. unpow2N/A

        \[\leadsto \ell \cdot \left(\mathsf{neg}\left(\frac{\mathsf{PI}\left(\right)}{\color{blue}{F \cdot F}}\right)\right) \]
      6. lower-*.f6489.0

        \[\leadsto \ell \cdot \left(-\frac{\pi}{\color{blue}{F \cdot F}}\right) \]
    8. Simplified89.0%

      \[\leadsto \ell \cdot \color{blue}{\left(-\frac{\pi}{F \cdot F}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification83.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\pi \cdot \ell + \tan \left(\pi \cdot \ell\right) \cdot \frac{-1}{F \cdot F} \leq -5 \cdot 10^{+171}:\\ \;\;\;\;\pi \cdot \ell\\ \mathbf{elif}\;\pi \cdot \ell + \tan \left(\pi \cdot \ell\right) \cdot \frac{-1}{F \cdot F} \leq -2 \cdot 10^{-292}:\\ \;\;\;\;\ell \cdot \left(-\frac{\pi}{F \cdot F}\right)\\ \mathbf{else}:\\ \;\;\;\;\pi \cdot \ell\\ \end{array} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024218 
(FPCore (F l)
  :name "VandenBroeck and Keller, Equation (6)"
  :precision binary64
  (- (* PI l) (* (/ 1.0 (* F F)) (tan (* PI l)))))