?

Average Error: 25.7 → 15.2
Time: 13.9s
Precision: binary64
Cost: 7436

?

\[\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}\\ \mathbf{if}\;d \leq -4.7 \cdot 10^{+123}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4 \cdot 10^{-28}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-101}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.7 \cdot 10^{+109}:\\ \;\;\;\;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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))))
   (if (<= d -4.7e+123)
     (/ b d)
     (if (<= d -4e-28)
       t_0
       (if (<= d 3.4e-101)
         (+ (/ a c) (/ (* d b) (pow c 2.0)))
         (if (<= d 1.7e+109) 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 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -4.7e+123) {
		tmp = b / d;
	} else if (d <= -4e-28) {
		tmp = t_0;
	} else if (d <= 3.4e-101) {
		tmp = (a / c) + ((d * b) / pow(c, 2.0));
	} else if (d <= 1.7e+109) {
		tmp = 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) :: tmp
    t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
    if (d <= (-4.7d+123)) then
        tmp = b / d
    else if (d <= (-4d-28)) then
        tmp = t_0
    else if (d <= 3.4d-101) then
        tmp = (a / c) + ((d * b) / (c ** 2.0d0))
    else if (d <= 1.7d+109) then
        tmp = 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 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -4.7e+123) {
		tmp = b / d;
	} else if (d <= -4e-28) {
		tmp = t_0;
	} else if (d <= 3.4e-101) {
		tmp = (a / c) + ((d * b) / Math.pow(c, 2.0));
	} else if (d <= 1.7e+109) {
		tmp = 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 = ((a * c) + (b * d)) / ((c * c) + (d * d))
	tmp = 0
	if d <= -4.7e+123:
		tmp = b / d
	elif d <= -4e-28:
		tmp = t_0
	elif d <= 3.4e-101:
		tmp = (a / c) + ((d * b) / math.pow(c, 2.0))
	elif d <= 1.7e+109:
		tmp = 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(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (d <= -4.7e+123)
		tmp = Float64(b / d);
	elseif (d <= -4e-28)
		tmp = t_0;
	elseif (d <= 3.4e-101)
		tmp = Float64(Float64(a / c) + Float64(Float64(d * b) / (c ^ 2.0)));
	elseif (d <= 1.7e+109)
		tmp = 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 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	tmp = 0.0;
	if (d <= -4.7e+123)
		tmp = b / d;
	elseif (d <= -4e-28)
		tmp = t_0;
	elseif (d <= 3.4e-101)
		tmp = (a / c) + ((d * b) / (c ^ 2.0));
	elseif (d <= 1.7e+109)
		tmp = 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[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -4.7e+123], N[(b / d), $MachinePrecision], If[LessEqual[d, -4e-28], t$95$0, If[LessEqual[d, 3.4e-101], N[(N[(a / c), $MachinePrecision] + N[(N[(d * b), $MachinePrecision] / N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.7e+109], t$95$0, N[(b / d), $MachinePrecision]]]]]]
\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}\\
\mathbf{if}\;d \leq -4.7 \cdot 10^{+123}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;d \leq -4 \cdot 10^{-28}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;d \leq 1.7 \cdot 10^{+109}:\\
\;\;\;\;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

Original25.7
Target0.5
Herbie15.2
\[\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 < -4.69999999999999979e123 or 1.70000000000000003e109 < d

    1. Initial program 40.5

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

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

    if -4.69999999999999979e123 < d < -3.99999999999999988e-28 or 3.39999999999999989e-101 < d < 1.70000000000000003e109

    1. Initial program 17.4

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

    if -3.99999999999999988e-28 < d < 3.39999999999999989e-101

    1. Initial program 18.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -4.7 \cdot 10^{+123}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4 \cdot 10^{-28}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-101}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.7 \cdot 10^{+109}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error17.7
Cost1752
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.5 \cdot 10^{+125}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4 \cdot 10^{-28}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -6.5 \cdot 10^{-112}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -2.4 \cdot 10^{-234}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{-101}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{+108}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 2
Error22.9
Cost1100
\[\begin{array}{l} \mathbf{if}\;d \leq -5.5 \cdot 10^{+64}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.05 \cdot 10^{+35}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -4.2 \cdot 10^{-28}:\\ \;\;\;\;\frac{d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 2.9 \cdot 10^{-6}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error23.3
Cost720
\[\begin{array}{l} \mathbf{if}\;d \leq -5.8 \cdot 10^{+63}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.6 \cdot 10^{+20}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -4.2 \cdot 10^{-28}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.4 \cdot 10^{-6}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error37.6
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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