?

Average Error: 25.6 → 15.5
Time: 15.2s
Precision: binary64
Cost: 7700

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\ t_1 := c \cdot c + d \cdot d\\ \mathbf{if}\;c \leq -1.85 \cdot 10^{+52}:\\ \;\;\;\;\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\ \mathbf{elif}\;c \leq -4.5:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2.35 \cdot 10^{-135}:\\ \;\;\;\;\frac{1}{\frac{t_1}{c \cdot a + d \cdot b}}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{-132}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.3 \cdot 10^{+139}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{t_1}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + b \cdot \frac{d}{{c}^{2}}\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (+ (/ b d) (* a (/ c (pow d 2.0))))) (t_1 (+ (* c c) (* d d))))
   (if (<= c -1.85e+52)
     (+ (/ a c) (* d (/ b (pow c 2.0))))
     (if (<= c -4.5)
       t_0
       (if (<= c -2.35e-135)
         (/ 1.0 (/ t_1 (+ (* c a) (* d b))))
         (if (<= c 2.4e-132)
           t_0
           (if (<= c 1.3e+139)
             (/ (+ (* a c) (* b d)) t_1)
             (+ (/ a c) (* b (/ d (pow c 2.0)))))))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double t_0 = (b / d) + (a * (c / pow(d, 2.0)));
	double t_1 = (c * c) + (d * d);
	double tmp;
	if (c <= -1.85e+52) {
		tmp = (a / c) + (d * (b / pow(c, 2.0)));
	} else if (c <= -4.5) {
		tmp = t_0;
	} else if (c <= -2.35e-135) {
		tmp = 1.0 / (t_1 / ((c * a) + (d * b)));
	} else if (c <= 2.4e-132) {
		tmp = t_0;
	} else if (c <= 1.3e+139) {
		tmp = ((a * c) + (b * d)) / t_1;
	} else {
		tmp = (a / c) + (b * (d / pow(c, 2.0)));
	}
	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 = ((a * c) + (b * 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 / d) + (a * (c / (d ** 2.0d0)))
    t_1 = (c * c) + (d * d)
    if (c <= (-1.85d+52)) then
        tmp = (a / c) + (d * (b / (c ** 2.0d0)))
    else if (c <= (-4.5d0)) then
        tmp = t_0
    else if (c <= (-2.35d-135)) then
        tmp = 1.0d0 / (t_1 / ((c * a) + (d * b)))
    else if (c <= 2.4d-132) then
        tmp = t_0
    else if (c <= 1.3d+139) then
        tmp = ((a * c) + (b * d)) / t_1
    else
        tmp = (a / c) + (b * (d / (c ** 2.0d0)))
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double t_0 = (b / d) + (a * (c / Math.pow(d, 2.0)));
	double t_1 = (c * c) + (d * d);
	double tmp;
	if (c <= -1.85e+52) {
		tmp = (a / c) + (d * (b / Math.pow(c, 2.0)));
	} else if (c <= -4.5) {
		tmp = t_0;
	} else if (c <= -2.35e-135) {
		tmp = 1.0 / (t_1 / ((c * a) + (d * b)));
	} else if (c <= 2.4e-132) {
		tmp = t_0;
	} else if (c <= 1.3e+139) {
		tmp = ((a * c) + (b * d)) / t_1;
	} else {
		tmp = (a / c) + (b * (d / Math.pow(c, 2.0)));
	}
	return tmp;
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	t_0 = (b / d) + (a * (c / math.pow(d, 2.0)))
	t_1 = (c * c) + (d * d)
	tmp = 0
	if c <= -1.85e+52:
		tmp = (a / c) + (d * (b / math.pow(c, 2.0)))
	elif c <= -4.5:
		tmp = t_0
	elif c <= -2.35e-135:
		tmp = 1.0 / (t_1 / ((c * a) + (d * b)))
	elif c <= 2.4e-132:
		tmp = t_0
	elif c <= 1.3e+139:
		tmp = ((a * c) + (b * d)) / t_1
	else:
		tmp = (a / c) + (b * (d / math.pow(c, 2.0)))
	return tmp
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function code(a, b, c, d)
	t_0 = Float64(Float64(b / d) + Float64(a * Float64(c / (d ^ 2.0))))
	t_1 = Float64(Float64(c * c) + Float64(d * d))
	tmp = 0.0
	if (c <= -1.85e+52)
		tmp = Float64(Float64(a / c) + Float64(d * Float64(b / (c ^ 2.0))));
	elseif (c <= -4.5)
		tmp = t_0;
	elseif (c <= -2.35e-135)
		tmp = Float64(1.0 / Float64(t_1 / Float64(Float64(c * a) + Float64(d * b))));
	elseif (c <= 2.4e-132)
		tmp = t_0;
	elseif (c <= 1.3e+139)
		tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / t_1);
	else
		tmp = Float64(Float64(a / c) + Float64(b * Float64(d / (c ^ 2.0))));
	end
	return tmp
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
end
function tmp_2 = code(a, b, c, d)
	t_0 = (b / d) + (a * (c / (d ^ 2.0)));
	t_1 = (c * c) + (d * d);
	tmp = 0.0;
	if (c <= -1.85e+52)
		tmp = (a / c) + (d * (b / (c ^ 2.0)));
	elseif (c <= -4.5)
		tmp = t_0;
	elseif (c <= -2.35e-135)
		tmp = 1.0 / (t_1 / ((c * a) + (d * b)));
	elseif (c <= 2.4e-132)
		tmp = t_0;
	elseif (c <= 1.3e+139)
		tmp = ((a * c) + (b * d)) / t_1;
	else
		tmp = (a / c) + (b * (d / (c ^ 2.0)));
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * 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[(b / d), $MachinePrecision] + N[(a * N[(c / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -1.85e+52], N[(N[(a / c), $MachinePrecision] + N[(d * N[(b / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -4.5], t$95$0, If[LessEqual[c, -2.35e-135], N[(1.0 / N[(t$95$1 / N[(N[(c * a), $MachinePrecision] + N[(d * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.4e-132], t$95$0, If[LessEqual[c, 1.3e+139], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / t$95$1), $MachinePrecision], N[(N[(a / c), $MachinePrecision] + N[(b * N[(d / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\
t_1 := c \cdot c + d \cdot d\\
\mathbf{if}\;c \leq -1.85 \cdot 10^{+52}:\\
\;\;\;\;\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\

\mathbf{elif}\;c \leq -4.5:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq -2.35 \cdot 10^{-135}:\\
\;\;\;\;\frac{1}{\frac{t_1}{c \cdot a + d \cdot b}}\\

\mathbf{elif}\;c \leq 2.4 \cdot 10^{-132}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq 1.3 \cdot 10^{+139}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{t_1}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{c} + b \cdot \frac{d}{{c}^{2}}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original25.6
Target0.5
Herbie15.5
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation?

  1. Split input into 5 regimes
  2. if c < -1.85e52

    1. Initial program 34.9

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

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

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

      [Start]16.7

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

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

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

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

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

    if -1.85e52 < c < -4.5 or -2.34999999999999988e-135 < c < 2.40000000000000015e-132

    1. Initial program 21.5

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

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

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

      [Start]14.6

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

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

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

    if -4.5 < c < -2.34999999999999988e-135

    1. Initial program 15.3

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

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

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

    if 2.40000000000000015e-132 < c < 1.30000000000000011e139

    1. Initial program 16.9

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

    if 1.30000000000000011e139 < c

    1. Initial program 42.0

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

      \[\leadsto \color{blue}{\frac{1}{c \cdot c + d \cdot d} \cdot \left(a \cdot c + b \cdot d\right)} \]
    3. Taylor expanded in c around inf 14.8

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}} \]
    4. Simplified13.8

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

      [Start]14.8

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

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

      \[ \frac{a}{c} + \color{blue}{b \cdot \frac{d}{{c}^{2}}} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification15.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.85 \cdot 10^{+52}:\\ \;\;\;\;\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\ \mathbf{elif}\;c \leq -4.5:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\ \mathbf{elif}\;c \leq -2.35 \cdot 10^{-135}:\\ \;\;\;\;\frac{1}{\frac{c \cdot c + d \cdot d}{c \cdot a + d \cdot b}}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{-132}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.3 \cdot 10^{+139}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + b \cdot \frac{d}{{c}^{2}}\\ \end{array} \]

Alternatives

Alternative 1
Error16.7
Cost7700
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ \mathbf{if}\;c \leq -3 \cdot 10^{+51}:\\ \;\;\;\;\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\ \mathbf{elif}\;c \leq -4.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -5.8 \cdot 10^{-140}:\\ \;\;\;\;\frac{1}{\frac{t_0}{c \cdot a + d \cdot b}}\\ \mathbf{elif}\;c \leq 2.5 \cdot 10^{-197}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+144}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + b \cdot \frac{d}{{c}^{2}}\\ \end{array} \]
Alternative 2
Error14.9
Cost7436
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.3 \cdot 10^{+153}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -6.2 \cdot 10^{-128}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 8.8 \cdot 10^{-61}:\\ \;\;\;\;\frac{a}{c} + b \cdot \frac{d}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.2 \cdot 10^{+96}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error16.8
Cost1752
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -3.3 \cdot 10^{+123}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.25 \cdot 10^{+51}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -3.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -4.5 \cdot 10^{-139}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.5 \cdot 10^{-198}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{+140}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 4
Error16.9
Cost1752
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ t_2 := \frac{t_1}{t_0}\\ \mathbf{if}\;c \leq -1.35 \cdot 10^{+123}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.25 \cdot 10^{+51}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;c \leq -4.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -1.02 \cdot 10^{-139}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq 3.5 \cdot 10^{-195}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 4.5 \cdot 10^{+144}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 5
Error16.9
Cost1752
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;c \leq -1.25 \cdot 10^{+123}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.25 \cdot 10^{+51}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;c \leq -4.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -7 \cdot 10^{-139}:\\ \;\;\;\;\frac{1}{\frac{t_0}{c \cdot a + d \cdot b}}\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{-199}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 8.5 \cdot 10^{+140}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 6
Error21.4
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{c}{t_0} \cdot a\\ \mathbf{if}\;c \leq -4.5 \cdot 10^{+51}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -0.00088:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -1.56 \cdot 10^{-58}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -4.2 \cdot 10^{-135}:\\ \;\;\;\;\frac{d}{t_0} \cdot b\\ \mathbf{elif}\;c \leq 6.2 \cdot 10^{-48}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 5 \cdot 10^{+139}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 7
Error21.4
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{c}{t_0} \cdot a\\ \mathbf{if}\;c \leq -3 \cdot 10^{+51}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -4.4:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -2.3 \cdot 10^{-57}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1.15 \cdot 10^{-134}:\\ \;\;\;\;\frac{b}{\frac{t_0}{d}}\\ \mathbf{elif}\;c \leq 1.15 \cdot 10^{-49}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 7.5 \cdot 10^{+144}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 8
Error21.2
Cost1364
\[\begin{array}{l} t_0 := \frac{c}{c \cdot c + d \cdot d} \cdot a\\ \mathbf{if}\;c \leq -3.2 \cdot 10^{+51}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -0.057:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -6.5 \cdot 10^{-57}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.3 \cdot 10^{-49}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 3.8 \cdot 10^{+141}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Error22.6
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -1.5 \cdot 10^{-65}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{+17}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 10
Error36.6
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

herbie shell --seed 2023074 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))