Complex division, real part

Percentage Accurate: 62.4% → 83.2%
Time: 6.6s
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: 62.4% 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.2% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\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 -5 \cdot 10^{+85}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d \leq -5.2 \cdot 10^{-130}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{-101}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{+122}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (fma d b (* c a)) (fma c c (* d d))))
        (t_1 (/ (fma (/ a d) c b) d)))
   (if (<= d -5e+85)
     t_1
     (if (<= d -5.2e-130)
       t_0
       (if (<= d 1.1e-101)
         (/ (fma (/ d c) b a) c)
         (if (<= d 2.5e+122) t_0 t_1))))))
double code(double a, double b, double c, double d) {
	double t_0 = fma(d, b, (c * a)) / fma(c, c, (d * d));
	double t_1 = fma((a / d), c, b) / d;
	double tmp;
	if (d <= -5e+85) {
		tmp = t_1;
	} else if (d <= -5.2e-130) {
		tmp = t_0;
	} else if (d <= 1.1e-101) {
		tmp = fma((d / c), b, a) / c;
	} else if (d <= 2.5e+122) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = Float64(fma(d, b, Float64(c * a)) / fma(c, c, Float64(d * d)))
	t_1 = Float64(fma(Float64(a / d), c, b) / d)
	tmp = 0.0
	if (d <= -5e+85)
		tmp = t_1;
	elseif (d <= -5.2e-130)
		tmp = t_0;
	elseif (d <= 1.1e-101)
		tmp = Float64(fma(Float64(d / c), b, a) / c);
	elseif (d <= 2.5e+122)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -5e+85], t$95$1, If[LessEqual[d, -5.2e-130], t$95$0, If[LessEqual[d, 1.1e-101], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 2.5e+122], t$95$0, t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\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 -5 \cdot 10^{+85}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d \leq -5.2 \cdot 10^{-130}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;d \leq 2.5 \cdot 10^{+122}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -5.0000000000000001e85 or 2.49999999999999994e122 < d

    1. Initial program 36.0%

      \[\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-/.f6492.0

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

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

    if -5.0000000000000001e85 < d < -5.2000000000000001e-130 or 1.0999999999999999e-101 < d < 2.49999999999999994e122

    1. Initial program 86.9%

      \[\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 \frac{a \cdot c + b \cdot d}{\color{blue}{c \cdot c + d \cdot d}} \]
      2. lift-*.f64N/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{d \cdot b + c \cdot a}}{\mathsf{fma}\left(c, c, d \cdot d\right)} \]
      8. lift-fma.f6487.0

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

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

    if -5.2000000000000001e-130 < d < 1.0999999999999999e-101

    1. Initial program 65.9%

      \[\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 \frac{a \cdot c + b \cdot d}{\color{blue}{c \cdot c + d \cdot d}} \]
      2. lift-*.f64N/A

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

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

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    6. 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. *-commutativeN/A

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

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

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

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

Alternative 2: 63.6% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ t_1 := \frac{b}{t\_0} \cdot d\\ \mathbf{if}\;d \leq -3 \cdot 10^{+132}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -3 \cdot 10^{+77}:\\ \;\;\;\;\frac{a}{t\_0} \cdot c\\ \mathbf{elif}\;d \leq -9.2 \cdot 10^{-97}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d \leq 460000000:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2.9 \cdot 10^{+123}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (fma d d (* c c))) (t_1 (* (/ b t_0) d)))
   (if (<= d -3e+132)
     (/ b d)
     (if (<= d -3e+77)
       (* (/ a t_0) c)
       (if (<= d -9.2e-97)
         t_1
         (if (<= d 460000000.0) (/ a c) (if (<= d 2.9e+123) t_1 (/ b d))))))))
double code(double a, double b, double c, double d) {
	double t_0 = fma(d, d, (c * c));
	double t_1 = (b / t_0) * d;
	double tmp;
	if (d <= -3e+132) {
		tmp = b / d;
	} else if (d <= -3e+77) {
		tmp = (a / t_0) * c;
	} else if (d <= -9.2e-97) {
		tmp = t_1;
	} else if (d <= 460000000.0) {
		tmp = a / c;
	} else if (d <= 2.9e+123) {
		tmp = t_1;
	} else {
		tmp = b / d;
	}
	return tmp;
}
function code(a, b, c, d)
	t_0 = fma(d, d, Float64(c * c))
	t_1 = Float64(Float64(b / t_0) * d)
	tmp = 0.0
	if (d <= -3e+132)
		tmp = Float64(b / d);
	elseif (d <= -3e+77)
		tmp = Float64(Float64(a / t_0) * c);
	elseif (d <= -9.2e-97)
		tmp = t_1;
	elseif (d <= 460000000.0)
		tmp = Float64(a / c);
	elseif (d <= 2.9e+123)
		tmp = t_1;
	else
		tmp = Float64(b / d);
	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[(N[(b / t$95$0), $MachinePrecision] * d), $MachinePrecision]}, If[LessEqual[d, -3e+132], N[(b / d), $MachinePrecision], If[LessEqual[d, -3e+77], N[(N[(a / t$95$0), $MachinePrecision] * c), $MachinePrecision], If[LessEqual[d, -9.2e-97], t$95$1, If[LessEqual[d, 460000000.0], N[(a / c), $MachinePrecision], If[LessEqual[d, 2.9e+123], t$95$1, N[(b / d), $MachinePrecision]]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;d \leq -3 \cdot 10^{+77}:\\
\;\;\;\;\frac{a}{t\_0} \cdot c\\

\mathbf{elif}\;d \leq -9.2 \cdot 10^{-97}:\\
\;\;\;\;t\_1\\

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

\mathbf{elif}\;d \leq 2.9 \cdot 10^{+123}:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d < -2.9999999999999998e132 or 2.9000000000000001e123 < d

    1. Initial program 32.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-/.f6486.2

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Applied rewrites86.2%

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

    if -2.9999999999999998e132 < d < -2.9999999999999998e77

    1. Initial program 80.2%

      \[\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. *-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. +-commutativeN/A

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

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

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

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

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

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

    if -2.9999999999999998e77 < d < -9.19999999999999976e-97 or 4.6e8 < d < 2.9000000000000001e123

    1. Initial program 87.4%

      \[\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. *-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. *-commutativeN/A

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

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

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

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

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

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

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

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

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

    if -9.19999999999999976e-97 < d < 4.6e8

    1. Initial program 72.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-/.f6473.7

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

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

Alternative 3: 64.9% accurate, 0.7× speedup?

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

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d < -9.8000000000000003e132 or 2.9000000000000001e123 < d

    1. Initial program 32.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-/.f6486.2

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Applied rewrites86.2%

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

    if -9.8000000000000003e132 < d < -9.9999999999999998e-67

    1. Initial program 90.3%

      \[\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 \frac{\color{blue}{a \cdot c + b \cdot d}}{c \cdot c + d \cdot d} \]
      2. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -9.9999999999999998e-67 < d < 4.6e8

    1. Initial program 72.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-/.f6471.5

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

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

    if 4.6e8 < d < 2.9000000000000001e123

    1. Initial program 82.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{b \cdot d}{{c}^{2} + {d}^{2}}} \]
    4. 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. *-commutativeN/A

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

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

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

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

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

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

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

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

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

Alternative 4: 78.5% accurate, 0.9× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -8.2e19

    1. Initial program 64.7%

      \[\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 \frac{a \cdot c + b \cdot d}{\color{blue}{c \cdot c + d \cdot d}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{c \cdot c} + d \cdot d} \]
      3. lower-fma.f6464.7

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

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    6. 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. *-commutativeN/A

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

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

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

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

    if -8.2e19 < c < 6.5000000000000004e31

    1. Initial program 70.4%

      \[\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 \frac{a \cdot c + b \cdot d}{\color{blue}{c \cdot c + d \cdot d}} \]
      2. lift-*.f64N/A

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

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

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    6. 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. *-commutativeN/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-/.f6486.6

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

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

    if 6.5000000000000004e31 < c

    1. Initial program 49.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 + \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-/.f6483.5

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{b}{c}}, d, a\right)}{c} \]
    5. Applied rewrites83.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 5: 78.6% accurate, 0.9× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -8.2e19 or 6.5000000000000004e31 < c

    1. Initial program 57.2%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. 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-/.f6483.8

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

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

    if -8.2e19 < c < 6.5000000000000004e31

    1. Initial program 70.4%

      \[\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 \frac{a \cdot c + b \cdot d}{\color{blue}{c \cdot c + d \cdot d}} \]
      2. lift-*.f64N/A

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

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

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    6. 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. *-commutativeN/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-/.f6486.6

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

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

Alternative 6: 77.3% accurate, 0.9× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -8.2e19 or 6.5000000000000004e31 < c

    1. Initial program 57.2%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. 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-/.f6483.8

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

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

    if -8.2e19 < c < 6.5000000000000004e31

    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 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-/.f6486.0

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

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

Alternative 7: 72.2% accurate, 0.9× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -1.2e20 or 1.4e33 < c

    1. Initial program 57.2%

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

      \[\leadsto \color{blue}{\frac{a}{c}} \]
    4. Step-by-step derivation
      1. lower-/.f6471.5

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

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

    if -1.2e20 < c < 1.4e33

    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 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-/.f6486.0

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

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

Alternative 8: 64.0% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;c \leq -3.8 \cdot 10^{-13}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 6.8 \cdot 10^{-9}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (<= c -3.8e-13) (/ a c) (if (<= c 6.8e-9) (/ b d) (/ a c))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -3.8e-13) {
		tmp = a / c;
	} else if (c <= 6.8e-9) {
		tmp = b / d;
	} else {
		tmp = a / c;
	}
	return tmp;
}
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    real(8) :: tmp
    if (c <= (-3.8d-13)) then
        tmp = a / c
    else if (c <= 6.8d-9) then
        tmp = b / d
    else
        tmp = a / c
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -3.8e-13) {
		tmp = a / c;
	} else if (c <= 6.8e-9) {
		tmp = b / d;
	} else {
		tmp = a / c;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if c <= -3.8e-13:
		tmp = a / c
	elif c <= 6.8e-9:
		tmp = b / d
	else:
		tmp = a / c
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if (c <= -3.8e-13)
		tmp = Float64(a / c);
	elseif (c <= 6.8e-9)
		tmp = Float64(b / d);
	else
		tmp = Float64(a / c);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if (c <= -3.8e-13)
		tmp = a / c;
	elseif (c <= 6.8e-9)
		tmp = b / d;
	else
		tmp = a / c;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[LessEqual[c, -3.8e-13], N[(a / c), $MachinePrecision], If[LessEqual[c, 6.8e-9], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;c \leq -3.8 \cdot 10^{-13}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{elif}\;c \leq 6.8 \cdot 10^{-9}:\\
\;\;\;\;\frac{b}{d}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -3.8e-13 or 6.7999999999999997e-9 < c

    1. Initial program 58.5%

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

      \[\leadsto \color{blue}{\frac{a}{c}} \]
    4. Step-by-step derivation
      1. lower-/.f6467.9

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

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

    if -3.8e-13 < c < 6.7999999999999997e-9

    1. Initial program 70.7%

      \[\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-/.f6471.0

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Applied rewrites71.0%

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

Alternative 9: 42.6% 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 64.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-/.f6439.5

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

    \[\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 2024327 
(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))))