
(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 3 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))))
(if (<= t_0 -2e-319)
(* a2 (/ a1 (* b1 b2)))
(if (or (<= t_0 0.0) (not (<= t_0 1e+277)))
(* (/ a1 b1) (/ a2 b2))
t_0))))assert(a1 < a2);
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 <= -2e-319) {
tmp = a2 * (a1 / (b1 * b2));
} else if ((t_0 <= 0.0) || !(t_0 <= 1e+277)) {
tmp = (a1 / b1) * (a2 / b2);
} else {
tmp = t_0;
}
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) :: t_0
real(8) :: tmp
t_0 = (a1 * a2) / (b1 * b2)
if (t_0 <= (-2d-319)) then
tmp = a2 * (a1 / (b1 * b2))
else if ((t_0 <= 0.0d0) .or. (.not. (t_0 <= 1d+277))) then
tmp = (a1 / b1) * (a2 / b2)
else
tmp = t_0
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 t_0 = (a1 * a2) / (b1 * b2);
double tmp;
if (t_0 <= -2e-319) {
tmp = a2 * (a1 / (b1 * b2));
} else if ((t_0 <= 0.0) || !(t_0 <= 1e+277)) {
tmp = (a1 / b1) * (a2 / b2);
} else {
tmp = t_0;
}
return tmp;
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): t_0 = (a1 * a2) / (b1 * b2) tmp = 0 if t_0 <= -2e-319: tmp = a2 * (a1 / (b1 * b2)) elif (t_0 <= 0.0) or not (t_0 <= 1e+277): tmp = (a1 / b1) * (a2 / b2) else: tmp = t_0 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)) tmp = 0.0 if (t_0 <= -2e-319) tmp = Float64(a2 * Float64(a1 / Float64(b1 * b2))); elseif ((t_0 <= 0.0) || !(t_0 <= 1e+277)) tmp = Float64(Float64(a1 / b1) * Float64(a2 / b2)); else tmp = t_0; 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);
tmp = 0.0;
if (t_0 <= -2e-319)
tmp = a2 * (a1 / (b1 * b2));
elseif ((t_0 <= 0.0) || ~((t_0 <= 1e+277)))
tmp = (a1 / b1) * (a2 / b2);
else
tmp = t_0;
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]}, If[LessEqual[t$95$0, -2e-319], N[(a2 * N[(a1 / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[t$95$0, 0.0], N[Not[LessEqual[t$95$0, 1e+277]], $MachinePrecision]], N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\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}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-319}:\\
\;\;\;\;a2 \cdot \frac{a1}{b1 \cdot b2}\\
\mathbf{elif}\;t_0 \leq 0 \lor \neg \left(t_0 \leq 10^{+277}\right):\\
\;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -1.99998e-319Initial program 94.0%
associate-/l*91.2%
*-commutative91.2%
associate-/l*86.5%
Simplified86.5%
associate-/l*91.2%
*-commutative91.2%
associate-/r/93.2%
Applied egg-rr93.2%
if -1.99998e-319 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < -0.0 or 1e277 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) Initial program 70.3%
times-frac98.2%
Simplified98.2%
if -0.0 < (/.f64 (*.f64 a1 a2) (*.f64 b1 b2)) < 1e277Initial program 98.7%
Final simplification96.3%
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 b2) -1e-222)
(and (not (<= (* b1 b2) 1e-285)) (<= (* b1 b2) 1e+260)))
(* a2 (/ a1 (* b1 b2)))
(* (/ a1 b1) (/ a2 b2))))assert(a1 < a2);
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
double tmp;
if (((b1 * b2) <= -1e-222) || (!((b1 * b2) <= 1e-285) && ((b1 * b2) <= 1e+260))) {
tmp = a2 * (a1 / (b1 * b2));
} else {
tmp = (a1 / b1) * (a2 / 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 * b2) <= (-1d-222)) .or. (.not. ((b1 * b2) <= 1d-285)) .and. ((b1 * b2) <= 1d+260)) then
tmp = a2 * (a1 / (b1 * b2))
else
tmp = (a1 / b1) * (a2 / 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 * b2) <= -1e-222) || (!((b1 * b2) <= 1e-285) && ((b1 * b2) <= 1e+260))) {
tmp = a2 * (a1 / (b1 * b2));
} else {
tmp = (a1 / b1) * (a2 / b2);
}
return tmp;
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): tmp = 0 if ((b1 * b2) <= -1e-222) or (not ((b1 * b2) <= 1e-285) and ((b1 * b2) <= 1e+260)): tmp = a2 * (a1 / (b1 * b2)) else: tmp = (a1 / b1) * (a2 / b2) return tmp
a1, a2 = sort([a1, a2]) b1, b2 = sort([b1, b2]) function code(a1, a2, b1, b2) tmp = 0.0 if ((Float64(b1 * b2) <= -1e-222) || (!(Float64(b1 * b2) <= 1e-285) && (Float64(b1 * b2) <= 1e+260))) tmp = Float64(a2 * Float64(a1 / Float64(b1 * b2))); else tmp = Float64(Float64(a1 / b1) * Float64(a2 / 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 * b2) <= -1e-222) || (~(((b1 * b2) <= 1e-285)) && ((b1 * b2) <= 1e+260)))
tmp = a2 * (a1 / (b1 * b2));
else
tmp = (a1 / b1) * (a2 / 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[N[(b1 * b2), $MachinePrecision], -1e-222], And[N[Not[LessEqual[N[(b1 * b2), $MachinePrecision], 1e-285]], $MachinePrecision], LessEqual[N[(b1 * b2), $MachinePrecision], 1e+260]]], N[(a2 * N[(a1 / N[(b1 * b2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\begin{array}{l}
\mathbf{if}\;b1 \cdot b2 \leq -1 \cdot 10^{-222} \lor \neg \left(b1 \cdot b2 \leq 10^{-285}\right) \land b1 \cdot b2 \leq 10^{+260}:\\
\;\;\;\;a2 \cdot \frac{a1}{b1 \cdot b2}\\
\mathbf{else}:\\
\;\;\;\;\frac{a1}{b1} \cdot \frac{a2}{b2}\\
\end{array}
\end{array}
if (*.f64 b1 b2) < -1.00000000000000005e-222 or 1.00000000000000007e-285 < (*.f64 b1 b2) < 1.00000000000000007e260Initial program 91.2%
associate-/l*92.8%
*-commutative92.8%
associate-/l*86.7%
Simplified86.7%
associate-/l*92.8%
*-commutative92.8%
associate-/r/93.5%
Applied egg-rr93.5%
if -1.00000000000000005e-222 < (*.f64 b1 b2) < 1.00000000000000007e-285 or 1.00000000000000007e260 < (*.f64 b1 b2) Initial program 64.1%
times-frac98.4%
Simplified98.4%
Final simplification94.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 (* (/ a1 b1) (/ a2 b2)))
assert(a1 < a2);
assert(b1 < b2);
double code(double a1, double a2, double b1, double b2) {
return (a1 / b1) * (a2 / b2);
}
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 = (a1 / b1) * (a2 / b2)
end function
assert a1 < a2;
assert b1 < b2;
public static double code(double a1, double a2, double b1, double b2) {
return (a1 / b1) * (a2 / b2);
}
[a1, a2] = sort([a1, a2]) [b1, b2] = sort([b1, b2]) def code(a1, a2, b1, b2): return (a1 / b1) * (a2 / b2)
a1, a2 = sort([a1, a2]) b1, b2 = sort([b1, b2]) function code(a1, a2, b1, b2) return Float64(Float64(a1 / b1) * Float64(a2 / b2)) end
a1, a2 = num2cell(sort([a1, a2])){:}
b1, b2 = num2cell(sort([b1, b2])){:}
function tmp = code(a1, a2, b1, b2)
tmp = (a1 / b1) * (a2 / b2);
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[(a1 / b1), $MachinePrecision] * N[(a2 / b2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
[b1, b2] = \mathsf{sort}([b1, b2])\\
\\
\frac{a1}{b1} \cdot \frac{a2}{b2}
\end{array}
Initial program 84.3%
times-frac88.0%
Simplified88.0%
Final simplification88.0%
(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 2023178
(FPCore (a1 a2 b1 b2)
:name "Quotient of products"
:precision binary64
:herbie-target
(* (/ a1 b1) (/ a2 b2))
(/ (* a1 a2) (* b1 b2)))