Quotient of products

?

Percentage Accurate: 86.1% → 96.8%
Time: 4.3s
Precision: binary64
Cost: 2512

?

\[ \begin{array}{c}[a1, a2] = \mathsf{sort}([a1, a2])\\ [b1, b2] = \mathsf{sort}([b1, b2])\\ \end{array} \]
\[\frac{a1 \cdot a2}{b1 \cdot b2} \]
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ t_1 := \frac{\frac{a1}{b2}}{\frac{b1}{a2}}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq -1 \cdot 10^{-312}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{-291}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 10^{+308}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \end{array} \]
(FPCore (a1 a2 b1 b2) :precision binary64 (/ (* a1 a2) (* b1 b2)))
(FPCore (a1 a2 b1 b2)
 :precision binary64
 (let* ((t_0 (/ (* a1 a2) (* b1 b2))) (t_1 (/ (/ a1 b2) (/ b1 a2))))
   (if (<= t_0 (- INFINITY))
     t_1
     (if (<= t_0 -1e-312)
       t_0
       (if (<= t_0 2e-291)
         t_1
         (if (<= t_0 1e+308) t_0 (/ (* a2 (/ a1 b1)) b2)))))))
double code(double a1, double a2, double b1, double b2) {
	return (a1 * a2) / (b1 * b2);
}
double code(double a1, double a2, double b1, double b2) {
	double t_0 = (a1 * a2) / (b1 * b2);
	double t_1 = (a1 / b2) / (b1 / a2);
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = t_1;
	} else if (t_0 <= -1e-312) {
		tmp = t_0;
	} else if (t_0 <= 2e-291) {
		tmp = t_1;
	} else if (t_0 <= 1e+308) {
		tmp = t_0;
	} else {
		tmp = (a2 * (a1 / b1)) / b2;
	}
	return tmp;
}
public static double code(double a1, double a2, double b1, double b2) {
	return (a1 * a2) / (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 / b2) / (b1 / a2);
	double tmp;
	if (t_0 <= -Double.POSITIVE_INFINITY) {
		tmp = t_1;
	} else if (t_0 <= -1e-312) {
		tmp = t_0;
	} else if (t_0 <= 2e-291) {
		tmp = t_1;
	} else if (t_0 <= 1e+308) {
		tmp = t_0;
	} else {
		tmp = (a2 * (a1 / b1)) / b2;
	}
	return tmp;
}
def code(a1, a2, b1, b2):
	return (a1 * a2) / (b1 * b2)
def code(a1, a2, b1, b2):
	t_0 = (a1 * a2) / (b1 * b2)
	t_1 = (a1 / b2) / (b1 / a2)
	tmp = 0
	if t_0 <= -math.inf:
		tmp = t_1
	elif t_0 <= -1e-312:
		tmp = t_0
	elif t_0 <= 2e-291:
		tmp = t_1
	elif t_0 <= 1e+308:
		tmp = t_0
	else:
		tmp = (a2 * (a1 / b1)) / b2
	return tmp
function code(a1, a2, b1, b2)
	return Float64(Float64(a1 * a2) / Float64(b1 * b2))
end
function code(a1, a2, b1, b2)
	t_0 = Float64(Float64(a1 * a2) / Float64(b1 * b2))
	t_1 = Float64(Float64(a1 / b2) / Float64(b1 / a2))
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = t_1;
	elseif (t_0 <= -1e-312)
		tmp = t_0;
	elseif (t_0 <= 2e-291)
		tmp = t_1;
	elseif (t_0 <= 1e+308)
		tmp = t_0;
	else
		tmp = Float64(Float64(a2 * Float64(a1 / b1)) / b2);
	end
	return tmp
end
function tmp = code(a1, a2, b1, b2)
	tmp = (a1 * a2) / (b1 * b2);
end
function tmp_2 = code(a1, a2, b1, b2)
	t_0 = (a1 * a2) / (b1 * b2);
	t_1 = (a1 / b2) / (b1 / a2);
	tmp = 0.0;
	if (t_0 <= -Inf)
		tmp = t_1;
	elseif (t_0 <= -1e-312)
		tmp = t_0;
	elseif (t_0 <= 2e-291)
		tmp = t_1;
	elseif (t_0 <= 1e+308)
		tmp = t_0;
	else
		tmp = (a2 * (a1 / b1)) / b2;
	end
	tmp_2 = tmp;
end
code[a1_, a2_, b1_, b2_] := N[(N[(a1 * a2), $MachinePrecision] / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]
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 / b2), $MachinePrecision] / N[(b1 / a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, -1e-312], t$95$0, If[LessEqual[t$95$0, 2e-291], t$95$1, If[LessEqual[t$95$0, 1e+308], t$95$0, N[(N[(a2 * N[(a1 / b1), $MachinePrecision]), $MachinePrecision] / b2), $MachinePrecision]]]]]]]
\frac{a1 \cdot a2}{b1 \cdot b2}
\begin{array}{l}
t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\
t_1 := \frac{\frac{a1}{b2}}{\frac{b1}{a2}}\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq -1 \cdot 10^{-312}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;t_0 \leq 10^{+308}:\\
\;\;\;\;t_0\\

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


\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.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original86.1%
Target86.5%
Herbie96.8%
\[\frac{a1}{b1} \cdot \frac{a2}{b2} \]

Derivation?

  1. Split input into 3 regimes
  2. if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -inf.0 or -9.9999999999847e-313 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 1.99999999999999992e-291

    1. Initial program 80.2%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified96.6%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
      Step-by-step derivation

      [Start]80.2%

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

      times-frac [=>]96.6%

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

      \[\leadsto \color{blue}{\frac{\frac{a1}{b2}}{\frac{b1}{a2}}} \]
      Step-by-step derivation

      [Start]96.6%

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

      frac-times [=>]80.2%

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

      *-commutative [=>]80.2%

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

      frac-times [<=]94.3%

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

      clear-num [=>]93.2%

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

      un-div-inv [=>]93.2%

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

    if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -9.9999999999847e-313 or 1.99999999999999992e-291 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 1e308

    1. Initial program 99.3%

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

    if 1e308 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))

    1. Initial program 68.5%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified96.8%

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
      Step-by-step derivation

      [Start]68.5%

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

      times-frac [=>]96.8%

      \[ \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
    3. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\frac{\frac{a1}{b1} \cdot a2}{b2}} \]
      Step-by-step derivation

      [Start]96.8%

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

      associate-*r/ [=>]99.8%

      \[ \color{blue}{\frac{\frac{a1}{b1} \cdot a2}{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{\frac{a1}{b2}}{\frac{b1}{a2}}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq -1 \cdot 10^{-312}:\\ \;\;\;\;\frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq 2 \cdot 10^{-291}:\\ \;\;\;\;\frac{\frac{a1}{b2}}{\frac{b1}{a2}}\\ \mathbf{elif}\;\frac{a1 \cdot a2}{b1 \cdot b2} \leq 10^{+308}:\\ \;\;\;\;\frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy96.8%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ t_1 := \frac{\frac{a1}{b2}}{\frac{b1}{a2}}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq -1 \cdot 10^{-312}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{-291}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 10^{+308}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \end{array} \]
Alternative 2
Accuracy96.5%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;\frac{a1}{b1 \cdot \frac{b2}{a2}}\\ \mathbf{elif}\;t_0 \leq -2 \cdot 10^{-276}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a2}{b2 \cdot \frac{b1}{a1}}\\ \mathbf{elif}\;t_0 \leq 10^{+308}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \end{array} \]
Alternative 3
Accuracy95.7%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;\frac{a1}{b1 \cdot \frac{b2}{a2}}\\ \mathbf{elif}\;t_0 \leq -2 \cdot 10^{-276}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a2}{b2 \cdot \frac{b1}{a1}}\\ \mathbf{elif}\;t_0 \leq 10^{+308}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a1 \cdot \frac{a2}{b1}}{b2}\\ \end{array} \]
Alternative 4
Accuracy95.6%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;\frac{a1}{b1 \cdot \frac{b2}{a2}}\\ \mathbf{elif}\;t_0 \leq -2 \cdot 10^{-276}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a2}{b2 \cdot \frac{b1}{a1}}\\ \mathbf{elif}\;t_0 \leq 10^{+308}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \end{array} \]
Alternative 5
Accuracy87.1%
Cost580
\[\begin{array}{l} \mathbf{if}\;b1 \leq -3 \cdot 10^{-27}:\\ \;\;\;\;\frac{a1}{b1 \cdot \frac{b2}{a2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \end{array} \]
Alternative 6
Accuracy86.5%
Cost448
\[\frac{a1}{b1} \cdot \frac{a2}{b2} \]
Alternative 7
Accuracy86.5%
Cost448
\[\frac{a2}{b2 \cdot \frac{b1}{a1}} \]

Reproduce?

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

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

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