?

Average Error: 41.55% → 19.27%
Time: 11.2s
Precision: binary64
Cost: 14024

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;c \leq -5 \cdot 10^{+54}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -1.6 \cdot 10^{-119}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 4.05 \cdot 10^{+30}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= c -5e+54)
   (- (/ b c) (* (/ d c) (/ a c)))
   (if (<= c -1.6e-119)
     (* (/ 1.0 (hypot c d)) (/ (- (* c b) (* d a)) (hypot c d)))
     (if (<= c 4.05e+30)
       (/ (- (/ (* c b) d) a) d)
       (/ (- b (/ d (/ c a))) c)))))
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 tmp;
	if (c <= -5e+54) {
		tmp = (b / c) - ((d / c) * (a / c));
	} else if (c <= -1.6e-119) {
		tmp = (1.0 / hypot(c, d)) * (((c * b) - (d * a)) / hypot(c, d));
	} else if (c <= 4.05e+30) {
		tmp = (((c * b) / d) - a) / d;
	} else {
		tmp = (b - (d / (c / a))) / c;
	}
	return tmp;
}
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 tmp;
	if (c <= -5e+54) {
		tmp = (b / c) - ((d / c) * (a / c));
	} else if (c <= -1.6e-119) {
		tmp = (1.0 / Math.hypot(c, d)) * (((c * b) - (d * a)) / Math.hypot(c, d));
	} else if (c <= 4.05e+30) {
		tmp = (((c * b) / d) - a) / d;
	} else {
		tmp = (b - (d / (c / a))) / c;
	}
	return tmp;
}
def code(a, b, c, d):
	return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	tmp = 0
	if c <= -5e+54:
		tmp = (b / c) - ((d / c) * (a / c))
	elif c <= -1.6e-119:
		tmp = (1.0 / math.hypot(c, d)) * (((c * b) - (d * a)) / math.hypot(c, d))
	elif c <= 4.05e+30:
		tmp = (((c * b) / d) - a) / d
	else:
		tmp = (b - (d / (c / a))) / c
	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)
	tmp = 0.0
	if (c <= -5e+54)
		tmp = Float64(Float64(b / c) - Float64(Float64(d / c) * Float64(a / c)));
	elseif (c <= -1.6e-119)
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d)));
	elseif (c <= 4.05e+30)
		tmp = Float64(Float64(Float64(Float64(c * b) / d) - a) / d);
	else
		tmp = Float64(Float64(b - Float64(d / Float64(c / a))) / c);
	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)
	tmp = 0.0;
	if (c <= -5e+54)
		tmp = (b / c) - ((d / c) * (a / c));
	elseif (c <= -1.6e-119)
		tmp = (1.0 / hypot(c, d)) * (((c * b) - (d * a)) / hypot(c, d));
	elseif (c <= 4.05e+30)
		tmp = (((c * b) / d) - a) / d;
	else
		tmp = (b - (d / (c / a))) / c;
	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_] := If[LessEqual[c, -5e+54], N[(N[(b / c), $MachinePrecision] - N[(N[(d / c), $MachinePrecision] * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1.6e-119], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 4.05e+30], N[(N[(N[(N[(c * b), $MachinePrecision] / d), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], N[(N[(b - N[(d / N[(c / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -5 \cdot 10^{+54}:\\
\;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\

\mathbf{elif}\;c \leq -1.6 \cdot 10^{-119}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \leq 4.05 \cdot 10^{+30}:\\
\;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\

\mathbf{else}:\\
\;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original41.55%
Target0.75%
Herbie19.27%
\[\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 < -5.00000000000000005e54

    1. Initial program 58.81

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

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

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

      [Start]27.92

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

      +-commutative [=>]27.92

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

      mul-1-neg [=>]27.92

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

      unsub-neg [=>]27.92

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

      *-commutative [=>]27.92

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

      unpow2 [=>]27.92

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

      times-frac [=>]18.6

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

    if -5.00000000000000005e54 < c < -1.59999999999999997e-119

    1. Initial program 24.37

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}} \]

    if -1.59999999999999997e-119 < c < 4.0499999999999998e30

    1. Initial program 31.59

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\frac{1}{\frac{1}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}}}} \]
    3. Taylor expanded in c around 0 25.64

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

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

      [Start]25.64

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

      +-commutative [=>]25.64

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

      mul-1-neg [=>]25.64

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

      unsub-neg [=>]25.64

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

      *-commutative [=>]25.64

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

      unpow2 [=>]25.64

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

      times-frac [=>]23.34

      \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d} \]
    5. Applied egg-rr20.96

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

    if 4.0499999999999998e30 < c

    1. Initial program 52.84

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

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

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

      [Start]27.72

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

      +-commutative [=>]27.72

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

      mul-1-neg [=>]27.72

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

      unsub-neg [=>]27.72

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

      *-commutative [=>]27.72

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

      unpow2 [=>]27.72

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

      times-frac [=>]20.18

      \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}} \]
    4. Applied egg-rr19.59

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5 \cdot 10^{+54}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -1.6 \cdot 10^{-119}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 4.05 \cdot 10^{+30}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]

Alternatives

Alternative 1
Error20.66%
Cost1224
\[\begin{array}{l} \mathbf{if}\;c \leq -2.3 \cdot 10^{+54}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -1.15 \cdot 10^{-120}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 3.2 \cdot 10^{+31}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 2
Error30.72%
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -3.92 \cdot 10^{+33} \lor \neg \left(c \leq 5.8 \cdot 10^{+30}\right):\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 3
Error30.26%
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -3.92 \cdot 10^{+33} \lor \neg \left(c \leq 5.8 \cdot 10^{+30}\right):\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 4
Error23.69%
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -3.92 \cdot 10^{+33} \lor \neg \left(c \leq 2.2 \cdot 10^{+31}\right):\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \end{array} \]
Alternative 5
Error23.74%
Cost840
\[\begin{array}{l} \mathbf{if}\;c \leq -3.92 \cdot 10^{+33}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq 2.3 \cdot 10^{+31}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 6
Error36.26%
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -3.92 \cdot 10^{+33}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 4.7 \cdot 10^{+30}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 7
Error53.71%
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -2.1 \cdot 10^{+208}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{+199}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 8
Error88.43%
Cost192
\[\frac{a}{d} \]

Error

Reproduce?

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