| Alternative 1 | |
|---|---|
| Error | 14.4 |
| Cost | 7436 |
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(t_1 (+ (/ b d) (* c (/ a (pow d 2.0))))))
(if (<= d -5.5e+64)
t_1
(if (<= d -1.45e-114)
t_0
(if (<= d 1.18e-113)
(+ (/ a c) (/ (* d b) (pow c 2.0)))
(if (<= d 1.3e+89) t_0 t_1))))))double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_1 = (b / d) + (c * (a / pow(d, 2.0)));
double tmp;
if (d <= -5.5e+64) {
tmp = t_1;
} else if (d <= -1.45e-114) {
tmp = t_0;
} else if (d <= 1.18e-113) {
tmp = (a / c) + ((d * b) / pow(c, 2.0));
} else if (d <= 1.3e+89) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
t_1 = (b / d) + (c * (a / (d ** 2.0d0)))
if (d <= (-5.5d+64)) then
tmp = t_1
else if (d <= (-1.45d-114)) then
tmp = t_0
else if (d <= 1.18d-113) then
tmp = (a / c) + ((d * b) / (c ** 2.0d0))
else if (d <= 1.3d+89) then
tmp = t_0
else
tmp = t_1
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_1 = (b / d) + (c * (a / Math.pow(d, 2.0)));
double tmp;
if (d <= -5.5e+64) {
tmp = t_1;
} else if (d <= -1.45e-114) {
tmp = t_0;
} else if (d <= 1.18e-113) {
tmp = (a / c) + ((d * b) / Math.pow(c, 2.0));
} else if (d <= 1.3e+89) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
def code(a, b, c, d): t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d)) t_1 = (b / d) + (c * (a / math.pow(d, 2.0))) tmp = 0 if d <= -5.5e+64: tmp = t_1 elif d <= -1.45e-114: tmp = t_0 elif d <= 1.18e-113: tmp = (a / c) + ((d * b) / math.pow(c, 2.0)) elif d <= 1.3e+89: tmp = t_0 else: tmp = t_1 return tmp
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function code(a, b, c, d) t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) t_1 = Float64(Float64(b / d) + Float64(c * Float64(a / (d ^ 2.0)))) tmp = 0.0 if (d <= -5.5e+64) tmp = t_1; elseif (d <= -1.45e-114) tmp = t_0; elseif (d <= 1.18e-113) tmp = Float64(Float64(a / c) + Float64(Float64(d * b) / (c ^ 2.0))); elseif (d <= 1.3e+89) tmp = t_0; else tmp = t_1; end return tmp end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
function tmp_2 = code(a, b, c, d) t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d)); t_1 = (b / d) + (c * (a / (d ^ 2.0))); tmp = 0.0; if (d <= -5.5e+64) tmp = t_1; elseif (d <= -1.45e-114) tmp = t_0; elseif (d <= 1.18e-113) tmp = (a / c) + ((d * b) / (c ^ 2.0)); elseif (d <= 1.3e+89) tmp = t_0; else tmp = t_1; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b / d), $MachinePrecision] + N[(c * N[(a / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -5.5e+64], t$95$1, If[LessEqual[d, -1.45e-114], t$95$0, If[LessEqual[d, 1.18e-113], N[(N[(a / c), $MachinePrecision] + N[(N[(d * b), $MachinePrecision] / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.3e+89], t$95$0, t$95$1]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
t_1 := \frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\
\mathbf{if}\;d \leq -5.5 \cdot 10^{+64}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;d \leq -1.45 \cdot 10^{-114}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq 1.18 \cdot 10^{-113}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\
\mathbf{elif}\;d \leq 1.3 \cdot 10^{+89}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
Results
| Original | 26.3 |
|---|---|
| Target | 0.5 |
| Herbie | 14.1 |
if d < -5.4999999999999996e64 or 1.3e89 < d Initial program 37.4
Applied egg-rr37.4
Taylor expanded in c around 0 15.2
Simplified15.2
[Start]15.2 | \[ -0.5 \cdot \left(c \cdot \left(-1 \cdot \frac{a}{{d}^{2}} - \frac{a}{{d}^{2}}\right)\right) + \frac{b}{d}
\] |
|---|---|
rational_best-simplify-1 [=>]15.2 | \[ \color{blue}{\frac{b}{d} + -0.5 \cdot \left(c \cdot \left(-1 \cdot \frac{a}{{d}^{2}} - \frac{a}{{d}^{2}}\right)\right)}
\] |
rational_best-simplify-113 [=>]15.2 | \[ \frac{b}{d} + \color{blue}{c \cdot \left(-0.5 \cdot \left(-1 \cdot \frac{a}{{d}^{2}} - \frac{a}{{d}^{2}}\right)\right)}
\] |
rational_best-simplify-3 [=>]15.2 | \[ \frac{b}{d} + c \cdot \left(-0.5 \cdot \left(\color{blue}{\frac{a}{{d}^{2}} \cdot -1} - \frac{a}{{d}^{2}}\right)\right)
\] |
rational_best-simplify-17 [=>]15.2 | \[ \frac{b}{d} + c \cdot \left(-0.5 \cdot \left(\color{blue}{\left(-\frac{a}{{d}^{2}}\right)} - \frac{a}{{d}^{2}}\right)\right)
\] |
Applied egg-rr15.2
Simplified15.2
[Start]15.2 | \[ \frac{b}{d} + \left(\frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right) + \frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right)\right)
\] |
|---|---|
rational_best-simplify-62 [=>]15.2 | \[ \frac{b}{d} + \color{blue}{\left(\frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right) - \left(-\frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right)\right)\right)}
\] |
rational_best-simplify-113 [=>]15.2 | \[ \frac{b}{d} + \left(\color{blue}{c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right)} - \left(-\frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right)\right)\right)
\] |
rational_best-simplify-3 [=>]15.2 | \[ \frac{b}{d} + \left(c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right) - \left(-\color{blue}{\left(c \cdot 0.5\right) \cdot \frac{a}{{d}^{2}}}\right)\right)
\] |
rational_best-simplify-52 [=>]15.2 | \[ \frac{b}{d} + \left(c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right) - \color{blue}{\frac{a}{{d}^{2}} \cdot \left(-c \cdot 0.5\right)}\right)
\] |
rational_best-simplify-3 [=>]15.2 | \[ \frac{b}{d} + \left(c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right) - \frac{a}{{d}^{2}} \cdot \left(-\color{blue}{0.5 \cdot c}\right)\right)
\] |
rational_best-simplify-52 [=>]15.2 | \[ \frac{b}{d} + \left(c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right) - \frac{a}{{d}^{2}} \cdot \color{blue}{\left(c \cdot \left(-0.5\right)\right)}\right)
\] |
metadata-eval [=>]15.2 | \[ \frac{b}{d} + \left(c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right) - \frac{a}{{d}^{2}} \cdot \left(c \cdot \color{blue}{-0.5}\right)\right)
\] |
rational_best-simplify-113 [=>]15.2 | \[ \frac{b}{d} + \left(c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5\right) - \color{blue}{c \cdot \left(\frac{a}{{d}^{2}} \cdot -0.5\right)}\right)
\] |
rational_best-simplify-110 [=>]15.2 | \[ \frac{b}{d} + \color{blue}{c \cdot \left(\frac{a}{{d}^{2}} \cdot 0.5 - \frac{a}{{d}^{2}} \cdot -0.5\right)}
\] |
rational_best-simplify-110 [=>]15.2 | \[ \frac{b}{d} + c \cdot \color{blue}{\left(\frac{a}{{d}^{2}} \cdot \left(0.5 - -0.5\right)\right)}
\] |
metadata-eval [=>]15.2 | \[ \frac{b}{d} + c \cdot \left(\frac{a}{{d}^{2}} \cdot \color{blue}{1}\right)
\] |
rational_best-simplify-3 [<=]15.2 | \[ \frac{b}{d} + c \cdot \color{blue}{\left(1 \cdot \frac{a}{{d}^{2}}\right)}
\] |
rational_best-simplify-11 [<=]15.2 | \[ \frac{b}{d} + c \cdot \color{blue}{\begin{array}{l}
\color{blue}{\mathbf{if}\;-1 \ne 0:\\
\;\;\;\;\frac{a}{{d}^{2}}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{{d}^{2}}\\
}
\end{array}}
\] |
rational_best-simplify-5 [=>]15.2 | \[ \frac{b}{d} + c \cdot \color{blue}{\frac{a}{{d}^{2}}}
\] |
if -5.4999999999999996e64 < d < -1.44999999999999998e-114 or 1.18e-113 < d < 1.3e89Initial program 15.6
if -1.44999999999999998e-114 < d < 1.18e-113Initial program 22.8
Taylor expanded in c around inf 10.9
Final simplification14.1
| Alternative 1 | |
|---|---|
| Error | 14.4 |
| Cost | 7436 |
| Alternative 2 | |
|---|---|
| Error | 16.1 |
| Cost | 1488 |
| Alternative 3 | |
|---|---|
| Error | 22.2 |
| Cost | 1232 |
| Alternative 4 | |
|---|---|
| Error | 23.3 |
| Cost | 1100 |
| Alternative 5 | |
|---|---|
| Error | 24.4 |
| Cost | 720 |
| Alternative 6 | |
|---|---|
| Error | 37.6 |
| Cost | 192 |
herbie shell --seed 2023104
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:herbie-target
(if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))