?

Average Error: 26.5 → 14.6
Time: 9.9s
Precision: binary64
Cost: 7436

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;d \leq -8.5 \cdot 10^{+102}:\\ \;\;\;\;\frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\ \mathbf{elif}\;d \leq -4.8 \cdot 10^{-127}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;d \leq 1.6 \cdot 10^{-104}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 4 \cdot 10^{+136}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \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 (+ (* c c) (* d d))) (t_1 (+ (* a c) (* b d))))
   (if (<= d -8.5e+102)
     (+ (/ b d) (* c (/ a (pow d 2.0))))
     (if (<= d -4.8e-127)
       (* (/ 1.0 t_0) t_1)
       (if (<= d 1.6e-104)
         (+ (/ a c) (/ b (/ (pow c 2.0) d)))
         (if (<= d 4e+136) (/ t_1 t_0) (/ b d)))))))
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 = (c * c) + (d * d);
	double t_1 = (a * c) + (b * d);
	double tmp;
	if (d <= -8.5e+102) {
		tmp = (b / d) + (c * (a / pow(d, 2.0)));
	} else if (d <= -4.8e-127) {
		tmp = (1.0 / t_0) * t_1;
	} else if (d <= 1.6e-104) {
		tmp = (a / c) + (b / (pow(c, 2.0) / d));
	} else if (d <= 4e+136) {
		tmp = t_1 / t_0;
	} else {
		tmp = b / d;
	}
	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 = (c * c) + (d * d)
    t_1 = (a * c) + (b * d)
    if (d <= (-8.5d+102)) then
        tmp = (b / d) + (c * (a / (d ** 2.0d0)))
    else if (d <= (-4.8d-127)) then
        tmp = (1.0d0 / t_0) * t_1
    else if (d <= 1.6d-104) then
        tmp = (a / c) + (b / ((c ** 2.0d0) / d))
    else if (d <= 4d+136) then
        tmp = t_1 / t_0
    else
        tmp = b / d
    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 = (c * c) + (d * d);
	double t_1 = (a * c) + (b * d);
	double tmp;
	if (d <= -8.5e+102) {
		tmp = (b / d) + (c * (a / Math.pow(d, 2.0)));
	} else if (d <= -4.8e-127) {
		tmp = (1.0 / t_0) * t_1;
	} else if (d <= 1.6e-104) {
		tmp = (a / c) + (b / (Math.pow(c, 2.0) / d));
	} else if (d <= 4e+136) {
		tmp = t_1 / t_0;
	} else {
		tmp = b / d;
	}
	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 = (c * c) + (d * d)
	t_1 = (a * c) + (b * d)
	tmp = 0
	if d <= -8.5e+102:
		tmp = (b / d) + (c * (a / math.pow(d, 2.0)))
	elif d <= -4.8e-127:
		tmp = (1.0 / t_0) * t_1
	elif d <= 1.6e-104:
		tmp = (a / c) + (b / (math.pow(c, 2.0) / d))
	elif d <= 4e+136:
		tmp = t_1 / t_0
	else:
		tmp = b / d
	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(c * c) + Float64(d * d))
	t_1 = Float64(Float64(a * c) + Float64(b * d))
	tmp = 0.0
	if (d <= -8.5e+102)
		tmp = Float64(Float64(b / d) + Float64(c * Float64(a / (d ^ 2.0))));
	elseif (d <= -4.8e-127)
		tmp = Float64(Float64(1.0 / t_0) * t_1);
	elseif (d <= 1.6e-104)
		tmp = Float64(Float64(a / c) + Float64(b / Float64((c ^ 2.0) / d)));
	elseif (d <= 4e+136)
		tmp = Float64(t_1 / t_0);
	else
		tmp = Float64(b / d);
	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 = (c * c) + (d * d);
	t_1 = (a * c) + (b * d);
	tmp = 0.0;
	if (d <= -8.5e+102)
		tmp = (b / d) + (c * (a / (d ^ 2.0)));
	elseif (d <= -4.8e-127)
		tmp = (1.0 / t_0) * t_1;
	elseif (d <= 1.6e-104)
		tmp = (a / c) + (b / ((c ^ 2.0) / d));
	elseif (d <= 4e+136)
		tmp = t_1 / t_0;
	else
		tmp = b / d;
	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[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -8.5e+102], N[(N[(b / d), $MachinePrecision] + N[(c * N[(a / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -4.8e-127], N[(N[(1.0 / t$95$0), $MachinePrecision] * t$95$1), $MachinePrecision], If[LessEqual[d, 1.6e-104], N[(N[(a / c), $MachinePrecision] + N[(b / N[(N[Power[c, 2.0], $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 4e+136], N[(t$95$1 / t$95$0), $MachinePrecision], N[(b / d), $MachinePrecision]]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := c \cdot c + d \cdot d\\
t_1 := a \cdot c + b \cdot d\\
\mathbf{if}\;d \leq -8.5 \cdot 10^{+102}:\\
\;\;\;\;\frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\

\mathbf{elif}\;d \leq -4.8 \cdot 10^{-127}:\\
\;\;\;\;\frac{1}{t_0} \cdot t_1\\

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

\mathbf{elif}\;d \leq 4 \cdot 10^{+136}:\\
\;\;\;\;\frac{t_1}{t_0}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.5
Target0.5
Herbie14.6
\[\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 d < -8.4999999999999996e102

    1. Initial program 41.1

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

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

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

      [Start]15.4

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

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

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

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

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

    if -8.4999999999999996e102 < d < -4.79999999999999964e-127

    1. Initial program 17.4

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

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

    if -4.79999999999999964e-127 < d < 1.59999999999999994e-104

    1. Initial program 22.0

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

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

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

      [Start]10.7

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

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

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

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

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

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

    if 1.59999999999999994e-104 < d < 4.00000000000000023e136

    1. Initial program 17.0

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

    if 4.00000000000000023e136 < d

    1. Initial program 42.3

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

      \[\leadsto \color{blue}{\frac{b}{d}} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification14.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -8.5 \cdot 10^{+102}:\\ \;\;\;\;\frac{b}{d} + c \cdot \frac{a}{{d}^{2}}\\ \mathbf{elif}\;d \leq -4.8 \cdot 10^{-127}:\\ \;\;\;\;\frac{1}{c \cdot c + d \cdot d} \cdot \left(a \cdot c + b \cdot d\right)\\ \mathbf{elif}\;d \leq 1.6 \cdot 10^{-104}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 4 \cdot 10^{+136}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error15.6
Cost7436
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;d \leq -1.25 \cdot 10^{+104}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.2 \cdot 10^{-125}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;d \leq 1.25 \cdot 10^{-118}:\\ \;\;\;\;\frac{a}{c} + d \cdot \frac{b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 4 \cdot 10^{+136}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 2
Error15.0
Cost7436
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;d \leq -1.3 \cdot 10^{+104}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.9 \cdot 10^{-126}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;d \leq 1.65 \cdot 10^{-105}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+136}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error14.7
Cost7436
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;d \leq -2.1 \cdot 10^{+102}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\ \mathbf{elif}\;d \leq -2.1 \cdot 10^{-126}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;d \leq 9.2 \cdot 10^{-105}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{{c}^{2}}{d}}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+136}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
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 -2.4 \cdot 10^{+104}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.7 \cdot 10^{-127}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.65 \cdot 10^{-105}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 3.8 \cdot 10^{+136}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 5
Error16.2
Cost1488
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := a \cdot c + b \cdot d\\ \mathbf{if}\;d \leq -7.6 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4 \cdot 10^{-125}:\\ \;\;\;\;\frac{1}{t_0} \cdot t_1\\ \mathbf{elif}\;d \leq 1.65 \cdot 10^{-105}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+136}:\\ \;\;\;\;\frac{t_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 6
Error21.1
Cost1232
\[\begin{array}{l} t_0 := \frac{d}{c \cdot c + d \cdot d} \cdot b\\ \mathbf{if}\;d \leq -2 \cdot 10^{+154}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.9 \cdot 10^{-125}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 6.5 \cdot 10^{-88}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+136}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 7
Error22.4
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -4 \cdot 10^{+64}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -8.2 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{c \cdot c + d \cdot d} \cdot d\\ \mathbf{elif}\;d \leq 10^{-13}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 8
Error23.0
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -3.9 \cdot 10^{-75}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 4 \cdot 10^{-15}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 9
Error37.0
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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