?

Average Error: 26.6 → 14.5
Time: 10.9s
Precision: binary64
Cost: 7568

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\ \mathbf{if}\;d \leq -3.6 \cdot 10^{+28}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -3.1 \cdot 10^{-156}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.65 \cdot 10^{-143}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
        (t_1 (+ (/ b d) (* c (/ a (pow d 2.0))))))
   (if (<= d -3.6e+28)
     t_1
     (if (<= d -3.1e-156)
       t_0
       (if (<= d 1.65e-143)
         (+ (/ a c) (/ b (/ (pow c 2.0) d)))
         (if (<= d 4.2e+87) t_0 t_1))))))
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 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double t_1 = (b / d) + (c * (a / pow(d, 2.0)));
	double tmp;
	if (d <= -3.6e+28) {
		tmp = t_1;
	} else if (d <= -3.1e-156) {
		tmp = t_0;
	} else if (d <= 1.65e-143) {
		tmp = (a / c) + (b / (pow(c, 2.0) / d));
	} else if (d <= 4.2e+87) {
		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 = ((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 = ((a * c) + (b * d)) / ((c * c) + (d * d))
    t_1 = (b / d) + (c * (a / (d ** 2.0d0)))
    if (d <= (-3.6d+28)) then
        tmp = t_1
    else if (d <= (-3.1d-156)) then
        tmp = t_0
    else if (d <= 1.65d-143) then
        tmp = (a / c) + (b / ((c ** 2.0d0) / d))
    else if (d <= 4.2d+87) 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 ((a * c) + (b * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double t_1 = (b / d) + (c * (a / Math.pow(d, 2.0)));
	double tmp;
	if (d <= -3.6e+28) {
		tmp = t_1;
	} else if (d <= -3.1e-156) {
		tmp = t_0;
	} else if (d <= 1.65e-143) {
		tmp = (a / c) + (b / (Math.pow(c, 2.0) / d));
	} else if (d <= 4.2e+87) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	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 = ((a * c) + (b * d)) / ((c * c) + (d * d))
	t_1 = (b / d) + (c * (a / math.pow(d, 2.0)))
	tmp = 0
	if d <= -3.6e+28:
		tmp = t_1
	elif d <= -3.1e-156:
		tmp = t_0
	elif d <= 1.65e-143:
		tmp = (a / c) + (b / (math.pow(c, 2.0) / d))
	elif d <= 4.2e+87:
		tmp = t_0
	else:
		tmp = t_1
	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(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	t_1 = Float64(Float64(b / d) + Float64(c * Float64(a / (d ^ 2.0))))
	tmp = 0.0
	if (d <= -3.6e+28)
		tmp = t_1;
	elseif (d <= -3.1e-156)
		tmp = t_0;
	elseif (d <= 1.65e-143)
		tmp = Float64(Float64(a / c) + Float64(b / Float64((c ^ 2.0) / d)));
	elseif (d <= 4.2e+87)
		tmp = t_0;
	else
		tmp = t_1;
	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 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	t_1 = (b / d) + (c * (a / (d ^ 2.0)));
	tmp = 0.0;
	if (d <= -3.6e+28)
		tmp = t_1;
	elseif (d <= -3.1e-156)
		tmp = t_0;
	elseif (d <= 1.65e-143)
		tmp = (a / c) + (b / ((c ^ 2.0) / d));
	elseif (d <= 4.2e+87)
		tmp = t_0;
	else
		tmp = t_1;
	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[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b / d), $MachinePrecision] + N[(c * N[(a / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -3.6e+28], t$95$1, If[LessEqual[d, -3.1e-156], t$95$0, If[LessEqual[d, 1.65e-143], N[(N[(a / c), $MachinePrecision] + N[(b / N[(N[Power[c, 2.0], $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 4.2e+87], t$95$0, t$95$1]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
t_1 := \frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\
\mathbf{if}\;d \leq -3.6 \cdot 10^{+28}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq -3.1 \cdot 10^{-156}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq 1.65 \cdot 10^{-143}:\\
\;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\

\mathbf{elif}\;d \leq 4.2 \cdot 10^{+87}:\\
\;\;\;\;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.6
Target0.5
Herbie14.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 3 regimes
  2. if d < -3.5999999999999999e28 or 4.2e87 < d

    1. Initial program 36.7

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

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

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

      [Start]17.8

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

      rational.json-simplify-2 [<=]17.8

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

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

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

    if -3.5999999999999999e28 < d < -3.0999999999999998e-156 or 1.65e-143 < d < 4.2e87

    1. Initial program 14.5

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

    if -3.0999999999999998e-156 < d < 1.65e-143

    1. Initial program 24.4

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

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

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

      [Start]9.9

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

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

      \[ \frac{a}{c} + \color{blue}{b \cdot \frac{d}{{c}^{2}}} \]
    4. Applied egg-rr11.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.6 \cdot 10^{+28}:\\ \;\;\;\;\frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\ \mathbf{elif}\;d \leq -3.1 \cdot 10^{-156}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.65 \cdot 10^{-143}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+87}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\ \end{array} \]

Alternatives

Alternative 1
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^{+141}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -7 \cdot 10^{-151}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.9 \cdot 10^{-143}:\\ \;\;\;\;\frac{a}{c} + b \cdot \frac{d}{{c}^{2}}\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{+87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \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 -1.7 \cdot 10^{+137}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.75 \cdot 10^{-156}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.7 \cdot 10^{-143}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 7.5 \cdot 10^{+85}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error23.0
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{b}{t_0} \cdot d\\ \mathbf{if}\;d \leq -5.8 \cdot 10^{+46}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.65 \cdot 10^{-154}:\\ \;\;\;\;\frac{c}{t_0} \cdot a\\ \mathbf{elif}\;d \leq 4.4 \cdot 10^{-103}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 6500000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+114}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error22.5
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{d}{t_0} \cdot b\\ \mathbf{if}\;d \leq -1.55 \cdot 10^{+47}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.1 \cdot 10^{-150}:\\ \;\;\;\;\frac{c}{t_0} \cdot a\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{-90}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 330000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2 \cdot 10^{+142}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 5
Error22.5
Cost1496
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{b}{\frac{t_0}{d}}\\ \mathbf{if}\;d \leq -6.6 \cdot 10^{+46}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.25 \cdot 10^{-150}:\\ \;\;\;\;\frac{c}{t_0} \cdot a\\ \mathbf{elif}\;d \leq 2.9 \cdot 10^{-93}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 310000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{+142}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 6
Error16.4
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -9.2 \cdot 10^{+141}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.08 \cdot 10^{-259}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 10^{-143}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{+87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 7
Error22.3
Cost1364
\[\begin{array}{l} t_0 := \frac{b}{c \cdot c + d \cdot d} \cdot d\\ \mathbf{if}\;d \leq -1.3 \cdot 10^{+27}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 4.4 \cdot 10^{-91}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 80000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 8
Error23.0
Cost712
\[\begin{array}{l} \mathbf{if}\;d \leq -1.9 \cdot 10^{+26}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{2 \cdot \frac{d}{b}}\\ \end{array} \]
Alternative 9
Error22.9
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -1.6 \cdot 10^{+26}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 10
Error37.9
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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