?

Average Error: 26.0 → 13.4
Time: 16.2s
Precision: binary64
Cost: 2064

?

\[\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 := a \cdot \frac{-d}{t_0}\\ t_2 := \frac{b}{c} + t_1\\ t_3 := b \cdot \frac{c}{t_0}\\ \mathbf{if}\;c \leq -3.6 \cdot 10^{+68}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq -8.2 \cdot 10^{-122}:\\ \;\;\;\;t_1 - c \cdot \frac{-b}{t_0}\\ \mathbf{elif}\;c \leq 1.12 \cdot 10^{-112}:\\ \;\;\;\;t_3 + a \cdot \frac{-1}{d}\\ \mathbf{elif}\;c \leq 3 \cdot 10^{+153}:\\ \;\;\;\;t_3 + t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \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 (* a (/ (- d) t_0)))
        (t_2 (+ (/ b c) t_1))
        (t_3 (* b (/ c t_0))))
   (if (<= c -3.6e+68)
     t_2
     (if (<= c -8.2e-122)
       (- t_1 (* c (/ (- b) t_0)))
       (if (<= c 1.12e-112)
         (+ t_3 (* a (/ -1.0 d)))
         (if (<= c 3e+153) (+ t_3 t_1) t_2))))))
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 * (-d / t_0);
	double t_2 = (b / c) + t_1;
	double t_3 = b * (c / t_0);
	double tmp;
	if (c <= -3.6e+68) {
		tmp = t_2;
	} else if (c <= -8.2e-122) {
		tmp = t_1 - (c * (-b / t_0));
	} else if (c <= 1.12e-112) {
		tmp = t_3 + (a * (-1.0 / d));
	} else if (c <= 3e+153) {
		tmp = t_3 + t_1;
	} else {
		tmp = t_2;
	}
	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) :: t_3
    real(8) :: tmp
    t_0 = (c * c) + (d * d)
    t_1 = a * (-d / t_0)
    t_2 = (b / c) + t_1
    t_3 = b * (c / t_0)
    if (c <= (-3.6d+68)) then
        tmp = t_2
    else if (c <= (-8.2d-122)) then
        tmp = t_1 - (c * (-b / t_0))
    else if (c <= 1.12d-112) then
        tmp = t_3 + (a * ((-1.0d0) / d))
    else if (c <= 3d+153) then
        tmp = t_3 + t_1
    else
        tmp = t_2
    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 * (-d / t_0);
	double t_2 = (b / c) + t_1;
	double t_3 = b * (c / t_0);
	double tmp;
	if (c <= -3.6e+68) {
		tmp = t_2;
	} else if (c <= -8.2e-122) {
		tmp = t_1 - (c * (-b / t_0));
	} else if (c <= 1.12e-112) {
		tmp = t_3 + (a * (-1.0 / d));
	} else if (c <= 3e+153) {
		tmp = t_3 + t_1;
	} else {
		tmp = t_2;
	}
	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 * (-d / t_0)
	t_2 = (b / c) + t_1
	t_3 = b * (c / t_0)
	tmp = 0
	if c <= -3.6e+68:
		tmp = t_2
	elif c <= -8.2e-122:
		tmp = t_1 - (c * (-b / t_0))
	elif c <= 1.12e-112:
		tmp = t_3 + (a * (-1.0 / d))
	elif c <= 3e+153:
		tmp = t_3 + t_1
	else:
		tmp = t_2
	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(a * Float64(Float64(-d) / t_0))
	t_2 = Float64(Float64(b / c) + t_1)
	t_3 = Float64(b * Float64(c / t_0))
	tmp = 0.0
	if (c <= -3.6e+68)
		tmp = t_2;
	elseif (c <= -8.2e-122)
		tmp = Float64(t_1 - Float64(c * Float64(Float64(-b) / t_0)));
	elseif (c <= 1.12e-112)
		tmp = Float64(t_3 + Float64(a * Float64(-1.0 / d)));
	elseif (c <= 3e+153)
		tmp = Float64(t_3 + t_1);
	else
		tmp = t_2;
	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 * (-d / t_0);
	t_2 = (b / c) + t_1;
	t_3 = b * (c / t_0);
	tmp = 0.0;
	if (c <= -3.6e+68)
		tmp = t_2;
	elseif (c <= -8.2e-122)
		tmp = t_1 - (c * (-b / t_0));
	elseif (c <= 1.12e-112)
		tmp = t_3 + (a * (-1.0 / d));
	elseif (c <= 3e+153)
		tmp = t_3 + t_1;
	else
		tmp = t_2;
	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[(a * N[((-d) / t$95$0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(b / c), $MachinePrecision] + t$95$1), $MachinePrecision]}, Block[{t$95$3 = N[(b * N[(c / t$95$0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -3.6e+68], t$95$2, If[LessEqual[c, -8.2e-122], N[(t$95$1 - N[(c * N[((-b) / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.12e-112], N[(t$95$3 + N[(a * N[(-1.0 / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3e+153], N[(t$95$3 + t$95$1), $MachinePrecision], t$95$2]]]]]]]]
\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 := a \cdot \frac{-d}{t_0}\\
t_2 := \frac{b}{c} + t_1\\
t_3 := b \cdot \frac{c}{t_0}\\
\mathbf{if}\;c \leq -3.6 \cdot 10^{+68}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;c \leq -8.2 \cdot 10^{-122}:\\
\;\;\;\;t_1 - c \cdot \frac{-b}{t_0}\\

\mathbf{elif}\;c \leq 1.12 \cdot 10^{-112}:\\
\;\;\;\;t_3 + a \cdot \frac{-1}{d}\\

\mathbf{elif}\;c \leq 3 \cdot 10^{+153}:\\
\;\;\;\;t_3 + t_1\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.0
Target0.5
Herbie13.4
\[\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 c < -3.5999999999999999e68 or 3.00000000000000019e153 < c

    1. Initial program 39.6

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

      \[\leadsto \color{blue}{b \cdot \frac{c}{c \cdot c + d \cdot d} + a \cdot \frac{-d}{c \cdot c + d \cdot d}} \]
    3. Taylor expanded in c around inf 14.5

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

    if -3.5999999999999999e68 < c < -8.2000000000000001e-122

    1. Initial program 14.4

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

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

    if -8.2000000000000001e-122 < c < 1.12e-112

    1. Initial program 22.6

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

      \[\leadsto \color{blue}{b \cdot \frac{c}{c \cdot c + d \cdot d} + a \cdot \frac{-d}{c \cdot c + d \cdot d}} \]
    3. Taylor expanded in d around inf 10.8

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

    if 1.12e-112 < c < 3.00000000000000019e153

    1. Initial program 18.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -3.6 \cdot 10^{+68}:\\ \;\;\;\;\frac{b}{c} + a \cdot \frac{-d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq -8.2 \cdot 10^{-122}:\\ \;\;\;\;a \cdot \frac{-d}{c \cdot c + d \cdot d} - c \cdot \frac{-b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.12 \cdot 10^{-112}:\\ \;\;\;\;b \cdot \frac{c}{c \cdot c + d \cdot d} + a \cdot \frac{-1}{d}\\ \mathbf{elif}\;c \leq 3 \cdot 10^{+153}:\\ \;\;\;\;b \cdot \frac{c}{c \cdot c + d \cdot d} + a \cdot \frac{-d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} + a \cdot \frac{-d}{c \cdot c + d \cdot d}\\ \end{array} \]

Alternatives

Alternative 1
Error13.0
Cost2064
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := b \cdot \frac{c}{t_0}\\ t_2 := a \cdot \frac{-d}{t_0}\\ t_3 := t_1 + t_2\\ t_4 := \frac{b}{c} + t_2\\ \mathbf{if}\;c \leq -7.2 \cdot 10^{+77}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;c \leq -2.5 \cdot 10^{-133}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;c \leq 2.7 \cdot 10^{-110}:\\ \;\;\;\;t_1 + a \cdot \frac{-1}{d}\\ \mathbf{elif}\;c \leq 1.3 \cdot 10^{+154}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t_4\\ \end{array} \]
Alternative 2
Error15.8
Cost1616
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{a}{-d}\\ t_2 := b \cdot c - a \cdot d\\ \mathbf{if}\;d \leq -2.8 \cdot 10^{+110}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -6.8 \cdot 10^{-16}:\\ \;\;\;\;\frac{t_2}{t_0}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{+16}:\\ \;\;\;\;\frac{b}{c} + a \cdot \frac{-d}{t_0}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+132}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error14.2
Cost1552
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{b \cdot c - a \cdot d}{t_0}\\ t_2 := \frac{b}{c} + a \cdot \frac{-d}{t_0}\\ \mathbf{if}\;c \leq -7.6 \cdot 10^{+63}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq -1.06 \cdot 10^{-131}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 7.2 \cdot 10^{-81}:\\ \;\;\;\;b \cdot \frac{c}{t_0} + a \cdot \frac{-1}{d}\\ \mathbf{elif}\;c \leq 2.5 \cdot 10^{+148}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 4
Error23.8
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ \mathbf{if}\;c \leq -1.15 \cdot 10^{+66}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -3300000000000:\\ \;\;\;\;\frac{d}{t_0} \cdot \left(-a\right)\\ \mathbf{elif}\;c \leq -1.2 \cdot 10^{-10}:\\ \;\;\;\;\frac{b}{c} + a \cdot \frac{-1}{d}\\ \mathbf{elif}\;c \leq -2.05 \cdot 10^{-67}:\\ \;\;\;\;\frac{b}{t_0} \cdot c\\ \mathbf{elif}\;c \leq 2.75 \cdot 10^{-132}:\\ \;\;\;\;\frac{a}{-d}\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{+146}:\\ \;\;\;\;\frac{c \cdot b}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 5
Error15.9
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -3.7 \cdot 10^{+100}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -2.2 \cdot 10^{-186}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.5 \cdot 10^{-198}:\\ \;\;\;\;\frac{a}{-d}\\ \mathbf{elif}\;c \leq 6.5 \cdot 10^{+147}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 6
Error15.8
Cost1488
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{a}{-d}\\ t_2 := \frac{b \cdot c - a \cdot d}{t_0}\\ \mathbf{if}\;d \leq -1.45 \cdot 10^{+112}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -6.8 \cdot 10^{-16}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{+16}:\\ \;\;\;\;\frac{b}{c} + a \cdot \frac{-d}{t_0}\\ \mathbf{elif}\;d \leq 3.7 \cdot 10^{+131}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Error22.9
Cost1100
\[\begin{array}{l} \mathbf{if}\;c \leq -9 \cdot 10^{+66}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{-128}:\\ \;\;\;\;\frac{a}{-d}\\ \mathbf{elif}\;c \leq 1.7 \cdot 10^{+153}:\\ \;\;\;\;\frac{c}{c \cdot c + d \cdot d} \cdot b\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 8
Error23.5
Cost1100
\[\begin{array}{l} \mathbf{if}\;c \leq -1.2 \cdot 10^{+65}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{-128}:\\ \;\;\;\;\frac{a}{-d}\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{+145}:\\ \;\;\;\;\frac{c \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Error23.5
Cost968
\[\begin{array}{l} t_0 := \frac{a}{-d}\\ \mathbf{if}\;d \leq -5 \cdot 10^{+106}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2.35 \cdot 10^{+73}:\\ \;\;\;\;\frac{b}{c \cdot c + d \cdot d} \cdot c\\ \mathbf{elif}\;d \leq -0.0004:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.6 \cdot 10^{+33}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error23.2
Cost520
\[\begin{array}{l} t_0 := \frac{a}{-d}\\ \mathbf{if}\;d \leq -0.24:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.02 \cdot 10^{+34}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 11
Error37.8
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

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