Quotient of products

?

Percentage Accurate: 85.5% → 92.7%
Time: 7.0s
Precision: binary64
Cost: 1489

?

\[ \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}{b1} \cdot \frac{a2}{b2}\\ \mathbf{if}\;b1 \cdot b2 \leq -1 \cdot 10^{+198}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-113}:\\ \;\;\;\;\frac{a2}{\frac{b1 \cdot b2}{a1}}\\ \mathbf{elif}\;b1 \cdot b2 \leq 5 \cdot 10^{-266} \lor \neg \left(b1 \cdot b2 \leq 5 \cdot 10^{+133}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot 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 b1) (/ a2 b2))))
   (if (<= (* b1 b2) -1e+198)
     t_0
     (if (<= (* b1 b2) -5e-113)
       (/ a2 (/ (* b1 b2) a1))
       (if (or (<= (* b1 b2) 5e-266) (not (<= (* b1 b2) 5e+133)))
         t_0
         (* a1 (/ a2 (* 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 / b1) * (a2 / b2);
	double tmp;
	if ((b1 * b2) <= -1e+198) {
		tmp = t_0;
	} else if ((b1 * b2) <= -5e-113) {
		tmp = a2 / ((b1 * b2) / a1);
	} else if (((b1 * b2) <= 5e-266) || !((b1 * b2) <= 5e+133)) {
		tmp = t_0;
	} else {
		tmp = a1 * (a2 / (b1 * b2));
	}
	return tmp;
}
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
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) :: t_0
    real(8) :: tmp
    t_0 = (a1 / b1) * (a2 / b2)
    if ((b1 * b2) <= (-1d+198)) then
        tmp = t_0
    else if ((b1 * b2) <= (-5d-113)) then
        tmp = a2 / ((b1 * b2) / a1)
    else if (((b1 * b2) <= 5d-266) .or. (.not. ((b1 * b2) <= 5d+133))) then
        tmp = t_0
    else
        tmp = a1 * (a2 / (b1 * b2))
    end if
    code = tmp
end function
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 / b1) * (a2 / b2);
	double tmp;
	if ((b1 * b2) <= -1e+198) {
		tmp = t_0;
	} else if ((b1 * b2) <= -5e-113) {
		tmp = a2 / ((b1 * b2) / a1);
	} else if (((b1 * b2) <= 5e-266) || !((b1 * b2) <= 5e+133)) {
		tmp = t_0;
	} else {
		tmp = a1 * (a2 / (b1 * b2));
	}
	return tmp;
}
def code(a1, a2, b1, b2):
	return (a1 * a2) / (b1 * b2)
def code(a1, a2, b1, b2):
	t_0 = (a1 / b1) * (a2 / b2)
	tmp = 0
	if (b1 * b2) <= -1e+198:
		tmp = t_0
	elif (b1 * b2) <= -5e-113:
		tmp = a2 / ((b1 * b2) / a1)
	elif ((b1 * b2) <= 5e-266) or not ((b1 * b2) <= 5e+133):
		tmp = t_0
	else:
		tmp = a1 * (a2 / (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 / b1) * Float64(a2 / b2))
	tmp = 0.0
	if (Float64(b1 * b2) <= -1e+198)
		tmp = t_0;
	elseif (Float64(b1 * b2) <= -5e-113)
		tmp = Float64(a2 / Float64(Float64(b1 * b2) / a1));
	elseif ((Float64(b1 * b2) <= 5e-266) || !(Float64(b1 * b2) <= 5e+133))
		tmp = t_0;
	else
		tmp = Float64(a1 * Float64(a2 / Float64(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 / b1) * (a2 / b2);
	tmp = 0.0;
	if ((b1 * b2) <= -1e+198)
		tmp = t_0;
	elseif ((b1 * b2) <= -5e-113)
		tmp = a2 / ((b1 * b2) / a1);
	elseif (((b1 * b2) <= 5e-266) || ~(((b1 * b2) <= 5e+133)))
		tmp = t_0;
	else
		tmp = a1 * (a2 / (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 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(b1 * b2), $MachinePrecision], -1e+198], t$95$0, If[LessEqual[N[(b1 * b2), $MachinePrecision], -5e-113], N[(a2 / N[(N[(b1 * b2), $MachinePrecision] / a1), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[N[(b1 * b2), $MachinePrecision], 5e-266], N[Not[LessEqual[N[(b1 * b2), $MachinePrecision], 5e+133]], $MachinePrecision]], t$95$0, N[(a1 * N[(a2 / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\frac{a1 \cdot a2}{b1 \cdot b2}
\begin{array}{l}
t_0 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\
\mathbf{if}\;b1 \cdot b2 \leq -1 \cdot 10^{+198}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-113}:\\
\;\;\;\;\frac{a2}{\frac{b1 \cdot b2}{a1}}\\

\mathbf{elif}\;b1 \cdot b2 \leq 5 \cdot 10^{-266} \lor \neg \left(b1 \cdot b2 \leq 5 \cdot 10^{+133}\right):\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;a1 \cdot \frac{a2}{b1 \cdot 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 5 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%
Target85.9%
Herbie92.7%
\[\frac{a1}{b1} \cdot \frac{a2}{b2} \]

Derivation?

  1. Split input into 3 regimes
  2. if (*.f64 b1 b2) < -1.00000000000000002e198 or -4.9999999999999997e-113 < (*.f64 b1 b2) < 4.99999999999999992e-266 or 4.99999999999999961e133 < (*.f64 b1 b2)

    1. Initial program 79.4%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified94.3%

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

      [Start]79.4%

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

      times-frac [=>]94.3%

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

    if -1.00000000000000002e198 < (*.f64 b1 b2) < -4.9999999999999997e-113

    1. Initial program 95.2%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified83.1%

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

      [Start]95.2%

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

      times-frac [=>]83.1%

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

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

      [Start]83.1%

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

      frac-times [=>]95.2%

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

      *-commutative [=>]95.2%

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

      associate-/l* [=>]93.9%

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

    if 4.99999999999999992e-266 < (*.f64 b1 b2) < 4.99999999999999961e133

    1. Initial program 91.6%

      \[\frac{a1 \cdot a2}{b1 \cdot b2} \]
    2. Simplified83.3%

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

      [Start]91.6%

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

      associate-/l* [=>]91.5%

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

      *-commutative [=>]91.5%

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

      associate-/l* [=>]83.3%

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

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

      [Start]83.3%

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

      clear-num [=>]83.3%

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

      associate-/r/ [=>]81.9%

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

      clear-num [<=]81.8%

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

      associate-/l/ [=>]90.1%

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

      *-commutative [<=]90.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b1 \cdot b2 \leq -1 \cdot 10^{+198}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-113}:\\ \;\;\;\;\frac{a2}{\frac{b1 \cdot b2}{a1}}\\ \mathbf{elif}\;b1 \cdot b2 \leq 5 \cdot 10^{-266} \lor \neg \left(b1 \cdot b2 \leq 5 \cdot 10^{+133}\right):\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy92.7%
Cost1489
\[\begin{array}{l} t_0 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{if}\;b1 \cdot b2 \leq -1 \cdot 10^{+198}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-113}:\\ \;\;\;\;\frac{a2}{\frac{b1 \cdot b2}{a1}}\\ \mathbf{elif}\;b1 \cdot b2 \leq 5 \cdot 10^{-266} \lor \neg \left(b1 \cdot b2 \leq 5 \cdot 10^{+133}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\ \end{array} \]
Alternative 2
Accuracy92.9%
Cost1490
\[\begin{array}{l} \mathbf{if}\;b1 \cdot b2 \leq -5 \cdot 10^{+156} \lor \neg \left(b1 \cdot b2 \leq -1 \cdot 10^{-189}\right) \land \left(b1 \cdot b2 \leq 5 \cdot 10^{-266} \lor \neg \left(b1 \cdot b2 \leq 5 \cdot 10^{+133}\right)\right):\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\ \end{array} \]
Alternative 3
Accuracy89.9%
Cost1480
\[\begin{array}{l} t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{-279}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 4 \cdot 10^{+305}:\\ \;\;\;\;\frac{a2}{\frac{b1 \cdot b2}{a1}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \end{array} \]
Alternative 4
Accuracy86.1%
Cost580
\[\begin{array}{l} \mathbf{if}\;a2 \leq 3.45 \cdot 10^{+165}:\\ \;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{else}:\\ \;\;\;\;\frac{a1}{\frac{b2}{\frac{a2}{b1}}}\\ \end{array} \]
Alternative 5
Accuracy85.9%
Cost448
\[\frac{a1}{b1} \cdot \frac{a2}{b2} \]

Reproduce?

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

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

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