# ?

Average Error: 13.9 → 8.1
Time: 18.7s
Precision: binary64
Cost: 72456

# ?

$\left(\left(-10 \leq a \land a \leq 10\right) \land \left(1 \leq p \land p \leq 2\right)\right) \land \left(-101 \leq b \land b \leq 10\right)$
$\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
$\begin{array}{l} t_0 := {\left(\left|a\right|\right)}^{p}\\ t_1 := {\left(\left|b\right|\right)}^{p}\\ t_2 := \frac{t_0}{a} - \frac{t_1}{a}\\ t_3 := b \cdot t_1\\ \mathbf{if}\;a \leq -2.4 \cdot 10^{-141}:\\ \;\;\;\;\frac{a \cdot t_0 - t_3}{a - b}\\ \mathbf{elif}\;a \leq -1 \cdot 10^{-178}:\\ \;\;\;\;\mathsf{fma}\left(t_2, b, t_0\right) + \frac{t_2}{\frac{a}{b \cdot b}}\\ \mathbf{elif}\;a \leq 3.3 \cdot 10^{-144}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot {a}^{p} - t_3}{a - b}\\ \end{array}$
(FPCore (a p b)
:precision binary64
(/ (- (* a (pow (fabs a) p)) (* b (pow (fabs b) p))) (- a b)))
(FPCore (a p b)
:precision binary64
(let* ((t_0 (pow (fabs a) p))
(t_1 (pow (fabs b) p))
(t_2 (- (/ t_0 a) (/ t_1 a)))
(t_3 (* b t_1)))
(if (<= a -2.4e-141)
(/ (- (* a t_0) t_3) (- a b))
(if (<= a -1e-178)
(+ (fma t_2 b t_0) (/ t_2 (/ a (* b b))))
(if (<= a 3.3e-144) t_1 (/ (- (* a (pow a p)) t_3) (- a b)))))))
double code(double a, double p, double b) {
return ((a * pow(fabs(a), p)) - (b * pow(fabs(b), p))) / (a - b);
}

double code(double a, double p, double b) {
double t_0 = pow(fabs(a), p);
double t_1 = pow(fabs(b), p);
double t_2 = (t_0 / a) - (t_1 / a);
double t_3 = b * t_1;
double tmp;
if (a <= -2.4e-141) {
tmp = ((a * t_0) - t_3) / (a - b);
} else if (a <= -1e-178) {
tmp = fma(t_2, b, t_0) + (t_2 / (a / (b * b)));
} else if (a <= 3.3e-144) {
tmp = t_1;
} else {
tmp = ((a * pow(a, p)) - t_3) / (a - b);
}
return tmp;
}

function code(a, p, b)
return Float64(Float64(Float64(a * (abs(a) ^ p)) - Float64(b * (abs(b) ^ p))) / Float64(a - b))
end

function code(a, p, b)
t_0 = abs(a) ^ p
t_1 = abs(b) ^ p
t_2 = Float64(Float64(t_0 / a) - Float64(t_1 / a))
t_3 = Float64(b * t_1)
tmp = 0.0
if (a <= -2.4e-141)
tmp = Float64(Float64(Float64(a * t_0) - t_3) / Float64(a - b));
elseif (a <= -1e-178)
tmp = Float64(fma(t_2, b, t_0) + Float64(t_2 / Float64(a / Float64(b * b))));
elseif (a <= 3.3e-144)
tmp = t_1;
else
tmp = Float64(Float64(Float64(a * (a ^ p)) - t_3) / Float64(a - b));
end
return tmp
end

code[a_, p_, b_] := N[(N[(N[(a * N[Power[N[Abs[a], $MachinePrecision], p],$MachinePrecision]), $MachinePrecision] - N[(b * N[Power[N[Abs[b],$MachinePrecision], p], $MachinePrecision]),$MachinePrecision]), $MachinePrecision] / N[(a - b),$MachinePrecision]), $MachinePrecision]  code[a_, p_, b_] := Block[{t$95$0 = N[Power[N[Abs[a],$MachinePrecision], p], $MachinePrecision]}, Block[{t$95$1 = N[Power[N[Abs[b],$MachinePrecision], p], $MachinePrecision]}, Block[{t$95$2 = N[(N[(t$95$0 / a),$MachinePrecision] - N[(t$95$1 / a), $MachinePrecision]),$MachinePrecision]}, Block[{t$95$3 = N[(b * t$95$1), $MachinePrecision]}, If[LessEqual[a, -2.4e-141], N[(N[(N[(a * t$95$0),$MachinePrecision] - t$95$3), $MachinePrecision] / N[(a - b),$MachinePrecision]), $MachinePrecision], If[LessEqual[a, -1e-178], N[(N[(t$95$2 * b + t$95$0),$MachinePrecision] + N[(t$95$2 / N[(a / N[(b * b), $MachinePrecision]),$MachinePrecision]), $MachinePrecision]),$MachinePrecision], If[LessEqual[a, 3.3e-144], t$95$1, N[(N[(N[(a * N[Power[a, p], $MachinePrecision]),$MachinePrecision] - t$95$3), $MachinePrecision] / N[(a - b),$MachinePrecision]), \$MachinePrecision]]]]]]]]

\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}

\begin{array}{l}
t_0 := {\left(\left|a\right|\right)}^{p}\\
t_1 := {\left(\left|b\right|\right)}^{p}\\
t_2 := \frac{t_0}{a} - \frac{t_1}{a}\\
t_3 := b \cdot t_1\\
\mathbf{if}\;a \leq -2.4 \cdot 10^{-141}:\\
\;\;\;\;\frac{a \cdot t_0 - t_3}{a - b}\\

\mathbf{elif}\;a \leq -1 \cdot 10^{-178}:\\
\;\;\;\;\mathsf{fma}\left(t_2, b, t_0\right) + \frac{t_2}{\frac{a}{b \cdot b}}\\

\mathbf{elif}\;a \leq 3.3 \cdot 10^{-144}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{a \cdot {a}^{p} - t_3}{a - b}\\

\end{array}


# Derivation?

1. Split input into 4 regimes
2. ## if a < -2.4000000000000001e-141

1. Initial program 3.8

$\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$

## if -2.4000000000000001e-141 < a < -9.9999999999999995e-179

1. Initial program 26.9

$\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
2. Taylor expanded in b around 0 37.1

$\leadsto \color{blue}{\frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a} + \left(\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot b + e^{\log \left(\left|a\right|\right) \cdot p}\right)}$
3. Simplified34.5

$\leadsto \color{blue}{\mathsf{fma}\left(-1 \cdot \left(\frac{{\left(\left|b\right|\right)}^{p}}{a} - \frac{{\left(\left|a\right|\right)}^{p}}{a}\right), b, {\left(\left|a\right|\right)}^{p}\right) + \frac{-1 \cdot \left(\frac{{\left(\left|b\right|\right)}^{p}}{a} - \frac{{\left(\left|a\right|\right)}^{p}}{a}\right)}{\frac{a}{b \cdot b}}}$
Proof
[Start]37.1 $\frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a} + \left(\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot b + e^{\log \left(\left|a\right|\right) \cdot p}\right)$ $\color{blue}{\left(\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot b + e^{\log \left(\left|a\right|\right) \cdot p}\right) + \frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a}}$ $\color{blue}{\mathsf{fma}\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}, b, e^{\log \left(\left|a\right|\right) \cdot p}\right)} + \frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a}$ $\mathsf{fma}\left(\color{blue}{-1 \cdot \left(\frac{{\left(\left|b\right|\right)}^{p}}{a} - \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right)}, b, e^{\log \left(\left|a\right|\right) \cdot p}\right) + \frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a}$ $\mathsf{fma}\left(-1 \cdot \left(\frac{{\left(\left|b\right|\right)}^{p}}{a} - \frac{\color{blue}{{\left(\left|a\right|\right)}^{p}}}{a}\right), b, e^{\log \left(\left|a\right|\right) \cdot p}\right) + \frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a}$ $\mathsf{fma}\left(-1 \cdot \left(\frac{{\left(\left|b\right|\right)}^{p}}{a} - \frac{{\left(\left|a\right|\right)}^{p}}{a}\right), b, \color{blue}{{\left(\left|a\right|\right)}^{p}}\right) + \frac{\left(-1 \cdot \frac{{\left(\left|b\right|\right)}^{p}}{a} - -1 \cdot \frac{e^{\log \left(\left|a\right|\right) \cdot p}}{a}\right) \cdot {b}^{2}}{a}$

## if -9.9999999999999995e-179 < a < 3.29999999999999995e-144

1. Initial program 21.7

$\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
2. Taylor expanded in a around 0 8.8

$\leadsto \color{blue}{{\left(\left|b\right|\right)}^{p}}$

## if 3.29999999999999995e-144 < a

1. Initial program 4.2

$\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
2. Applied egg-rr43.8

$\leadsto \frac{\color{blue}{\left(e^{\mathsf{log1p}\left(a \cdot {a}^{p}\right)} - 1\right)} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
3. Simplified4.2

$\leadsto \frac{\color{blue}{a \cdot {a}^{p}} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
Proof
[Start]43.8 $\frac{\left(e^{\mathsf{log1p}\left(a \cdot {a}^{p}\right)} - 1\right) - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$ $\frac{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(a \cdot {a}^{p}\right)\right)} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$ $\frac{\color{blue}{a \cdot {a}^{p}} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}$
3. Recombined 4 regimes into one program.
4. Final simplification8.1

$\leadsto \begin{array}{l} \mathbf{if}\;a \leq -2.4 \cdot 10^{-141}:\\ \;\;\;\;\frac{a \cdot {\left(\left|a\right|\right)}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}\\ \mathbf{elif}\;a \leq -1 \cdot 10^{-178}:\\ \;\;\;\;\mathsf{fma}\left(\frac{{\left(\left|a\right|\right)}^{p}}{a} - \frac{{\left(\left|b\right|\right)}^{p}}{a}, b, {\left(\left|a\right|\right)}^{p}\right) + \frac{\frac{{\left(\left|a\right|\right)}^{p}}{a} - \frac{{\left(\left|b\right|\right)}^{p}}{a}}{\frac{a}{b \cdot b}}\\ \mathbf{elif}\;a \leq 3.3 \cdot 10^{-144}:\\ \;\;\;\;{\left(\left|b\right|\right)}^{p}\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot {a}^{p} - b \cdot {\left(\left|b\right|\right)}^{p}}{a - b}\\ \end{array}$

# Alternatives

Alternative 1
Error8.1
Cost39432
$\begin{array}{l} t_0 := {\left(\left|a\right|\right)}^{p}\\ t_1 := {\left(\left|b\right|\right)}^{p}\\ t_2 := b \cdot t_1\\ \mathbf{if}\;a \leq -5.4 \cdot 10^{-141}:\\ \;\;\;\;\frac{a \cdot t_0 - t_2}{a - b}\\ \mathbf{elif}\;a \leq -1 \cdot 10^{-178}:\\ \;\;\;\;t_0 + b \cdot \frac{t_0 - t_1}{a}\\ \mathbf{elif}\;a \leq 7.2 \cdot 10^{-146}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot {a}^{p} - t_2}{a - b}\\ \end{array}$
Alternative 2
Error8.1
Cost26564
$\begin{array}{l} t_0 := {\left(\left|a\right|\right)}^{p}\\ t_1 := {\left(\left|b\right|\right)}^{p}\\ t_2 := b \cdot t_1\\ \mathbf{if}\;a \leq -5 \cdot 10^{-141}:\\ \;\;\;\;\frac{a \cdot t_0 - t_2}{a - b}\\ \mathbf{elif}\;a \leq -1 \cdot 10^{-178}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;a \leq 2.4 \cdot 10^{-145}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot {a}^{p} - t_2}{a - b}\\ \end{array}$
Alternative 3
Error10.8
Cost20560
$\begin{array}{l} t_0 := {\left(\left|b\right|\right)}^{p}\\ t_1 := {\left(\left|a\right|\right)}^{p}\\ \mathbf{if}\;b \leq -6.2 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq -3 \cdot 10^{-139}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq -1.25 \cdot 10^{-183}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 3.4 \cdot 10^{-153}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot t_1 - b \cdot {b}^{p}}{a - b}\\ \end{array}$
Alternative 4
Error10.9
Cost20296
$\begin{array}{l} t_0 := {\left(\left|b\right|\right)}^{p}\\ \mathbf{if}\;a \leq -1 \cdot 10^{-178}:\\ \;\;\;\;{\left(\left|a\right|\right)}^{p}\\ \mathbf{elif}\;a \leq 2.5 \cdot 10^{-146}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot {a}^{p} - b \cdot t_0}{a - b}\\ \end{array}$
Alternative 5
Error13.8
Cost14556
$\begin{array}{l} t_0 := {\left(\left|b\right|\right)}^{p}\\ t_1 := \left(a \cdot a + \left(b \cdot b + a \cdot b\right)\right) \cdot \frac{{b}^{p}}{b \cdot b}\\ t_2 := {\left(\left|a\right|\right)}^{p}\\ \mathbf{if}\;b \leq -1.55 \cdot 10^{-117}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq -1.3 \cdot 10^{-139}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq -1.25 \cdot 10^{-183}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 1.5 \cdot 10^{-142}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 2.9 \cdot 10^{-110}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 9.6 \cdot 10^{-101}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 2.7 \cdot 10^{-77}:\\ \;\;\;\;\frac{a \cdot {a}^{p} - b \cdot {b}^{p}}{a - b}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array}$
Alternative 6
Error14.0
Cost14428
$\begin{array}{l} t_0 := {\left(\left|b\right|\right)}^{p}\\ t_1 := \left(a \cdot a + \left(b \cdot b + a \cdot b\right)\right) \cdot \frac{{b}^{p}}{b \cdot b}\\ t_2 := {\left(\left|a\right|\right)}^{p}\\ \mathbf{if}\;b \leq -1.8 \cdot 10^{-116}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq -3.1 \cdot 10^{-139}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq -1.5 \cdot 10^{-183}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 3 \cdot 10^{-142}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 3.1 \cdot 10^{-111}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 1.3 \cdot 10^{-100}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \leq 2.7 \cdot 10^{-77}:\\ \;\;\;\;{a}^{p} - \frac{b}{\frac{a - b}{{b}^{p}}}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array}$
Alternative 7
Error14.6
Cost14284
$\begin{array}{l} \mathbf{if}\;a \leq -1 \cdot 10^{-178}:\\ \;\;\;\;{\left(\left|a\right|\right)}^{p}\\ \mathbf{elif}\;a \leq 7.7 \cdot 10^{-199}:\\ \;\;\;\;{\left(\left|b\right|\right)}^{p}\\ \mathbf{elif}\;a \leq 6.5 \cdot 10^{-112}:\\ \;\;\;\;\frac{a}{\frac{a - b}{{a}^{p}}} - \frac{b}{\frac{a - b}{{b}^{p}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{{a}^{p}}{a \cdot a} \cdot \left(a \cdot a + \left(b \cdot b + a \cdot b\right)\right)\\ \end{array}$
Alternative 8
Error12.8
Cost13456
$\begin{array}{l} t_0 := {\left(\left|b\right|\right)}^{p}\\ t_1 := {\left(\left|a\right|\right)}^{p}\\ \mathbf{if}\;b \leq -1.6 \cdot 10^{-117}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq -1.8 \cdot 10^{-139}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq -1.5 \cdot 10^{-183}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 1.55 \cdot 10^{-142}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a + \left(b \cdot b + a \cdot b\right)\right) \cdot \frac{{b}^{p}}{b \cdot b}\\ \end{array}$
Alternative 9
Error20.4
Cost13060
$\begin{array}{l} \mathbf{if}\;b \leq 9 \cdot 10^{-143}:\\ \;\;\;\;{\left(\left|a\right|\right)}^{p}\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a + \left(b \cdot b + a \cdot b\right)\right) \cdot \frac{{b}^{p}}{b \cdot b}\\ \end{array}$
Alternative 10
Error37.0
Cost7948
$\begin{array}{l} \mathbf{if}\;b \leq 1.5 \cdot 10^{-291}:\\ \;\;\;\;{a}^{p}\\ \mathbf{elif}\;b \leq 9.6 \cdot 10^{-218}:\\ \;\;\;\;{b}^{p}\\ \mathbf{elif}\;b \leq 1.9 \cdot 10^{-156}:\\ \;\;\;\;{a}^{p}\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a + \left(b \cdot b + a \cdot b\right)\right) \cdot \frac{{b}^{p}}{b \cdot b}\\ \end{array}$
Alternative 11
Error37.4
Cost6925
$\begin{array}{l} \mathbf{if}\;b \leq 1.65 \cdot 10^{-293} \lor \neg \left(b \leq 1.3 \cdot 10^{-217}\right) \land b \leq 1.25 \cdot 10^{-156}:\\ \;\;\;\;{a}^{p}\\ \mathbf{else}:\\ \;\;\;\;{b}^{p}\\ \end{array}$
Alternative 12
Error45.0
Cost6660
$\begin{array}{l} \mathbf{if}\;a \leq 1.6 \cdot 10^{-303}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;{a}^{p}\\ \end{array}$
Alternative 13
Error60.3
Cost64
$1$

# Reproduce?

herbie shell --seed 1
(FPCore (a p b)
:name "(a*abs(a)^p- b*abs(b)^p) / (a-b)"
:precision binary64
:pre (and (and (and (<= -10.0 a) (<= a 10.0)) (and (<= 1.0 p) (<= p 2.0))) (and (<= -101.0 b) (<= b 10.0)))
(/ (- (* a (pow (fabs a) p)) (* b (pow (fabs b) p))) (- a b)))