Average Error: 11.5 → 2.1
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{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq -5 \cdot 10^{-288}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+286}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{a2}{b2}}{\frac{b1}{a1}}\\ \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 b1) (/ a2 b2))))
   (if (<= t_0 (- INFINITY))
     t_1
     (if (<= t_0 -5e-288)
       t_0
       (if (<= t_0 0.0)
         t_1
         (if (<= t_0 2e+286) t_0 (/ (/ a2 b2) (/ b1 a1))))))))
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 / b1) * (a2 / b2);
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = t_1;
	} else if (t_0 <= -5e-288) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 2e+286) {
		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);
}
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 <= -5e-288) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 2e+286) {
		tmp = t_0;
	} else {
		tmp = (a2 / b2) / (b1 / a1);
	}
	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 / b1) * (a2 / b2)
	tmp = 0
	if t_0 <= -math.inf:
		tmp = t_1
	elif t_0 <= -5e-288:
		tmp = t_0
	elif t_0 <= 0.0:
		tmp = t_1
	elif t_0 <= 2e+286:
		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
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 <= -5e-288)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 2e+286)
		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
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 <= -5e-288)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 2e+286)
		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]
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, -5e-288], t$95$0, If[LessEqual[t$95$0, 0.0], t$95$1, If[LessEqual[t$95$0, 2e+286], t$95$0, N[(N[(a2 / b2), $MachinePrecision] / N[(b1 / a1), $MachinePrecision]), $MachinePrecision]]]]]]]
\frac{a1 \cdot a2}{b1 \cdot 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 -5 \cdot 10^{-288}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;t_0 \leq 2 \cdot 10^{+286}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original11.5
Target10.5
Herbie2.1
\[\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 -5.00000000000000011e-288 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 0.0

    1. Initial program 18.6

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

      \[\leadsto \color{blue}{\frac{a1}{b1} \cdot \frac{a2}{b2}} \]
      Proof
      (*.f64 (/.f64 a1 b1) (/.f64 a2 b2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= times-frac_binary64 (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))): 76 points increase in error, 54 points decrease in error

    if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -5.00000000000000011e-288 or 0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 2.00000000000000007e286

    1. Initial program 0.8

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

    if 2.00000000000000007e286 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))

    1. Initial program 57.9

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

      \[\leadsto \color{blue}{a1 \cdot \frac{a2}{b1 \cdot b2}} \]
      Proof
      (*.f64 a1 (/.f64 a2 (*.f64 b1 b2))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> associate-*r/_binary64 (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))): 54 points increase in error, 30 points decrease in error
    3. Taylor expanded in a1 around 0 57.9

      \[\leadsto \color{blue}{\frac{a1 \cdot a2}{b2 \cdot b1}} \]
    4. Simplified7.1

      \[\leadsto \color{blue}{\frac{\frac{a2}{b2}}{\frac{b1}{a1}}} \]
      Proof
      (/.f64 (/.f64 a2 b2) (/.f64 b1 a1)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 (/.f64 a2 b2) a1) b1)): 38 points increase in error, 43 points decrease in error
      (/.f64 (Rewrite=> *-commutative_binary64 (*.f64 a1 (/.f64 a2 b2))) b1): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 a1 b1) (/.f64 a2 b2))): 42 points increase in error, 42 points decrease in error
      (Rewrite<= times-frac_binary64 (/.f64 (*.f64 a1 a2) (*.f64 b1 b2))): 76 points increase in error, 54 points decrease in error
      (/.f64 (*.f64 a1 a2) (Rewrite=> *-commutative_binary64 (*.f64 b2 b1))): 0 points increase in error, 0 points decrease in error
  3. Recombined 3 regimes into one program.
  4. Final simplification2.1

    \[\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 -5 \cdot 10^{-288}:\\ \;\;\;\;\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 2 \cdot 10^{+286}:\\ \;\;\;\;\frac{a1 \cdot a2}{b1 \cdot b2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{a2}{b2}}{\frac{b1}{a1}}\\ \end{array} \]

Alternatives

Alternative 1
Error2.1
Cost2512
\[\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 -5 \cdot 10^{-288}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+286}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error6.9
Cost1488
\[\begin{array}{l} t_0 := a1 \cdot \frac{a2}{b1 \cdot b2}\\ t_1 := a1 \cdot \frac{\frac{a2}{b1}}{b2}\\ \mathbf{if}\;b1 \cdot b2 \leq -2 \cdot 10^{+268}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-288}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b1 \cdot b2 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b1 \cdot b2 \leq 5 \cdot 10^{+124}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error7.2
Cost1488
\[\begin{array}{l} t_0 := a1 \cdot \frac{\frac{a2}{b1}}{b2}\\ \mathbf{if}\;b1 \cdot b2 \leq -2 \cdot 10^{+268}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-288}:\\ \;\;\;\;a1 \cdot \frac{a2}{b1 \cdot b2}\\ \mathbf{elif}\;b1 \cdot b2 \leq 10^{-160}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b1 \cdot b2 \leq 2 \cdot 10^{+86}:\\ \;\;\;\;a2 \cdot \frac{a1}{b1 \cdot b2}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error5.2
Cost1488
\[\begin{array}{l} t_0 := a1 \cdot \frac{a2}{b1 \cdot b2}\\ t_1 := \frac{a1}{b1} \cdot \frac{a2}{b2}\\ \mathbf{if}\;b1 \cdot b2 \leq -5 \cdot 10^{+159}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b1 \cdot b2 \leq -5 \cdot 10^{-288}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b1 \cdot b2 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b1 \cdot b2 \leq 5 \cdot 10^{+146}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error11.2
Cost448
\[a1 \cdot \frac{a2}{b1 \cdot b2} \]

Error

Reproduce

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

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

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