Octave 3.8, jcobi/4, as called

Percentage Accurate: 27.9% → 100.0%
Time: 5.1s
Alternatives: 4
Speedup: 25.0×

Specification

?
\[i > 0\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\\ \frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{t\_0}}{t\_0 - 1} \end{array} \end{array} \]
(FPCore (i)
 :precision binary64
 (let* ((t_0 (* (* 2.0 i) (* 2.0 i))))
   (/ (/ (* (* i i) (* i i)) t_0) (- t_0 1.0))))
double code(double i) {
	double t_0 = (2.0 * i) * (2.0 * i);
	return (((i * i) * (i * i)) / t_0) / (t_0 - 1.0);
}
real(8) function code(i)
    real(8), intent (in) :: i
    real(8) :: t_0
    t_0 = (2.0d0 * i) * (2.0d0 * i)
    code = (((i * i) * (i * i)) / t_0) / (t_0 - 1.0d0)
end function
public static double code(double i) {
	double t_0 = (2.0 * i) * (2.0 * i);
	return (((i * i) * (i * i)) / t_0) / (t_0 - 1.0);
}
def code(i):
	t_0 = (2.0 * i) * (2.0 * i)
	return (((i * i) * (i * i)) / t_0) / (t_0 - 1.0)
function code(i)
	t_0 = Float64(Float64(2.0 * i) * Float64(2.0 * i))
	return Float64(Float64(Float64(Float64(i * i) * Float64(i * i)) / t_0) / Float64(t_0 - 1.0))
end
function tmp = code(i)
	t_0 = (2.0 * i) * (2.0 * i);
	tmp = (((i * i) * (i * i)) / t_0) / (t_0 - 1.0);
end
code[i_] := Block[{t$95$0 = N[(N[(2.0 * i), $MachinePrecision] * N[(2.0 * i), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[(i * i), $MachinePrecision] * N[(i * i), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision] / N[(t$95$0 - 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\\
\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{t\_0}}{t\_0 - 1}
\end{array}
\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 4 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: 27.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\\ \frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{t\_0}}{t\_0 - 1} \end{array} \end{array} \]
(FPCore (i)
 :precision binary64
 (let* ((t_0 (* (* 2.0 i) (* 2.0 i))))
   (/ (/ (* (* i i) (* i i)) t_0) (- t_0 1.0))))
double code(double i) {
	double t_0 = (2.0 * i) * (2.0 * i);
	return (((i * i) * (i * i)) / t_0) / (t_0 - 1.0);
}
real(8) function code(i)
    real(8), intent (in) :: i
    real(8) :: t_0
    t_0 = (2.0d0 * i) * (2.0d0 * i)
    code = (((i * i) * (i * i)) / t_0) / (t_0 - 1.0d0)
end function
public static double code(double i) {
	double t_0 = (2.0 * i) * (2.0 * i);
	return (((i * i) * (i * i)) / t_0) / (t_0 - 1.0);
}
def code(i):
	t_0 = (2.0 * i) * (2.0 * i)
	return (((i * i) * (i * i)) / t_0) / (t_0 - 1.0)
function code(i)
	t_0 = Float64(Float64(2.0 * i) * Float64(2.0 * i))
	return Float64(Float64(Float64(Float64(i * i) * Float64(i * i)) / t_0) / Float64(t_0 - 1.0))
end
function tmp = code(i)
	t_0 = (2.0 * i) * (2.0 * i);
	tmp = (((i * i) * (i * i)) / t_0) / (t_0 - 1.0);
end
code[i_] := Block[{t$95$0 = N[(N[(2.0 * i), $MachinePrecision] * N[(2.0 * i), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[(i * i), $MachinePrecision] * N[(i * i), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision] / N[(t$95$0 - 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\\
\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{t\_0}}{t\_0 - 1}
\end{array}
\end{array}

Alternative 1: 100.0% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;i \leq 5000000:\\ \;\;\;\;\frac{i \cdot \left(i \cdot 0.25\right)}{-1 - {i}^{2} \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.0625\\ \end{array} \end{array} \]
(FPCore (i)
 :precision binary64
 (if (<= i 5000000.0)
   (/ (* i (* i 0.25)) (- -1.0 (* (pow i 2.0) -4.0)))
   0.0625))
double code(double i) {
	double tmp;
	if (i <= 5000000.0) {
		tmp = (i * (i * 0.25)) / (-1.0 - (pow(i, 2.0) * -4.0));
	} else {
		tmp = 0.0625;
	}
	return tmp;
}
real(8) function code(i)
    real(8), intent (in) :: i
    real(8) :: tmp
    if (i <= 5000000.0d0) then
        tmp = (i * (i * 0.25d0)) / ((-1.0d0) - ((i ** 2.0d0) * (-4.0d0)))
    else
        tmp = 0.0625d0
    end if
    code = tmp
end function
public static double code(double i) {
	double tmp;
	if (i <= 5000000.0) {
		tmp = (i * (i * 0.25)) / (-1.0 - (Math.pow(i, 2.0) * -4.0));
	} else {
		tmp = 0.0625;
	}
	return tmp;
}
def code(i):
	tmp = 0
	if i <= 5000000.0:
		tmp = (i * (i * 0.25)) / (-1.0 - (math.pow(i, 2.0) * -4.0))
	else:
		tmp = 0.0625
	return tmp
function code(i)
	tmp = 0.0
	if (i <= 5000000.0)
		tmp = Float64(Float64(i * Float64(i * 0.25)) / Float64(-1.0 - Float64((i ^ 2.0) * -4.0)));
	else
		tmp = 0.0625;
	end
	return tmp
end
function tmp_2 = code(i)
	tmp = 0.0;
	if (i <= 5000000.0)
		tmp = (i * (i * 0.25)) / (-1.0 - ((i ^ 2.0) * -4.0));
	else
		tmp = 0.0625;
	end
	tmp_2 = tmp;
end
code[i_] := If[LessEqual[i, 5000000.0], N[(N[(i * N[(i * 0.25), $MachinePrecision]), $MachinePrecision] / N[(-1.0 - N[(N[Power[i, 2.0], $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0625]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;i \leq 5000000:\\
\;\;\;\;\frac{i \cdot \left(i \cdot 0.25\right)}{-1 - {i}^{2} \cdot -4}\\

\mathbf{else}:\\
\;\;\;\;0.0625\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if i < 5e6

    1. Initial program 26.8%

      \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
    3. Add Preprocessing
    4. Step-by-step derivation
      1. associate-*r*100.0%

        \[\leadsto \color{blue}{\left(0.25 \cdot i\right) \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}} \]
      2. frac-2neg100.0%

        \[\leadsto \left(0.25 \cdot i\right) \cdot \color{blue}{\frac{-i}{-\mathsf{fma}\left(i, i \cdot 4, -1\right)}} \]
      3. associate-*r/100.0%

        \[\leadsto \color{blue}{\frac{\left(0.25 \cdot i\right) \cdot \left(-i\right)}{-\mathsf{fma}\left(i, i \cdot 4, -1\right)}} \]
      4. *-commutative100.0%

        \[\leadsto \frac{\color{blue}{\left(i \cdot 0.25\right)} \cdot \left(-i\right)}{-\mathsf{fma}\left(i, i \cdot 4, -1\right)} \]
      5. fma-undefine100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{-\color{blue}{\left(i \cdot \left(i \cdot 4\right) + -1\right)}} \]
      6. distribute-neg-in100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\color{blue}{\left(-i \cdot \left(i \cdot 4\right)\right) + \left(--1\right)}} \]
      7. associate-*r*100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\left(-\color{blue}{\left(i \cdot i\right) \cdot 4}\right) + \left(--1\right)} \]
      8. *-commutative100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\left(-\color{blue}{4 \cdot \left(i \cdot i\right)}\right) + \left(--1\right)} \]
      9. metadata-eval100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\left(-\color{blue}{\left(2 \cdot 2\right)} \cdot \left(i \cdot i\right)\right) + \left(--1\right)} \]
      10. swap-sqr100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\left(-\color{blue}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}\right) + \left(--1\right)} \]
      11. distribute-neg-in100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\color{blue}{-\left(\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) + -1\right)}} \]
      12. +-commutative100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{-\color{blue}{\left(-1 + \left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\right)}} \]
      13. distribute-neg-in100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\color{blue}{\left(--1\right) + \left(-\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\right)}} \]
      14. metadata-eval100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{\color{blue}{1} + \left(-\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)\right)} \]
      15. swap-sqr100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + \left(-\color{blue}{\left(2 \cdot 2\right) \cdot \left(i \cdot i\right)}\right)} \]
      16. metadata-eval100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + \left(-\color{blue}{4} \cdot \left(i \cdot i\right)\right)} \]
      17. *-commutative100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + \left(-\color{blue}{\left(i \cdot i\right) \cdot 4}\right)} \]
      18. distribute-rgt-neg-in100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + \color{blue}{\left(i \cdot i\right) \cdot \left(-4\right)}} \]
      19. pow2100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + \color{blue}{{i}^{2}} \cdot \left(-4\right)} \]
      20. metadata-eval100.0%

        \[\leadsto \frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + {i}^{2} \cdot \color{blue}{-4}} \]
    5. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{\left(i \cdot 0.25\right) \cdot \left(-i\right)}{1 + {i}^{2} \cdot -4}} \]

    if 5e6 < i

    1. Initial program 22.7%

      \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
    2. Simplified49.1%

      \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
    3. Add Preprocessing
    4. Taylor expanded in i around inf 100.0%

      \[\leadsto \color{blue}{0.0625} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq 5000000:\\ \;\;\;\;\frac{i \cdot \left(i \cdot 0.25\right)}{-1 - {i}^{2} \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.0625\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 100.0% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;i \leq 50000000:\\ \;\;\;\;0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;0.0625\\ \end{array} \end{array} \]
(FPCore (i)
 :precision binary64
 (if (<= i 50000000.0) (* 0.25 (* i (/ i (fma i (* i 4.0) -1.0)))) 0.0625))
double code(double i) {
	double tmp;
	if (i <= 50000000.0) {
		tmp = 0.25 * (i * (i / fma(i, (i * 4.0), -1.0)));
	} else {
		tmp = 0.0625;
	}
	return tmp;
}
function code(i)
	tmp = 0.0
	if (i <= 50000000.0)
		tmp = Float64(0.25 * Float64(i * Float64(i / fma(i, Float64(i * 4.0), -1.0))));
	else
		tmp = 0.0625;
	end
	return tmp
end
code[i_] := If[LessEqual[i, 50000000.0], N[(0.25 * N[(i * N[(i / N[(i * N[(i * 4.0), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0625]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;i \leq 50000000:\\
\;\;\;\;0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)\\

\mathbf{else}:\\
\;\;\;\;0.0625\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if i < 5e7

    1. Initial program 26.8%

      \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
    3. Add Preprocessing

    if 5e7 < i

    1. Initial program 22.7%

      \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
    2. Simplified49.1%

      \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
    3. Add Preprocessing
    4. Taylor expanded in i around inf 100.0%

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

Alternative 3: 98.9% accurate, 2.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;i \leq 0.5:\\ \;\;\;\;0.25 \cdot \left(-i \cdot i\right)\\ \mathbf{else}:\\ \;\;\;\;0.0625\\ \end{array} \end{array} \]
(FPCore (i) :precision binary64 (if (<= i 0.5) (* 0.25 (- (* i i))) 0.0625))
double code(double i) {
	double tmp;
	if (i <= 0.5) {
		tmp = 0.25 * -(i * i);
	} else {
		tmp = 0.0625;
	}
	return tmp;
}
real(8) function code(i)
    real(8), intent (in) :: i
    real(8) :: tmp
    if (i <= 0.5d0) then
        tmp = 0.25d0 * -(i * i)
    else
        tmp = 0.0625d0
    end if
    code = tmp
end function
public static double code(double i) {
	double tmp;
	if (i <= 0.5) {
		tmp = 0.25 * -(i * i);
	} else {
		tmp = 0.0625;
	}
	return tmp;
}
def code(i):
	tmp = 0
	if i <= 0.5:
		tmp = 0.25 * -(i * i)
	else:
		tmp = 0.0625
	return tmp
function code(i)
	tmp = 0.0
	if (i <= 0.5)
		tmp = Float64(0.25 * Float64(-Float64(i * i)));
	else
		tmp = 0.0625;
	end
	return tmp
end
function tmp_2 = code(i)
	tmp = 0.0;
	if (i <= 0.5)
		tmp = 0.25 * -(i * i);
	else
		tmp = 0.0625;
	end
	tmp_2 = tmp;
end
code[i_] := If[LessEqual[i, 0.5], N[(0.25 * (-N[(i * i), $MachinePrecision])), $MachinePrecision], 0.0625]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;i \leq 0.5:\\
\;\;\;\;0.25 \cdot \left(-i \cdot i\right)\\

\mathbf{else}:\\
\;\;\;\;0.0625\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if i < 0.5

    1. Initial program 25.1%

      \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
    3. Add Preprocessing
    4. Taylor expanded in i around 0 99.2%

      \[\leadsto 0.25 \cdot \left(i \cdot \color{blue}{\left(-1 \cdot i\right)}\right) \]
    5. Step-by-step derivation
      1. neg-mul-199.2%

        \[\leadsto 0.25 \cdot \left(i \cdot \color{blue}{\left(-i\right)}\right) \]
    6. Simplified99.2%

      \[\leadsto 0.25 \cdot \left(i \cdot \color{blue}{\left(-i\right)}\right) \]

    if 0.5 < i

    1. Initial program 24.4%

      \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
    2. Simplified50.3%

      \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
    3. Add Preprocessing
    4. Taylor expanded in i around inf 99.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq 0.5:\\ \;\;\;\;0.25 \cdot \left(-i \cdot i\right)\\ \mathbf{else}:\\ \;\;\;\;0.0625\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 49.8% accurate, 25.0× speedup?

\[\begin{array}{l} \\ 0.0625 \end{array} \]
(FPCore (i) :precision binary64 0.0625)
double code(double i) {
	return 0.0625;
}
real(8) function code(i)
    real(8), intent (in) :: i
    code = 0.0625d0
end function
public static double code(double i) {
	return 0.0625;
}
def code(i):
	return 0.0625
function code(i)
	return 0.0625
end
function tmp = code(i)
	tmp = 0.0625;
end
code[i_] := 0.0625
\begin{array}{l}

\\
0.0625
\end{array}
Derivation
  1. Initial program 24.8%

    \[\frac{\frac{\left(i \cdot i\right) \cdot \left(i \cdot i\right)}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right)}}{\left(2 \cdot i\right) \cdot \left(2 \cdot i\right) - 1} \]
  2. Simplified74.5%

    \[\leadsto \color{blue}{0.25 \cdot \left(i \cdot \frac{i}{\mathsf{fma}\left(i, i \cdot 4, -1\right)}\right)} \]
  3. Add Preprocessing
  4. Taylor expanded in i around inf 52.0%

    \[\leadsto \color{blue}{0.0625} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024100 
(FPCore (i)
  :name "Octave 3.8, jcobi/4, as called"
  :precision binary64
  :pre (> i 0.0)
  (/ (/ (* (* i i) (* i i)) (* (* 2.0 i) (* 2.0 i))) (- (* (* 2.0 i) (* 2.0 i)) 1.0)))