Complex division, real part

Percentage Accurate: 61.8% → 80.7%
Time: 10.0s
Alternatives: 8
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 8 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.8% 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: 80.7% accurate, 0.8× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if c < -6.6000000000000002e111

    1. Initial program 38.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \frac{d \cdot b}{\color{blue}{c \cdot c}} + \frac{a}{c} \]
      8. times-fracN/A

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

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

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

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

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

    if -6.6000000000000002e111 < c < -1.1599999999999999e-102

    1. Initial program 95.7%

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

    if -1.1599999999999999e-102 < c < 6.79999999999999984e57

    1. Initial program 68.8%

      \[\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. associate-/l*N/A

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

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

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

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

    if 6.79999999999999984e57 < c

    1. Initial program 54.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 + \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. associate-/l*N/A

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -6.6 \cdot 10^{+111}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -1.16 \cdot 10^{-102}:\\ \;\;\;\;\frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 6.8 \cdot 10^{+57}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 80.6% accurate, 0.8× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -6.6000000000000002e111 or 6.79999999999999984e57 < c

    1. Initial program 45.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 + \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. associate-/l*N/A

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

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

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

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

    if -6.6000000000000002e111 < c < -1.1599999999999999e-102

    1. Initial program 95.7%

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

    if -1.1599999999999999e-102 < c < 6.79999999999999984e57

    1. Initial program 68.8%

      \[\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. associate-/l*N/A

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -6.6 \cdot 10^{+111}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \mathbf{elif}\;c \leq -1.16 \cdot 10^{-102}:\\ \;\;\;\;\frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 6.8 \cdot 10^{+57}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 72.1% accurate, 0.8× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -4.09999999999999986e111 or 2.00000000000000009e93 < c

    1. Initial program 43.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-/.f6484.9

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

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

    if -4.09999999999999986e111 < c < -9.600000000000001e-100

    1. Initial program 95.7%

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

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

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

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

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

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

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

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

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

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

    if -9.600000000000001e-100 < c < 2.00000000000000009e93

    1. Initial program 69.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. associate-/l*N/A

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.1 \cdot 10^{+111}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -9.6 \cdot 10^{-100}:\\ \;\;\;\;\frac{c \cdot a}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;c \leq 2 \cdot 10^{+93}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 66.6% accurate, 0.8× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -4.09999999999999986e111 or 2.00000000000000009e93 < c

    1. Initial program 43.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-/.f6484.9

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

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

    if -4.09999999999999986e111 < c < -1.5499999999999999e-146

    1. Initial program 92.8%

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

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

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

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

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

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

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

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

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

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

    if -1.5499999999999999e-146 < c < 2.00000000000000009e93

    1. Initial program 69.3%

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

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

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

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

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

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

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

        \[\leadsto \frac{b \cdot d}{\mathsf{fma}\left(d, d, \color{blue}{c \cdot c}\right)} \]
      7. lower-*.f6448.3

        \[\leadsto \frac{b \cdot d}{\mathsf{fma}\left(d, d, \color{blue}{c \cdot c}\right)} \]
    5. Simplified48.3%

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

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

        \[\leadsto \frac{b \cdot d}{\color{blue}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \]
      3. associate-/l*N/A

        \[\leadsto \color{blue}{b \cdot \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \]
      4. clear-numN/A

        \[\leadsto b \cdot \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{d}}} \]
      5. un-div-invN/A

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

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

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

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

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

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

        \[\leadsto \frac{b}{\frac{\color{blue}{c \cdot c} + d \cdot d}{d}} \]
      12. lift-fma.f6455.5

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

      \[\leadsto \color{blue}{\frac{b}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{d}}} \]
    8. Taylor expanded in c around 0

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

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

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

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

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

        \[\leadsto \frac{b}{\mathsf{fma}\left(c, \color{blue}{\frac{c}{d}}, d\right)} \]
    10. Simplified72.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.1 \cdot 10^{+111}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -1.55 \cdot 10^{-146}:\\ \;\;\;\;\frac{c \cdot a}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;c \leq 2 \cdot 10^{+93}:\\ \;\;\;\;\frac{b}{\mathsf{fma}\left(c, \frac{c}{d}, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 78.0% accurate, 0.9× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -1.24999999999999993e-21 or 0.00679999999999999962 < d

    1. Initial program 58.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. associate-/l*N/A

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

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

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

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

    if -1.24999999999999993e-21 < d < 0.00679999999999999962

    1. Initial program 75.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. associate-/l*N/A

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

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

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

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

Alternative 6: 63.7% accurate, 0.9× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -4.09999999999999986e111 or 2.00000000000000009e93 < c

    1. Initial program 43.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-/.f6484.9

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

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

    if -4.09999999999999986e111 < c < -1.5499999999999999e-146

    1. Initial program 92.8%

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

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

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

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

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

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

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

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

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

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

    if -1.5499999999999999e-146 < c < 2.00000000000000009e93

    1. Initial program 69.3%

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

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Simplified65.1%

      \[\leadsto \color{blue}{\frac{b}{d}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.1 \cdot 10^{+111}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -1.55 \cdot 10^{-146}:\\ \;\;\;\;\frac{c \cdot a}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;c \leq 2 \cdot 10^{+93}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 63.4% accurate, 1.6× speedup?

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

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

\mathbf{elif}\;d \leq 0.0068:\\
\;\;\;\;\frac{a}{c}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -6e74 or 0.00679999999999999962 < d

    1. Initial program 58.8%

      \[\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-/.f6472.3

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Simplified72.3%

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

    if -6e74 < d < 0.00679999999999999962

    1. Initial program 72.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. Simplified63.1%

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

Alternative 8: 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 66.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-/.f6442.6

      \[\leadsto \color{blue}{\frac{a}{c}} \]
  5. Simplified42.6%

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

Developer Target 1: 99.3% 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 2024207 
(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))))