Complex division, real part

Percentage Accurate: 62.0% → 83.0%
Time: 7.1s
Alternatives: 7
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 7 alternatives:

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

Initial Program: 62.0% 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: 83.0% accurate, 0.5× speedup?

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

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

\mathbf{elif}\;d \leq -0.00012:\\
\;\;\;\;t\_1\\

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

\mathbf{elif}\;d \leq 1.6 \cdot 10^{+69}:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -1.20000000000000001e147 or 1.59999999999999992e69 < d

    1. Initial program 35.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-/.f6488.3

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

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

    if -1.20000000000000001e147 < d < -1.20000000000000003e-4 or 1.45e-101 < d < 1.59999999999999992e69

    1. Initial program 81.8%

      \[\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{a \cdot c}{{c}^{2} + {d}^{2}} + \frac{b \cdot d}{{c}^{2} + {d}^{2}}} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.20000000000000003e-4 < d < 1.45e-101

    1. Initial program 67.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-/.f6488.6

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

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

Alternative 2: 80.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{if}\;d \leq -3.8 \cdot 10^{+42}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{-101}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{+68}:\\ \;\;\;\;\frac{a \cdot c + d \cdot b}{c \cdot c + d \cdot d}\\ \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 -3.8e+42)
     t_0
     (if (<= d 1.45e-101)
       (/ (fma b (/ d c) a) c)
       (if (<= d 7.2e+68) (/ (+ (* a c) (* d b)) (+ (* c c) (* d d))) 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 <= -3.8e+42) {
		tmp = t_0;
	} else if (d <= 1.45e-101) {
		tmp = fma(b, (d / c), a) / c;
	} else if (d <= 7.2e+68) {
		tmp = ((a * c) + (d * b)) / ((c * c) + (d * d));
	} 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 <= -3.8e+42)
		tmp = t_0;
	elseif (d <= 1.45e-101)
		tmp = Float64(fma(b, Float64(d / c), a) / c);
	elseif (d <= 7.2e+68)
		tmp = Float64(Float64(Float64(a * c) + Float64(d * b)) / Float64(Float64(c * c) + Float64(d * d)));
	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, -3.8e+42], t$95$0, If[LessEqual[d, 1.45e-101], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 7.2e+68], N[(N[(N[(a * c), $MachinePrecision] + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $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 -3.8 \cdot 10^{+42}:\\
\;\;\;\;t\_0\\

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -3.7999999999999998e42 or 7.1999999999999998e68 < d

    1. Initial program 43.3%

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

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

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

    if -3.7999999999999998e42 < d < 1.45e-101

    1. Initial program 69.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-/.f6486.3

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

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

    if 1.45e-101 < d < 7.1999999999999998e68

    1. Initial program 83.5%

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Add Preprocessing
  3. Recombined 3 regimes into one program.
  4. Final simplification85.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.8 \cdot 10^{+42}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{-101}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{+68}:\\ \;\;\;\;\frac{a \cdot c + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 70.6% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{if}\;d \leq -9 \cdot 10^{+44}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq -0.00012:\\ \;\;\;\;d \cdot \frac{b}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{-41}:\\ \;\;\;\;\frac{a}{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 -9e+44)
     t_0
     (if (<= d -0.00012)
       (* d (/ b (fma c c (* d d))))
       (if (<= d 1.1e-41) (/ 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 <= -9e+44) {
		tmp = t_0;
	} else if (d <= -0.00012) {
		tmp = d * (b / fma(c, c, (d * d)));
	} else if (d <= 1.1e-41) {
		tmp = 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 <= -9e+44)
		tmp = t_0;
	elseif (d <= -0.00012)
		tmp = Float64(d * Float64(b / fma(c, c, Float64(d * d))));
	elseif (d <= 1.1e-41)
		tmp = Float64(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, -9e+44], t$95$0, If[LessEqual[d, -0.00012], N[(d * N[(b / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.1e-41], N[(a / 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 -9 \cdot 10^{+44}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;d \leq -0.00012:\\
\;\;\;\;d \cdot \frac{b}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -9e44 or 1.1e-41 < d

    1. Initial program 48.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. 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.7

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

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

    if -9e44 < d < -1.20000000000000003e-4

    1. Initial program 90.5%

      \[\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{a \cdot c}{{c}^{2} + {d}^{2}} + \frac{b \cdot d}{{c}^{2} + {d}^{2}}} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.20000000000000003e-4 < d < 1.1e-41

    1. Initial program 70.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.2

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

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

Alternative 4: 77.7% 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 -3.8 \cdot 10^{+42}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{-41}:\\ \;\;\;\;\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 -3.8e+42) t_0 (if (<= d 2.3e-41) (/ (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 <= -3.8e+42) {
		tmp = t_0;
	} else if (d <= 2.3e-41) {
		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 <= -3.8e+42)
		tmp = t_0;
	elseif (d <= 2.3e-41)
		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, -3.8e+42], t$95$0, If[LessEqual[d, 2.3e-41], 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 -3.8 \cdot 10^{+42}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;d \leq 2.3 \cdot 10^{-41}:\\
\;\;\;\;\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 < -3.7999999999999998e42 or 2.3000000000000001e-41 < d

    1. Initial program 48.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. 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.9

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

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

    if -3.7999999999999998e42 < d < 2.3000000000000001e-41

    1. Initial program 71.8%

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

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

      \[\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 5: 65.4% accurate, 0.9× speedup?

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

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

\mathbf{elif}\;d \leq -0.00012:\\
\;\;\;\;d \cdot \frac{b}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -1.20000000000000001e147 or 2.25e-41 < d

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

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

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

    if -1.20000000000000001e147 < d < -1.20000000000000003e-4

    1. Initial program 79.7%

      \[\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{a \cdot c}{{c}^{2} + {d}^{2}} + \frac{b \cdot d}{{c}^{2} + {d}^{2}}} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.20000000000000003e-4 < d < 2.25e-41

    1. Initial program 70.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.2

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

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

Alternative 6: 64.6% accurate, 1.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;d \leq -0.00019:\\
\;\;\;\;\frac{b}{d}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -1.9000000000000001e-4 or 2.25e-41 < d

    1. Initial program 51.6%

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

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

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

    if -1.9000000000000001e-4 < d < 2.25e-41

    1. Initial program 70.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.2

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

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

Alternative 7: 43.5% 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.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.8

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

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

Developer Target 1: 99.5% 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 2024214 
(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))))