?

Average Error: 26.0 → 15.5
Time: 10.0s
Precision: binary64
Cost: 7500

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ t_1 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -900000000000:\\ \;\;\;\;t_1 + b \cdot \frac{c}{{d}^{2}}\\ \mathbf{elif}\;d \leq -1.35 \cdot 10^{-139}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.6 \cdot 10^{-149}:\\ \;\;\;\;\frac{b}{c} + a \cdot \left(-\frac{d}{{c}^{2}}\right)\\ \mathbf{elif}\;d \leq 3.8 \cdot 10^{+108}:\\ \;\;\;\;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 (/ (- (* b c) (* a d)) (+ (* c c) (* d d)))) (t_1 (- (/ a d))))
   (if (<= d -900000000000.0)
     (+ t_1 (* b (/ c (pow d 2.0))))
     (if (<= d -1.35e-139)
       t_0
       (if (<= d 1.6e-149)
         (+ (/ b c) (* a (- (/ d (pow c 2.0)))))
         (if (<= d 3.8e+108) 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 = ((b * c) - (a * d)) / ((c * c) + (d * d));
	double t_1 = -(a / d);
	double tmp;
	if (d <= -900000000000.0) {
		tmp = t_1 + (b * (c / pow(d, 2.0)));
	} else if (d <= -1.35e-139) {
		tmp = t_0;
	} else if (d <= 1.6e-149) {
		tmp = (b / c) + (a * -(d / pow(c, 2.0)));
	} else if (d <= 3.8e+108) {
		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 = ((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 = ((b * c) - (a * d)) / ((c * c) + (d * d))
    t_1 = -(a / d)
    if (d <= (-900000000000.0d0)) then
        tmp = t_1 + (b * (c / (d ** 2.0d0)))
    else if (d <= (-1.35d-139)) then
        tmp = t_0
    else if (d <= 1.6d-149) then
        tmp = (b / c) + (a * -(d / (c ** 2.0d0)))
    else if (d <= 3.8d+108) 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 ((b * c) - (a * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double t_0 = ((b * c) - (a * d)) / ((c * c) + (d * d));
	double t_1 = -(a / d);
	double tmp;
	if (d <= -900000000000.0) {
		tmp = t_1 + (b * (c / Math.pow(d, 2.0)));
	} else if (d <= -1.35e-139) {
		tmp = t_0;
	} else if (d <= 1.6e-149) {
		tmp = (b / c) + (a * -(d / Math.pow(c, 2.0)));
	} else if (d <= 3.8e+108) {
		tmp = 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 = ((b * c) - (a * d)) / ((c * c) + (d * d))
	t_1 = -(a / d)
	tmp = 0
	if d <= -900000000000.0:
		tmp = t_1 + (b * (c / math.pow(d, 2.0)))
	elif d <= -1.35e-139:
		tmp = t_0
	elif d <= 1.6e-149:
		tmp = (b / c) + (a * -(d / math.pow(c, 2.0)))
	elif d <= 3.8e+108:
		tmp = 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(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d)))
	t_1 = Float64(-Float64(a / d))
	tmp = 0.0
	if (d <= -900000000000.0)
		tmp = Float64(t_1 + Float64(b * Float64(c / (d ^ 2.0))));
	elseif (d <= -1.35e-139)
		tmp = t_0;
	elseif (d <= 1.6e-149)
		tmp = Float64(Float64(b / c) + Float64(a * Float64(-Float64(d / (c ^ 2.0)))));
	elseif (d <= 3.8e+108)
		tmp = 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_2 = code(a, b, c, d)
	t_0 = ((b * c) - (a * d)) / ((c * c) + (d * d));
	t_1 = -(a / d);
	tmp = 0.0;
	if (d <= -900000000000.0)
		tmp = t_1 + (b * (c / (d ^ 2.0)));
	elseif (d <= -1.35e-139)
		tmp = t_0;
	elseif (d <= 1.6e-149)
		tmp = (b / c) + (a * -(d / (c ^ 2.0)));
	elseif (d <= 3.8e+108)
		tmp = t_0;
	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[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = (-N[(a / d), $MachinePrecision])}, If[LessEqual[d, -900000000000.0], N[(t$95$1 + N[(b * N[(c / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -1.35e-139], t$95$0, If[LessEqual[d, 1.6e-149], N[(N[(b / c), $MachinePrecision] + N[(a * (-N[(d / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 3.8e+108], t$95$0, t$95$1]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\
t_1 := -\frac{a}{d}\\
\mathbf{if}\;d \leq -900000000000:\\
\;\;\;\;t_1 + b \cdot \frac{c}{{d}^{2}}\\

\mathbf{elif}\;d \leq -1.35 \cdot 10^{-139}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq 1.6 \cdot 10^{-149}:\\
\;\;\;\;\frac{b}{c} + a \cdot \left(-\frac{d}{{c}^{2}}\right)\\

\mathbf{elif}\;d \leq 3.8 \cdot 10^{+108}:\\
\;\;\;\;t_0\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.0
Target0.4
Herbie15.5
\[\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 < -9e11

    1. Initial program 32.1

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

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

      \[\leadsto \color{blue}{\left(-\frac{a}{d}\right) + b \cdot \frac{c}{{d}^{2}}} \]
      Proof

      [Start]18.5

      \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}} \]

      rational.json-simplify-2 [=>]18.5

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

      rational.json-simplify-9 [=>]18.5

      \[ \color{blue}{\left(-\frac{a}{d}\right)} + \frac{c \cdot b}{{d}^{2}} \]

      rational.json-simplify-49 [=>]18.5

      \[ \left(-\frac{a}{d}\right) + \color{blue}{b \cdot \frac{c}{{d}^{2}}} \]

    if -9e11 < d < -1.3499999999999999e-139 or 1.60000000000000001e-149 < d < 3.80000000000000008e108

    1. Initial program 15.6

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

    if -1.3499999999999999e-139 < d < 1.60000000000000001e-149

    1. Initial program 24.2

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

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

      \[\leadsto \color{blue}{\frac{b}{c} + a \cdot \left(-\frac{d}{{c}^{2}}\right)} \]
      Proof

      [Start]10.5

      \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c} \]

      rational.json-simplify-1 [=>]10.5

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

      rational.json-simplify-2 [=>]10.5

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

      rational.json-simplify-49 [=>]11.3

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

      rational.json-simplify-43 [=>]11.3

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

      rational.json-simplify-9 [=>]11.3

      \[ \frac{b}{c} + a \cdot \color{blue}{\left(-\frac{d}{{c}^{2}}\right)} \]

    if 3.80000000000000008e108 < d

    1. Initial program 39.9

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

      \[\leadsto \color{blue}{-1 \cdot \frac{a}{d}} \]
    3. Simplified17.1

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

      [Start]17.1

      \[ -1 \cdot \frac{a}{d} \]

      rational.json-simplify-2 [=>]17.1

      \[ \color{blue}{\frac{a}{d} \cdot -1} \]

      rational.json-simplify-9 [=>]17.1

      \[ \color{blue}{-\frac{a}{d}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification15.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -900000000000:\\ \;\;\;\;\left(-\frac{a}{d}\right) + b \cdot \frac{c}{{d}^{2}}\\ \mathbf{elif}\;d \leq -1.35 \cdot 10^{-139}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.6 \cdot 10^{-149}:\\ \;\;\;\;\frac{b}{c} + a \cdot \left(-\frac{d}{{c}^{2}}\right)\\ \mathbf{elif}\;d \leq 3.8 \cdot 10^{+108}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;-\frac{a}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error14.9
Cost7500
\[\begin{array}{l} t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -7.5 \cdot 10^{+98}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -5.2 \cdot 10^{-109}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.95 \cdot 10^{-139}:\\ \;\;\;\;\left(-\frac{a}{d}\right) + b \cdot \frac{c}{{d}^{2}}\\ \mathbf{elif}\;c \leq 10^{+99}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 2
Error21.1
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{b}{\frac{t_0}{c}}\\ \mathbf{if}\;c \leq -8 \cdot 10^{+150}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -4.2 \cdot 10^{-6}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -7 \cdot 10^{-21}:\\ \;\;\;\;-\frac{a}{\frac{t_0}{d}}\\ \mathbf{elif}\;c \leq -1.3 \cdot 10^{-68}:\\ \;\;\;\;\frac{c}{t_0} \cdot b\\ \mathbf{elif}\;c \leq 5.7 \cdot 10^{-139}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{+121}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 3
Error16.7
Cost1488
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ t_1 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2 \cdot 10^{+119}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -5.4 \cdot 10^{-273}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{-170}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 5.9 \cdot 10^{+107}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error21.1
Cost1232
\[\begin{array}{l} t_0 := \frac{c}{c \cdot c + d \cdot d} \cdot b\\ \mathbf{if}\;c \leq -1.32 \cdot 10^{+122}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -2.5 \cdot 10^{-70}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9.2 \cdot 10^{-140}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;c \leq 3 \cdot 10^{+121}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 5
Error21.0
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{\frac{c \cdot c + d \cdot d}{c}}\\ \mathbf{if}\;c \leq -3.35 \cdot 10^{+152}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -2.15 \cdot 10^{-70}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 6.8 \cdot 10^{-140}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;c \leq 1.85 \cdot 10^{+121}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 6
Error23.5
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -4 \cdot 10^{+117}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -4.4 \cdot 10^{-70}:\\ \;\;\;\;\frac{b}{c \cdot c + d \cdot d} \cdot c\\ \mathbf{elif}\;c \leq 1.8 \cdot 10^{-132}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 7
Error24.4
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -0.00065:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.25 \cdot 10^{-131}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 8
Error37.4
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

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