Complex division, real part

Percentage Accurate: 61.4% → 81.6%
Time: 7.1s
Alternatives: 9
Speedup: 1.1×

Specification

?
\[\begin{array}{l} \\ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

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

Initial Program: 61.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: 81.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -4.6 \cdot 10^{+69}:\\ \;\;\;\;\frac{a + d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;c \leq -7800:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq -1.1 \cdot 10^{-74}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;c \leq 6.8 \cdot 10^{-77}:\\ \;\;\;\;\frac{b + \frac{a \cdot c}{d}}{d}\\ \mathbf{elif}\;c \leq 6.2 \cdot 10^{+117}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))))
   (if (<= c -4.6e+69)
     (/ (+ a (* d (/ b c))) c)
     (if (<= c -7800.0)
       (/ (+ b (* a (/ c d))) d)
       (if (<= c -1.1e-74)
         t_0
         (if (<= c 6.8e-77)
           (/ (+ b (/ (* a c) d)) d)
           (if (<= c 6.2e+117) t_0 (+ (/ a c) (/ (* b (/ d c)) c)))))))))
double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (c <= -4.6e+69) {
		tmp = (a + (d * (b / c))) / c;
	} else if (c <= -7800.0) {
		tmp = (b + (a * (c / d))) / d;
	} else if (c <= -1.1e-74) {
		tmp = t_0;
	} else if (c <= 6.8e-77) {
		tmp = (b + ((a * c) / d)) / d;
	} else if (c <= 6.2e+117) {
		tmp = t_0;
	} else {
		tmp = (a / c) + ((b * (d / c)) / 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) :: t_0
    real(8) :: tmp
    t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
    if (c <= (-4.6d+69)) then
        tmp = (a + (d * (b / c))) / c
    else if (c <= (-7800.0d0)) then
        tmp = (b + (a * (c / d))) / d
    else if (c <= (-1.1d-74)) then
        tmp = t_0
    else if (c <= 6.8d-77) then
        tmp = (b + ((a * c) / d)) / d
    else if (c <= 6.2d+117) then
        tmp = t_0
    else
        tmp = (a / c) + ((b * (d / c)) / c)
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (c <= -4.6e+69) {
		tmp = (a + (d * (b / c))) / c;
	} else if (c <= -7800.0) {
		tmp = (b + (a * (c / d))) / d;
	} else if (c <= -1.1e-74) {
		tmp = t_0;
	} else if (c <= 6.8e-77) {
		tmp = (b + ((a * c) / d)) / d;
	} else if (c <= 6.2e+117) {
		tmp = t_0;
	} else {
		tmp = (a / c) + ((b * (d / c)) / c);
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
	tmp = 0
	if c <= -4.6e+69:
		tmp = (a + (d * (b / c))) / c
	elif c <= -7800.0:
		tmp = (b + (a * (c / d))) / d
	elif c <= -1.1e-74:
		tmp = t_0
	elif c <= 6.8e-77:
		tmp = (b + ((a * c) / d)) / d
	elif c <= 6.2e+117:
		tmp = t_0
	else:
		tmp = (a / c) + ((b * (d / c)) / c)
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (c <= -4.6e+69)
		tmp = Float64(Float64(a + Float64(d * Float64(b / c))) / c);
	elseif (c <= -7800.0)
		tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d);
	elseif (c <= -1.1e-74)
		tmp = t_0;
	elseif (c <= 6.8e-77)
		tmp = Float64(Float64(b + Float64(Float64(a * c) / d)) / d);
	elseif (c <= 6.2e+117)
		tmp = t_0;
	else
		tmp = Float64(Float64(a / c) + Float64(Float64(b * Float64(d / c)) / c));
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	tmp = 0.0;
	if (c <= -4.6e+69)
		tmp = (a + (d * (b / c))) / c;
	elseif (c <= -7800.0)
		tmp = (b + (a * (c / d))) / d;
	elseif (c <= -1.1e-74)
		tmp = t_0;
	elseif (c <= 6.8e-77)
		tmp = (b + ((a * c) / d)) / d;
	elseif (c <= 6.2e+117)
		tmp = t_0;
	else
		tmp = (a / c) + ((b * (d / c)) / c);
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -4.6e+69], N[(N[(a + N[(d * N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -7800.0], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, -1.1e-74], t$95$0, If[LessEqual[c, 6.8e-77], N[(N[(b + N[(N[(a * c), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 6.2e+117], t$95$0, N[(N[(a / c), $MachinePrecision] + N[(N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{if}\;c \leq -4.6 \cdot 10^{+69}:\\
\;\;\;\;\frac{a + d \cdot \frac{b}{c}}{c}\\

\mathbf{elif}\;c \leq -7800:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\

\mathbf{elif}\;c \leq -1.1 \cdot 10^{-74}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;c \leq 6.2 \cdot 10^{+117}:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\


\end{array}
\end{array}
Derivation
  1. Split input into 5 regimes
  2. if c < -4.60000000000000033e69

    1. Initial program 39.0%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. *-commutative68.6%

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

        \[\leadsto \frac{a + \color{blue}{d \cdot \frac{b}{c}}}{c} \]
    5. Applied egg-rr77.1%

      \[\leadsto \frac{a + \color{blue}{d \cdot \frac{b}{c}}}{c} \]

    if -4.60000000000000033e69 < c < -7800

    1. Initial program 42.2%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. associate-/l*66.4%

        \[\leadsto \frac{b + \color{blue}{a \cdot \frac{c}{d}}}{d} \]
    5. Simplified66.4%

      \[\leadsto \color{blue}{\frac{b + a \cdot \frac{c}{d}}{d}} \]

    if -7800 < c < -1.10000000000000005e-74 or 6.79999999999999966e-77 < c < 6.1999999999999995e117

    1. Initial program 91.1%

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

    if -1.10000000000000005e-74 < c < 6.79999999999999966e-77

    1. Initial program 73.9%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]

    if 6.1999999999999995e117 < c

    1. Initial program 34.7%

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{b \cdot d}{{c}^{2}}} \]
    4. Step-by-step derivation
      1. associate-/l*82.4%

        \[\leadsto \frac{a}{c} + \color{blue}{b \cdot \frac{d}{{c}^{2}}} \]
    5. Simplified82.4%

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

        \[\leadsto \frac{a}{c} + b \cdot \frac{d}{\color{blue}{c \cdot c}} \]
      2. associate-*r/74.3%

        \[\leadsto \frac{a}{c} + \color{blue}{\frac{b \cdot d}{c \cdot c}} \]
      3. associate-/r*87.5%

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

        \[\leadsto \frac{a}{c} + \frac{\color{blue}{b \cdot \frac{d}{c}}}{c} \]
    7. Applied egg-rr95.5%

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

Alternative 2: 84.6% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := a \cdot c + b \cdot d\\ \mathbf{if}\;\frac{t\_0}{c \cdot c + d \cdot d} \leq 10^{+275}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{t\_0}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (+ (* a c) (* b d))))
   (if (<= (/ t_0 (+ (* c c) (* d d))) 1e+275)
     (* (/ 1.0 (hypot c d)) (/ t_0 (hypot c d)))
     (/ (+ b (* c (/ a d))) d))))
double code(double a, double b, double c, double d) {
	double t_0 = (a * c) + (b * d);
	double tmp;
	if ((t_0 / ((c * c) + (d * d))) <= 1e+275) {
		tmp = (1.0 / hypot(c, d)) * (t_0 / hypot(c, d));
	} else {
		tmp = (b + (c * (a / d))) / d;
	}
	return tmp;
}
public static double code(double a, double b, double c, double d) {
	double t_0 = (a * c) + (b * d);
	double tmp;
	if ((t_0 / ((c * c) + (d * d))) <= 1e+275) {
		tmp = (1.0 / Math.hypot(c, d)) * (t_0 / Math.hypot(c, d));
	} else {
		tmp = (b + (c * (a / d))) / d;
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = (a * c) + (b * d)
	tmp = 0
	if (t_0 / ((c * c) + (d * d))) <= 1e+275:
		tmp = (1.0 / math.hypot(c, d)) * (t_0 / math.hypot(c, d))
	else:
		tmp = (b + (c * (a / d))) / d
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(a * c) + Float64(b * d))
	tmp = 0.0
	if (Float64(t_0 / Float64(Float64(c * c) + Float64(d * d))) <= 1e+275)
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(t_0 / hypot(c, d)));
	else
		tmp = Float64(Float64(b + Float64(c * Float64(a / d))) / d);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = (a * c) + (b * d);
	tmp = 0.0;
	if ((t_0 / ((c * c) + (d * d))) <= 1e+275)
		tmp = (1.0 / hypot(c, d)) * (t_0 / hypot(c, d));
	else
		tmp = (b + (c * (a / d))) / d;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e+275], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(t$95$0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 9.9999999999999996e274

    1. Initial program 82.2%

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity82.2%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{c \cdot c + d \cdot d} \]
      2. add-sqr-sqrt82.2%

        \[\leadsto \frac{1 \cdot \left(a \cdot c + b \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
      3. times-frac82.2%

        \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
      4. hypot-define82.2%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
      5. fma-define82.2%

        \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d}} \]
      6. hypot-define97.6%

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

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

        \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\color{blue}{a \cdot c + b \cdot d}}{\mathsf{hypot}\left(c, d\right)} \]
      2. +-commutative97.6%

        \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\color{blue}{b \cdot d + a \cdot c}}{\mathsf{hypot}\left(c, d\right)} \]
    6. Applied egg-rr97.6%

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

    if 9.9999999999999996e274 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 15.0%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. *-commutative53.6%

        \[\leadsto \frac{b + \frac{\color{blue}{c \cdot a}}{d}}{d} \]
      2. *-un-lft-identity53.6%

        \[\leadsto \frac{b + \frac{c \cdot a}{\color{blue}{1 \cdot d}}}{d} \]
      3. times-frac61.0%

        \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]
    5. Applied egg-rr61.0%

      \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 10^{+275}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a \cdot c + b \cdot d}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 81.8% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+66}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq -1.6 \cdot 10^{-122}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{-111}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 2.7 \cdot 10^{+52}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))))
   (if (<= d -1.8e+66)
     (/ (+ b (* c (/ a d))) d)
     (if (<= d -1.6e-122)
       t_0
       (if (<= d 3.5e-111)
         (/ (+ a (* b (/ d c))) c)
         (if (<= d 2.7e+52) t_0 (* (/ d (hypot c d)) (/ b (hypot c d)))))))))
double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -1.8e+66) {
		tmp = (b + (c * (a / d))) / d;
	} else if (d <= -1.6e-122) {
		tmp = t_0;
	} else if (d <= 3.5e-111) {
		tmp = (a + (b * (d / c))) / c;
	} else if (d <= 2.7e+52) {
		tmp = t_0;
	} else {
		tmp = (d / hypot(c, d)) * (b / hypot(c, d));
	}
	return tmp;
}
public static double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -1.8e+66) {
		tmp = (b + (c * (a / d))) / d;
	} else if (d <= -1.6e-122) {
		tmp = t_0;
	} else if (d <= 3.5e-111) {
		tmp = (a + (b * (d / c))) / c;
	} else if (d <= 2.7e+52) {
		tmp = t_0;
	} else {
		tmp = (d / Math.hypot(c, d)) * (b / Math.hypot(c, d));
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
	tmp = 0
	if d <= -1.8e+66:
		tmp = (b + (c * (a / d))) / d
	elif d <= -1.6e-122:
		tmp = t_0
	elif d <= 3.5e-111:
		tmp = (a + (b * (d / c))) / c
	elif d <= 2.7e+52:
		tmp = t_0
	else:
		tmp = (d / math.hypot(c, d)) * (b / math.hypot(c, d))
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (d <= -1.8e+66)
		tmp = Float64(Float64(b + Float64(c * Float64(a / d))) / d);
	elseif (d <= -1.6e-122)
		tmp = t_0;
	elseif (d <= 3.5e-111)
		tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c);
	elseif (d <= 2.7e+52)
		tmp = t_0;
	else
		tmp = Float64(Float64(d / hypot(c, d)) * Float64(b / hypot(c, d)));
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	tmp = 0.0;
	if (d <= -1.8e+66)
		tmp = (b + (c * (a / d))) / d;
	elseif (d <= -1.6e-122)
		tmp = t_0;
	elseif (d <= 3.5e-111)
		tmp = (a + (b * (d / c))) / c;
	elseif (d <= 2.7e+52)
		tmp = t_0;
	else
		tmp = (d / hypot(c, d)) * (b / hypot(c, d));
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -1.8e+66], N[(N[(b + N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -1.6e-122], t$95$0, If[LessEqual[d, 3.5e-111], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 2.7e+52], t$95$0, N[(N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{if}\;d \leq -1.8 \cdot 10^{+66}:\\
\;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\

\mathbf{elif}\;d \leq -1.6 \cdot 10^{-122}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;d \leq 2.7 \cdot 10^{+52}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d < -1.8e66

    1. Initial program 39.2%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. *-commutative71.3%

        \[\leadsto \frac{b + \frac{\color{blue}{c \cdot a}}{d}}{d} \]
      2. *-un-lft-identity71.3%

        \[\leadsto \frac{b + \frac{c \cdot a}{\color{blue}{1 \cdot d}}}{d} \]
      3. times-frac81.9%

        \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]
    5. Applied egg-rr81.9%

      \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]

    if -1.8e66 < d < -1.6000000000000001e-122 or 3.5e-111 < d < 2.7e52

    1. Initial program 85.8%

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

    if -1.6000000000000001e-122 < d < 3.5e-111

    1. Initial program 76.1%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. associate-/l*96.3%

        \[\leadsto \frac{a + \color{blue}{b \cdot \frac{d}{c}}}{c} \]
    5. Simplified96.3%

      \[\leadsto \color{blue}{\frac{a + b \cdot \frac{d}{c}}{c}} \]

    if 2.7e52 < d

    1. Initial program 46.8%

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

      \[\leadsto \frac{\color{blue}{b \cdot d}}{c \cdot c + d \cdot d} \]
    4. Step-by-step derivation
      1. *-commutative46.3%

        \[\leadsto \frac{\color{blue}{d \cdot b}}{c \cdot c + d \cdot d} \]
      2. add-sqr-sqrt46.3%

        \[\leadsto \frac{d \cdot b}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
      3. hypot-undefine46.3%

        \[\leadsto \frac{d \cdot b}{\color{blue}{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{c \cdot c + d \cdot d}} \]
      4. hypot-undefine46.3%

        \[\leadsto \frac{d \cdot b}{\mathsf{hypot}\left(c, d\right) \cdot \color{blue}{\mathsf{hypot}\left(c, d\right)}} \]
      5. times-frac85.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.8 \cdot 10^{+66}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq -1.6 \cdot 10^{-122}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{-111}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 2.7 \cdot 10^{+52}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 81.0% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -8.5 \cdot 10^{+66}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq -1.95 \cdot 10^{-124}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{-112}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 10^{+57}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))))
   (if (<= d -8.5e+66)
     (/ (+ b (* c (/ a d))) d)
     (if (<= d -1.95e-124)
       t_0
       (if (<= d 4.8e-112)
         (/ (+ a (* b (/ d c))) c)
         (if (<= d 1e+57) t_0 (+ (/ b d) (* a (/ c (pow d 2.0))))))))))
double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -8.5e+66) {
		tmp = (b + (c * (a / d))) / d;
	} else if (d <= -1.95e-124) {
		tmp = t_0;
	} else if (d <= 4.8e-112) {
		tmp = (a + (b * (d / c))) / c;
	} else if (d <= 1e+57) {
		tmp = t_0;
	} else {
		tmp = (b / d) + (a * (c / pow(d, 2.0)));
	}
	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) :: t_0
    real(8) :: tmp
    t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
    if (d <= (-8.5d+66)) then
        tmp = (b + (c * (a / d))) / d
    else if (d <= (-1.95d-124)) then
        tmp = t_0
    else if (d <= 4.8d-112) then
        tmp = (a + (b * (d / c))) / c
    else if (d <= 1d+57) then
        tmp = t_0
    else
        tmp = (b / d) + (a * (c / (d ** 2.0d0)))
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -8.5e+66) {
		tmp = (b + (c * (a / d))) / d;
	} else if (d <= -1.95e-124) {
		tmp = t_0;
	} else if (d <= 4.8e-112) {
		tmp = (a + (b * (d / c))) / c;
	} else if (d <= 1e+57) {
		tmp = t_0;
	} else {
		tmp = (b / d) + (a * (c / Math.pow(d, 2.0)));
	}
	return tmp;
}
def code(a, b, c, d):
	t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
	tmp = 0
	if d <= -8.5e+66:
		tmp = (b + (c * (a / d))) / d
	elif d <= -1.95e-124:
		tmp = t_0
	elif d <= 4.8e-112:
		tmp = (a + (b * (d / c))) / c
	elif d <= 1e+57:
		tmp = t_0
	else:
		tmp = (b / d) + (a * (c / math.pow(d, 2.0)))
	return tmp
function code(a, b, c, d)
	t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (d <= -8.5e+66)
		tmp = Float64(Float64(b + Float64(c * Float64(a / d))) / d);
	elseif (d <= -1.95e-124)
		tmp = t_0;
	elseif (d <= 4.8e-112)
		tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c);
	elseif (d <= 1e+57)
		tmp = t_0;
	else
		tmp = Float64(Float64(b / d) + Float64(a * Float64(c / (d ^ 2.0))));
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	tmp = 0.0;
	if (d <= -8.5e+66)
		tmp = (b + (c * (a / d))) / d;
	elseif (d <= -1.95e-124)
		tmp = t_0;
	elseif (d <= 4.8e-112)
		tmp = (a + (b * (d / c))) / c;
	elseif (d <= 1e+57)
		tmp = t_0;
	else
		tmp = (b / d) + (a * (c / (d ^ 2.0)));
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -8.5e+66], N[(N[(b + N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -1.95e-124], t$95$0, If[LessEqual[d, 4.8e-112], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1e+57], t$95$0, N[(N[(b / d), $MachinePrecision] + N[(a * N[(c / N[Power[d, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{if}\;d \leq -8.5 \cdot 10^{+66}:\\
\;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\

\mathbf{elif}\;d \leq -1.95 \cdot 10^{-124}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;d \leq 10^{+57}:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d < -8.5000000000000004e66

    1. Initial program 39.2%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. *-commutative71.3%

        \[\leadsto \frac{b + \frac{\color{blue}{c \cdot a}}{d}}{d} \]
      2. *-un-lft-identity71.3%

        \[\leadsto \frac{b + \frac{c \cdot a}{\color{blue}{1 \cdot d}}}{d} \]
      3. times-frac81.9%

        \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]
    5. Applied egg-rr81.9%

      \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]

    if -8.5000000000000004e66 < d < -1.94999999999999996e-124 or 4.8000000000000001e-112 < d < 1.00000000000000005e57

    1. Initial program 85.0%

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

    if -1.94999999999999996e-124 < d < 4.8000000000000001e-112

    1. Initial program 76.1%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. associate-/l*96.3%

        \[\leadsto \frac{a + \color{blue}{b \cdot \frac{d}{c}}}{c} \]
    5. Simplified96.3%

      \[\leadsto \color{blue}{\frac{a + b \cdot \frac{d}{c}}{c}} \]

    if 1.00000000000000005e57 < d

    1. Initial program 45.6%

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{a \cdot c}{{d}^{2}}} \]
    4. Step-by-step derivation
      1. associate-/l*83.6%

        \[\leadsto \frac{b}{d} + \color{blue}{a \cdot \frac{c}{{d}^{2}}} \]
    5. Simplified83.6%

      \[\leadsto \color{blue}{\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification87.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -8.5 \cdot 10^{+66}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq -1.95 \cdot 10^{-124}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{-112}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 10^{+57}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{{d}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 78.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -2.45 \cdot 10^{+26} \lor \neg \left(d \leq 5.8 \cdot 10^{-21}\right):\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (or (<= d -2.45e+26) (not (<= d 5.8e-21)))
   (/ (+ b (* c (/ a d))) d)
   (/ (+ a (* b (/ d c))) c)))
double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -2.45e+26) || !(d <= 5.8e-21)) {
		tmp = (b + (c * (a / d))) / d;
	} else {
		tmp = (a + (b * (d / c))) / 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 ((d <= (-2.45d+26)) .or. (.not. (d <= 5.8d-21))) then
        tmp = (b + (c * (a / d))) / d
    else
        tmp = (a + (b * (d / c))) / c
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -2.45e+26) || !(d <= 5.8e-21)) {
		tmp = (b + (c * (a / d))) / d;
	} else {
		tmp = (a + (b * (d / c))) / c;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if (d <= -2.45e+26) or not (d <= 5.8e-21):
		tmp = (b + (c * (a / d))) / d
	else:
		tmp = (a + (b * (d / c))) / c
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if ((d <= -2.45e+26) || !(d <= 5.8e-21))
		tmp = Float64(Float64(b + Float64(c * Float64(a / d))) / d);
	else
		tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if ((d <= -2.45e+26) || ~((d <= 5.8e-21)))
		tmp = (b + (c * (a / d))) / d;
	else
		tmp = (a + (b * (d / c))) / c;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.45e+26], N[Not[LessEqual[d, 5.8e-21]], $MachinePrecision]], N[(N[(b + N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.45 \cdot 10^{+26} \lor \neg \left(d \leq 5.8 \cdot 10^{-21}\right):\\
\;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\

\mathbf{else}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\


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

    1. Initial program 50.7%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. *-commutative73.6%

        \[\leadsto \frac{b + \frac{\color{blue}{c \cdot a}}{d}}{d} \]
      2. *-un-lft-identity73.6%

        \[\leadsto \frac{b + \frac{c \cdot a}{\color{blue}{1 \cdot d}}}{d} \]
      3. times-frac78.2%

        \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]
    5. Applied egg-rr78.2%

      \[\leadsto \frac{b + \color{blue}{\frac{c}{1} \cdot \frac{a}{d}}}{d} \]

    if -2.44999999999999987e26 < d < 5.8e-21

    1. Initial program 78.8%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. associate-/l*88.6%

        \[\leadsto \frac{a + \color{blue}{b \cdot \frac{d}{c}}}{c} \]
    5. Simplified88.6%

      \[\leadsto \color{blue}{\frac{a + b \cdot \frac{d}{c}}{c}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification83.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.45 \cdot 10^{+26} \lor \neg \left(d \leq 5.8 \cdot 10^{-21}\right):\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 77.5% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -9 \cdot 10^{+29} \lor \neg \left(d \leq 3.5 \cdot 10^{-23}\right):\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (or (<= d -9e+29) (not (<= d 3.5e-23)))
   (/ (+ b (* a (/ c d))) d)
   (/ (+ a (* b (/ d c))) c)))
double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -9e+29) || !(d <= 3.5e-23)) {
		tmp = (b + (a * (c / d))) / d;
	} else {
		tmp = (a + (b * (d / c))) / 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 ((d <= (-9d+29)) .or. (.not. (d <= 3.5d-23))) then
        tmp = (b + (a * (c / d))) / d
    else
        tmp = (a + (b * (d / c))) / c
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -9e+29) || !(d <= 3.5e-23)) {
		tmp = (b + (a * (c / d))) / d;
	} else {
		tmp = (a + (b * (d / c))) / c;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if (d <= -9e+29) or not (d <= 3.5e-23):
		tmp = (b + (a * (c / d))) / d
	else:
		tmp = (a + (b * (d / c))) / c
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if ((d <= -9e+29) || !(d <= 3.5e-23))
		tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d);
	else
		tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if ((d <= -9e+29) || ~((d <= 3.5e-23)))
		tmp = (b + (a * (c / d))) / d;
	else
		tmp = (a + (b * (d / c))) / c;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -9e+29], N[Not[LessEqual[d, 3.5e-23]], $MachinePrecision]], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d \leq -9 \cdot 10^{+29} \lor \neg \left(d \leq 3.5 \cdot 10^{-23}\right):\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\

\mathbf{else}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -9.0000000000000005e29 or 3.49999999999999993e-23 < d

    1. Initial program 50.7%

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

      \[\leadsto \color{blue}{\frac{b + \frac{a \cdot c}{d}}{d}} \]
    4. Step-by-step derivation
      1. associate-/l*77.5%

        \[\leadsto \frac{b + \color{blue}{a \cdot \frac{c}{d}}}{d} \]
    5. Simplified77.5%

      \[\leadsto \color{blue}{\frac{b + a \cdot \frac{c}{d}}{d}} \]

    if -9.0000000000000005e29 < d < 3.49999999999999993e-23

    1. Initial program 78.8%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. associate-/l*88.6%

        \[\leadsto \frac{a + \color{blue}{b \cdot \frac{d}{c}}}{c} \]
    5. Simplified88.6%

      \[\leadsto \color{blue}{\frac{a + b \cdot \frac{d}{c}}{c}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification82.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -9 \cdot 10^{+29} \lor \neg \left(d \leq 3.5 \cdot 10^{-23}\right):\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 73.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -1.6 \cdot 10^{+31} \lor \neg \left(d \leq 1.25 \cdot 10^{+60}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (or (<= d -1.6e+31) (not (<= d 1.25e+60)))
   (/ b d)
   (/ (+ a (* b (/ d c))) c)))
double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -1.6e+31) || !(d <= 1.25e+60)) {
		tmp = b / d;
	} else {
		tmp = (a + (b * (d / c))) / 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 ((d <= (-1.6d+31)) .or. (.not. (d <= 1.25d+60))) then
        tmp = b / d
    else
        tmp = (a + (b * (d / c))) / c
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	double tmp;
	if ((d <= -1.6e+31) || !(d <= 1.25e+60)) {
		tmp = b / d;
	} else {
		tmp = (a + (b * (d / c))) / c;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if (d <= -1.6e+31) or not (d <= 1.25e+60):
		tmp = b / d
	else:
		tmp = (a + (b * (d / c))) / c
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if ((d <= -1.6e+31) || !(d <= 1.25e+60))
		tmp = Float64(b / d);
	else
		tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c);
	end
	return tmp
end
function tmp_2 = code(a, b, c, d)
	tmp = 0.0;
	if ((d <= -1.6e+31) || ~((d <= 1.25e+60)))
		tmp = b / d;
	else
		tmp = (a + (b * (d / c))) / c;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -1.6e+31], N[Not[LessEqual[d, 1.25e+60]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.6 \cdot 10^{+31} \lor \neg \left(d \leq 1.25 \cdot 10^{+60}\right):\\
\;\;\;\;\frac{b}{d}\\

\mathbf{else}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d < -1.6e31 or 1.24999999999999994e60 < d

    1. Initial program 45.6%

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

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

    if -1.6e31 < d < 1.24999999999999994e60

    1. Initial program 79.0%

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

      \[\leadsto \color{blue}{\frac{a + \frac{b \cdot d}{c}}{c}} \]
    4. Step-by-step derivation
      1. associate-/l*82.2%

        \[\leadsto \frac{a + \color{blue}{b \cdot \frac{d}{c}}}{c} \]
    5. Simplified82.2%

      \[\leadsto \color{blue}{\frac{a + b \cdot \frac{d}{c}}{c}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.6 \cdot 10^{+31} \lor \neg \left(d \leq 1.25 \cdot 10^{+60}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 63.4% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;c \leq -5.6 \cdot 10^{+124} \lor \neg \left(c \leq 0.0032\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (if (or (<= c -5.6e+124) (not (<= c 0.0032))) (/ a c) (/ b d)))
double code(double a, double b, double c, double d) {
	double tmp;
	if ((c <= -5.6e+124) || !(c <= 0.0032)) {
		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 ((c <= (-5.6d+124)) .or. (.not. (c <= 0.0032d0))) 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 ((c <= -5.6e+124) || !(c <= 0.0032)) {
		tmp = a / c;
	} else {
		tmp = b / d;
	}
	return tmp;
}
def code(a, b, c, d):
	tmp = 0
	if (c <= -5.6e+124) or not (c <= 0.0032):
		tmp = a / c
	else:
		tmp = b / d
	return tmp
function code(a, b, c, d)
	tmp = 0.0
	if ((c <= -5.6e+124) || !(c <= 0.0032))
		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 ((c <= -5.6e+124) || ~((c <= 0.0032)))
		tmp = a / c;
	else
		tmp = b / d;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -5.6e+124], N[Not[LessEqual[c, 0.0032]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;c \leq -5.6 \cdot 10^{+124} \lor \neg \left(c \leq 0.0032\right):\\
\;\;\;\;\frac{a}{c}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if c < -5.59999999999999999e124 or 0.00320000000000000015 < c

    1. Initial program 49.2%

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

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

    if -5.59999999999999999e124 < c < 0.00320000000000000015

    1. Initial program 72.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.6 \cdot 10^{+124} \lor \neg \left(c \leq 0.0032\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 42.1% accurate, 5.0× 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.1%

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

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

Developer target: 99.2% accurate, 0.1× 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 2024108 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :alt
  (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))))