Falkner and Boettcher, Appendix A

?

Percentage Accurate: 90.3% → 97.5%
Time: 13.4s
Precision: binary64
Cost: 14660

?

\[\frac{a \cdot {k}^{m}}{\left(1 + 10 \cdot k\right) + k \cdot k} \]
\[\begin{array}{l} t_0 := a \cdot {k}^{m}\\ t_1 := \frac{t_0}{\left(1 + k \cdot 10\right) + k \cdot k}\\ \mathbf{if}\;t_1 \leq 2 \cdot 10^{+214}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (a k m)
 :precision binary64
 (/ (* a (pow k m)) (+ (+ 1.0 (* 10.0 k)) (* k k))))
(FPCore (a k m)
 :precision binary64
 (let* ((t_0 (* a (pow k m))) (t_1 (/ t_0 (+ (+ 1.0 (* k 10.0)) (* k k)))))
   (if (<= t_1 2e+214) t_1 t_0)))
double code(double a, double k, double m) {
	return (a * pow(k, m)) / ((1.0 + (10.0 * k)) + (k * k));
}
double code(double a, double k, double m) {
	double t_0 = a * pow(k, m);
	double t_1 = t_0 / ((1.0 + (k * 10.0)) + (k * k));
	double tmp;
	if (t_1 <= 2e+214) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(a, k, m)
    real(8), intent (in) :: a
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    code = (a * (k ** m)) / ((1.0d0 + (10.0d0 * k)) + (k * k))
end function
real(8) function code(a, k, m)
    real(8), intent (in) :: a
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = a * (k ** m)
    t_1 = t_0 / ((1.0d0 + (k * 10.0d0)) + (k * k))
    if (t_1 <= 2d+214) then
        tmp = t_1
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double a, double k, double m) {
	return (a * Math.pow(k, m)) / ((1.0 + (10.0 * k)) + (k * k));
}
public static double code(double a, double k, double m) {
	double t_0 = a * Math.pow(k, m);
	double t_1 = t_0 / ((1.0 + (k * 10.0)) + (k * k));
	double tmp;
	if (t_1 <= 2e+214) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(a, k, m):
	return (a * math.pow(k, m)) / ((1.0 + (10.0 * k)) + (k * k))
def code(a, k, m):
	t_0 = a * math.pow(k, m)
	t_1 = t_0 / ((1.0 + (k * 10.0)) + (k * k))
	tmp = 0
	if t_1 <= 2e+214:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
function code(a, k, m)
	return Float64(Float64(a * (k ^ m)) / Float64(Float64(1.0 + Float64(10.0 * k)) + Float64(k * k)))
end
function code(a, k, m)
	t_0 = Float64(a * (k ^ m))
	t_1 = Float64(t_0 / Float64(Float64(1.0 + Float64(k * 10.0)) + Float64(k * k)))
	tmp = 0.0
	if (t_1 <= 2e+214)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp = code(a, k, m)
	tmp = (a * (k ^ m)) / ((1.0 + (10.0 * k)) + (k * k));
end
function tmp_2 = code(a, k, m)
	t_0 = a * (k ^ m);
	t_1 = t_0 / ((1.0 + (k * 10.0)) + (k * k));
	tmp = 0.0;
	if (t_1 <= 2e+214)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[a_, k_, m_] := N[(N[(a * N[Power[k, m], $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 + N[(10.0 * k), $MachinePrecision]), $MachinePrecision] + N[(k * k), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, k_, m_] := Block[{t$95$0 = N[(a * N[Power[k, m], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 / N[(N[(1.0 + N[(k * 10.0), $MachinePrecision]), $MachinePrecision] + N[(k * k), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 2e+214], t$95$1, t$95$0]]]
\frac{a \cdot {k}^{m}}{\left(1 + 10 \cdot k\right) + k \cdot k}
\begin{array}{l}
t_0 := a \cdot {k}^{m}\\
t_1 := \frac{t_0}{\left(1 + k \cdot 10\right) + k \cdot k}\\
\mathbf{if}\;t_1 \leq 2 \cdot 10^{+214}:\\
\;\;\;\;t_1\\

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


\end{array}

Local Percentage Accuracy?

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.

Herbie found 16 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 a (pow.f64 k m)) (+.f64 (+.f64 1 (*.f64 10 k)) (*.f64 k k))) < 1.9999999999999999e214

    1. Initial program 96.4%

      \[\frac{a \cdot {k}^{m}}{\left(1 + 10 \cdot k\right) + k \cdot k} \]

    if 1.9999999999999999e214 < (/.f64 (*.f64 a (pow.f64 k m)) (+.f64 (+.f64 1 (*.f64 10 k)) (*.f64 k k)))

    1. Initial program 55.3%

      \[\frac{a \cdot {k}^{m}}{\left(1 + 10 \cdot k\right) + k \cdot k} \]
    2. Simplified55.3%

      \[\leadsto \color{blue}{a \cdot \frac{{k}^{m}}{\mathsf{fma}\left(k, k + 10, 1\right)}} \]
      Step-by-step derivation

      [Start]55.3

      \[ \frac{a \cdot {k}^{m}}{\left(1 + 10 \cdot k\right) + k \cdot k} \]

      associate-*r/ [<=]55.3

      \[ \color{blue}{a \cdot \frac{{k}^{m}}{\left(1 + 10 \cdot k\right) + k \cdot k}} \]

      associate-+l+ [=>]55.3

      \[ a \cdot \frac{{k}^{m}}{\color{blue}{1 + \left(10 \cdot k + k \cdot k\right)}} \]

      +-commutative [=>]55.3

      \[ a \cdot \frac{{k}^{m}}{\color{blue}{\left(10 \cdot k + k \cdot k\right) + 1}} \]

      distribute-rgt-out [=>]55.3

      \[ a \cdot \frac{{k}^{m}}{\color{blue}{k \cdot \left(10 + k\right)} + 1} \]

      fma-def [=>]55.3

      \[ a \cdot \frac{{k}^{m}}{\color{blue}{\mathsf{fma}\left(k, 10 + k, 1\right)}} \]

      +-commutative [=>]55.3

      \[ a \cdot \frac{{k}^{m}}{\mathsf{fma}\left(k, \color{blue}{k + 10}, 1\right)} \]
    3. Taylor expanded in k around 0 66.0%

      \[\leadsto a \cdot \color{blue}{e^{\log k \cdot m}} \]
    4. Simplified100.0%

      \[\leadsto a \cdot \color{blue}{{k}^{m}} \]
      Step-by-step derivation

      [Start]66.0

      \[ a \cdot e^{\log k \cdot m} \]

      exp-to-pow [=>]100.0

      \[ a \cdot \color{blue}{{k}^{m}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot {k}^{m}}{\left(1 + k \cdot 10\right) + k \cdot k} \leq 2 \cdot 10^{+214}:\\ \;\;\;\;\frac{a \cdot {k}^{m}}{\left(1 + k \cdot 10\right) + k \cdot k}\\ \mathbf{else}:\\ \;\;\;\;a \cdot {k}^{m}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy96.4%
Cost7048
\[\begin{array}{l} \mathbf{if}\;m \leq -4.5 \cdot 10^{-17}:\\ \;\;\;\;a \cdot {k}^{m}\\ \mathbf{elif}\;m \leq 1.9 \cdot 10^{-34}:\\ \;\;\;\;\frac{a}{1 + k \cdot \left(k + 10\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{{k}^{\left(2 - m\right)}}\\ \end{array} \]
Alternative 2
Accuracy96.4%
Cost7048
\[\begin{array}{l} \mathbf{if}\;m \leq -4.5 \cdot 10^{-17}:\\ \;\;\;\;\frac{1}{\frac{{k}^{\left(-m\right)}}{a}}\\ \mathbf{elif}\;m \leq 1.9 \cdot 10^{-34}:\\ \;\;\;\;\frac{a}{1 + k \cdot \left(k + 10\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{{k}^{\left(2 - m\right)}}\\ \end{array} \]
Alternative 3
Accuracy96.9%
Cost6921
\[\begin{array}{l} \mathbf{if}\;m \leq -4.5 \cdot 10^{-17} \lor \neg \left(m \leq 0.0038\right):\\ \;\;\;\;a \cdot {k}^{m}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{1 + k \cdot \left(k + 10\right)}\\ \end{array} \]
Alternative 4
Accuracy66.5%
Cost1096
\[\begin{array}{l} \mathbf{if}\;m \leq -82000:\\ \;\;\;\;\left(1 + \frac{a}{k \cdot k}\right) + -1\\ \mathbf{elif}\;m \leq 1.82:\\ \;\;\;\;\frac{a}{1 + k \cdot \left(k + 10\right)}\\ \mathbf{else}:\\ \;\;\;\;a \cdot \left(\left(1 + k \cdot -10\right) - \left(k \cdot k\right) \cdot -99\right)\\ \end{array} \]
Alternative 5
Accuracy55.5%
Cost840
\[\begin{array}{l} \mathbf{if}\;m \leq -82000:\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{elif}\;m \leq 5.2 \cdot 10^{+30}:\\ \;\;\;\;\frac{a}{1 + k \cdot k}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + a \cdot \frac{0.1}{k}\right) + -1\\ \end{array} \]
Alternative 6
Accuracy59.5%
Cost840
\[\begin{array}{l} \mathbf{if}\;m \leq -82000:\\ \;\;\;\;\left(1 + \frac{a}{k \cdot k}\right) + -1\\ \mathbf{elif}\;m \leq 7.6 \cdot 10^{+29}:\\ \;\;\;\;\frac{a}{1 + k \cdot k}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + a \cdot \frac{0.1}{k}\right) + -1\\ \end{array} \]
Alternative 7
Accuracy60.3%
Cost840
\[\begin{array}{l} \mathbf{if}\;m \leq -82000:\\ \;\;\;\;\left(1 + \frac{a}{k \cdot k}\right) + -1\\ \mathbf{elif}\;m \leq 5.8 \cdot 10^{+29}:\\ \;\;\;\;\frac{a}{1 + k \cdot \left(k + 10\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + a \cdot \frac{0.1}{k}\right) + -1\\ \end{array} \]
Alternative 8
Accuracy48.0%
Cost712
\[\begin{array}{l} \mathbf{if}\;k \leq -1.06 \cdot 10^{-303}:\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{elif}\;k \leq 6.6 \cdot 10^{-6}:\\ \;\;\;\;a \cdot \left(1 + k \cdot -10\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{a}{k}}{k}\\ \end{array} \]
Alternative 9
Accuracy48.0%
Cost712
\[\begin{array}{l} \mathbf{if}\;k \leq -1.06 \cdot 10^{-303}:\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{elif}\;k \leq 6.6 \cdot 10^{-6}:\\ \;\;\;\;a \cdot \left(1 + k \cdot -10\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{k} \cdot \frac{1}{k}\\ \end{array} \]
Alternative 10
Accuracy48.1%
Cost712
\[\begin{array}{l} \mathbf{if}\;k \leq -1.06 \cdot 10^{-303}:\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{elif}\;k \leq 10.2:\\ \;\;\;\;\frac{a}{1 + k \cdot 10}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{k} \cdot \frac{1}{k}\\ \end{array} \]
Alternative 11
Accuracy54.1%
Cost712
\[\begin{array}{l} \mathbf{if}\;m \leq -82000:\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{elif}\;m \leq 1.5 \cdot 10^{+18}:\\ \;\;\;\;\frac{a}{1 + k \cdot k}\\ \mathbf{else}:\\ \;\;\;\;a \cdot \left(1 + k \cdot -10\right)\\ \end{array} \]
Alternative 12
Accuracy28.9%
Cost585
\[\begin{array}{l} \mathbf{if}\;k \leq -1.5 \cdot 10^{+90} \lor \neg \left(k \leq 6.6 \cdot 10^{-6}\right):\\ \;\;\;\;\frac{a}{k \cdot 10}\\ \mathbf{else}:\\ \;\;\;\;a\\ \end{array} \]
Alternative 13
Accuracy46.7%
Cost585
\[\begin{array}{l} \mathbf{if}\;k \leq -1.06 \cdot 10^{-303} \lor \neg \left(k \leq 6.6 \cdot 10^{-6}\right):\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{else}:\\ \;\;\;\;a\\ \end{array} \]
Alternative 14
Accuracy47.8%
Cost584
\[\begin{array}{l} \mathbf{if}\;k \leq -1.06 \cdot 10^{-303}:\\ \;\;\;\;\frac{a}{k \cdot k}\\ \mathbf{elif}\;k \leq 6.6 \cdot 10^{-6}:\\ \;\;\;\;a\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{a}{k}}{k}\\ \end{array} \]
Alternative 15
Accuracy20.5%
Cost64
\[a \]

Reproduce?

herbie shell --seed 2023160 
(FPCore (a k m)
  :name "Falkner and Boettcher, Appendix A"
  :precision binary64
  (/ (* a (pow k m)) (+ (+ 1.0 (* 10.0 k)) (* k k))))