Complex division, real part

Percentage Accurate: 62.2% → 80.2%
Time: 4.1s
Alternatives: 7
Speedup: 1.6×

Specification

?
\[\begin{array}{l} \\ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(a, b, c, d)
use fmin_fmax_functions
    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}

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 7 alternatives:

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

Initial Program: 62.2% 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));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(a, b, c, d)
use fmin_fmax_functions
    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.2% accurate, 0.8× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d < -8.8e133 or 4.2e38 < d

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

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

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

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

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

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

      \[\leadsto \frac{c \cdot \left(\frac{a}{d} + \frac{b}{c}\right)}{d} \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      2. lower-*.f64N/A

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      3. lower-+.f64N/A

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      4. lower-/.f64N/A

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      5. lift-/.f6481.1

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
    8. Applied rewrites81.1%

      \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
    9. Step-by-step derivation
      1. lift-*.f64N/A

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

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      3. lift-/.f64N/A

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

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      5. *-commutativeN/A

        \[\leadsto \frac{c \cdot \left(\frac{a}{d} + \frac{b}{c}\right)}{d} \]
      6. distribute-rgt-inN/A

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

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

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

        \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, \frac{b}{c} \cdot c\right)}{d} \]
      10. lift-/.f6481.2

        \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, \frac{b}{c} \cdot c\right)}{d} \]
    10. Applied rewrites81.2%

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

      \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d} \]
    12. Step-by-step derivation
      1. Applied rewrites92.2%

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

      if -8.8e133 < d < -4.19999999999999982e-60

      1. Initial program 76.4%

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

      if -4.19999999999999982e-60 < d < 4.2e38

      1. Initial program 69.7%

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

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

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

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

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

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

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

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

    Alternative 2: 72.4% accurate, 0.8× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;c \leq -2.25 \cdot 10^{+132}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -400000:\\ \;\;\;\;a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;c \leq 2.7 \cdot 10^{+153}:\\ \;\;\;\;\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 -2.25e+132)
       (/ a c)
       (if (<= c -400000.0)
         (* a (/ c (fma d d (* c c))))
         (if (<= c 2.7e+153) (/ (fma a (/ c d) b) d) (/ a c)))))
    double code(double a, double b, double c, double d) {
    	double tmp;
    	if (c <= -2.25e+132) {
    		tmp = a / c;
    	} else if (c <= -400000.0) {
    		tmp = a * (c / fma(d, d, (c * c)));
    	} else if (c <= 2.7e+153) {
    		tmp = fma(a, (c / d), b) / d;
    	} else {
    		tmp = a / c;
    	}
    	return tmp;
    }
    
    function code(a, b, c, d)
    	tmp = 0.0
    	if (c <= -2.25e+132)
    		tmp = Float64(a / c);
    	elseif (c <= -400000.0)
    		tmp = Float64(a * Float64(c / fma(d, d, Float64(c * c))));
    	elseif (c <= 2.7e+153)
    		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, -2.25e+132], N[(a / c), $MachinePrecision], If[LessEqual[c, -400000.0], N[(a * N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.7e+153], 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 -2.25 \cdot 10^{+132}:\\
    \;\;\;\;\frac{a}{c}\\
    
    \mathbf{elif}\;c \leq -400000:\\
    \;\;\;\;a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
    
    \mathbf{elif}\;c \leq 2.7 \cdot 10^{+153}:\\
    \;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{a}{c}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if c < -2.24999999999999986e132 or 2.7000000000000001e153 < c

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

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

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

      if -2.24999999999999986e132 < c < -4e5

      1. Initial program 90.6%

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

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

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

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

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

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

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

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

          \[\leadsto a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \]
        8. lift-*.f6473.5

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

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

      if -4e5 < c < 2.7000000000000001e153

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

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

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

          \[\leadsto \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d} \]
        5. lower-/.f6478.9

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

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

    Alternative 3: 77.9% accurate, 0.9× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -3.65 \cdot 10^{-53} \lor \neg \left(d \leq 4.2 \cdot 10^{+38}\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \end{array} \end{array} \]
    (FPCore (a b c d)
     :precision binary64
     (if (or (<= d -3.65e-53) (not (<= d 4.2e+38)))
       (/ (fma (/ a d) c b) d)
       (/ (fma b (/ d c) a) c)))
    double code(double a, double b, double c, double d) {
    	double tmp;
    	if ((d <= -3.65e-53) || !(d <= 4.2e+38)) {
    		tmp = fma((a / d), c, b) / d;
    	} else {
    		tmp = fma(b, (d / c), a) / c;
    	}
    	return tmp;
    }
    
    function code(a, b, c, d)
    	tmp = 0.0
    	if ((d <= -3.65e-53) || !(d <= 4.2e+38))
    		tmp = Float64(fma(Float64(a / d), c, b) / d);
    	else
    		tmp = Float64(fma(b, Float64(d / c), a) / c);
    	end
    	return tmp
    end
    
    code[a_, b_, c_, d_] := If[Or[LessEqual[d, -3.65e-53], N[Not[LessEqual[d, 4.2e+38]], $MachinePrecision]], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;d \leq -3.65 \cdot 10^{-53} \lor \neg \left(d \leq 4.2 \cdot 10^{+38}\right):\\
    \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if d < -3.65000000000000008e-53 or 4.2e38 < d

      1. Initial program 55.5%

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

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

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

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

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

          \[\leadsto \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d} \]
        5. lower-/.f6482.1

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

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

        \[\leadsto \frac{c \cdot \left(\frac{a}{d} + \frac{b}{c}\right)}{d} \]
      7. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        2. lower-*.f64N/A

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        3. lower-+.f64N/A

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        4. lower-/.f64N/A

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        5. lift-/.f6467.6

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      8. Applied rewrites67.6%

        \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
      9. Step-by-step derivation
        1. lift-*.f64N/A

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

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        3. lift-/.f64N/A

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

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        5. *-commutativeN/A

          \[\leadsto \frac{c \cdot \left(\frac{a}{d} + \frac{b}{c}\right)}{d} \]
        6. distribute-rgt-inN/A

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

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

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

          \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, \frac{b}{c} \cdot c\right)}{d} \]
        10. lift-/.f6467.6

          \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, \frac{b}{c} \cdot c\right)}{d} \]
      10. Applied rewrites67.6%

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

        \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d} \]
      12. Step-by-step derivation
        1. Applied rewrites83.1%

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

        if -3.65000000000000008e-53 < d < 4.2e38

        1. Initial program 70.5%

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

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

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

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

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

            \[\leadsto \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c} \]
          5. lower-/.f6484.9

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

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

        \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.65 \cdot 10^{-53} \lor \neg \left(d \leq 4.2 \cdot 10^{+38}\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\ \end{array} \]
      15. Add Preprocessing

      Alternative 4: 70.2% accurate, 0.9× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -2.9 \cdot 10^{-53} \lor \neg \left(d \leq 540000\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \end{array} \]
      (FPCore (a b c d)
       :precision binary64
       (if (or (<= d -2.9e-53) (not (<= d 540000.0)))
         (/ (fma (/ a d) c b) d)
         (/ a c)))
      double code(double a, double b, double c, double d) {
      	double tmp;
      	if ((d <= -2.9e-53) || !(d <= 540000.0)) {
      		tmp = fma((a / d), c, b) / d;
      	} else {
      		tmp = a / c;
      	}
      	return tmp;
      }
      
      function code(a, b, c, d)
      	tmp = 0.0
      	if ((d <= -2.9e-53) || !(d <= 540000.0))
      		tmp = Float64(fma(Float64(a / d), c, b) / d);
      	else
      		tmp = Float64(a / c);
      	end
      	return tmp
      end
      
      code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.9e-53], N[Not[LessEqual[d, 540000.0]], $MachinePrecision]], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;d \leq -2.9 \cdot 10^{-53} \lor \neg \left(d \leq 540000\right):\\
      \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
      
      \mathbf{else}:\\
      \;\;\;\;\frac{a}{c}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if d < -2.8999999999999998e-53 or 5.4e5 < d

        1. Initial program 56.7%

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

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

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

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

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

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

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

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

          \[\leadsto \frac{c \cdot \left(\frac{a}{d} + \frac{b}{c}\right)}{d} \]
        7. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
          2. lower-*.f64N/A

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
          3. lower-+.f64N/A

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
          4. lower-/.f64N/A

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
          5. lift-/.f6465.7

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        8. Applied rewrites65.7%

          \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
        9. Step-by-step derivation
          1. lift-*.f64N/A

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

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
          3. lift-/.f64N/A

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

            \[\leadsto \frac{\left(\frac{a}{d} + \frac{b}{c}\right) \cdot c}{d} \]
          5. *-commutativeN/A

            \[\leadsto \frac{c \cdot \left(\frac{a}{d} + \frac{b}{c}\right)}{d} \]
          6. distribute-rgt-inN/A

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

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

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

            \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, \frac{b}{c} \cdot c\right)}{d} \]
          10. lift-/.f6465.7

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

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

          \[\leadsto \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d} \]
        12. Step-by-step derivation
          1. Applied rewrites81.2%

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

          if -2.8999999999999998e-53 < d < 5.4e5

          1. Initial program 69.7%

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

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

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

            \[\leadsto \color{blue}{\frac{a}{c}} \]
        13. Recombined 2 regimes into one program.
        14. Final simplification75.0%

          \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.9 \cdot 10^{-53} \lor \neg \left(d \leq 540000\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
        15. Add Preprocessing

        Alternative 5: 65.4% accurate, 0.9× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -2.5 \cdot 10^{+131}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.65 \cdot 10^{-60}:\\ \;\;\;\;b \cdot \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;d \leq 1.25 \cdot 10^{+38}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \end{array} \]
        (FPCore (a b c d)
         :precision binary64
         (if (<= d -2.5e+131)
           (/ b d)
           (if (<= d -4.65e-60)
             (* b (/ d (fma d d (* c c))))
             (if (<= d 1.25e+38) (/ a c) (/ b d)))))
        double code(double a, double b, double c, double d) {
        	double tmp;
        	if (d <= -2.5e+131) {
        		tmp = b / d;
        	} else if (d <= -4.65e-60) {
        		tmp = b * (d / fma(d, d, (c * c)));
        	} else if (d <= 1.25e+38) {
        		tmp = a / c;
        	} else {
        		tmp = b / d;
        	}
        	return tmp;
        }
        
        function code(a, b, c, d)
        	tmp = 0.0
        	if (d <= -2.5e+131)
        		tmp = Float64(b / d);
        	elseif (d <= -4.65e-60)
        		tmp = Float64(b * Float64(d / fma(d, d, Float64(c * c))));
        	elseif (d <= 1.25e+38)
        		tmp = Float64(a / c);
        	else
        		tmp = Float64(b / d);
        	end
        	return tmp
        end
        
        code[a_, b_, c_, d_] := If[LessEqual[d, -2.5e+131], N[(b / d), $MachinePrecision], If[LessEqual[d, -4.65e-60], N[(b * N[(d / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.25e+38], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;d \leq -2.5 \cdot 10^{+131}:\\
        \;\;\;\;\frac{b}{d}\\
        
        \mathbf{elif}\;d \leq -4.65 \cdot 10^{-60}:\\
        \;\;\;\;b \cdot \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
        
        \mathbf{elif}\;d \leq 1.25 \cdot 10^{+38}:\\
        \;\;\;\;\frac{a}{c}\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{b}{d}\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if d < -2.49999999999999998e131 or 1.24999999999999992e38 < 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 c around 0

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

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

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

          if -2.49999999999999998e131 < d < -4.6500000000000001e-60

          1. Initial program 75.9%

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

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

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

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

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

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

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

              \[\leadsto b \cdot \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)} \]
            8. lift-*.f6465.8

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

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

          if -4.6500000000000001e-60 < d < 1.24999999999999992e38

          1. Initial program 69.7%

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

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

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

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

        Alternative 6: 63.7% accurate, 1.6× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d \leq -7 \cdot 10^{-41} \lor \neg \left(d \leq 1.25 \cdot 10^{+38}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \end{array} \]
        (FPCore (a b c d)
         :precision binary64
         (if (or (<= d -7e-41) (not (<= d 1.25e+38))) (/ b d) (/ a c)))
        double code(double a, double b, double c, double d) {
        	double tmp;
        	if ((d <= -7e-41) || !(d <= 1.25e+38)) {
        		tmp = b / d;
        	} else {
        		tmp = a / c;
        	}
        	return tmp;
        }
        
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(a, b, c, d)
        use fmin_fmax_functions
            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 <= (-7d-41)) .or. (.not. (d <= 1.25d+38))) then
                tmp = b / d
            else
                tmp = a / c
            end if
            code = tmp
        end function
        
        public static double code(double a, double b, double c, double d) {
        	double tmp;
        	if ((d <= -7e-41) || !(d <= 1.25e+38)) {
        		tmp = b / d;
        	} else {
        		tmp = a / c;
        	}
        	return tmp;
        }
        
        def code(a, b, c, d):
        	tmp = 0
        	if (d <= -7e-41) or not (d <= 1.25e+38):
        		tmp = b / d
        	else:
        		tmp = a / c
        	return tmp
        
        function code(a, b, c, d)
        	tmp = 0.0
        	if ((d <= -7e-41) || !(d <= 1.25e+38))
        		tmp = Float64(b / d);
        	else
        		tmp = Float64(a / c);
        	end
        	return tmp
        end
        
        function tmp_2 = code(a, b, c, d)
        	tmp = 0.0;
        	if ((d <= -7e-41) || ~((d <= 1.25e+38)))
        		tmp = b / d;
        	else
        		tmp = a / c;
        	end
        	tmp_2 = tmp;
        end
        
        code[a_, b_, c_, d_] := If[Or[LessEqual[d, -7e-41], N[Not[LessEqual[d, 1.25e+38]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;d \leq -7 \cdot 10^{-41} \lor \neg \left(d \leq 1.25 \cdot 10^{+38}\right):\\
        \;\;\;\;\frac{b}{d}\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{a}{c}\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if d < -6.9999999999999999e-41 or 1.24999999999999992e38 < d

          1. Initial program 55.2%

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

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

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

          if -6.9999999999999999e-41 < d < 1.24999999999999992e38

          1. Initial program 70.7%

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

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

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

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

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

        Alternative 7: 43.1% 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;
        }
        
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(a, b, c, d)
        use fmin_fmax_functions
            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 62.2%

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

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

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

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

        Developer Target 1: 99.4% 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;
        }
        
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(a, b, c, d)
        use fmin_fmax_functions
            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 2025077 
        (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))))