?

Average Error: 26.3 → 14.1
Time: 7.5s
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 -5.5 \cdot 10^{+64}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1.45 \cdot 10^{-114}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.18 \cdot 10^{-113}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.3 \cdot 10^{+89}:\\ \;\;\;\;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 -5.5e+64)
     t_1
     (if (<= d -1.45e-114)
       t_0
       (if (<= d 1.18e-113)
         (+ (/ a c) (/ (* d b) (pow c 2.0)))
         (if (<= d 1.3e+89) 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 <= -5.5e+64) {
		tmp = t_1;
	} else if (d <= -1.45e-114) {
		tmp = t_0;
	} else if (d <= 1.18e-113) {
		tmp = (a / c) + ((d * b) / pow(c, 2.0));
	} else if (d <= 1.3e+89) {
		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 <= (-5.5d+64)) then
        tmp = t_1
    else if (d <= (-1.45d-114)) then
        tmp = t_0
    else if (d <= 1.18d-113) then
        tmp = (a / c) + ((d * b) / (c ** 2.0d0))
    else if (d <= 1.3d+89) 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 <= -5.5e+64) {
		tmp = t_1;
	} else if (d <= -1.45e-114) {
		tmp = t_0;
	} else if (d <= 1.18e-113) {
		tmp = (a / c) + ((d * b) / Math.pow(c, 2.0));
	} else if (d <= 1.3e+89) {
		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 <= -5.5e+64:
		tmp = t_1
	elif d <= -1.45e-114:
		tmp = t_0
	elif d <= 1.18e-113:
		tmp = (a / c) + ((d * b) / math.pow(c, 2.0))
	elif d <= 1.3e+89:
		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 <= -5.5e+64)
		tmp = t_1;
	elseif (d <= -1.45e-114)
		tmp = t_0;
	elseif (d <= 1.18e-113)
		tmp = Float64(Float64(a / c) + Float64(Float64(d * b) / (c ^ 2.0)));
	elseif (d <= 1.3e+89)
		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 <= -5.5e+64)
		tmp = t_1;
	elseif (d <= -1.45e-114)
		tmp = t_0;
	elseif (d <= 1.18e-113)
		tmp = (a / c) + ((d * b) / (c ^ 2.0));
	elseif (d <= 1.3e+89)
		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, -5.5e+64], t$95$1, If[LessEqual[d, -1.45e-114], t$95$0, If[LessEqual[d, 1.18e-113], N[(N[(a / c), $MachinePrecision] + N[(N[(d * b), $MachinePrecision] / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.3e+89], 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 -5.5 \cdot 10^{+64}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq -1.45 \cdot 10^{-114}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;d \leq 1.3 \cdot 10^{+89}:\\
\;\;\;\;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.3
Target0.5
Herbie14.1
\[\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 < -5.4999999999999996e64 or 1.3e89 < d

    1. Initial program 37.4

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

      \[\leadsto \frac{\color{blue}{\frac{a \cdot \left(-c\right) - \left(a \cdot c + \left(b \cdot d + b \cdot d\right)\right)}{-2}}}{c \cdot c + d \cdot d} \]
    3. Taylor expanded in c around 0 15.2

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

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

      [Start]15.2

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

      rational_best-simplify-1 [=>]15.2

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

      rational_best-simplify-113 [=>]15.2

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

      rational_best-simplify-3 [=>]15.2

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

      rational_best-simplify-17 [=>]15.2

      \[ \frac{b}{d} + c \cdot \left(-0.5 \cdot \left(\color{blue}{\left(-\frac{a}{{d}^{2}}\right)} - \frac{a}{{d}^{2}}\right)\right) \]
    5. Applied egg-rr15.2

      \[\leadsto \frac{b}{d} + \color{blue}{\left(\frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right) + \frac{a}{{d}^{2}} \cdot \left(c \cdot 0.5\right)\right)} \]
    6. Simplified15.2

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

      [Start]15.2

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

      rational_best-simplify-62 [=>]15.2

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

      rational_best-simplify-113 [=>]15.2

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

      rational_best-simplify-3 [=>]15.2

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

      rational_best-simplify-52 [=>]15.2

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

      rational_best-simplify-3 [=>]15.2

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

      rational_best-simplify-52 [=>]15.2

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

      metadata-eval [=>]15.2

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

      rational_best-simplify-113 [=>]15.2

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

      rational_best-simplify-110 [=>]15.2

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

      rational_best-simplify-110 [=>]15.2

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

      metadata-eval [=>]15.2

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

      rational_best-simplify-3 [<=]15.2

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

      rational_best-simplify-11 [<=]15.2

      \[ \frac{b}{d} + c \cdot \color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;-1 \ne 0:\\ \;\;\;\;\frac{a}{{d}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{{d}^{2}}\\ } \end{array}} \]

      rational_best-simplify-5 [=>]15.2

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

    if -5.4999999999999996e64 < d < -1.44999999999999998e-114 or 1.18e-113 < d < 1.3e89

    1. Initial program 15.6

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

    if -1.44999999999999998e-114 < d < 1.18e-113

    1. Initial program 22.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -5.5 \cdot 10^{+64}:\\ \;\;\;\;\frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\ \mathbf{elif}\;d \leq -1.45 \cdot 10^{-114}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.18 \cdot 10^{-113}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.3 \cdot 10^{+89}:\\ \;\;\;\;\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.4
Cost7436
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -3.5 \cdot 10^{+154}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -9.5 \cdot 10^{-116}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7.5 \cdot 10^{-117}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 6.2 \cdot 10^{+121}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 2
Error16.1
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -3.5 \cdot 10^{+154}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.3 \cdot 10^{-116}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.68 \cdot 10^{-192}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 1.7 \cdot 10^{+120}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error22.2
Cost1232
\[\begin{array}{l} t_0 := \frac{d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.6 \cdot 10^{+65}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.4 \cdot 10^{-105}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.8 \cdot 10^{-11}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2.45 \cdot 10^{+70}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{+75}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error23.3
Cost1100
\[\begin{array}{l} \mathbf{if}\;c \leq -5.6 \cdot 10^{-60}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 6.6 \cdot 10^{-75}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{+37}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 9.2 \cdot 10^{+86}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 5
Error24.4
Cost720
\[\begin{array}{l} \mathbf{if}\;c \leq -4.9 \cdot 10^{-60}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{-74}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.1 \cdot 10^{+37}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 8.8 \cdot 10^{+86}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 6
Error37.6
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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