
(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:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
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 (/ (/ a2 b2) (/ b1 a1))))
(if (<= t_0 (- INFINITY))
t_1
(if (<= t_0 -1e-252)
t_0
(if (<= t_0 0.0)
(* (/ a2 b1) (/ a1 b2))
(if (<= t_0 5e+270) t_0 t_1))))))assert(a1 < a2);
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
double t_0 = (a1 * a2) / (b1 * b2);
double t_1 = (a2 / b2) / (b1 / a1);
double tmp;
if (t_0 <= -((double) INFINITY)) {
tmp = t_1;
} else if (t_0 <= -1e-252) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = (a2 / b1) * (a1 / b2);
} else if (t_0 <= 5e+270) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
assert a1 < a2;
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 = (a2 / b2) / (b1 / a1);
double tmp;
if (t_0 <= -Double.POSITIVE_INFINITY) {
tmp = t_1;
} else if (t_0 <= -1e-252) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = (a2 / b1) * (a1 / b2);
} else if (t_0 <= 5e+270) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): t_0 = (a1 * a2) / (b1 * b2) t_1 = (a2 / b2) / (b1 / a1) tmp = 0 if t_0 <= -math.inf: tmp = t_1 elif t_0 <= -1e-252: tmp = t_0 elif t_0 <= 0.0: tmp = (a2 / b1) * (a1 / b2) elif t_0 <= 5e+270: tmp = t_0 else: tmp = t_1 return tmp
a1, a2 = sort([a1, a2]) b1, b2 = sort([b1, b2]) function code(a1, a2, b1, b2) t_0 = Float64(Float64(a1 * a2) / Float64(b1 * b2)) t_1 = Float64(Float64(a2 / b2) / Float64(b1 / a1)) tmp = 0.0 if (t_0 <= Float64(-Inf)) tmp = t_1; elseif (t_0 <= -1e-252) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(Float64(a2 / b1) * Float64(a1 / b2)); elseif (t_0 <= 5e+270) tmp = t_0; else tmp = t_1; end return tmp end
a1, a2 = num2cell(sort([a1, a2])){:}
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
t_0 = (a1 * a2) / (b1 * b2);
t_1 = (a2 / b2) / (b1 / a1);
tmp = 0.0;
if (t_0 <= -Inf)
tmp = t_1;
elseif (t_0 <= -1e-252)
tmp = t_0;
elseif (t_0 <= 0.0)
tmp = (a2 / b1) * (a1 / b2);
elseif (t_0 <= 5e+270)
tmp = t_0;
else
tmp = t_1;
end
tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
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[(a2 / b2), $MachinePrecision] / N[(b1 / a1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, -1e-252], t$95$0, If[LessEqual[t$95$0, 0.0], N[(N[(a2 / b1), $MachinePrecision] * N[(a1 / b2), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 5e+270], t$95$0, t$95$1]]]]]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\
t_1 := \frac{\frac{a2}{b2}}{\frac{b1}{a1}}\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;t_1\\
\mathbf{elif}\;t_0 \leq -1 \cdot 10^{-252}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\
\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+270}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\end{array}
if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -inf.0 or 4.99999999999999976e270 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) Initial program 73.5%
times-frac96.2%
Simplified96.2%
*-commutative96.2%
clear-num96.2%
un-div-inv96.3%
Applied egg-rr96.3%
if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -9.99999999999999943e-253 or 0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 4.99999999999999976e270Initial program 97.8%
if -9.99999999999999943e-253 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 0.0Initial program 78.9%
associate-/l*83.4%
*-commutative83.4%
associate-/l*93.3%
Simplified93.3%
associate-/r/98.0%
*-commutative98.0%
Applied egg-rr98.0%
Final simplification97.4%
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
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 (* (/ a2 b2) (/ a1 b1))))
(if (<= t_0 (- INFINITY))
t_1
(if (<= t_0 -1e-252)
t_0
(if (<= t_0 0.0)
(* (/ a2 b1) (/ a1 b2))
(if (<= t_0 5e+277) t_0 t_1))))))assert(a1 < a2);
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
double t_0 = (a1 * a2) / (b1 * b2);
double t_1 = (a2 / b2) * (a1 / b1);
double tmp;
if (t_0 <= -((double) INFINITY)) {
tmp = t_1;
} else if (t_0 <= -1e-252) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = (a2 / b1) * (a1 / b2);
} else if (t_0 <= 5e+277) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
assert a1 < a2;
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 = (a2 / b2) * (a1 / b1);
double tmp;
if (t_0 <= -Double.POSITIVE_INFINITY) {
tmp = t_1;
} else if (t_0 <= -1e-252) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = (a2 / b1) * (a1 / b2);
} else if (t_0 <= 5e+277) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): t_0 = (a1 * a2) / (b1 * b2) t_1 = (a2 / b2) * (a1 / b1) tmp = 0 if t_0 <= -math.inf: tmp = t_1 elif t_0 <= -1e-252: tmp = t_0 elif t_0 <= 0.0: tmp = (a2 / b1) * (a1 / b2) elif t_0 <= 5e+277: tmp = t_0 else: tmp = t_1 return tmp
a1, a2 = sort([a1, a2]) b1, b2 = sort([b1, b2]) function code(a1, a2, b1, b2) t_0 = Float64(Float64(a1 * a2) / Float64(b1 * b2)) t_1 = Float64(Float64(a2 / b2) * Float64(a1 / b1)) tmp = 0.0 if (t_0 <= Float64(-Inf)) tmp = t_1; elseif (t_0 <= -1e-252) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(Float64(a2 / b1) * Float64(a1 / b2)); elseif (t_0 <= 5e+277) tmp = t_0; else tmp = t_1; end return tmp end
a1, a2 = num2cell(sort([a1, a2])){:}
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
t_0 = (a1 * a2) / (b1 * b2);
t_1 = (a2 / b2) * (a1 / b1);
tmp = 0.0;
if (t_0 <= -Inf)
tmp = t_1;
elseif (t_0 <= -1e-252)
tmp = t_0;
elseif (t_0 <= 0.0)
tmp = (a2 / b1) * (a1 / b2);
elseif (t_0 <= 5e+277)
tmp = t_0;
else
tmp = t_1;
end
tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
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[(a2 / b2), $MachinePrecision] * N[(a1 / b1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, -1e-252], t$95$0, If[LessEqual[t$95$0, 0.0], N[(N[(a2 / b1), $MachinePrecision] * N[(a1 / b2), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 5e+277], t$95$0, t$95$1]]]]]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
t_0 := \frac{a1 \cdot a2}{b1 \cdot b2}\\
t_1 := \frac{a2}{b2} \cdot \frac{a1}{b1}\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;t_1\\
\mathbf{elif}\;t_0 \leq -1 \cdot 10^{-252}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\
\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+277}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\end{array}
if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -inf.0 or 4.99999999999999982e277 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) Initial program 73.2%
times-frac97.4%
Simplified97.4%
if -inf.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -9.99999999999999943e-253 or 0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 4.99999999999999982e277Initial program 97.8%
if -9.99999999999999943e-253 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 0.0Initial program 78.9%
associate-/l*83.4%
*-commutative83.4%
associate-/l*93.3%
Simplified93.3%
associate-/r/98.0%
*-commutative98.0%
Applied egg-rr98.0%
Final simplification97.7%
NOTE: a1 and a2 should be sorted in increasing order before calling this function. NOTE: b1 and b2 should be sorted in increasing order before calling this function. (FPCore (a1 a2 b1 b2) :precision binary64 (if (or (<= b1 -1.1e+168) (not (<= b1 3.6e-188))) (* (/ a2 b2) (/ a1 b1)) (* (/ a2 b1) (/ a1 b2))))
assert(a1 < a2);
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
double tmp;
if ((b1 <= -1.1e+168) || !(b1 <= 3.6e-188)) {
tmp = (a2 / b2) * (a1 / b1);
} else {
tmp = (a2 / b1) * (a1 / b2);
}
return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
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 <= (-1.1d+168)) .or. (.not. (b1 <= 3.6d-188))) then
tmp = (a2 / b2) * (a1 / b1)
else
tmp = (a2 / b1) * (a1 / b2)
end if
code = tmp
end function
assert a1 < a2;
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
double tmp;
if ((b1 <= -1.1e+168) || !(b1 <= 3.6e-188)) {
tmp = (a2 / b2) * (a1 / b1);
} else {
tmp = (a2 / b1) * (a1 / b2);
}
return tmp;
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): tmp = 0 if (b1 <= -1.1e+168) or not (b1 <= 3.6e-188): tmp = (a2 / b2) * (a1 / b1) else: tmp = (a2 / b1) * (a1 / b2) return tmp
a1, a2 = sort([a1, a2]) b1, b2 = sort([b1, b2]) function code(a1, a2, b1, b2) tmp = 0.0 if ((b1 <= -1.1e+168) || !(b1 <= 3.6e-188)) tmp = Float64(Float64(a2 / b2) * Float64(a1 / b1)); else tmp = Float64(Float64(a2 / b1) * Float64(a1 / b2)); end return tmp end
a1, a2 = num2cell(sort([a1, a2])){:}
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp_2 = code(a1, a2, b1, b2)
tmp = 0.0;
if ((b1 <= -1.1e+168) || ~((b1 <= 3.6e-188)))
tmp = (a2 / b2) * (a1 / b1);
else
tmp = (a2 / b1) * (a1 / b2);
end
tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function. NOTE: b1 and b2 should be sorted in increasing order before calling this function. code[a1_, a2_, b1_, b2_] := If[Or[LessEqual[b1, -1.1e+168], N[Not[LessEqual[b1, 3.6e-188]], $MachinePrecision]], N[(N[(a2 / b2), $MachinePrecision] * N[(a1 / b1), $MachinePrecision]), $MachinePrecision], N[(N[(a2 / b1), $MachinePrecision] * N[(a1 / b2), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
\mathbf{if}\;b1 \leq -1.1 \cdot 10^{+168} \lor \neg \left(b1 \leq 3.6 \cdot 10^{-188}\right):\\
\;\;\;\;\frac{a2}{b2} \cdot \frac{a1}{b1}\\
\mathbf{else}:\\
\;\;\;\;\frac{a2}{b1} \cdot \frac{a1}{b2}\\
\end{array}
\end{array}
if b1 < -1.1000000000000001e168 or 3.5999999999999997e-188 < b1 Initial program 87.8%
times-frac89.9%
Simplified89.9%
if -1.1000000000000001e168 < b1 < 3.5999999999999997e-188Initial program 83.1%
associate-/l*83.8%
*-commutative83.8%
associate-/l*86.0%
Simplified86.0%
associate-/r/88.1%
*-commutative88.1%
Applied egg-rr88.1%
Final simplification89.0%
NOTE: a1 and a2 should be sorted in increasing order before calling this function. NOTE: b1 and b2 should be sorted in increasing order before calling this function. (FPCore (a1 a2 b1 b2) :precision binary64 (* (/ a2 b2) (/ a1 b1)))
assert(a1 < a2);
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
return (a2 / b2) * (a1 / b1);
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
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 = (a2 / b2) * (a1 / b1)
end function
assert a1 < a2;
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
return (a2 / b2) * (a1 / b1);
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): return (a2 / b2) * (a1 / b1)
a1, a2 = sort([a1, a2]) b1, b2 = sort([b1, b2]) function code(a1, a2, b1, b2) return Float64(Float64(a2 / b2) * Float64(a1 / b1)) end
a1, a2 = num2cell(sort([a1, a2])){:}
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp = code(a1, a2, b1, b2)
tmp = (a2 / b2) * (a1 / b1);
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function. NOTE: b1 and b2 should be sorted in increasing order before calling this function. code[a1_, a2_, b1_, b2_] := N[(N[(a2 / b2), $MachinePrecision] * N[(a1 / b1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\frac{a2}{b2} \cdot \frac{a1}{b1}
\end{array}
Initial program 85.6%
times-frac89.5%
Simplified89.5%
Final simplification89.5%
(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}
herbie shell --seed 2023174
(FPCore (a1 a2 b1 b2)
:name "Quotient of products"
:precision binary64
:herbie-target
(* (/ a1 b1) (/ a2 b2))
(/ (* a1 a2) (* b1 b2)))