?

Average Error: 25.8 → 15.7
Time: 17.7s
Precision: binary64
Cost: 14928

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := a \cdot c + b \cdot d\\ t_1 := c \cdot c + d \cdot d\\ t_2 := \frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\ \mathbf{if}\;c \leq -5.2 \cdot 10^{+134}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.8 \cdot 10^{-162}:\\ \;\;\;\;\frac{t_0}{t_1}\\ \mathbf{elif}\;c \leq 2.6 \cdot 10^{-110}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+34}:\\ \;\;\;\;\frac{1}{t_1} \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;3 \cdot t_2 - 2 \cdot t_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 (+ (* a c) (* b d)))
        (t_1 (+ (* c c) (* d d)))
        (t_2 (+ (/ a c) (* d (/ b (pow c 2.0))))))
   (if (<= c -5.2e+134)
     (/ a c)
     (if (<= c -3.8e-162)
       (/ t_0 t_1)
       (if (<= c 2.6e-110)
         (/ b d)
         (if (<= c 1.9e+34)
           (* (/ 1.0 t_1) t_0)
           (- (* 3.0 t_2) (* 2.0 t_2))))))))
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);
	double t_1 = (c * c) + (d * d);
	double t_2 = (a / c) + (d * (b / pow(c, 2.0)));
	double tmp;
	if (c <= -5.2e+134) {
		tmp = a / c;
	} else if (c <= -3.8e-162) {
		tmp = t_0 / t_1;
	} else if (c <= 2.6e-110) {
		tmp = b / d;
	} else if (c <= 1.9e+34) {
		tmp = (1.0 / t_1) * t_0;
	} else {
		tmp = (3.0 * t_2) - (2.0 * t_2);
	}
	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) :: t_2
    real(8) :: tmp
    t_0 = (a * c) + (b * d)
    t_1 = (c * c) + (d * d)
    t_2 = (a / c) + (d * (b / (c ** 2.0d0)))
    if (c <= (-5.2d+134)) then
        tmp = a / c
    else if (c <= (-3.8d-162)) then
        tmp = t_0 / t_1
    else if (c <= 2.6d-110) then
        tmp = b / d
    else if (c <= 1.9d+34) then
        tmp = (1.0d0 / t_1) * t_0
    else
        tmp = (3.0d0 * t_2) - (2.0d0 * t_2)
    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);
	double t_1 = (c * c) + (d * d);
	double t_2 = (a / c) + (d * (b / Math.pow(c, 2.0)));
	double tmp;
	if (c <= -5.2e+134) {
		tmp = a / c;
	} else if (c <= -3.8e-162) {
		tmp = t_0 / t_1;
	} else if (c <= 2.6e-110) {
		tmp = b / d;
	} else if (c <= 1.9e+34) {
		tmp = (1.0 / t_1) * t_0;
	} else {
		tmp = (3.0 * t_2) - (2.0 * t_2);
	}
	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)
	t_1 = (c * c) + (d * d)
	t_2 = (a / c) + (d * (b / math.pow(c, 2.0)))
	tmp = 0
	if c <= -5.2e+134:
		tmp = a / c
	elif c <= -3.8e-162:
		tmp = t_0 / t_1
	elif c <= 2.6e-110:
		tmp = b / d
	elif c <= 1.9e+34:
		tmp = (1.0 / t_1) * t_0
	else:
		tmp = (3.0 * t_2) - (2.0 * t_2)
	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(a * c) + Float64(b * d))
	t_1 = Float64(Float64(c * c) + Float64(d * d))
	t_2 = Float64(Float64(a / c) + Float64(d * Float64(b / (c ^ 2.0))))
	tmp = 0.0
	if (c <= -5.2e+134)
		tmp = Float64(a / c);
	elseif (c <= -3.8e-162)
		tmp = Float64(t_0 / t_1);
	elseif (c <= 2.6e-110)
		tmp = Float64(b / d);
	elseif (c <= 1.9e+34)
		tmp = Float64(Float64(1.0 / t_1) * t_0);
	else
		tmp = Float64(Float64(3.0 * t_2) - Float64(2.0 * t_2));
	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);
	t_1 = (c * c) + (d * d);
	t_2 = (a / c) + (d * (b / (c ^ 2.0)));
	tmp = 0.0;
	if (c <= -5.2e+134)
		tmp = a / c;
	elseif (c <= -3.8e-162)
		tmp = t_0 / t_1;
	elseif (c <= 2.6e-110)
		tmp = b / d;
	elseif (c <= 1.9e+34)
		tmp = (1.0 / t_1) * t_0;
	else
		tmp = (3.0 * t_2) - (2.0 * t_2);
	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[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(a / c), $MachinePrecision] + N[(d * N[(b / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -5.2e+134], N[(a / c), $MachinePrecision], If[LessEqual[c, -3.8e-162], N[(t$95$0 / t$95$1), $MachinePrecision], If[LessEqual[c, 2.6e-110], N[(b / d), $MachinePrecision], If[LessEqual[c, 1.9e+34], N[(N[(1.0 / t$95$1), $MachinePrecision] * t$95$0), $MachinePrecision], N[(N[(3.0 * t$95$2), $MachinePrecision] - N[(2.0 * t$95$2), $MachinePrecision]), $MachinePrecision]]]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := a \cdot c + b \cdot d\\
t_1 := c \cdot c + d \cdot d\\
t_2 := \frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\
\mathbf{if}\;c \leq -5.2 \cdot 10^{+134}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{elif}\;c \leq -3.8 \cdot 10^{-162}:\\
\;\;\;\;\frac{t_0}{t_1}\\

\mathbf{elif}\;c \leq 2.6 \cdot 10^{-110}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;c \leq 1.9 \cdot 10^{+34}:\\
\;\;\;\;\frac{1}{t_1} \cdot t_0\\

\mathbf{else}:\\
\;\;\;\;3 \cdot t_2 - 2 \cdot t_2\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original25.8
Target0.6
Herbie15.7
\[\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 < -5.2000000000000003e134

    1. Initial program 42.4

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

      \[\leadsto \color{blue}{\frac{a}{c}} \]

    if -5.2000000000000003e134 < c < -3.80000000000000005e-162

    1. Initial program 16.1

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

    if -3.80000000000000005e-162 < c < 2.5999999999999999e-110

    1. Initial program 23.5

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

      \[\leadsto \color{blue}{\frac{b}{d}} \]

    if 2.5999999999999999e-110 < c < 1.9000000000000001e34

    1. Initial program 15.0

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

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

    if 1.9000000000000001e34 < c

    1. Initial program 34.6

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

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

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

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

      [Start]18.2

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

      rational_best-simplify-2 [=>]18.2

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

      rational_best-simplify-51 [=>]18.2

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

      rational_best-simplify-2 [<=]18.2

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

      rational_best-simplify-47 [=>]18.3

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

      rational_best-simplify-2 [=>]18.3

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

      rational_best-simplify-51 [=>]18.3

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

      rational_best-simplify-1 [<=]18.3

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

      rational_best-simplify-2 [<=]18.3

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

      rational_best-simplify-47 [=>]16.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.2 \cdot 10^{+134}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.8 \cdot 10^{-162}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 2.6 \cdot 10^{-110}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+34}:\\ \;\;\;\;\frac{1}{c \cdot c + d \cdot d} \cdot \left(a \cdot c + b \cdot d\right)\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\right) - 2 \cdot \left(\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error15.7
Cost8080
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;c \leq -5.5 \cdot 10^{+134}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.1 \cdot 10^{-161}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{elif}\;c \leq 2.6 \cdot 10^{-110}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+34}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot \frac{3}{c} + d \cdot \frac{b}{{c}^{2}}\right) - a \cdot \frac{2}{c}\\ \end{array} \]
Alternative 2
Error15.9
Cost7952
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;c \leq -8.5 \cdot 10^{+134}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.2 \cdot 10^{-161}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{-109}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+34}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{d}{{c}^{2}} + a \cdot \left(\frac{3}{c} + \frac{-2}{c}\right)\\ \end{array} \]
Alternative 3
Error15.8
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -1.6 \cdot 10^{+135}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.4 \cdot 10^{-161}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3 \cdot 10^{-110}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 10^{+96}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 4
Error23.3
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -1.25 \cdot 10^{+66}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.95 \cdot 10^{-64}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 5
Error37.7
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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