?

Average Error: 26.5 → 11.2
Time: 1.8min
Precision: binary64
Cost: 1996

?

\[\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 := -1 \cdot \frac{a}{d} + \frac{c}{d} \cdot \frac{b}{d}\\ t_2 := b \cdot c - a \cdot d\\ \mathbf{if}\;d \leq -1.4 \cdot 10^{+121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -4.7 \cdot 10^{-152}:\\ \;\;\;\;\frac{\begin{array}{l} \mathbf{if}\;t_2 \ne 0:\\ \;\;\;\;\frac{1}{\frac{1}{t_2}}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array}}{t_0}\\ \mathbf{elif}\;d \leq 1.4 \cdot 10^{-164}:\\ \;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\ \mathbf{elif}\;d \leq 7 \cdot 10^{+92}:\\ \;\;\;\;\frac{t_2}{t_0}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
(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 (+ (* -1.0 (/ a d)) (* (/ c d) (/ b d))))
        (t_2 (- (* b c) (* a d))))
   (if (<= d -1.4e+121)
     t_1
     (if (<= d -4.7e-152)
       (/ (if (!= t_2 0.0) (/ 1.0 (/ 1.0 t_2)) t_2) t_0)
       (if (<= d 1.4e-164)
         (/ (- b (/ (* a d) c)) c)
         (if (<= d 7e+92) (/ t_2 t_0) 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 = (-1.0 * (a / d)) + ((c / d) * (b / d));
	double t_2 = (b * c) - (a * d);
	double tmp;
	if (d <= -1.4e+121) {
		tmp = t_1;
	} else if (d <= -4.7e-152) {
		double tmp_1;
		if (t_2 != 0.0) {
			tmp_1 = 1.0 / (1.0 / t_2);
		} else {
			tmp_1 = t_2;
		}
		tmp = tmp_1 / t_0;
	} else if (d <= 1.4e-164) {
		tmp = (b - ((a * d) / c)) / c;
	} else if (d <= 7e+92) {
		tmp = t_2 / 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 = ((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) :: t_2
    real(8) :: tmp
    real(8) :: tmp_1
    t_0 = (c * c) + (d * d)
    t_1 = ((-1.0d0) * (a / d)) + ((c / d) * (b / d))
    t_2 = (b * c) - (a * d)
    if (d <= (-1.4d+121)) then
        tmp = t_1
    else if (d <= (-4.7d-152)) then
        if (t_2 /= 0.0d0) then
            tmp_1 = 1.0d0 / (1.0d0 / t_2)
        else
            tmp_1 = t_2
        end if
        tmp = tmp_1 / t_0
    else if (d <= 1.4d-164) then
        tmp = (b - ((a * d) / c)) / c
    else if (d <= 7d+92) then
        tmp = t_2 / 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 ((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 = (-1.0 * (a / d)) + ((c / d) * (b / d));
	double t_2 = (b * c) - (a * d);
	double tmp;
	if (d <= -1.4e+121) {
		tmp = t_1;
	} else if (d <= -4.7e-152) {
		double tmp_1;
		if (t_2 != 0.0) {
			tmp_1 = 1.0 / (1.0 / t_2);
		} else {
			tmp_1 = t_2;
		}
		tmp = tmp_1 / t_0;
	} else if (d <= 1.4e-164) {
		tmp = (b - ((a * d) / c)) / c;
	} else if (d <= 7e+92) {
		tmp = t_2 / t_0;
	} 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 = (-1.0 * (a / d)) + ((c / d) * (b / d))
	t_2 = (b * c) - (a * d)
	tmp = 0
	if d <= -1.4e+121:
		tmp = t_1
	elif d <= -4.7e-152:
		tmp_1 = 0
		if t_2 != 0.0:
			tmp_1 = 1.0 / (1.0 / t_2)
		else:
			tmp_1 = t_2
		tmp = tmp_1 / t_0
	elif d <= 1.4e-164:
		tmp = (b - ((a * d) / c)) / c
	elif d <= 7e+92:
		tmp = t_2 / t_0
	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(-1.0 * Float64(a / d)) + Float64(Float64(c / d) * Float64(b / d)))
	t_2 = Float64(Float64(b * c) - Float64(a * d))
	tmp = 0.0
	if (d <= -1.4e+121)
		tmp = t_1;
	elseif (d <= -4.7e-152)
		tmp_1 = 0.0
		if (t_2 != 0.0)
			tmp_1 = Float64(1.0 / Float64(1.0 / t_2));
		else
			tmp_1 = t_2;
		end
		tmp = Float64(tmp_1 / t_0);
	elseif (d <= 1.4e-164)
		tmp = Float64(Float64(b - Float64(Float64(a * d) / c)) / c);
	elseif (d <= 7e+92)
		tmp = Float64(t_2 / t_0);
	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_3 = code(a, b, c, d)
	t_0 = (c * c) + (d * d);
	t_1 = (-1.0 * (a / d)) + ((c / d) * (b / d));
	t_2 = (b * c) - (a * d);
	tmp = 0.0;
	if (d <= -1.4e+121)
		tmp = t_1;
	elseif (d <= -4.7e-152)
		tmp_2 = 0.0;
		if (t_2 ~= 0.0)
			tmp_2 = 1.0 / (1.0 / t_2);
		else
			tmp_2 = t_2;
		end
		tmp = tmp_2 / t_0;
	elseif (d <= 1.4e-164)
		tmp = (b - ((a * d) / c)) / c;
	elseif (d <= 7e+92)
		tmp = t_2 / t_0;
	else
		tmp = t_1;
	end
	tmp_3 = 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[(-1.0 * N[(a / d), $MachinePrecision]), $MachinePrecision] + N[(N[(c / d), $MachinePrecision] * N[(b / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -1.4e+121], t$95$1, If[LessEqual[d, -4.7e-152], N[(If[Unequal[t$95$2, 0.0], N[(1.0 / N[(1.0 / t$95$2), $MachinePrecision]), $MachinePrecision], t$95$2] / t$95$0), $MachinePrecision], If[LessEqual[d, 1.4e-164], N[(N[(b - N[(N[(a * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 7e+92], N[(t$95$2 / t$95$0), $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 := -1 \cdot \frac{a}{d} + \frac{c}{d} \cdot \frac{b}{d}\\
t_2 := b \cdot c - a \cdot d\\
\mathbf{if}\;d \leq -1.4 \cdot 10^{+121}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq -4.7 \cdot 10^{-152}:\\
\;\;\;\;\frac{\begin{array}{l}
\mathbf{if}\;t_2 \ne 0:\\
\;\;\;\;\frac{1}{\frac{1}{t_2}}\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}}{t_0}\\

\mathbf{elif}\;d \leq 1.4 \cdot 10^{-164}:\\
\;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\

\mathbf{elif}\;d \leq 7 \cdot 10^{+92}:\\
\;\;\;\;\frac{t_2}{t_0}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error?

Target

Original26.5
Target0.4
Herbie11.2
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation?

  1. Split input into 4 regimes
  2. if d < -1.40000000000000003e121 or 6.99999999999999972e92 < d

    1. Initial program 40.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in d around inf 14.4

      \[\leadsto \color{blue}{-1 \cdot \frac{a}{d} + c \cdot \left({\left(\frac{1}{d}\right)}^{2} \cdot b\right)} \]
    3. Applied egg-rr11.1

      \[\leadsto -1 \cdot \frac{a}{d} + \color{blue}{\frac{\frac{b}{d}}{d} \cdot c} \]
    4. Applied egg-rr9.4

      \[\leadsto -1 \cdot \frac{a}{d} + \color{blue}{\frac{c}{d} \cdot \frac{b}{d}} \]

    if -1.40000000000000003e121 < d < -4.70000000000000012e-152

    1. Initial program 16.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr16.5

      \[\leadsto \frac{\color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;b \cdot c - a \cdot d \ne 0:\\ \;\;\;\;\frac{1}{\frac{1}{b \cdot c - a \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;b \cdot c - a \cdot d\\ } \end{array}}}{c \cdot c + d \cdot d} \]

    if -4.70000000000000012e-152 < d < 1.4000000000000001e-164

    1. Initial program 24.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around inf 10.7

      \[\leadsto \color{blue}{\frac{b}{c} + -1 \cdot \left(a \cdot \left(d \cdot {\left(\frac{1}{c}\right)}^{2}\right)\right)} \]
    3. Simplified10.7

      \[\leadsto \color{blue}{\frac{b}{c} - a \cdot \left(d \cdot \frac{{c}^{-1}}{c}\right)} \]
      Proof
    4. Taylor expanded in b around 0 9.6

      \[\leadsto \color{blue}{-1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}} \]
    5. Simplified5.4

      \[\leadsto \color{blue}{\frac{b - \frac{a \cdot d}{c}}{c}} \]
      Proof

    if 1.4000000000000001e-164 < d < 6.99999999999999972e92

    1. Initial program 14.8

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
  3. Recombined 4 regimes into one program.

Alternatives

Alternative 1
Error11.2
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ t_1 := -1 \cdot \frac{a}{d} + \frac{c}{d} \cdot \frac{b}{d}\\ \mathbf{if}\;d \leq -1.15 \cdot 10^{+122}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{-152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.4 \cdot 10^{-164}:\\ \;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\ \mathbf{elif}\;d \leq 9 \cdot 10^{+92}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error15.6
Cost1232
\[\begin{array}{l} t_0 := \frac{\frac{-c}{d} \cdot b + a}{-d}\\ t_1 := \frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{if}\;c \leq -3.2 \cdot 10^{+66}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1.05 \cdot 10^{-32}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2.55 \cdot 10^{-54}:\\ \;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\ \mathbf{elif}\;c \leq 3.7 \cdot 10^{-13}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error16.0
Cost1232
\[\begin{array}{l} t_0 := \frac{a \cdot d}{c}\\ t_1 := \frac{\frac{-c}{d} \cdot b + a}{-d}\\ \mathbf{if}\;c \leq -6.8 \cdot 10^{+67}:\\ \;\;\;\;\frac{b}{c} - \begin{array}{l} \mathbf{if}\;a \ne 0:\\ \;\;\;\;\frac{d}{\frac{c}{a} \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{c}\\ \end{array}\\ \mathbf{elif}\;c \leq -3.9 \cdot 10^{-28}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -4.7 \cdot 10^{-54}:\\ \;\;\;\;\frac{b - t_0}{c}\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{-13}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{a}{c} \cdot d}{c}\\ \end{array} \]
Alternative 4
Error20.4
Cost1104
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ t_1 := \frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{if}\;c \leq -6 \cdot 10^{+65}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1.35 \cdot 10^{-27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.52 \cdot 10^{-65}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 3.9 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error20.4
Cost1104
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ t_1 := \frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{if}\;c \leq -1.7 \cdot 10^{+66}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -2.1 \cdot 10^{-29}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -7.2 \cdot 10^{-66}:\\ \;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\ \mathbf{elif}\;c \leq 5.6 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error15.6
Cost1104
\[\begin{array}{l} t_0 := \frac{\frac{c \cdot b}{d} - a}{d}\\ t_1 := \frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{if}\;c \leq -7.2 \cdot 10^{+51}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -9 \cdot 10^{-27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.46 \cdot 10^{-55}:\\ \;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\ \mathbf{elif}\;c \leq 5.6 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Error15.7
Cost1104
\[\begin{array}{l} t_0 := \frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{if}\;c \leq -5 \cdot 10^{+67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.7 \cdot 10^{-30}:\\ \;\;\;\;\frac{\frac{-b}{d} \cdot c + a}{-d}\\ \mathbf{elif}\;c \leq -2.25 \cdot 10^{-55}:\\ \;\;\;\;\frac{b - \frac{a \cdot d}{c}}{c}\\ \mathbf{elif}\;c \leq 5.4 \cdot 10^{-14}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error23.8
Cost784
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;c \leq -7 \cdot 10^{+66}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -1.05 \cdot 10^{-33}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2.5 \cdot 10^{-55}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 9.6 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Error37.9
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

herbie shell --seed 2023033 
(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))))