Quotient of products

?

Percentage Accurate: 85.5% → 95.4%
Time: 3.8s
Precision: binary64
Cost: 2512

?

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

\mathbf{elif}\;t_0 \leq -5 \cdot 10^{-159}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\

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

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


\end{array}
\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 8 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

Original85.5%
Target86.3%
Herbie95.4%
\[\frac{a1}{b1} \cdot \frac{a2}{b2} \]

Derivation?

  1. Split input into 4 regimes
  2. if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -inf.0

    1. Initial program 77.5%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified95.0%

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

      [Start]77.5%

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

      times-frac [=>]95.0%

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

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

      [Start]95.0%

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

      frac-times [=>]77.5%

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

      *-commutative [=>]77.5%

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

      frac-times [<=]97.3%

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

      associate-*r/ [=>]84.9%

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

    if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -5.00000000000000032e-159 or -0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 9.9999999999999994e303

    1. Initial program 98.8%

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

    if -5.00000000000000032e-159 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -0.0

    1. Initial program 87.0%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified98.4%

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

      [Start]87.0%

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

      times-frac [=>]98.4%

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

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

      [Start]98.4%

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

      associate-*r/ [=>]94.7%

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

    if 9.9999999999999994e303 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))

    1. Initial program 68.4%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified99.7%

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

      [Start]68.4%

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

      times-frac [=>]99.7%

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

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

      [Start]99.7%

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

      *-commutative [=>]99.7%

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

      clear-num [=>]99.7%

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

      un-div-inv [=>]99.8%

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

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

Alternatives

Alternative 1
Accuracy95.4%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b2}}{b1}\\ \mathbf{elif}\;t_0 \leq -5 \cdot 10^{-159}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \mathbf{elif}\;t_0 \leq 10^{+304}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{a2}{b2}}{\frac{b1}{a1}}\\ \end{array} \]
Alternative 2
Accuracy95.0%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -1 \cdot 10^{+298}:\\ \;\;\;\;\frac{a1}{b2 \cdot \frac{b1}{a2}}\\ \mathbf{elif}\;t_0 \leq -2 \cdot 10^{-121}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a1}{\frac{b2}{\frac{a2}{b1}}}\\ \mathbf{elif}\;t_0 \leq 10^{+304}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \end{array} \]
Alternative 3
Accuracy95.2%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -1 \cdot 10^{+298}:\\ \;\;\;\;\frac{a1}{b2 \cdot \frac{b1}{a2}}\\ \mathbf{elif}\;t_0 \leq -5 \cdot 10^{-159}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \mathbf{elif}\;t_0 \leq 10^{+304}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \end{array} \]
Alternative 4
Accuracy95.4%
Cost2512
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b2}}{b1}\\ \mathbf{elif}\;t_0 \leq -5 \cdot 10^{-159}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{a2 \cdot \frac{a1}{b1}}{b2}\\ \mathbf{elif}\;t_0 \leq 10^{+304}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \end{array} \]
Alternative 5
Accuracy86.4%
Cost845
\[\begin{array}{l} \mathbf{if}\;b1 \leq -3.2 \cdot 10^{+189}:\\ \;\;\;\;\frac{a1}{\frac{b2}{\frac{a2}{b1}}}\\ \mathbf{elif}\;b1 \leq -3.5 \cdot 10^{+126} \lor \neg \left(b1 \leq 2.9 \cdot 10^{-89}\right):\\ \;\;\;\;\frac{a2}{b2 \cdot \frac{b1}{a1}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{\frac{b1 \cdot b2}{a2}}\\ \end{array} \]
Alternative 6
Accuracy86.3%
Cost448
\[\frac{a1}{b1} \cdot \frac{a2}{b2} \]
Alternative 7
Accuracy86.2%
Cost448
\[\frac{a1}{b2 \cdot \frac{b1}{a2}} \]
Alternative 8
Accuracy86.2%
Cost448
\[\frac{a1}{\frac{b1 \cdot b2}{a2}} \]

Reproduce?

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

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

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