?

Average Error: 26.2 → 14.9
Time: 5.8s
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}\;c \leq -5.3 \cdot 10^{+101}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.75 \cdot 10^{-76}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.5 \cdot 10^{-90}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.7 \cdot 10^{+124}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \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 (<= c -5.3e+101)
     (/ a c)
     (if (<= c -3.75e-76)
       t_0
       (if (<= c 3.5e-90)
         (+ (/ b d) (/ (* c a) (pow d 2.0)))
         (if (<= c 1.7e+124) t_0 (/ a c)))))))
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 (c <= -5.3e+101) {
		tmp = a / c;
	} else if (c <= -3.75e-76) {
		tmp = t_0;
	} else if (c <= 3.5e-90) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 1.7e+124) {
		tmp = t_0;
	} else {
		tmp = a / c;
	}
	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 (c <= (-5.3d+101)) then
        tmp = a / c
    else if (c <= (-3.75d-76)) then
        tmp = t_0
    else if (c <= 3.5d-90) then
        tmp = (b / d) + ((c * a) / (d ** 2.0d0))
    else if (c <= 1.7d+124) then
        tmp = t_0
    else
        tmp = a / c
    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 (c <= -5.3e+101) {
		tmp = a / c;
	} else if (c <= -3.75e-76) {
		tmp = t_0;
	} else if (c <= 3.5e-90) {
		tmp = (b / d) + ((c * a) / Math.pow(d, 2.0));
	} else if (c <= 1.7e+124) {
		tmp = t_0;
	} else {
		tmp = a / c;
	}
	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 c <= -5.3e+101:
		tmp = a / c
	elif c <= -3.75e-76:
		tmp = t_0
	elif c <= 3.5e-90:
		tmp = (b / d) + ((c * a) / math.pow(d, 2.0))
	elif c <= 1.7e+124:
		tmp = t_0
	else:
		tmp = a / c
	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 (c <= -5.3e+101)
		tmp = Float64(a / c);
	elseif (c <= -3.75e-76)
		tmp = t_0;
	elseif (c <= 3.5e-90)
		tmp = Float64(Float64(b / d) + Float64(Float64(c * a) / (d ^ 2.0)));
	elseif (c <= 1.7e+124)
		tmp = t_0;
	else
		tmp = Float64(a / c);
	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 (c <= -5.3e+101)
		tmp = a / c;
	elseif (c <= -3.75e-76)
		tmp = t_0;
	elseif (c <= 3.5e-90)
		tmp = (b / d) + ((c * a) / (d ^ 2.0));
	elseif (c <= 1.7e+124)
		tmp = t_0;
	else
		tmp = a / c;
	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[c, -5.3e+101], N[(a / c), $MachinePrecision], If[LessEqual[c, -3.75e-76], t$95$0, If[LessEqual[c, 3.5e-90], N[(N[(b / d), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.7e+124], t$95$0, N[(a / c), $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}\;c \leq -5.3 \cdot 10^{+101}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{elif}\;c \leq -3.75 \cdot 10^{-76}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 1.7 \cdot 10^{+124}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.2
Target0.7
Herbie14.9
\[\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 c < -5.30000000000000006e101 or 1.7e124 < c

    1. Initial program 40.3

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

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

    if -5.30000000000000006e101 < c < -3.7499999999999998e-76 or 3.4999999999999999e-90 < c < 1.7e124

    1. Initial program 16.6

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

    if -3.7499999999999998e-76 < c < 3.4999999999999999e-90

    1. Initial program 21.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.3 \cdot 10^{+101}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.75 \cdot 10^{-76}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 3.5 \cdot 10^{-90}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.7 \cdot 10^{+124}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]

Alternatives

Alternative 1
Error23.6
Cost1760
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{c \cdot a}{t_0}\\ \mathbf{if}\;c \leq -7 \cdot 10^{+105}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -4.5 \cdot 10^{-93}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{-197}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{-170}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 2.1 \cdot 10^{-113}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 8 \cdot 10^{-62}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 1.25 \cdot 10^{-22}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 8 \cdot 10^{+69}:\\ \;\;\;\;\frac{d \cdot b}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 2
Error23.5
Cost1496
\[\begin{array}{l} t_0 := \frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -1.7 \cdot 10^{+103}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.4 \cdot 10^{-92}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{-197}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{-170}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.1 \cdot 10^{-114}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 7.6 \cdot 10^{-63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 8.2 \cdot 10^{+70}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 3
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 -1.9 \cdot 10^{+113}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -7.4 \cdot 10^{-165}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.62 \cdot 10^{-124}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2.2 \cdot 10^{+127}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error23.8
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -1.2 \cdot 10^{+27}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{+113}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 5
Error37.4
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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