| Alternative 1 | |
|---|---|
| Error | 13.2 |
| Cost | 7500 |
(FPCore (a b c d) :precision binary64 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (+ (* c c) (* d d))) (t_1 (/ (+ (- a) (* c (/ b d))) d)))
(if (<= d -2.9e+60)
t_1
(if (<= d -1.46e-86)
(/ (- (* b c) (* a d)) t_0)
(if (<= d 6.1e-50)
(+ (/ b c) (/ (- d) (/ (pow c 2.0) a)))
(if (<= d 2.1e+103)
(/ 2.0 (* t_0 (/ 2.0 (- (* c b) (* d a)))))
t_1))))))double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
double t_0 = (c * c) + (d * d);
double t_1 = (-a + (c * (b / d))) / d;
double tmp;
if (d <= -2.9e+60) {
tmp = t_1;
} else if (d <= -1.46e-86) {
tmp = ((b * c) - (a * d)) / t_0;
} else if (d <= 6.1e-50) {
tmp = (b / c) + (-d / (pow(c, 2.0) / a));
} else if (d <= 2.1e+103) {
tmp = 2.0 / (t_0 * (2.0 / ((c * b) - (d * a))));
} 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 = ((b * c) - (a * 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 = (c * c) + (d * d)
t_1 = (-a + (c * (b / d))) / d
if (d <= (-2.9d+60)) then
tmp = t_1
else if (d <= (-1.46d-86)) then
tmp = ((b * c) - (a * d)) / t_0
else if (d <= 6.1d-50) then
tmp = (b / c) + (-d / ((c ** 2.0d0) / a))
else if (d <= 2.1d+103) then
tmp = 2.0d0 / (t_0 * (2.0d0 / ((c * b) - (d * a))))
else
tmp = t_1
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
double t_0 = (c * c) + (d * d);
double t_1 = (-a + (c * (b / d))) / d;
double tmp;
if (d <= -2.9e+60) {
tmp = t_1;
} else if (d <= -1.46e-86) {
tmp = ((b * c) - (a * d)) / t_0;
} else if (d <= 6.1e-50) {
tmp = (b / c) + (-d / (Math.pow(c, 2.0) / a));
} else if (d <= 2.1e+103) {
tmp = 2.0 / (t_0 * (2.0 / ((c * b) - (d * a))));
} else {
tmp = t_1;
}
return tmp;
}
def code(a, b, c, d): return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d): t_0 = (c * c) + (d * d) t_1 = (-a + (c * (b / d))) / d tmp = 0 if d <= -2.9e+60: tmp = t_1 elif d <= -1.46e-86: tmp = ((b * c) - (a * d)) / t_0 elif d <= 6.1e-50: tmp = (b / c) + (-d / (math.pow(c, 2.0) / a)) elif d <= 2.1e+103: tmp = 2.0 / (t_0 * (2.0 / ((c * b) - (d * a)))) else: tmp = t_1 return tmp
function code(a, b, c, d) return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function code(a, b, c, d) t_0 = Float64(Float64(c * c) + Float64(d * d)) t_1 = Float64(Float64(Float64(-a) + Float64(c * Float64(b / d))) / d) tmp = 0.0 if (d <= -2.9e+60) tmp = t_1; elseif (d <= -1.46e-86) tmp = Float64(Float64(Float64(b * c) - Float64(a * d)) / t_0); elseif (d <= 6.1e-50) tmp = Float64(Float64(b / c) + Float64(Float64(-d) / Float64((c ^ 2.0) / a))); elseif (d <= 2.1e+103) tmp = Float64(2.0 / Float64(t_0 * Float64(2.0 / Float64(Float64(c * b) - Float64(d * a))))); else tmp = t_1; end return tmp end
function tmp = code(a, b, c, d) tmp = ((b * c) - (a * d)) / ((c * c) + (d * d)); end
function tmp_2 = code(a, b, c, d) t_0 = (c * c) + (d * d); t_1 = (-a + (c * (b / d))) / d; tmp = 0.0; if (d <= -2.9e+60) tmp = t_1; elseif (d <= -1.46e-86) tmp = ((b * c) - (a * d)) / t_0; elseif (d <= 6.1e-50) tmp = (b / c) + (-d / ((c ^ 2.0) / a)); elseif (d <= 2.1e+103) tmp = 2.0 / (t_0 * (2.0 / ((c * b) - (d * a)))); else tmp = t_1; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * 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[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[((-a) + N[(c * N[(b / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -2.9e+60], t$95$1, If[LessEqual[d, -1.46e-86], N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision], If[LessEqual[d, 6.1e-50], N[(N[(b / c), $MachinePrecision] + N[((-d) / N[(N[Power[c, 2.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 2.1e+103], N[(2.0 / N[(t$95$0 * N[(2.0 / N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := c \cdot c + d \cdot d\\
t_1 := \frac{\left(-a\right) + c \cdot \frac{b}{d}}{d}\\
\mathbf{if}\;d \leq -2.9 \cdot 10^{+60}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;d \leq -1.46 \cdot 10^{-86}:\\
\;\;\;\;\frac{b \cdot c - a \cdot d}{t_0}\\
\mathbf{elif}\;d \leq 6.1 \cdot 10^{-50}:\\
\;\;\;\;\frac{b}{c} + \frac{-d}{\frac{{c}^{2}}{a}}\\
\mathbf{elif}\;d \leq 2.1 \cdot 10^{+103}:\\
\;\;\;\;\frac{2}{t_0 \cdot \frac{2}{c \cdot b - d \cdot a}}\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
Results
| Original | 25.4 |
|---|---|
| Target | 0.5 |
| Herbie | 14.0 |
if d < -2.9e60 or 2.1000000000000002e103 < d Initial program 36.7
Applied egg-rr50.0
Applied egg-rr50.6
Simplified48.7
[Start]50.6 | \[ \frac{1}{c \cdot c} \cdot \frac{b \cdot c - a \cdot d}{\left(d \cdot d\right) \cdot \left(\frac{1}{d \cdot d} + \frac{1}{c \cdot c}\right)}
\] |
|---|---|
rational.json-simplify-2 [=>]50.6 | \[ \frac{1}{c \cdot c} \cdot \frac{\color{blue}{c \cdot b} - a \cdot d}{\left(d \cdot d\right) \cdot \left(\frac{1}{d \cdot d} + \frac{1}{c \cdot c}\right)}
\] |
rational.json-simplify-2 [=>]50.6 | \[ \frac{1}{c \cdot c} \cdot \frac{c \cdot b - a \cdot d}{\color{blue}{\left(\frac{1}{d \cdot d} + \frac{1}{c \cdot c}\right) \cdot \left(d \cdot d\right)}}
\] |
rational.json-simplify-43 [=>]48.7 | \[ \frac{1}{c \cdot c} \cdot \frac{c \cdot b - a \cdot d}{\color{blue}{d \cdot \left(d \cdot \left(\frac{1}{d \cdot d} + \frac{1}{c \cdot c}\right)\right)}}
\] |
Applied egg-rr42.6
Taylor expanded in c around 0 13.9
Simplified10.6
[Start]13.9 | \[ \frac{\frac{c \cdot b}{d} + -1 \cdot a}{d}
\] |
|---|---|
rational.json-simplify-1 [=>]13.9 | \[ \frac{\color{blue}{-1 \cdot a + \frac{c \cdot b}{d}}}{d}
\] |
rational.json-simplify-2 [=>]13.9 | \[ \frac{\color{blue}{a \cdot -1} + \frac{c \cdot b}{d}}{d}
\] |
rational.json-simplify-9 [=>]13.9 | \[ \frac{\color{blue}{\left(-a\right)} + \frac{c \cdot b}{d}}{d}
\] |
rational.json-simplify-2 [=>]13.9 | \[ \frac{\left(-a\right) + \frac{\color{blue}{b \cdot c}}{d}}{d}
\] |
rational.json-simplify-49 [=>]10.6 | \[ \frac{\left(-a\right) + \color{blue}{c \cdot \frac{b}{d}}}{d}
\] |
if -2.9e60 < d < -1.45999999999999993e-86Initial program 15.6
if -1.45999999999999993e-86 < d < 6.0999999999999996e-50Initial program 19.7
Taylor expanded in c around inf 12.5
Simplified16.2
[Start]12.5 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
rational.json-simplify-1 [=>]12.5 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
rational.json-simplify-49 [=>]16.2 | \[ \frac{b}{c} + -1 \cdot \color{blue}{\left(d \cdot \frac{a}{{c}^{2}}\right)}
\] |
rational.json-simplify-43 [=>]16.2 | \[ \frac{b}{c} + \color{blue}{d \cdot \left(\frac{a}{{c}^{2}} \cdot -1\right)}
\] |
rational.json-simplify-9 [=>]16.2 | \[ \frac{b}{c} + d \cdot \color{blue}{\left(-\frac{a}{{c}^{2}}\right)}
\] |
Applied egg-rr15.8
if 6.0999999999999996e-50 < d < 2.1000000000000002e103Initial program 17.4
Applied egg-rr17.4
Applied egg-rr17.7
Final simplification14.0
| Alternative 1 | |
|---|---|
| Error | 13.2 |
| Cost | 7500 |
| Alternative 2 | |
|---|---|
| Error | 14.2 |
| Cost | 7500 |
| Alternative 3 | |
|---|---|
| Error | 20.8 |
| Cost | 1556 |
| Alternative 4 | |
|---|---|
| Error | 13.8 |
| Cost | 1488 |
| Alternative 5 | |
|---|---|
| Error | 20.8 |
| Cost | 1432 |
| Alternative 6 | |
|---|---|
| Error | 19.3 |
| Cost | 1168 |
| Alternative 7 | |
|---|---|
| Error | 19.3 |
| Cost | 1168 |
| Alternative 8 | |
|---|---|
| Error | 19.2 |
| Cost | 1168 |
| Alternative 9 | |
|---|---|
| Error | 19.2 |
| Cost | 904 |
| Alternative 10 | |
|---|---|
| Error | 24.2 |
| Cost | 520 |
| Alternative 11 | |
|---|---|
| Error | 37.2 |
| Cost | 192 |
herbie shell --seed 2023075
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:herbie-target
(if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))