Kahan p13 Example 1

?

Percentage Accurate: 99.9% → 100.0%
Time: 14.5s
Precision: binary64
Cost: 14272

?

\[\frac{1 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}}{2 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}} \]
\[\begin{array}{l} t_1 := \frac{4}{\frac{1}{t} + \left(t + 2\right)}\\ \frac{\mathsf{fma}\left(t, t_1, 1\right)}{\mathsf{fma}\left(t, t_1, 2\right)} \end{array} \]
(FPCore (t)
 :precision binary64
 (/
  (+ 1.0 (* (/ (* 2.0 t) (+ 1.0 t)) (/ (* 2.0 t) (+ 1.0 t))))
  (+ 2.0 (* (/ (* 2.0 t) (+ 1.0 t)) (/ (* 2.0 t) (+ 1.0 t))))))
(FPCore (t)
 :precision binary64
 (let* ((t_1 (/ 4.0 (+ (/ 1.0 t) (+ t 2.0)))))
   (/ (fma t t_1 1.0) (fma t t_1 2.0))))
double code(double t) {
	return (1.0 + (((2.0 * t) / (1.0 + t)) * ((2.0 * t) / (1.0 + t)))) / (2.0 + (((2.0 * t) / (1.0 + t)) * ((2.0 * t) / (1.0 + t))));
}
double code(double t) {
	double t_1 = 4.0 / ((1.0 / t) + (t + 2.0));
	return fma(t, t_1, 1.0) / fma(t, t_1, 2.0);
}
function code(t)
	return Float64(Float64(1.0 + Float64(Float64(Float64(2.0 * t) / Float64(1.0 + t)) * Float64(Float64(2.0 * t) / Float64(1.0 + t)))) / Float64(2.0 + Float64(Float64(Float64(2.0 * t) / Float64(1.0 + t)) * Float64(Float64(2.0 * t) / Float64(1.0 + t)))))
end
function code(t)
	t_1 = Float64(4.0 / Float64(Float64(1.0 / t) + Float64(t + 2.0)))
	return Float64(fma(t, t_1, 1.0) / fma(t, t_1, 2.0))
end
code[t_] := N[(N[(1.0 + N[(N[(N[(2.0 * t), $MachinePrecision] / N[(1.0 + t), $MachinePrecision]), $MachinePrecision] * N[(N[(2.0 * t), $MachinePrecision] / N[(1.0 + t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 + N[(N[(N[(2.0 * t), $MachinePrecision] / N[(1.0 + t), $MachinePrecision]), $MachinePrecision] * N[(N[(2.0 * t), $MachinePrecision] / N[(1.0 + t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[t_] := Block[{t$95$1 = N[(4.0 / N[(N[(1.0 / t), $MachinePrecision] + N[(t + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(t * t$95$1 + 1.0), $MachinePrecision] / N[(t * t$95$1 + 2.0), $MachinePrecision]), $MachinePrecision]]
\frac{1 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}}{2 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}}
\begin{array}{l}
t_1 := \frac{4}{\frac{1}{t} + \left(t + 2\right)}\\
\frac{\mathsf{fma}\left(t, t_1, 1\right)}{\mathsf{fma}\left(t, t_1, 2\right)}
\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 8 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.

Bogosity?

Bogosity

Derivation?

  1. Initial program 100.0%

    \[\frac{1 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}}{2 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}} \]
  2. Simplified100.0%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(t, \frac{4}{\frac{1}{t} + \left(2 + t\right)}, 1\right)}{\mathsf{fma}\left(t, \frac{4}{\frac{1}{t} + \left(2 + t\right)}, 2\right)}} \]
    Step-by-step derivation

    [Start]100.0

    \[ \frac{1 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}}{2 + \frac{2 \cdot t}{1 + t} \cdot \frac{2 \cdot t}{1 + t}} \]
  3. Final simplification100.0%

    \[\leadsto \frac{\mathsf{fma}\left(t, \frac{4}{\frac{1}{t} + \left(t + 2\right)}, 1\right)}{\mathsf{fma}\left(t, \frac{4}{\frac{1}{t} + \left(t + 2\right)}, 2\right)} \]

Alternatives

Alternative 1
Accuracy99.9%
Cost1984
\[\begin{array}{l} t_1 := t \cdot \frac{\frac{t \cdot 4}{t + 1}}{t + 1}\\ \frac{1 + t_1}{2 + t_1} \end{array} \]
Alternative 2
Accuracy99.3%
Cost1225
\[\begin{array}{l} t_1 := t \cdot \left(t \cdot 4\right)\\ \mathbf{if}\;t \leq -0.62 \lor \neg \left(t \leq 0.43\right):\\ \;\;\;\;\frac{0.037037037037037035}{t \cdot t} + \left(0.8333333333333334 - \frac{0.2222222222222222}{t}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1 + t_1}{2 + t_1}\\ \end{array} \]
Alternative 3
Accuracy99.3%
Cost969
\[\begin{array}{l} \mathbf{if}\;t \leq -0.8 \lor \neg \left(t \leq 0.34\right):\\ \;\;\;\;\frac{0.037037037037037035}{t \cdot t} + \left(0.8333333333333334 - \frac{0.2222222222222222}{t}\right)\\ \mathbf{else}:\\ \;\;\;\;t \cdot t + 0.5\\ \end{array} \]
Alternative 4
Accuracy99.2%
Cost585
\[\begin{array}{l} \mathbf{if}\;t \leq -0.78 \lor \neg \left(t \leq 0.56\right):\\ \;\;\;\;0.8333333333333334 - \frac{0.2222222222222222}{t}\\ \mathbf{else}:\\ \;\;\;\;t \cdot t + 0.5\\ \end{array} \]
Alternative 5
Accuracy98.6%
Cost584
\[\begin{array}{l} \mathbf{if}\;t \leq -0.41:\\ \;\;\;\;0.8333333333333334\\ \mathbf{elif}\;t \leq 0.58:\\ \;\;\;\;t \cdot t + 0.5\\ \mathbf{else}:\\ \;\;\;\;0.8333333333333334\\ \end{array} \]
Alternative 6
Accuracy98.5%
Cost328
\[\begin{array}{l} \mathbf{if}\;t \leq -0.34:\\ \;\;\;\;0.8333333333333334\\ \mathbf{elif}\;t \leq 1:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0.8333333333333334\\ \end{array} \]
Alternative 7
Accuracy59.3%
Cost64
\[0.5 \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (t)
  :name "Kahan p13 Example 1"
  :precision binary64
  (/ (+ 1.0 (* (/ (* 2.0 t) (+ 1.0 t)) (/ (* 2.0 t) (+ 1.0 t)))) (+ 2.0 (* (/ (* 2.0 t) (+ 1.0 t)) (/ (* 2.0 t) (+ 1.0 t))))))