Complex division, real part

Percentage Accurate: 61.9% → 80.3%
Time: 10.2s
Alternatives: 10
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 10 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.9% 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.3% accurate, 0.7× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -3.70000000000000012e39 or 5.1999999999999998e38 < c

    1. Initial program 45.5%

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

        \[\leadsto \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.f6445.5

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(d, b, a \cdot c\right)}}{c \cdot c + d \cdot d} \]
    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. *-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. lower-fma.f64N/A

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

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

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

    if -3.70000000000000012e39 < c < 9.00000000000000045e-162

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

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

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

    if 9.00000000000000045e-162 < c < 5.1999999999999998e38

    1. Initial program 86.6%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -3.7 \cdot 10^{+39}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\ \mathbf{elif}\;c \leq 9 \cdot 10^{-162}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{+38}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 80.3% accurate, 0.7× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if c < -3.70000000000000012e39 or 5.1999999999999998e38 < c

    1. Initial program 45.5%

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

        \[\leadsto \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.f6445.5

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(d, b, a \cdot c\right)}}{c \cdot c + d \cdot d} \]
    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. *-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. lower-fma.f64N/A

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

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

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

    if -3.70000000000000012e39 < c < 9.00000000000000045e-162

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

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

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

    if 9.00000000000000045e-162 < c < 5.1999999999999998e38

    1. Initial program 86.6%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -3.7 \cdot 10^{+39}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\ \mathbf{elif}\;c \leq 9 \cdot 10^{-162}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{+38}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 78.4% accurate, 0.9× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -3.70000000000000012e39 or 4.2000000000000002e37 < c

    1. Initial program 45.5%

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

        \[\leadsto \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.f6445.5

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(d, b, a \cdot c\right)}}{c \cdot c + d \cdot d} \]
    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. *-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. lower-fma.f64N/A

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

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

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

    if -3.70000000000000012e39 < c < 4.2000000000000002e37

    1. Initial program 79.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-/.f6476.6

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

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

Alternative 4: 77.2% accurate, 0.9× speedup?

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

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

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

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


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

    1. Initial program 62.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-/.f6474.9

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

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

    if -1.8499999999999999e-88 < d < 1.09999999999999996e51

    1. Initial program 73.0%

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

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

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

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

    if 1.09999999999999996e51 < d

    1. Initial program 49.6%

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

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(d, b, a \cdot c\right)}}{c \cdot c + d \cdot d} \]
    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. *-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. lower-fma.f64N/A

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

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

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

Alternative 5: 76.9% accurate, 0.9× speedup?

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

\mathbf{elif}\;d \leq 1.1 \cdot 10^{+51}:\\
\;\;\;\;\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.8499999999999999e-88 or 1.09999999999999996e51 < d

    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 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-/.f6476.3

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

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

    if -1.8499999999999999e-88 < d < 1.09999999999999996e51

    1. Initial program 73.0%

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

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

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

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

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;c \leq -8.6 \cdot 10^{+55}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 3.2 \cdot 10^{+38}:\\ \;\;\;\;\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 -8.6e+55)
   (/ a c)
   (if (<= c 3.2e+38) (/ (fma a (/ c d) b) d) (/ a c))))
double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -8.6e+55) {
		tmp = a / c;
	} else if (c <= 3.2e+38) {
		tmp = fma(a, (c / d), b) / d;
	} else {
		tmp = a / c;
	}
	return tmp;
}
function code(a, b, c, d)
	tmp = 0.0
	if (c <= -8.6e+55)
		tmp = Float64(a / c);
	elseif (c <= 3.2e+38)
		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, -8.6e+55], N[(a / c), $MachinePrecision], If[LessEqual[c, 3.2e+38], 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 -8.6 \cdot 10^{+55}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{elif}\;c \leq 3.2 \cdot 10^{+38}:\\
\;\;\;\;\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 2 regimes
  2. if c < -8.5999999999999998e55 or 3.19999999999999985e38 < c

    1. Initial program 44.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-/.f6469.8

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

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

    if -8.5999999999999998e55 < c < 3.19999999999999985e38

    1. Initial program 80.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. 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-/.f6476.2

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

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

Alternative 7: 65.0% accurate, 0.9× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -7.4999999999999997e115 or 1.90000000000000011e27 < d

    1. Initial program 47.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-/.f6474.7

        \[\leadsto \color{blue}{\frac{b}{d}} \]
    5. Applied rewrites74.7%

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

    if -7.4999999999999997e115 < d < -3.5e-111

    1. Initial program 84.0%

      \[\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-*.f6462.1

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

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

    if -3.5e-111 < d < 1.90000000000000011e27

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

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

      \[\leadsto \color{blue}{\frac{a}{c}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification68.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -7.5 \cdot 10^{+115}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-111}:\\ \;\;\;\;\frac{d \cdot b}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;d \leq 1.9 \cdot 10^{+27}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 65.5% accurate, 0.9× speedup?

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

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -5.79999999999999995e85 or 1.90000000000000011e27 < d

    1. Initial program 49.5%

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

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

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

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

    if -5.79999999999999995e85 < d < -2.29999999999999996e-108

    1. Initial program 84.2%

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

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

      \[\leadsto \color{blue}{\frac{b \cdot d}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \]
    6. Step-by-step derivation
      1. Applied rewrites66.2%

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

      if -2.29999999999999996e-108 < d < 1.90000000000000011e27

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

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

        \[\leadsto \color{blue}{\frac{a}{c}} \]
    7. Recombined 3 regimes into one program.
    8. Final simplification68.5%

      \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -5.8 \cdot 10^{+85}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.3 \cdot 10^{-108}:\\ \;\;\;\;b \cdot \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;d \leq 1.9 \cdot 10^{+27}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
    9. Add Preprocessing

    Alternative 9: 63.2% accurate, 1.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -1.82 \cdot 10^{-90}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.9 \cdot 10^{+27}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \end{array} \]
    (FPCore (a b c d)
     :precision binary64
     (if (<= d -1.82e-90) (/ b d) (if (<= d 1.9e+27) (/ a c) (/ b d))))
    double code(double a, double b, double c, double d) {
    	double tmp;
    	if (d <= -1.82e-90) {
    		tmp = b / d;
    	} else if (d <= 1.9e+27) {
    		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 <= (-1.82d-90)) then
            tmp = b / d
        else if (d <= 1.9d+27) 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 <= -1.82e-90) {
    		tmp = b / d;
    	} else if (d <= 1.9e+27) {
    		tmp = a / c;
    	} else {
    		tmp = b / d;
    	}
    	return tmp;
    }
    
    def code(a, b, c, d):
    	tmp = 0
    	if d <= -1.82e-90:
    		tmp = b / d
    	elif d <= 1.9e+27:
    		tmp = a / c
    	else:
    		tmp = b / d
    	return tmp
    
    function code(a, b, c, d)
    	tmp = 0.0
    	if (d <= -1.82e-90)
    		tmp = Float64(b / d);
    	elseif (d <= 1.9e+27)
    		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 <= -1.82e-90)
    		tmp = b / d;
    	elseif (d <= 1.9e+27)
    		tmp = a / c;
    	else
    		tmp = b / d;
    	end
    	tmp_2 = tmp;
    end
    
    code[a_, b_, c_, d_] := If[LessEqual[d, -1.82e-90], N[(b / d), $MachinePrecision], If[LessEqual[d, 1.9e+27], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;d \leq -1.82 \cdot 10^{-90}:\\
    \;\;\;\;\frac{b}{d}\\
    
    \mathbf{elif}\;d \leq 1.9 \cdot 10^{+27}:\\
    \;\;\;\;\frac{a}{c}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{b}{d}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if d < -1.8199999999999999e-90 or 1.90000000000000011e27 < d

      1. Initial program 58.1%

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

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

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

      if -1.8199999999999999e-90 < d < 1.90000000000000011e27

      1. Initial program 73.0%

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

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

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

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

    Alternative 10: 42.4% 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 65.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-/.f6442.4

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

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