Complex division, real part

Percentage Accurate: 62.2% → 82.9%
Time: 8.6s
Alternatives: 6
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
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
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
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]
\begin{array}{l}

\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 62.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
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
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
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]
\begin{array}{l}

\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}

Alternative 1: 82.9% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{a \cdot c + d \cdot b}{c \cdot c + d \cdot d}\\ t_1 := \frac{b + \frac{a}{d} \cdot c}{d}\\ \mathbf{if}\;d \leq -4 \cdot 10^{+80}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d \leq -8.8 \cdot 10^{-112}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{-164}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{+25}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ (* a c) (* d b)) (+ (* c c) (* d d))))
        (t_1 (/ (+ b (* (/ a d) c)) d)))
   (if (<= d -4e+80)
     t_1
     (if (<= d -8.8e-112)
       t_0
       (if (<= d 3.5e-164)
         (/ (+ a (/ (* d b) c)) c)
         (if (<= d 2.8e+25) t_0 t_1))))))
double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (d * b)) / ((c * c) + (d * d));
	double t_1 = (b + ((a / d) * c)) / d;
	double tmp;
	if (d <= -4e+80) {
		tmp = t_1;
	} else if (d <= -8.8e-112) {
		tmp = t_0;
	} else if (d <= 3.5e-164) {
		tmp = (a + ((d * b) / c)) / c;
	} else if (d <= 2.8e+25) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	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
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = ((a * c) + (d * b)) / ((c * c) + (d * d))
    t_1 = (b + ((a / d) * c)) / d
    if (d <= (-4d+80)) then
        tmp = t_1
    else if (d <= (-8.8d-112)) then
        tmp = t_0
    else if (d <= 3.5d-164) then
        tmp = (a + ((d * b) / c)) / c
    else if (d <= 2.8d+25) then
        tmp = t_0
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (d * b)) / ((c * c) + (d * d));
	double t_1 = (b + ((a / d) * c)) / d;
	double tmp;
	if (d <= -4e+80) {
		tmp = t_1;
	} else if (d <= -8.8e-112) {
		tmp = t_0;
	} else if (d <= 3.5e-164) {
		tmp = (a + ((d * b) / c)) / c;
	} else if (d <= 2.8e+25) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = ((a * c) + (d * b)) / ((c * c) + (d * d))
	t_1 = (b + ((a / d) * c)) / d
	tmp = 0
	if d <= -4e+80:
		tmp = t_1
	elif d <= -8.8e-112:
		tmp = t_0
	elif d <= 3.5e-164:
		tmp = (a + ((d * b) / c)) / c
	elif d <= 2.8e+25:
		tmp = t_0
	else:
		tmp = t_1
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(Float64(a * c) + Float64(d * b)) / Float64(Float64(c * c) + Float64(d * d)))
	t_1 = Float64(Float64(b + Float64(Float64(a / d) * c)) / d)
	tmp = 0.0
	if (d <= -4e+80)
		tmp = t_1;
	elseif (d <= -8.8e-112)
		tmp = t_0;
	elseif (d <= 3.5e-164)
		tmp = Float64(Float64(a + Float64(Float64(d * b) / c)) / c);
	elseif (d <= 2.8e+25)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = ((a * c) + (d * b)) / ((c * c) + (d * d));
	t_1 = (b + ((a / d) * c)) / d;
	tmp = 0.0;
	if (d <= -4e+80)
		tmp = t_1;
	elseif (d <= -8.8e-112)
		tmp = t_0;
	elseif (d <= 3.5e-164)
		tmp = (a + ((d * b) / c)) / c;
	elseif (d <= 2.8e+25)
		tmp = t_0;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b + N[(N[(a / d), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -4e+80], t$95$1, If[LessEqual[d, -8.8e-112], t$95$0, If[LessEqual[d, 3.5e-164], N[(N[(a + N[(N[(d * b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 2.8e+25], t$95$0, t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{a \cdot c + d \cdot b}{c \cdot c + d \cdot d}\\
t_1 := \frac{b + \frac{a}{d} \cdot c}{d}\\
\mathbf{if}\;d \leq -4 \cdot 10^{+80}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d \leq -8.8 \cdot 10^{-112}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;d \leq 2.8 \cdot 10^{+25}:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -4e80 or 2.8000000000000002e25 < d

    1. Initial program 40.9%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(b + \frac{a \cdot c}{d}\right), \color{blue}{d}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(\frac{a \cdot c}{d}\right)\right), d\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\left(a \cdot c\right), d\right)\right), d\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\left(c \cdot a\right), d\right)\right), d\right) \]
      5. *-lowering-*.f6480.7%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\mathsf{*.f64}\left(c, a\right), d\right)\right), d\right) \]
    5. Simplified80.7%

      \[\leadsto \color{blue}{\frac{b + \frac{c \cdot a}{d}}{d}} \]
    6. Step-by-step derivation
      1. associate-/l*N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(c \cdot \frac{a}{d}\right)\right), d\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(\frac{a}{d} \cdot c\right)\right), d\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{*.f64}\left(\left(\frac{a}{d}\right), c\right)\right), d\right) \]
      4. /-lowering-/.f6483.1%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{*.f64}\left(\mathsf{/.f64}\left(a, d\right), c\right)\right), d\right) \]
    7. Applied egg-rr83.1%

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

    if -4e80 < d < -8.80000000000000085e-112 or 3.5e-164 < d < 2.8000000000000002e25

    1. Initial program 84.1%

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Add Preprocessing

    if -8.80000000000000085e-112 < d < 3.5e-164

    1. Initial program 59.4%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(a + \frac{b \cdot d}{c}\right), \color{blue}{c}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \left(\frac{b \cdot d}{c}\right)\right), c\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\left(b \cdot d\right), c\right)\right), c\right) \]
      4. *-lowering-*.f6492.7%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\mathsf{*.f64}\left(b, d\right), c\right)\right), c\right) \]
    5. Simplified92.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -4 \cdot 10^{+80}:\\ \;\;\;\;\frac{b + \frac{a}{d} \cdot c}{d}\\ \mathbf{elif}\;d \leq -8.8 \cdot 10^{-112}:\\ \;\;\;\;\frac{a \cdot c + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{-164}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{+25}:\\ \;\;\;\;\frac{a \cdot c + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + \frac{a}{d} \cdot c}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 78.1% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{b + \frac{a}{d} \cdot c}{d}\\ \mathbf{if}\;d \leq -270000000000:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{+87}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ b (* (/ a d) c)) d)))
   (if (<= d -270000000000.0)
     t_0
     (if (<= d 3.9e+87) (/ (+ a (/ (* d b) c)) c) t_0))))
double code(double a, double b, double c, double d) {
	double t_0 = (b + ((a / d) * c)) / d;
	double tmp;
	if (d <= -270000000000.0) {
		tmp = t_0;
	} else if (d <= 3.9e+87) {
		tmp = (a + ((d * b) / c)) / 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
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (b + ((a / d) * c)) / d
    if (d <= (-270000000000.0d0)) then
        tmp = t_0
    else if (d <= 3.9d+87) then
        tmp = (a + ((d * b) / c)) / c
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double t_0 = (b + ((a / d) * c)) / d;
	double tmp;
	if (d <= -270000000000.0) {
		tmp = t_0;
	} else if (d <= 3.9e+87) {
		tmp = (a + ((d * b) / c)) / c;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = (b + ((a / d) * c)) / d
	tmp = 0
	if d <= -270000000000.0:
		tmp = t_0
	elif d <= 3.9e+87:
		tmp = (a + ((d * b) / c)) / c
	else:
		tmp = t_0
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(b + Float64(Float64(a / d) * c)) / d)
	tmp = 0.0
	if (d <= -270000000000.0)
		tmp = t_0;
	elseif (d <= 3.9e+87)
		tmp = Float64(Float64(a + Float64(Float64(d * b) / c)) / c);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = (b + ((a / d) * c)) / d;
	tmp = 0.0;
	if (d <= -270000000000.0)
		tmp = t_0;
	elseif (d <= 3.9e+87)
		tmp = (a + ((d * b) / c)) / c;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(b + N[(N[(a / d), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -270000000000.0], t$95$0, If[LessEqual[d, 3.9e+87], N[(N[(a + N[(N[(d * b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{b + \frac{a}{d} \cdot c}{d}\\
\mathbf{if}\;d \leq -270000000000:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;d \leq 3.9 \cdot 10^{+87}:\\
\;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -2.7e11 or 3.9000000000000002e87 < d

    1. Initial program 50.2%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(b + \frac{a \cdot c}{d}\right), \color{blue}{d}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(\frac{a \cdot c}{d}\right)\right), d\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\left(a \cdot c\right), d\right)\right), d\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\left(c \cdot a\right), d\right)\right), d\right) \]
      5. *-lowering-*.f6482.9%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\mathsf{*.f64}\left(c, a\right), d\right)\right), d\right) \]
    5. Simplified82.9%

      \[\leadsto \color{blue}{\frac{b + \frac{c \cdot a}{d}}{d}} \]
    6. Step-by-step derivation
      1. associate-/l*N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(c \cdot \frac{a}{d}\right)\right), d\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(\frac{a}{d} \cdot c\right)\right), d\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{*.f64}\left(\left(\frac{a}{d}\right), c\right)\right), d\right) \]
      4. /-lowering-/.f6485.0%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{*.f64}\left(\mathsf{/.f64}\left(a, d\right), c\right)\right), d\right) \]
    7. Applied egg-rr85.0%

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

    if -2.7e11 < d < 3.9000000000000002e87

    1. Initial program 68.2%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(a + \frac{b \cdot d}{c}\right), \color{blue}{c}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \left(\frac{b \cdot d}{c}\right)\right), c\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\left(b \cdot d\right), c\right)\right), c\right) \]
      4. *-lowering-*.f6474.7%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\mathsf{*.f64}\left(b, d\right), c\right)\right), c\right) \]
    5. Simplified74.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -270000000000:\\ \;\;\;\;\frac{b + \frac{a}{d} \cdot c}{d}\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{+87}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + \frac{a}{d} \cdot c}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 75.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{if}\;c \leq -4.2 \cdot 10^{+72}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+31}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ a (/ (* d b) c)) c)))
   (if (<= c -4.2e+72) t_0 (if (<= c 1.9e+31) (/ (+ b (* a (/ c d))) d) t_0))))
double code(double a, double b, double c, double d) {
	double t_0 = (a + ((d * b) / c)) / c;
	double tmp;
	if (c <= -4.2e+72) {
		tmp = t_0;
	} else if (c <= 1.9e+31) {
		tmp = (b + (a * (c / d))) / d;
	} 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
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (a + ((d * b) / c)) / c
    if (c <= (-4.2d+72)) then
        tmp = t_0
    else if (c <= 1.9d+31) then
        tmp = (b + (a * (c / d))) / d
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double t_0 = (a + ((d * b) / c)) / c;
	double tmp;
	if (c <= -4.2e+72) {
		tmp = t_0;
	} else if (c <= 1.9e+31) {
		tmp = (b + (a * (c / d))) / d;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = (a + ((d * b) / c)) / c
	tmp = 0
	if c <= -4.2e+72:
		tmp = t_0
	elif c <= 1.9e+31:
		tmp = (b + (a * (c / d))) / d
	else:
		tmp = t_0
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(a + Float64(Float64(d * b) / c)) / c)
	tmp = 0.0
	if (c <= -4.2e+72)
		tmp = t_0;
	elseif (c <= 1.9e+31)
		tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = (a + ((d * b) / c)) / c;
	tmp = 0.0;
	if (c <= -4.2e+72)
		tmp = t_0;
	elseif (c <= 1.9e+31)
		tmp = (b + (a * (c / d))) / d;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a + N[(N[(d * b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -4.2e+72], t$95$0, If[LessEqual[c, 1.9e+31], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{a + \frac{d \cdot b}{c}}{c}\\
\mathbf{if}\;c \leq -4.2 \cdot 10^{+72}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;c \leq 1.9 \cdot 10^{+31}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -4.2000000000000003e72 or 1.9000000000000001e31 < c

    1. Initial program 49.0%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(a + \frac{b \cdot d}{c}\right), \color{blue}{c}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \left(\frac{b \cdot d}{c}\right)\right), c\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\left(b \cdot d\right), c\right)\right), c\right) \]
      4. *-lowering-*.f6477.4%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\mathsf{*.f64}\left(b, d\right), c\right)\right), c\right) \]
    5. Simplified77.4%

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

    if -4.2000000000000003e72 < c < 1.9000000000000001e31

    1. Initial program 69.9%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(b + \frac{a \cdot c}{d}\right), \color{blue}{d}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(\frac{a \cdot c}{d}\right)\right), d\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\left(a \cdot c\right), d\right)\right), d\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\left(c \cdot a\right), d\right)\right), d\right) \]
      5. *-lowering-*.f6478.9%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{/.f64}\left(\mathsf{*.f64}\left(c, a\right), d\right)\right), d\right) \]
    5. Simplified78.9%

      \[\leadsto \color{blue}{\frac{b + \frac{c \cdot a}{d}}{d}} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(\frac{a \cdot c}{d}\right)\right), d\right) \]
      2. associate-/l*N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \left(a \cdot \frac{c}{d}\right)\right), d\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{*.f64}\left(a, \left(\frac{c}{d}\right)\right)\right), d\right) \]
      4. /-lowering-/.f6479.1%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(b, \mathsf{*.f64}\left(a, \mathsf{/.f64}\left(c, d\right)\right)\right), d\right) \]
    7. Applied egg-rr79.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.2 \cdot 10^{+72}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+31}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 72.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -7 \cdot 10^{+16}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+82}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (<= d -7e+16)
   (/ b d)
   (if (<= d 2.35e+82) (/ (+ a (/ (* d b) c)) c) (/ b d))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (d <= -7e+16) {
		tmp = b / d;
	} else if (d <= 2.35e+82) {
		tmp = (a + ((d * b) / c)) / c;
	} 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
    real(8) :: tmp
    if (d <= (-7d+16)) then
        tmp = b / d
    else if (d <= 2.35d+82) then
        tmp = (a + ((d * b) / c)) / c
    else
        tmp = b / d
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if (d <= -7e+16) {
		tmp = b / d;
	} else if (d <= 2.35e+82) {
		tmp = (a + ((d * b) / c)) / c;
	} else {
		tmp = b / d;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if d <= -7e+16:
		tmp = b / d
	elif d <= 2.35e+82:
		tmp = (a + ((d * b) / c)) / c
	else:
		tmp = b / d
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if (d <= -7e+16)
		tmp = Float64(b / d);
	elseif (d <= 2.35e+82)
		tmp = Float64(Float64(a + Float64(Float64(d * b) / c)) / c);
	else
		tmp = Float64(b / d);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if (d <= -7e+16)
		tmp = b / d;
	elseif (d <= 2.35e+82)
		tmp = (a + ((d * b) / c)) / c;
	else
		tmp = b / d;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[LessEqual[d, -7e+16], N[(b / d), $MachinePrecision], If[LessEqual[d, 2.35e+82], N[(N[(a + N[(N[(d * b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d \leq -7 \cdot 10^{+16}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;d \leq 2.35 \cdot 10^{+82}:\\
\;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -7e16 or 2.35e82 < d

    1. Initial program 50.2%

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

      \[\leadsto \color{blue}{\frac{b}{d}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f6473.0%

        \[\leadsto \mathsf{/.f64}\left(b, \color{blue}{d}\right) \]
    5. Simplified73.0%

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

    if -7e16 < d < 2.35e82

    1. Initial program 68.2%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(a + \frac{b \cdot d}{c}\right), \color{blue}{c}\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \left(\frac{b \cdot d}{c}\right)\right), c\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\left(b \cdot d\right), c\right)\right), c\right) \]
      4. *-lowering-*.f6474.7%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{+.f64}\left(a, \mathsf{/.f64}\left(\mathsf{*.f64}\left(b, d\right), c\right)\right), c\right) \]
    5. Simplified74.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -7 \cdot 10^{+16}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+82}:\\ \;\;\;\;\frac{a + \frac{d \cdot b}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 63.2% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;c \leq -4.3 \cdot 10^{+72}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 3.2 \cdot 10^{+33}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (<= c -4.3e+72) (/ a c) (if (<= c 3.2e+33) (/ b d) (/ a c))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -4.3e+72) {
		tmp = a / c;
	} else if (c <= 3.2e+33) {
		tmp = b / d;
	} 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
    real(8) :: tmp
    if (c <= (-4.3d+72)) then
        tmp = a / c
    else if (c <= 3.2d+33) then
        tmp = b / d
    else
        tmp = a / c
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -4.3e+72) {
		tmp = a / c;
	} else if (c <= 3.2e+33) {
		tmp = b / d;
	} else {
		tmp = a / c;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if c <= -4.3e+72:
		tmp = a / c
	elif c <= 3.2e+33:
		tmp = b / d
	else:
		tmp = a / c
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if (c <= -4.3e+72)
		tmp = Float64(a / c);
	elseif (c <= 3.2e+33)
		tmp = Float64(b / d);
	else
		tmp = Float64(a / c);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if (c <= -4.3e+72)
		tmp = a / c;
	elseif (c <= 3.2e+33)
		tmp = b / d;
	else
		tmp = a / c;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[LessEqual[c, -4.3e+72], N[(a / c), $MachinePrecision], If[LessEqual[c, 3.2e+33], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;c \leq -4.3 \cdot 10^{+72}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{elif}\;c \leq 3.2 \cdot 10^{+33}:\\
\;\;\;\;\frac{b}{d}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -4.3000000000000001e72 or 3.20000000000000017e33 < c

    1. Initial program 49.0%

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

      \[\leadsto \color{blue}{\frac{a}{c}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f6468.0%

        \[\leadsto \mathsf{/.f64}\left(a, \color{blue}{c}\right) \]
    5. Simplified68.0%

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

    if -4.3000000000000001e72 < c < 3.20000000000000017e33

    1. Initial program 69.9%

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

      \[\leadsto \color{blue}{\frac{b}{d}} \]
    4. Step-by-step derivation
      1. /-lowering-/.f6465.2%

        \[\leadsto \mathsf{/.f64}\left(b, \color{blue}{d}\right) \]
    5. Simplified65.2%

      \[\leadsto \color{blue}{\frac{b}{d}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 43.0% accurate, 5.0× speedup?

\[\begin{array}{l} \\ \frac{a}{c} \end{array} \]
(FPCore (a b c d) :precision binary64 (/ a c))
double code(double a, double b, double c, double d) {
	return a / c;
}
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
end function
public static double code(double a, double b, double c, double d) {
	return a / c;
}
def code(a, b, c, d):
	return a / c
function code(a, b, c, d)
	return Float64(a / c)
end
function tmp = code(a, b, c, d)
	tmp = a / c;
end
code[a_, b_, c_, d_] := N[(a / c), $MachinePrecision]
\begin{array}{l}

\\
\frac{a}{c}
\end{array}
Derivation
  1. Initial program 61.5%

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

    \[\leadsto \color{blue}{\frac{a}{c}} \]
  4. Step-by-step derivation
    1. /-lowering-/.f6440.2%

      \[\leadsto \mathsf{/.f64}\left(a, \color{blue}{c}\right) \]
  5. Simplified40.2%

    \[\leadsto \color{blue}{\frac{a}{c}} \]
  6. Add Preprocessing

Developer Target 1: 99.3% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \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} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (< (fabs d) (fabs c))
   (/ (+ a (* b (/ d c))) (+ c (* d (/ d c))))
   (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (fabs(d) < fabs(c)) {
		tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
	} else {
		tmp = (b + (a * (c / d))) / (d + (c * (c / 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
    real(8) :: tmp
    if (abs(d) < abs(c)) then
        tmp = (a + (b * (d / c))) / (c + (d * (d / c)))
    else
        tmp = (b + (a * (c / d))) / (d + (c * (c / d)))
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if (Math.abs(d) < Math.abs(c)) {
		tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
	} else {
		tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if math.fabs(d) < math.fabs(c):
		tmp = (a + (b * (d / c))) / (c + (d * (d / c)))
	else:
		tmp = (b + (a * (c / d))) / (d + (c * (c / d)))
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if (abs(d) < abs(c))
		tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / Float64(c + Float64(d * Float64(d / c))));
	else
		tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / Float64(d + Float64(c * Float64(c / d))));
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if (abs(d) < abs(c))
		tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
	else
		tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[Less[N[Abs[d], $MachinePrecision], N[Abs[c], $MachinePrecision]], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c + N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(d + N[(c * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\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}
\end{array}

Reproduce

?
herbie shell --seed 2024139 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :alt
  (! :herbie-platform default (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))))