VandenBroeck and Keller, Equation (20)

Percentage Accurate: 6.8% → 99.0%
Time: 9.2s
Alternatives: 3
Speedup: 4.9×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\pi}{4} \cdot f\\ t_1 := e^{t\_0}\\ t_2 := e^{-t\_0}\\ -\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{t\_1 + t\_2}{t\_1 - t\_2}\right) \end{array} \end{array} \]
(FPCore (f)
 :precision binary64
 (let* ((t_0 (* (/ PI 4.0) f)) (t_1 (exp t_0)) (t_2 (exp (- t_0))))
   (- (* (/ 1.0 (/ PI 4.0)) (log (/ (+ t_1 t_2) (- t_1 t_2)))))))
double code(double f) {
	double t_0 = (((double) M_PI) / 4.0) * f;
	double t_1 = exp(t_0);
	double t_2 = exp(-t_0);
	return -((1.0 / (((double) M_PI) / 4.0)) * log(((t_1 + t_2) / (t_1 - t_2))));
}
public static double code(double f) {
	double t_0 = (Math.PI / 4.0) * f;
	double t_1 = Math.exp(t_0);
	double t_2 = Math.exp(-t_0);
	return -((1.0 / (Math.PI / 4.0)) * Math.log(((t_1 + t_2) / (t_1 - t_2))));
}
def code(f):
	t_0 = (math.pi / 4.0) * f
	t_1 = math.exp(t_0)
	t_2 = math.exp(-t_0)
	return -((1.0 / (math.pi / 4.0)) * math.log(((t_1 + t_2) / (t_1 - t_2))))
function code(f)
	t_0 = Float64(Float64(pi / 4.0) * f)
	t_1 = exp(t_0)
	t_2 = exp(Float64(-t_0))
	return Float64(-Float64(Float64(1.0 / Float64(pi / 4.0)) * log(Float64(Float64(t_1 + t_2) / Float64(t_1 - t_2)))))
end
function tmp = code(f)
	t_0 = (pi / 4.0) * f;
	t_1 = exp(t_0);
	t_2 = exp(-t_0);
	tmp = -((1.0 / (pi / 4.0)) * log(((t_1 + t_2) / (t_1 - t_2))));
end
code[f_] := Block[{t$95$0 = N[(N[(Pi / 4.0), $MachinePrecision] * f), $MachinePrecision]}, Block[{t$95$1 = N[Exp[t$95$0], $MachinePrecision]}, Block[{t$95$2 = N[Exp[(-t$95$0)], $MachinePrecision]}, (-N[(N[(1.0 / N[(Pi / 4.0), $MachinePrecision]), $MachinePrecision] * N[Log[N[(N[(t$95$1 + t$95$2), $MachinePrecision] / N[(t$95$1 - t$95$2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision])]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\pi}{4} \cdot f\\
t_1 := e^{t\_0}\\
t_2 := e^{-t\_0}\\
-\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{t\_1 + t\_2}{t\_1 - t\_2}\right)
\end{array}
\end{array}

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 3 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: 6.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\pi}{4} \cdot f\\ t_1 := e^{t\_0}\\ t_2 := e^{-t\_0}\\ -\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{t\_1 + t\_2}{t\_1 - t\_2}\right) \end{array} \end{array} \]
(FPCore (f)
 :precision binary64
 (let* ((t_0 (* (/ PI 4.0) f)) (t_1 (exp t_0)) (t_2 (exp (- t_0))))
   (- (* (/ 1.0 (/ PI 4.0)) (log (/ (+ t_1 t_2) (- t_1 t_2)))))))
double code(double f) {
	double t_0 = (((double) M_PI) / 4.0) * f;
	double t_1 = exp(t_0);
	double t_2 = exp(-t_0);
	return -((1.0 / (((double) M_PI) / 4.0)) * log(((t_1 + t_2) / (t_1 - t_2))));
}
public static double code(double f) {
	double t_0 = (Math.PI / 4.0) * f;
	double t_1 = Math.exp(t_0);
	double t_2 = Math.exp(-t_0);
	return -((1.0 / (Math.PI / 4.0)) * Math.log(((t_1 + t_2) / (t_1 - t_2))));
}
def code(f):
	t_0 = (math.pi / 4.0) * f
	t_1 = math.exp(t_0)
	t_2 = math.exp(-t_0)
	return -((1.0 / (math.pi / 4.0)) * math.log(((t_1 + t_2) / (t_1 - t_2))))
function code(f)
	t_0 = Float64(Float64(pi / 4.0) * f)
	t_1 = exp(t_0)
	t_2 = exp(Float64(-t_0))
	return Float64(-Float64(Float64(1.0 / Float64(pi / 4.0)) * log(Float64(Float64(t_1 + t_2) / Float64(t_1 - t_2)))))
end
function tmp = code(f)
	t_0 = (pi / 4.0) * f;
	t_1 = exp(t_0);
	t_2 = exp(-t_0);
	tmp = -((1.0 / (pi / 4.0)) * log(((t_1 + t_2) / (t_1 - t_2))));
end
code[f_] := Block[{t$95$0 = N[(N[(Pi / 4.0), $MachinePrecision] * f), $MachinePrecision]}, Block[{t$95$1 = N[Exp[t$95$0], $MachinePrecision]}, Block[{t$95$2 = N[Exp[(-t$95$0)], $MachinePrecision]}, (-N[(N[(1.0 / N[(Pi / 4.0), $MachinePrecision]), $MachinePrecision] * N[Log[N[(N[(t$95$1 + t$95$2), $MachinePrecision] / N[(t$95$1 - t$95$2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision])]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\pi}{4} \cdot f\\
t_1 := e^{t\_0}\\
t_2 := e^{-t\_0}\\
-\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{t\_1 + t\_2}{t\_1 - t\_2}\right)
\end{array}
\end{array}

Alternative 1: 99.0% accurate, 3.5× speedup?

\[\begin{array}{l} \\ \frac{\log \tanh \left(\left(\pi \cdot f\right) \cdot 0.25\right) \cdot 4}{\pi} \end{array} \]
(FPCore (f) :precision binary64 (/ (* (log (tanh (* (* PI f) 0.25))) 4.0) PI))
double code(double f) {
	return (log(tanh(((((double) M_PI) * f) * 0.25))) * 4.0) / ((double) M_PI);
}
public static double code(double f) {
	return (Math.log(Math.tanh(((Math.PI * f) * 0.25))) * 4.0) / Math.PI;
}
def code(f):
	return (math.log(math.tanh(((math.pi * f) * 0.25))) * 4.0) / math.pi
function code(f)
	return Float64(Float64(log(tanh(Float64(Float64(pi * f) * 0.25))) * 4.0) / pi)
end
function tmp = code(f)
	tmp = (log(tanh(((pi * f) * 0.25))) * 4.0) / pi;
end
code[f_] := N[(N[(N[Log[N[Tanh[N[(N[(Pi * f), $MachinePrecision] * 0.25), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * 4.0), $MachinePrecision] / Pi), $MachinePrecision]
\begin{array}{l}

\\
\frac{\log \tanh \left(\left(\pi \cdot f\right) \cdot 0.25\right) \cdot 4}{\pi}
\end{array}
Derivation
  1. Initial program 6.8%

    \[-\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{e^{\frac{\pi}{4} \cdot f} + e^{-\frac{\pi}{4} \cdot f}}{e^{\frac{\pi}{4} \cdot f} - e^{-\frac{\pi}{4} \cdot f}}\right) \]
  2. Applied rewrites97.0%

    \[\leadsto \color{blue}{\frac{\log \left(\frac{\sinh \left(f \cdot \left(0.25 \cdot \pi\right)\right)}{\cosh \left(\left(f \cdot \pi\right) \cdot -0.25\right)}\right) \cdot 4}{\pi}} \]
  3. Applied rewrites99.0%

    \[\leadsto \frac{\log \color{blue}{\tanh \left(\left(\pi \cdot f\right) \cdot 0.25\right)} \cdot 4}{\pi} \]
  4. Add Preprocessing

Alternative 2: 95.8% accurate, 3.8× speedup?

\[\begin{array}{l} \\ \frac{\left(\log f + \log \left(0.25 \cdot \pi\right)\right) \cdot 4}{\pi} \end{array} \]
(FPCore (f) :precision binary64 (/ (* (+ (log f) (log (* 0.25 PI))) 4.0) PI))
double code(double f) {
	return ((log(f) + log((0.25 * ((double) M_PI)))) * 4.0) / ((double) M_PI);
}
public static double code(double f) {
	return ((Math.log(f) + Math.log((0.25 * Math.PI))) * 4.0) / Math.PI;
}
def code(f):
	return ((math.log(f) + math.log((0.25 * math.pi))) * 4.0) / math.pi
function code(f)
	return Float64(Float64(Float64(log(f) + log(Float64(0.25 * pi))) * 4.0) / pi)
end
function tmp = code(f)
	tmp = ((log(f) + log((0.25 * pi))) * 4.0) / pi;
end
code[f_] := N[(N[(N[(N[Log[f], $MachinePrecision] + N[Log[N[(0.25 * Pi), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 4.0), $MachinePrecision] / Pi), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(\log f + \log \left(0.25 \cdot \pi\right)\right) \cdot 4}{\pi}
\end{array}
Derivation
  1. Initial program 6.8%

    \[-\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{e^{\frac{\pi}{4} \cdot f} + e^{-\frac{\pi}{4} \cdot f}}{e^{\frac{\pi}{4} \cdot f} - e^{-\frac{\pi}{4} \cdot f}}\right) \]
  2. Applied rewrites97.0%

    \[\leadsto \color{blue}{\frac{\log \left(\frac{\sinh \left(f \cdot \left(0.25 \cdot \pi\right)\right)}{\cosh \left(\left(f \cdot \pi\right) \cdot -0.25\right)}\right) \cdot 4}{\pi}} \]
  3. Applied rewrites99.0%

    \[\leadsto \frac{\log \color{blue}{\tanh \left(\left(\pi \cdot f\right) \cdot 0.25\right)} \cdot 4}{\pi} \]
  4. Taylor expanded in f around 0

    \[\leadsto \frac{\color{blue}{\left(\log f + \log \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right)\right)\right)} \cdot 4}{\pi} \]
  5. Step-by-step derivation
    1. lower-+.f64N/A

      \[\leadsto \frac{\left(\log f + \color{blue}{\log \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right)\right)}\right) \cdot 4}{\pi} \]
    2. lower-log.f64N/A

      \[\leadsto \frac{\left(\log f + \log \color{blue}{\left(\frac{1}{4} \cdot \mathsf{PI}\left(\right)\right)}\right) \cdot 4}{\pi} \]
    3. lower-log.f64N/A

      \[\leadsto \frac{\left(\log f + \log \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right)\right)\right) \cdot 4}{\pi} \]
    4. lower-*.f64N/A

      \[\leadsto \frac{\left(\log f + \log \left(\frac{1}{4} \cdot \mathsf{PI}\left(\right)\right)\right) \cdot 4}{\pi} \]
    5. lower-PI.f6495.8

      \[\leadsto \frac{\left(\log f + \log \left(0.25 \cdot \pi\right)\right) \cdot 4}{\pi} \]
  6. Applied rewrites95.8%

    \[\leadsto \frac{\color{blue}{\left(\log f + \log \left(0.25 \cdot \pi\right)\right)} \cdot 4}{\pi} \]
  7. Add Preprocessing

Alternative 3: 95.8% accurate, 4.9× speedup?

\[\begin{array}{l} \\ \frac{\log \left(0.25 \cdot \left(f \cdot \pi\right)\right) \cdot 4}{\pi} \end{array} \]
(FPCore (f) :precision binary64 (/ (* (log (* 0.25 (* f PI))) 4.0) PI))
double code(double f) {
	return (log((0.25 * (f * ((double) M_PI)))) * 4.0) / ((double) M_PI);
}
public static double code(double f) {
	return (Math.log((0.25 * (f * Math.PI))) * 4.0) / Math.PI;
}
def code(f):
	return (math.log((0.25 * (f * math.pi))) * 4.0) / math.pi
function code(f)
	return Float64(Float64(log(Float64(0.25 * Float64(f * pi))) * 4.0) / pi)
end
function tmp = code(f)
	tmp = (log((0.25 * (f * pi))) * 4.0) / pi;
end
code[f_] := N[(N[(N[Log[N[(0.25 * N[(f * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 4.0), $MachinePrecision] / Pi), $MachinePrecision]
\begin{array}{l}

\\
\frac{\log \left(0.25 \cdot \left(f \cdot \pi\right)\right) \cdot 4}{\pi}
\end{array}
Derivation
  1. Initial program 6.8%

    \[-\frac{1}{\frac{\pi}{4}} \cdot \log \left(\frac{e^{\frac{\pi}{4} \cdot f} + e^{-\frac{\pi}{4} \cdot f}}{e^{\frac{\pi}{4} \cdot f} - e^{-\frac{\pi}{4} \cdot f}}\right) \]
  2. Applied rewrites97.0%

    \[\leadsto \color{blue}{\frac{\log \left(\frac{\sinh \left(f \cdot \left(0.25 \cdot \pi\right)\right)}{\cosh \left(\left(f \cdot \pi\right) \cdot -0.25\right)}\right) \cdot 4}{\pi}} \]
  3. Applied rewrites99.0%

    \[\leadsto \frac{\log \color{blue}{\tanh \left(\left(\pi \cdot f\right) \cdot 0.25\right)} \cdot 4}{\pi} \]
  4. Taylor expanded in f around 0

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

      \[\leadsto \frac{\log \left(\frac{1}{4} \cdot \color{blue}{\left(f \cdot \mathsf{PI}\left(\right)\right)}\right) \cdot 4}{\pi} \]
    2. lower-*.f64N/A

      \[\leadsto \frac{\log \left(\frac{1}{4} \cdot \left(f \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right) \cdot 4}{\pi} \]
    3. lower-PI.f6495.8

      \[\leadsto \frac{\log \left(0.25 \cdot \left(f \cdot \pi\right)\right) \cdot 4}{\pi} \]
  6. Applied rewrites95.8%

    \[\leadsto \frac{\log \color{blue}{\left(0.25 \cdot \left(f \cdot \pi\right)\right)} \cdot 4}{\pi} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2025157 
(FPCore (f)
  :name "VandenBroeck and Keller, Equation (20)"
  :precision binary64
  (- (* (/ 1.0 (/ PI 4.0)) (log (/ (+ (exp (* (/ PI 4.0) f)) (exp (- (* (/ PI 4.0) f)))) (- (exp (* (/ PI 4.0) f)) (exp (- (* (/ PI 4.0) f)))))))))