Complex division, real part

Percentage Accurate: 61.6% → 81.3%
Time: 6.4s
Alternatives: 9
Speedup: 1.6×

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 9 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: 61.6% 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: 81.3% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(c, c, d \cdot d\right)\\ t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, c \cdot a\right)}}\\ \mathbf{elif}\;d \leq 8 \cdot 10^{-73}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\ \mathbf{elif}\;d \leq 1.55 \cdot 10^{+55}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{t\_0}, \frac{d}{a}, \frac{c}{t\_0}\right) \cdot a\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (fma c c (* d d))) (t_1 (/ (fma (/ a d) c b) d)))
   (if (<= d -7e+116)
     t_1
     (if (<= d -6.6e-59)
       (/ 1.0 (/ (fma d d (* c c)) (fma d b (* c a))))
       (if (<= d 8e-73)
         (/ (fma (/ b c) d a) c)
         (if (<= d 1.55e+55) (* (fma (/ b t_0) (/ d a) (/ c t_0)) a) t_1))))))
double code(double a, double b, double c, double d) {
	double t_0 = fma(c, c, (d * d));
	double t_1 = fma((a / d), c, b) / d;
	double tmp;
	if (d <= -7e+116) {
		tmp = t_1;
	} else if (d <= -6.6e-59) {
		tmp = 1.0 / (fma(d, d, (c * c)) / fma(d, b, (c * a)));
	} else if (d <= 8e-73) {
		tmp = fma((b / c), d, a) / c;
	} else if (d <= 1.55e+55) {
		tmp = fma((b / t_0), (d / a), (c / t_0)) * a;
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = fma(c, c, Float64(d * d))
	t_1 = Float64(fma(Float64(a / d), c, b) / d)
	tmp = 0.0
	if (d <= -7e+116)
		tmp = t_1;
	elseif (d <= -6.6e-59)
		tmp = Float64(1.0 / Float64(fma(d, d, Float64(c * c)) / fma(d, b, Float64(c * a))));
	elseif (d <= 8e-73)
		tmp = Float64(fma(Float64(b / c), d, a) / c);
	elseif (d <= 1.55e+55)
		tmp = Float64(fma(Float64(b / t_0), Float64(d / a), Float64(c / t_0)) * a);
	else
		tmp = t_1;
	end
	return tmp
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -7e+116], t$95$1, If[LessEqual[d, -6.6e-59], N[(1.0 / N[(N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 8e-73], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.55e+55], N[(N[(N[(b / t$95$0), $MachinePrecision] * N[(d / a), $MachinePrecision] + N[(c / t$95$0), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision], t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(c, c, d \cdot d\right)\\
t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\
\;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, c \cdot a\right)}}\\

\mathbf{elif}\;d \leq 8 \cdot 10^{-73}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\

\mathbf{elif}\;d \leq 1.55 \cdot 10^{+55}:\\
\;\;\;\;\mathsf{fma}\left(\frac{b}{t\_0}, \frac{d}{a}, \frac{c}{t\_0}\right) \cdot a\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d < -6.99999999999999993e116 or 1.54999999999999997e55 < d

    1. Initial program 33.1%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{c \cdot \frac{a}{d}} + b}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a}{d} \cdot c} + b}{d} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}}{d} \]
      7. lower-/.f6491.0

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{a}{d}}, c, b\right)}{d} \]
    5. Applied rewrites91.0%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}} \]

    if -6.99999999999999993e116 < d < -6.59999999999999964e-59

    1. Initial program 82.5%

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}} \]
      2. clear-numN/A

        \[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}} \]
      3. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}} \]
      4. lower-/.f6482.6

        \[\leadsto \frac{1}{\color{blue}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}} \]
      5. lift-+.f64N/A

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

        \[\leadsto \frac{1}{\frac{\color{blue}{d \cdot d + c \cdot c}}{a \cdot c + b \cdot d}} \]
      7. lift-*.f64N/A

        \[\leadsto \frac{1}{\frac{\color{blue}{d \cdot d} + c \cdot c}{a \cdot c + b \cdot d}} \]
      8. lower-fma.f6482.6

        \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{fma}\left(d, d, c \cdot c\right)}}{a \cdot c + b \cdot d}} \]
      9. lift-+.f64N/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{a \cdot c + b \cdot d}}} \]
      10. +-commutativeN/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{b \cdot d + a \cdot c}}} \]
      11. lift-*.f64N/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{b \cdot d} + a \cdot c}} \]
      12. *-commutativeN/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{d \cdot b} + a \cdot c}} \]
      13. lower-fma.f6482.6

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{\mathsf{fma}\left(d, b, a \cdot c\right)}}} \]
      14. lift-*.f64N/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, \color{blue}{a \cdot c}\right)}} \]
      15. *-commutativeN/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, \color{blue}{c \cdot a}\right)}} \]
      16. lower-*.f6482.6

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, \color{blue}{c \cdot a}\right)}} \]
    4. Applied rewrites82.6%

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

    if -6.59999999999999964e-59 < d < 7.99999999999999998e-73

    1. Initial program 71.9%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b \cdot d}{c} + a}}{c} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{d \cdot b}}{c} + a}{c} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{d \cdot \frac{b}{c}} + a}{c} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b}{c} \cdot d} + a}{c} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}}{c} \]
      7. lower-/.f6492.5

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{b}{c}}, d, a\right)}{c} \]
    5. Applied rewrites92.5%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}} \]

    if 7.99999999999999998e-73 < d < 1.54999999999999997e55

    1. Initial program 84.7%

      \[\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. lower-/.f6443.7

        \[\leadsto \color{blue}{\frac{a}{c}} \]
    5. Applied rewrites43.7%

      \[\leadsto \color{blue}{\frac{a}{c}} \]
    6. Taylor expanded in a around inf

      \[\leadsto \color{blue}{a \cdot \left(\frac{c}{{c}^{2} + {d}^{2}} + \frac{b \cdot d}{a \cdot \left({c}^{2} + {d}^{2}\right)}\right)} \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\frac{c}{{c}^{2} + {d}^{2}} + \frac{b \cdot d}{a \cdot \left({c}^{2} + {d}^{2}\right)}\right) \cdot a} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{c}{{c}^{2} + {d}^{2}} + \frac{b \cdot d}{a \cdot \left({c}^{2} + {d}^{2}\right)}\right) \cdot a} \]
    8. Applied rewrites89.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{b}{\mathsf{fma}\left(c, c, d \cdot d\right)}, \frac{d}{a}, \frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)}\right) \cdot a} \]
  3. Recombined 4 regimes into one program.
  4. Add Preprocessing

Alternative 2: 81.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, c \cdot a\right)}}\\ t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 1.85 \cdot 10^{-76}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\ \mathbf{elif}\;d \leq 1.76 \cdot 10^{+93}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ 1.0 (/ (fma d d (* c c)) (fma d b (* c a)))))
        (t_1 (/ (fma (/ a d) c b) d)))
   (if (<= d -7e+116)
     t_1
     (if (<= d -6.6e-59)
       t_0
       (if (<= d 1.85e-76)
         (/ (fma (/ b c) d a) c)
         (if (<= d 1.76e+93) t_0 t_1))))))
double code(double a, double b, double c, double d) {
	double t_0 = 1.0 / (fma(d, d, (c * c)) / fma(d, b, (c * a)));
	double t_1 = fma((a / d), c, b) / d;
	double tmp;
	if (d <= -7e+116) {
		tmp = t_1;
	} else if (d <= -6.6e-59) {
		tmp = t_0;
	} else if (d <= 1.85e-76) {
		tmp = fma((b / c), d, a) / c;
	} else if (d <= 1.76e+93) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = Float64(1.0 / Float64(fma(d, d, Float64(c * c)) / fma(d, b, Float64(c * a))))
	t_1 = Float64(fma(Float64(a / d), c, b) / d)
	tmp = 0.0
	if (d <= -7e+116)
		tmp = t_1;
	elseif (d <= -6.6e-59)
		tmp = t_0;
	elseif (d <= 1.85e-76)
		tmp = Float64(fma(Float64(b / c), d, a) / c);
	elseif (d <= 1.76e+93)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(1.0 / N[(N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -7e+116], t$95$1, If[LessEqual[d, -6.6e-59], t$95$0, If[LessEqual[d, 1.85e-76], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.76e+93], t$95$0, t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, c \cdot a\right)}}\\
t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;d \leq 1.85 \cdot 10^{-76}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\

\mathbf{elif}\;d \leq 1.76 \cdot 10^{+93}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -6.99999999999999993e116 or 1.75999999999999994e93 < d

    1. Initial program 30.5%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{c \cdot \frac{a}{d}} + b}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a}{d} \cdot c} + b}{d} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}}{d} \]
      7. lower-/.f6491.6

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{a}{d}}, c, b\right)}{d} \]
    5. Applied rewrites91.6%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}} \]

    if -6.99999999999999993e116 < d < -6.59999999999999964e-59 or 1.85000000000000006e-76 < d < 1.75999999999999994e93

    1. Initial program 83.1%

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}} \]
      2. clear-numN/A

        \[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}} \]
      3. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}} \]
      4. lower-/.f6483.2

        \[\leadsto \frac{1}{\color{blue}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}} \]
      5. lift-+.f64N/A

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

        \[\leadsto \frac{1}{\frac{\color{blue}{d \cdot d + c \cdot c}}{a \cdot c + b \cdot d}} \]
      7. lift-*.f64N/A

        \[\leadsto \frac{1}{\frac{\color{blue}{d \cdot d} + c \cdot c}{a \cdot c + b \cdot d}} \]
      8. lower-fma.f6483.2

        \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{fma}\left(d, d, c \cdot c\right)}}{a \cdot c + b \cdot d}} \]
      9. lift-+.f64N/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{a \cdot c + b \cdot d}}} \]
      10. +-commutativeN/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{b \cdot d + a \cdot c}}} \]
      11. lift-*.f64N/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{b \cdot d} + a \cdot c}} \]
      12. *-commutativeN/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{d \cdot b} + a \cdot c}} \]
      13. lower-fma.f6483.2

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\color{blue}{\mathsf{fma}\left(d, b, a \cdot c\right)}}} \]
      14. lift-*.f64N/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, \color{blue}{a \cdot c}\right)}} \]
      15. *-commutativeN/A

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, \color{blue}{c \cdot a}\right)}} \]
      16. lower-*.f6483.2

        \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, \color{blue}{c \cdot a}\right)}} \]
    4. Applied rewrites83.2%

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

    if -6.59999999999999964e-59 < d < 1.85000000000000006e-76

    1. Initial program 71.9%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b \cdot d}{c} + a}}{c} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{d \cdot b}}{c} + a}{c} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{d \cdot \frac{b}{c}} + a}{c} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b}{c} \cdot d} + a}{c} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}}{c} \]
      7. lower-/.f6492.5

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{b}{c}}, d, a\right)}{c} \]
    5. Applied rewrites92.5%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 82.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{b \cdot d + c \cdot a}{d \cdot d + c \cdot c}\\ t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 1.85 \cdot 10^{-76}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\ \mathbf{elif}\;d \leq 1.06 \cdot 10^{+83}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ (* b d) (* c a)) (+ (* d d) (* c c))))
        (t_1 (/ (fma (/ a d) c b) d)))
   (if (<= d -7e+116)
     t_1
     (if (<= d -6.6e-59)
       t_0
       (if (<= d 1.85e-76)
         (/ (fma (/ b c) d a) c)
         (if (<= d 1.06e+83) t_0 t_1))))))
double code(double a, double b, double c, double d) {
	double t_0 = ((b * d) + (c * a)) / ((d * d) + (c * c));
	double t_1 = fma((a / d), c, b) / d;
	double tmp;
	if (d <= -7e+116) {
		tmp = t_1;
	} else if (d <= -6.6e-59) {
		tmp = t_0;
	} else if (d <= 1.85e-76) {
		tmp = fma((b / c), d, a) / c;
	} else if (d <= 1.06e+83) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = Float64(Float64(Float64(b * d) + Float64(c * a)) / Float64(Float64(d * d) + Float64(c * c)))
	t_1 = Float64(fma(Float64(a / d), c, b) / d)
	tmp = 0.0
	if (d <= -7e+116)
		tmp = t_1;
	elseif (d <= -6.6e-59)
		tmp = t_0;
	elseif (d <= 1.85e-76)
		tmp = Float64(fma(Float64(b / c), d, a) / c);
	elseif (d <= 1.06e+83)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(b * d), $MachinePrecision] + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(N[(d * d), $MachinePrecision] + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -7e+116], t$95$1, If[LessEqual[d, -6.6e-59], t$95$0, If[LessEqual[d, 1.85e-76], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.06e+83], t$95$0, t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{b \cdot d + c \cdot a}{d \cdot d + c \cdot c}\\
t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;d \leq 1.85 \cdot 10^{-76}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\

\mathbf{elif}\;d \leq 1.06 \cdot 10^{+83}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -6.99999999999999993e116 or 1.05999999999999995e83 < d

    1. Initial program 31.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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{c \cdot \frac{a}{d}} + b}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a}{d} \cdot c} + b}{d} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}}{d} \]
      7. lower-/.f6491.7

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{a}{d}}, c, b\right)}{d} \]
    5. Applied rewrites91.7%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}} \]

    if -6.99999999999999993e116 < d < -6.59999999999999964e-59 or 1.85000000000000006e-76 < d < 1.05999999999999995e83

    1. Initial program 82.9%

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

    if -6.59999999999999964e-59 < d < 1.85000000000000006e-76

    1. Initial program 71.9%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b \cdot d}{c} + a}}{c} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{d \cdot b}}{c} + a}{c} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{d \cdot \frac{b}{c}} + a}{c} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b}{c} \cdot d} + a}{c} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}}{c} \]
      7. lower-/.f6492.5

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{b}{c}}, d, a\right)}{c} \]
    5. Applied rewrites92.5%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification89.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -7 \cdot 10^{+116}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{elif}\;d \leq -6.6 \cdot 10^{-59}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 1.85 \cdot 10^{-76}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\ \mathbf{elif}\;d \leq 1.06 \cdot 10^{+83}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 76.8% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -5.6 \cdot 10^{-57}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (<= d -5.6e-57)
   (/ (fma (/ c d) a b) d)
   (if (<= d 1.45e+21) (/ (fma (/ b c) d a) c) (/ (fma (/ a d) c b) d))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (d <= -5.6e-57) {
		tmp = fma((c / d), a, b) / d;
	} else if (d <= 1.45e+21) {
		tmp = fma((b / c), d, a) / c;
	} else {
		tmp = fma((a / d), c, b) / d;
	}
	return tmp;
}
function code(a, b, c, d)
	tmp = 0.0
	if (d <= -5.6e-57)
		tmp = Float64(fma(Float64(c / d), a, b) / d);
	elseif (d <= 1.45e+21)
		tmp = Float64(fma(Float64(b / c), d, a) / c);
	else
		tmp = Float64(fma(Float64(a / d), c, b) / d);
	end
	return tmp
end
code[a_, b_, c_, d_] := If[LessEqual[d, -5.6e-57], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 1.45e+21], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d \leq -5.6 \cdot 10^{-57}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\

\mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -5.5999999999999999e-57

    1. Initial program 56.6%

      \[\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. lower-/.f6423.7

        \[\leadsto \color{blue}{\frac{a}{c}} \]
    5. Applied rewrites23.7%

      \[\leadsto \color{blue}{\frac{a}{c}} \]
    6. Taylor expanded in d around inf

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

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-*l/N/A

        \[\leadsto \frac{\color{blue}{\frac{c}{d} \cdot a} + b}{d} \]
      5. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}}{d} \]
      6. lower-/.f6474.5

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{c}{d}}, a, b\right)}{d} \]
    8. Applied rewrites74.5%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}} \]

    if -5.5999999999999999e-57 < d < 1.45e21

    1. Initial program 73.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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b \cdot d}{c} + a}}{c} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{d \cdot b}}{c} + a}{c} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{d \cdot \frac{b}{c}} + a}{c} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b}{c} \cdot d} + a}{c} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}}{c} \]
      7. lower-/.f6486.4

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{b}{c}}, d, a\right)}{c} \]
    5. Applied rewrites86.4%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}} \]

    if 1.45e21 < d

    1. Initial program 38.7%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{c \cdot \frac{a}{d}} + b}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a}{d} \cdot c} + b}{d} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}}{d} \]
      7. lower-/.f6490.4

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{a}{d}}, c, b\right)}{d} \]
    5. Applied rewrites90.4%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 5: 76.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{if}\;d \leq -5.6 \cdot 10^{-57}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (fma (/ a d) c b) d)))
   (if (<= d -5.6e-57) t_0 (if (<= d 1.45e+21) (/ (fma (/ b c) d a) c) t_0))))
double code(double a, double b, double c, double d) {
	double t_0 = fma((a / d), c, b) / d;
	double tmp;
	if (d <= -5.6e-57) {
		tmp = t_0;
	} else if (d <= 1.45e+21) {
		tmp = fma((b / c), d, a) / c;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = Float64(fma(Float64(a / d), c, b) / d)
	tmp = 0.0
	if (d <= -5.6e-57)
		tmp = t_0;
	elseif (d <= 1.45e+21)
		tmp = Float64(fma(Float64(b / c), d, a) / c);
	else
		tmp = t_0;
	end
	return tmp
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -5.6e-57], t$95$0, If[LessEqual[d, 1.45e+21], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -5.6 \cdot 10^{-57}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -5.5999999999999999e-57 or 1.45e21 < d

    1. Initial program 48.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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{c \cdot \frac{a}{d}} + b}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a}{d} \cdot c} + b}{d} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}}{d} \]
      7. lower-/.f6480.7

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{a}{d}}, c, b\right)}{d} \]
    5. Applied rewrites80.7%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}} \]

    if -5.5999999999999999e-57 < d < 1.45e21

    1. Initial program 73.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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b \cdot d}{c} + a}}{c} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{d \cdot b}}{c} + a}{c} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{d \cdot \frac{b}{c}} + a}{c} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{b}{c} \cdot d} + a}{c} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}}{c} \]
      7. lower-/.f6486.4

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{b}{c}}, d, a\right)}{c} \]
    5. Applied rewrites86.4%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 70.4% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{if}\;d \leq -4.1 \cdot 10^{-57}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{-72}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (fma (/ a d) c b) d)))
   (if (<= d -4.1e-57) t_0 (if (<= d 1.45e-72) (/ a c) t_0))))
double code(double a, double b, double c, double d) {
	double t_0 = fma((a / d), c, b) / d;
	double tmp;
	if (d <= -4.1e-57) {
		tmp = t_0;
	} else if (d <= 1.45e-72) {
		tmp = a / c;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = Float64(fma(Float64(a / d), c, b) / d)
	tmp = 0.0
	if (d <= -4.1e-57)
		tmp = t_0;
	elseif (d <= 1.45e-72)
		tmp = Float64(a / c);
	else
		tmp = t_0;
	end
	return tmp
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -4.1e-57], t$95$0, If[LessEqual[d, 1.45e-72], N[(a / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -4.1 \cdot 10^{-57}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -4.1000000000000001e-57 or 1.44999999999999999e-72 < d

    1. Initial program 52.6%

      \[\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. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a \cdot c}{d} + b}}{d} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{c \cdot a}}{d} + b}{d} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{c \cdot \frac{a}{d}} + b}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{a}{d} \cdot c} + b}{d} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}}{d} \]
      7. lower-/.f6477.4

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{a}{d}}, c, b\right)}{d} \]
    5. Applied rewrites77.4%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}} \]

    if -4.1000000000000001e-57 < d < 1.44999999999999999e-72

    1. Initial program 71.9%

      \[\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. lower-/.f6477.5

        \[\leadsto \color{blue}{\frac{a}{c}} \]
    5. Applied rewrites77.5%

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

Alternative 7: 62.1% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -1.8 \cdot 10^{+125}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.8 \cdot 10^{-57}:\\ \;\;\;\;\frac{a}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot c\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (<= d -1.8e+125)
   (/ b d)
   (if (<= d -4.8e-57)
     (* (/ a (fma c c (* d d))) c)
     (if (<= d 1.45e+21) (/ a c) (/ b d)))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (d <= -1.8e+125) {
		tmp = b / d;
	} else if (d <= -4.8e-57) {
		tmp = (a / fma(c, c, (d * d))) * c;
	} else if (d <= 1.45e+21) {
		tmp = a / c;
	} else {
		tmp = b / d;
	}
	return tmp;
}
function code(a, b, c, d)
	tmp = 0.0
	if (d <= -1.8e+125)
		tmp = Float64(b / d);
	elseif (d <= -4.8e-57)
		tmp = Float64(Float64(a / fma(c, c, Float64(d * d))) * c);
	elseif (d <= 1.45e+21)
		tmp = Float64(a / c);
	else
		tmp = Float64(b / d);
	end
	return tmp
end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.8e+125], N[(b / d), $MachinePrecision], If[LessEqual[d, -4.8e-57], N[(N[(a / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * c), $MachinePrecision], If[LessEqual[d, 1.45e+21], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;d \leq -4.8 \cdot 10^{-57}:\\
\;\;\;\;\frac{a}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot c\\

\mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\
\;\;\;\;\frac{a}{c}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -1.8000000000000002e125 or 1.45e21 < d

    1. Initial program 35.5%

      \[\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. lower-/.f6482.6

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Applied rewrites82.6%

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

    if -1.8000000000000002e125 < d < -4.80000000000000012e-57

    1. Initial program 80.9%

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

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

        \[\leadsto \frac{\color{blue}{c \cdot a}}{{c}^{2} + {d}^{2}} \]
      2. associate-/l*N/A

        \[\leadsto \color{blue}{c \cdot \frac{a}{{c}^{2} + {d}^{2}}} \]
      3. *-commutativeN/A

        \[\leadsto \color{blue}{\frac{a}{{c}^{2} + {d}^{2}} \cdot c} \]
      4. lower-*.f64N/A

        \[\leadsto \color{blue}{\frac{a}{{c}^{2} + {d}^{2}} \cdot c} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{a}{{c}^{2} + {d}^{2}}} \cdot c \]
      6. unpow2N/A

        \[\leadsto \frac{a}{\color{blue}{c \cdot c} + {d}^{2}} \cdot c \]
      7. lower-fma.f64N/A

        \[\leadsto \frac{a}{\color{blue}{\mathsf{fma}\left(c, c, {d}^{2}\right)}} \cdot c \]
      8. unpow2N/A

        \[\leadsto \frac{a}{\mathsf{fma}\left(c, c, \color{blue}{d \cdot d}\right)} \cdot c \]
      9. lower-*.f6453.8

        \[\leadsto \frac{a}{\mathsf{fma}\left(c, c, \color{blue}{d \cdot d}\right)} \cdot c \]
    5. Applied rewrites53.8%

      \[\leadsto \color{blue}{\frac{a}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot c} \]

    if -4.80000000000000012e-57 < d < 1.45e21

    1. Initial program 73.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}{c}} \]
    4. Step-by-step derivation
      1. lower-/.f6472.9

        \[\leadsto \color{blue}{\frac{a}{c}} \]
    5. Applied rewrites72.9%

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

Alternative 8: 61.7% accurate, 1.6× speedup?

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

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

\mathbf{elif}\;d \leq 1.45 \cdot 10^{+21}:\\
\;\;\;\;\frac{a}{c}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -5.80000000000000043e124 or 1.45e21 < d

    1. Initial program 35.5%

      \[\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. lower-/.f6482.6

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Applied rewrites82.6%

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

    if -5.80000000000000043e124 < d < 1.45e21

    1. Initial program 75.3%

      \[\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. lower-/.f6463.1

        \[\leadsto \color{blue}{\frac{a}{c}} \]
    5. Applied rewrites63.1%

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

Alternative 9: 42.3% accurate, 3.2× 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 60.1%

    \[\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. lower-/.f6444.9

      \[\leadsto \color{blue}{\frac{a}{c}} \]
  5. Applied rewrites44.9%

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

Developer Target 1: 99.4% accurate, 0.6× 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 2024273 
(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))))