?

Average Error: 26.5 → 18.4
Time: 1.8min
Precision: binary64
Cost: 7564

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{b}{d} + c \cdot \left({\left(\frac{1}{d}\right)}^{2} \cdot a\right)\\ \mathbf{if}\;d \leq -3.1 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{-152}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{-81}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \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 (+ (/ b d) (* c (* (pow (/ 1.0 d) 2.0) a)))))
   (if (<= d -3.1e+53)
     t_0
     (if (<= d -4.5e-152)
       (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
       (if (<= d 1.5e-81) (/ a c) t_0)))))
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 = (b / d) + (c * (pow((1.0 / d), 2.0) * a));
	double tmp;
	if (d <= -3.1e+53) {
		tmp = t_0;
	} else if (d <= -4.5e-152) {
		tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
	} else if (d <= 1.5e-81) {
		tmp = a / c;
	} else {
		tmp = t_0;
	}
	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 = (b / d) + (c * (((1.0d0 / d) ** 2.0d0) * a))
    if (d <= (-3.1d+53)) then
        tmp = t_0
    else if (d <= (-4.5d-152)) then
        tmp = ((a * c) + (b * d)) / ((c * c) + (d * d))
    else if (d <= 1.5d-81) then
        tmp = a / c
    else
        tmp = t_0
    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 = (b / d) + (c * (Math.pow((1.0 / d), 2.0) * a));
	double tmp;
	if (d <= -3.1e+53) {
		tmp = t_0;
	} else if (d <= -4.5e-152) {
		tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
	} else if (d <= 1.5e-81) {
		tmp = a / c;
	} else {
		tmp = t_0;
	}
	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 = (b / d) + (c * (math.pow((1.0 / d), 2.0) * a))
	tmp = 0
	if d <= -3.1e+53:
		tmp = t_0
	elif d <= -4.5e-152:
		tmp = ((a * c) + (b * d)) / ((c * c) + (d * d))
	elif d <= 1.5e-81:
		tmp = a / c
	else:
		tmp = t_0
	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(b / d) + Float64(c * Float64((Float64(1.0 / d) ^ 2.0) * a)))
	tmp = 0.0
	if (d <= -3.1e+53)
		tmp = t_0;
	elseif (d <= -4.5e-152)
		tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)));
	elseif (d <= 1.5e-81)
		tmp = Float64(a / c);
	else
		tmp = t_0;
	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 = (b / d) + (c * (((1.0 / d) ^ 2.0) * a));
	tmp = 0.0;
	if (d <= -3.1e+53)
		tmp = t_0;
	elseif (d <= -4.5e-152)
		tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
	elseif (d <= 1.5e-81)
		tmp = a / c;
	else
		tmp = t_0;
	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[(b / d), $MachinePrecision] + N[(c * N[(N[Power[N[(1.0 / d), $MachinePrecision], 2.0], $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -3.1e+53], t$95$0, If[LessEqual[d, -4.5e-152], 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, 1.5e-81], N[(a / c), $MachinePrecision], t$95$0]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b}{d} + c \cdot \left({\left(\frac{1}{d}\right)}^{2} \cdot a\right)\\
\mathbf{if}\;d \leq -3.1 \cdot 10^{+53}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq -4.5 \cdot 10^{-152}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\

\mathbf{elif}\;d \leq 1.5 \cdot 10^{-81}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.5
Target0.5
Herbie18.4
\[\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 < -3.10000000000000019e53 or 1.4999999999999999e-81 < d

    1. Initial program 31.8

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

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

    if -3.10000000000000019e53 < d < -4.5000000000000004e-152

    1. Initial program 15.2

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

    if -4.5000000000000004e-152 < d < 1.4999999999999999e-81

    1. Initial program 22.3

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

      \[\leadsto \color{blue}{\frac{a}{c}} \]
  3. Recombined 3 regimes into one program.

Alternatives

Alternative 1
Error16.9
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.75 \cdot 10^{+104}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.3 \cdot 10^{-152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{-100}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 8.6 \cdot 10^{+52}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 2
Error23.5
Cost1100
\[\begin{array}{l} \mathbf{if}\;d \leq -1.25 \cdot 10^{-15}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{-81}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 1.05 \cdot 10^{-16}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error23.4
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -5.7 \cdot 10^{-18}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 5.1 \cdot 10^{-17}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error38.0
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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