Quotient of products

Percentage Accurate: 86.1% → 97.5%
Time: 3.7s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \frac{a1 \cdot a2}{b1 \cdot b2} \end{array} \]
(FPCore (a1 a2 b1 b2) :precision binary64 (/ (* a1 a2) (* b1 b2)))
double code(double a1, double a2, double b1, double b2) {
	return (a1 * a2) / (b1 * b2);
}
real(8) function code(a1, a2, b1, b2)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: b1
    real(8), intent (in) :: b2
    code = (a1 * a2) / (b1 * b2)
end function
public static double code(double a1, double a2, double b1, double b2) {
	return (a1 * a2) / (b1 * b2);
}
def code(a1, a2, b1, b2):
	return (a1 * a2) / (b1 * b2)
function code(a1, a2, b1, b2)
	return Float64(Float64(a1 * a2) / Float64(b1 * b2))
end
function tmp = code(a1, a2, b1, b2)
	tmp = (a1 * a2) / (b1 * b2);
end
code[a1_, a2_, b1_, b2_] := N[(N[(a1 * a2), $MachinePrecision] / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{a1 \cdot a2}{b1 \cdot b2}
\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 5 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: 86.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{a1 \cdot a2}{b1 \cdot b2} \end{array} \]
(FPCore (a1 a2 b1 b2) :precision binary64 (/ (* a1 a2) (* b1 b2)))
double code(double a1, double a2, double b1, double b2) {
	return (a1 * a2) / (b1 * b2);
}
real(8) function code(a1, a2, b1, b2)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: b1
    real(8), intent (in) :: b2
    code = (a1 * a2) / (b1 * b2)
end function
public static double code(double a1, double a2, double b1, double b2) {
	return (a1 * a2) / (b1 * b2);
}
def code(a1, a2, b1, b2):
	return (a1 * a2) / (b1 * b2)
function code(a1, a2, b1, b2)
	return Float64(Float64(a1 * a2) / Float64(b1 * b2))
end
function tmp = code(a1, a2, b1, b2)
	tmp = (a1 * a2) / (b1 * b2);
end
code[a1_, a2_, b1_, b2_] := N[(N[(a1 * a2), $MachinePrecision] / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{a1 \cdot a2}{b1 \cdot b2}
\end{array}

Alternative 1: 97.5% accurate, 0.2× speedup?

\[\begin{array}{l} [b1, b2] = \mathsf{sort}([b1, b2])\\ \\ \begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ t_1 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq -2 \cdot 10^{-322}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+297}:\\ \;\;\;\;\left(a1 \cdot a2\right) \cdot \frac{\frac{1}{b1}}{b2}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\ \end{array} \end{array} \]
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 b1 b2)
 :precision binary64
 (let* ((t_0 (/ (* a1 a2) (* b1 b2))) (t_1 (* (/ a1 b1) (/ a2 b2))))
   (if (<= t_0 (- INFINITY))
     t_1
     (if (<= t_0 -2e-322)
       t_0
       (if (<= t_0 0.0)
         t_1
         (if (<= t_0 5e+297)
           (* (* a1 a2) (/ (/ 1.0 b1) b2))
           (* (/ a2 b1) (/ a1 b2))))))))
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
	double t_0 = (a1 * a2) / (b1 * b2);
	double t_1 = (a1 / b1) * (a2 / b2);
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = t_1;
	} else if (t_0 <= -2e-322) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+297) {
		tmp = (a1 * a2) * ((1.0 / b1) / b2);
	} else {
		tmp = (a2 / b1) * (a1 / b2);
	}
	return tmp;
}
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
	double t_0 = (a1 * a2) / (b1 * b2);
	double t_1 = (a1 / b1) * (a2 / b2);
	double tmp;
	if (t_0 <= -Double.POSITIVE_INFINITY) {
		tmp = t_1;
	} else if (t_0 <= -2e-322) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+297) {
		tmp = (a1 * a2) * ((1.0 / b1) / b2);
	} else {
		tmp = (a2 / b1) * (a1 / b2);
	}
	return tmp;
}
[b1, b2] = sort([b1, b2])
def code(a1, a2, b1, b2):
	t_0 = (a1 * a2) / (b1 * b2)
	t_1 = (a1 / b1) * (a2 / b2)
	tmp = 0
	if t_0 <= -math.inf:
		tmp = t_1
	elif t_0 <= -2e-322:
		tmp = t_0
	elif t_0 <= 0.0:
		tmp = t_1
	elif t_0 <= 5e+297:
		tmp = (a1 * a2) * ((1.0 / b1) / b2)
	else:
		tmp = (a2 / b1) * (a1 / b2)
	return tmp
b1, b2 = sort([b1, b2])
function code(a1, a2, b1, b2)
	t_0 = Float64(Float64(a1 * a2) / Float64(b1 * b2))
	t_1 = Float64(Float64(a1 / b1) * Float64(a2 / b2))
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = t_1;
	elseif (t_0 <= -2e-322)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 5e+297)
		tmp = Float64(Float64(a1 * a2) * Float64(Float64(1.0 / b1) / b2));
	else
		tmp = Float64(Float64(a2 / b1) * Float64(a1 / b2));
	end
	return tmp
end
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
	t_0 = (a1 * a2) / (b1 * b2);
	t_1 = (a1 / b1) * (a2 / b2);
	tmp = 0.0;
	if (t_0 <= -Inf)
		tmp = t_1;
	elseif (t_0 <= -2e-322)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 5e+297)
		tmp = (a1 * a2) * ((1.0 / b1) / b2);
	else
		tmp = (a2 / b1) * (a1 / b2);
	end
	tmp_2 = tmp;
end
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
code[a1_, a2_, b1_, b2_] := Block[{t$95$0 = N[(N[(a1 * a2), $MachinePrecision] / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, -2e-322], t$95$0, If[LessEqual[t$95$0, 0.0], t$95$1, If[LessEqual[t$95$0, 5e+297], N[(N[(a1 * a2), $MachinePrecision] * N[(N[(1.0 / b1), $MachinePrecision] / b2), $MachinePrecision]), $MachinePrecision], N[(N[(a2 / b1), $MachinePrecision] * N[(a1 / b2), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\
t_1 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq -2 \cdot 10^{-322}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+297}:\\
\;\;\;\;\left(a1 \cdot a2\right) \cdot \frac{\frac{1}{b1}}{b2}\\

\mathbf{else}:\\
\;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -inf.0 or -1.97626e-322 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -0.0

    1. Initial program 71.3%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac92.9%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
    3. Simplified92.9%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]

    if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -1.97626e-322

    1. Initial program 99.5%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]

    if -0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 4.9999999999999998e297

    1. Initial program 99.2%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. clear-num99.3%

        \[\leadsto \color{blue}{\frac{1}{\frac{b1 \cdot b2}{a1 \cdot a2}}} \]
      2. associate-/r/99.3%

        \[\leadsto \color{blue}{\frac{1}{b1 \cdot b2} \cdot \left(a1 \cdot a2\right)} \]
      3. associate-/r*99.4%

        \[\leadsto \color{blue}{\frac{\frac{1}{b1}}{b2}} \cdot \left(a1 \cdot a2\right) \]
    3. Applied egg-rr99.4%

      \[\leadsto \color{blue}{\frac{\frac{1}{b1}}{b2} \cdot \left(a1 \cdot a2\right)} \]

    if 4.9999999999999998e297 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))

    1. Initial program 64.1%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac95.3%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
      2. associate-*l/83.5%

        \[\leadsto \color{blue}{\frac{a1 \cdot \frac{a2}{b2}}{b1}} \]
      3. associate-*r/90.5%

        \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
    3. Simplified90.5%

      \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
    4. Taylor expanded in a1 around 0 64.1%

      \[\leadsto \color{blue}{\frac{a1 \cdot a2}{b1 \cdot b2}} \]
    5. Step-by-step derivation
      1. *-commutative64.1%

        \[\leadsto \frac{\color{blue}{a2 \cdot a1}}{b1 \cdot b2} \]
      2. times-frac99.6%

        \[\leadsto \color{blue}{\frac{a2}{b1} \cdot \frac{a1}{b2}} \]
    6. Simplified99.6%

      \[\leadsto \color{blue}{\frac{a2}{b1} \cdot \frac{a1}{b2}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification96.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq -\infty:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq -2 \cdot 10^{-322}:\\ \;\;\;\;\frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq 0:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq 5 \cdot 10^{+297}:\\ \;\;\;\;\left(a1 \cdot a2\right) \cdot \frac{\frac{1}{b1}}{b2}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\ \end{array} \]

Alternative 2: 97.6% accurate, 0.2× speedup?

\[\begin{array}{l} [b1, b2] = \mathsf{sort}([b1, b2])\\ \\ \begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ t_1 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq -2 \cdot 10^{-322}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+297}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\ \end{array} \end{array} \]
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 b1 b2)
 :precision binary64
 (let* ((t_0 (/ (* a1 a2) (* b1 b2))) (t_1 (* (/ a1 b1) (/ a2 b2))))
   (if (<= t_0 (- INFINITY))
     t_1
     (if (<= t_0 -2e-322)
       t_0
       (if (<= t_0 0.0)
         t_1
         (if (<= t_0 5e+297) t_0 (* (/ a2 b1) (/ a1 b2))))))))
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
	double t_0 = (a1 * a2) / (b1 * b2);
	double t_1 = (a1 / b1) * (a2 / b2);
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = t_1;
	} else if (t_0 <= -2e-322) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+297) {
		tmp = t_0;
	} else {
		tmp = (a2 / b1) * (a1 / b2);
	}
	return tmp;
}
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
	double t_0 = (a1 * a2) / (b1 * b2);
	double t_1 = (a1 / b1) * (a2 / b2);
	double tmp;
	if (t_0 <= -Double.POSITIVE_INFINITY) {
		tmp = t_1;
	} else if (t_0 <= -2e-322) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+297) {
		tmp = t_0;
	} else {
		tmp = (a2 / b1) * (a1 / b2);
	}
	return tmp;
}
[b1, b2] = sort([b1, b2])
def code(a1, a2, b1, b2):
	t_0 = (a1 * a2) / (b1 * b2)
	t_1 = (a1 / b1) * (a2 / b2)
	tmp = 0
	if t_0 <= -math.inf:
		tmp = t_1
	elif t_0 <= -2e-322:
		tmp = t_0
	elif t_0 <= 0.0:
		tmp = t_1
	elif t_0 <= 5e+297:
		tmp = t_0
	else:
		tmp = (a2 / b1) * (a1 / b2)
	return tmp
b1, b2 = sort([b1, b2])
function code(a1, a2, b1, b2)
	t_0 = Float64(Float64(a1 * a2) / Float64(b1 * b2))
	t_1 = Float64(Float64(a1 / b1) * Float64(a2 / b2))
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = t_1;
	elseif (t_0 <= -2e-322)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 5e+297)
		tmp = t_0;
	else
		tmp = Float64(Float64(a2 / b1) * Float64(a1 / b2));
	end
	return tmp
end
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
	t_0 = (a1 * a2) / (b1 * b2);
	t_1 = (a1 / b1) * (a2 / b2);
	tmp = 0.0;
	if (t_0 <= -Inf)
		tmp = t_1;
	elseif (t_0 <= -2e-322)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 5e+297)
		tmp = t_0;
	else
		tmp = (a2 / b1) * (a1 / b2);
	end
	tmp_2 = tmp;
end
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
code[a1_, a2_, b1_, b2_] := Block[{t$95$0 = N[(N[(a1 * a2), $MachinePrecision] / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, -2e-322], t$95$0, If[LessEqual[t$95$0, 0.0], t$95$1, If[LessEqual[t$95$0, 5e+297], t$95$0, N[(N[(a2 / b1), $MachinePrecision] * N[(a1 / b2), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\
t_1 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq -2 \cdot 10^{-322}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+297}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -inf.0 or -1.97626e-322 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -0.0

    1. Initial program 71.3%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac92.9%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
    3. Simplified92.9%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]

    if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -1.97626e-322 or -0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 4.9999999999999998e297

    1. Initial program 99.4%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]

    if 4.9999999999999998e297 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))

    1. Initial program 64.1%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac95.3%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
      2. associate-*l/83.5%

        \[\leadsto \color{blue}{\frac{a1 \cdot \frac{a2}{b2}}{b1}} \]
      3. associate-*r/90.5%

        \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
    3. Simplified90.5%

      \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
    4. Taylor expanded in a1 around 0 64.1%

      \[\leadsto \color{blue}{\frac{a1 \cdot a2}{b1 \cdot b2}} \]
    5. Step-by-step derivation
      1. *-commutative64.1%

        \[\leadsto \frac{\color{blue}{a2 \cdot a1}}{b1 \cdot b2} \]
      2. times-frac99.6%

        \[\leadsto \color{blue}{\frac{a2}{b1} \cdot \frac{a1}{b2}} \]
    6. Simplified99.6%

      \[\leadsto \color{blue}{\frac{a2}{b1} \cdot \frac{a1}{b2}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification96.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq -\infty:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq -2 \cdot 10^{-322}:\\ \;\;\;\;\frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq 0:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq 5 \cdot 10^{+297}:\\ \;\;\;\;\frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\ \end{array} \]

Alternative 3: 86.8% accurate, 0.6× speedup?

\[\begin{array}{l} [b1, b2] = \mathsf{sort}([b1, b2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 5 \cdot 10^{-218} \lor \neg \left(a2 \leq 3.1 \cdot 10^{+174}\right):\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\ \end{array} \end{array} \]
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 b1 b2)
 :precision binary64
 (if (or (<= a2 5e-218) (not (<= a2 3.1e+174)))
   (* (/ a1 b1) (/ a2 b2))
   (* a1 (/ a2 (* b1 b2)))))
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
	double tmp;
	if ((a2 <= 5e-218) || !(a2 <= 3.1e+174)) {
		tmp = (a1 / b1) * (a2 / b2);
	} else {
		tmp = a1 * (a2 / (b1 * b2));
	}
	return tmp;
}
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, b1, b2)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: b1
    real(8), intent (in) :: b2
    real(8) :: tmp
    if ((a2 <= 5d-218) .or. (.not. (a2 <= 3.1d+174))) then
        tmp = (a1 / b1) * (a2 / b2)
    else
        tmp = a1 * (a2 / (b1 * b2))
    end if
    code = tmp
end function
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
	double tmp;
	if ((a2 <= 5e-218) || !(a2 <= 3.1e+174)) {
		tmp = (a1 / b1) * (a2 / b2);
	} else {
		tmp = a1 * (a2 / (b1 * b2));
	}
	return tmp;
}
[b1, b2] = sort([b1, b2])
def code(a1, a2, b1, b2):
	tmp = 0
	if (a2 <= 5e-218) or not (a2 <= 3.1e+174):
		tmp = (a1 / b1) * (a2 / b2)
	else:
		tmp = a1 * (a2 / (b1 * b2))
	return tmp
b1, b2 = sort([b1, b2])
function code(a1, a2, b1, b2)
	tmp = 0.0
	if ((a2 <= 5e-218) || !(a2 <= 3.1e+174))
		tmp = Float64(Float64(a1 / b1) * Float64(a2 / b2));
	else
		tmp = Float64(a1 * Float64(a2 / Float64(b1 * b2)));
	end
	return tmp
end
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
	tmp = 0.0;
	if ((a2 <= 5e-218) || ~((a2 <= 3.1e+174)))
		tmp = (a1 / b1) * (a2 / b2);
	else
		tmp = a1 * (a2 / (b1 * b2));
	end
	tmp_2 = tmp;
end
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
code[a1_, a2_, b1_, b2_] := If[Or[LessEqual[a2, 5e-218], N[Not[LessEqual[a2, 3.1e+174]], $MachinePrecision]], N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision], N[(a1 * N[(a2 / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 5 \cdot 10^{-218} \lor \neg \left(a2 \leq 3.1 \cdot 10^{+174}\right):\\
\;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\

\mathbf{else}:\\
\;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 5.00000000000000041e-218 or 3.1e174 < a2

    1. Initial program 79.9%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac87.6%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
    3. Simplified87.6%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]

    if 5.00000000000000041e-218 < a2 < 3.1e174

    1. Initial program 87.5%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. *-commutative87.5%

        \[\leadsto \frac{\color{blue}{a2 \cdot a1}}{b1 \cdot b2} \]
      2. associate-*l/93.1%

        \[\leadsto \color{blue}{\frac{a2}{b1 \cdot b2} \cdot a1} \]
      3. *-commutative93.1%

        \[\leadsto \frac{a2}{\color{blue}{b2 \cdot b1}} \cdot a1 \]
    3. Applied egg-rr93.1%

      \[\leadsto \color{blue}{\frac{a2}{b2 \cdot b1} \cdot a1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification89.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 5 \cdot 10^{-218} \lor \neg \left(a2 \leq 3.1 \cdot 10^{+174}\right):\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\ \end{array} \]

Alternative 4: 86.5% accurate, 0.8× speedup?

\[\begin{array}{l} [b1, b2] = \mathsf{sort}([b1, b2])\\ \\ \begin{array}{l} \mathbf{if}\;b1 \leq -4.1 \cdot 10^{-198}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{\frac{a2}{b2}}{b1}\\ \end{array} \end{array} \]
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 b1 b2)
 :precision binary64
 (if (<= b1 -4.1e-198) (* (/ a1 b1) (/ a2 b2)) (* a1 (/ (/ a2 b2) b1))))
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
	double tmp;
	if (b1 <= -4.1e-198) {
		tmp = (a1 / b1) * (a2 / b2);
	} else {
		tmp = a1 * ((a2 / b2) / b1);
	}
	return tmp;
}
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, b1, b2)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: b1
    real(8), intent (in) :: b2
    real(8) :: tmp
    if (b1 <= (-4.1d-198)) then
        tmp = (a1 / b1) * (a2 / b2)
    else
        tmp = a1 * ((a2 / b2) / b1)
    end if
    code = tmp
end function
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
	double tmp;
	if (b1 <= -4.1e-198) {
		tmp = (a1 / b1) * (a2 / b2);
	} else {
		tmp = a1 * ((a2 / b2) / b1);
	}
	return tmp;
}
[b1, b2] = sort([b1, b2])
def code(a1, a2, b1, b2):
	tmp = 0
	if b1 <= -4.1e-198:
		tmp = (a1 / b1) * (a2 / b2)
	else:
		tmp = a1 * ((a2 / b2) / b1)
	return tmp
b1, b2 = sort([b1, b2])
function code(a1, a2, b1, b2)
	tmp = 0.0
	if (b1 <= -4.1e-198)
		tmp = Float64(Float64(a1 / b1) * Float64(a2 / b2));
	else
		tmp = Float64(a1 * Float64(Float64(a2 / b2) / b1));
	end
	return tmp
end
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
	tmp = 0.0;
	if (b1 <= -4.1e-198)
		tmp = (a1 / b1) * (a2 / b2);
	else
		tmp = a1 * ((a2 / b2) / b1);
	end
	tmp_2 = tmp;
end
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
code[a1_, a2_, b1_, b2_] := If[LessEqual[b1, -4.1e-198], N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision], N[(a1 * N[(N[(a2 / b2), $MachinePrecision] / b1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
\mathbf{if}\;b1 \leq -4.1 \cdot 10^{-198}:\\
\;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\

\mathbf{else}:\\
\;\;\;\;a1 \cdot \frac{\frac{a2}{b2}}{b1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b1 < -4.10000000000000012e-198

    1. Initial program 84.7%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac89.0%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
    3. Simplified89.0%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]

    if -4.10000000000000012e-198 < b1

    1. Initial program 80.7%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Step-by-step derivation
      1. times-frac82.8%

        \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
      2. associate-*l/86.0%

        \[\leadsto \color{blue}{\frac{a1 \cdot \frac{a2}{b2}}{b1}} \]
      3. associate-*r/87.9%

        \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
    3. Simplified87.9%

      \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b1 \leq -4.1 \cdot 10^{-198}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{\frac{a2}{b2}}{b1}\\ \end{array} \]

Alternative 5: 86.6% accurate, 1.0× speedup?

\[\begin{array}{l} [b1, b2] = \mathsf{sort}([b1, b2])\\ \\ a1 \cdot \frac{\frac{a2}{b2}}{b1} \end{array} \]
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 b1 b2) :precision binary64 (* a1 (/ (/ a2 b2) b1)))
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
	return a1 * ((a2 / b2) / b1);
}
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, b1, b2)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: b1
    real(8), intent (in) :: b2
    code = a1 * ((a2 / b2) / b1)
end function
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
	return a1 * ((a2 / b2) / b1);
}
[b1, b2] = sort([b1, b2])
def code(a1, a2, b1, b2):
	return a1 * ((a2 / b2) / b1)
b1, b2 = sort([b1, b2])
function code(a1, a2, b1, b2)
	return Float64(a1 * Float64(Float64(a2 / b2) / b1))
end
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp = code(a1, a2, b1, b2)
	tmp = a1 * ((a2 / b2) / b1);
end
NOTE: b1 and b2 should be sorted in increasing order before calling this function.
code[a1_, a2_, b1_, b2_] := N[(a1 * N[(N[(a2 / b2), $MachinePrecision] / b1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
a1 \cdot \frac{\frac{a2}{b2}}{b1}
\end{array}
Derivation
  1. Initial program 82.4%

    \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
  2. Step-by-step derivation
    1. times-frac85.4%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
    2. associate-*l/85.1%

      \[\leadsto \color{blue}{\frac{a1 \cdot \frac{a2}{b2}}{b1}} \]
    3. associate-*r/86.2%

      \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
  3. Simplified86.2%

    \[\leadsto \color{blue}{a1 \cdot \frac{\frac{a2}{b2}}{b1}} \]
  4. Final simplification86.2%

    \[\leadsto a1 \cdot \frac{\frac{a2}{b2}}{b1} \]

Developer target: 86.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{a1}{b1} \cdot \frac{a2}{b2} \end{array} \]
(FPCore (a1 a2 b1 b2) :precision binary64 (* (/ a1 b1) (/ a2 b2)))
double code(double a1, double a2, double b1, double b2) {
	return (a1 / b1) * (a2 / b2);
}
real(8) function code(a1, a2, b1, b2)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: b1
    real(8), intent (in) :: b2
    code = (a1 / b1) * (a2 / b2)
end function
public static double code(double a1, double a2, double b1, double b2) {
	return (a1 / b1) * (a2 / b2);
}
def code(a1, a2, b1, b2):
	return (a1 / b1) * (a2 / b2)
function code(a1, a2, b1, b2)
	return Float64(Float64(a1 / b1) * Float64(a2 / b2))
end
function tmp = code(a1, a2, b1, b2)
	tmp = (a1 / b1) * (a2 / b2);
end
code[a1_, a2_, b1_, b2_] := N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{a1}{b1} \cdot \frac{a2}{b2}
\end{array}

Reproduce

?
herbie shell --seed 2023301 
(FPCore (a1 a2 b1 b2)
  :name "Quotient of products"
  :precision binary64

  :herbie-target
  (* (/ a1 b1) (/ a2 b2))

  (/ (* a1 a2) (* b1 b2)))