
(FPCore (a b) :precision binary64 (/ (fabs (- a b)) 2.0))
double code(double a, double b) {
return fabs((a - b)) / 2.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = abs((a - b)) / 2.0d0
end function
public static double code(double a, double b) {
return Math.abs((a - b)) / 2.0;
}
def code(a, b): return math.fabs((a - b)) / 2.0
function code(a, b) return Float64(abs(Float64(a - b)) / 2.0) end
function tmp = code(a, b) tmp = abs((a - b)) / 2.0; end
code[a_, b_] := N[(N[Abs[N[(a - b), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left|a - b\right|}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (/ (fabs (- a b)) 2.0))
double code(double a, double b) {
return fabs((a - b)) / 2.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = abs((a - b)) / 2.0d0
end function
public static double code(double a, double b) {
return Math.abs((a - b)) / 2.0;
}
def code(a, b): return math.fabs((a - b)) / 2.0
function code(a, b) return Float64(abs(Float64(a - b)) / 2.0) end
function tmp = code(a, b) tmp = abs((a - b)) / 2.0; end
code[a_, b_] := N[(N[Abs[N[(a - b), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left|a - b\right|}{2}
\end{array}
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* (- b a) 0.5))
assert(a < b);
double code(double a, double b) {
return (b - a) * 0.5;
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (b - a) * 0.5d0
end function
assert a < b;
public static double code(double a, double b) {
return (b - a) * 0.5;
}
[a, b] = sort([a, b]) def code(a, b): return (b - a) * 0.5
a, b = sort([a, b]) function code(a, b) return Float64(Float64(b - a) * 0.5) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = (b - a) * 0.5;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(b - a), $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\left(b - a\right) \cdot 0.5
\end{array}
Initial program 100.0%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
Applied rewrites52.6%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -3.5e-166) (* -0.5 a) (* 0.5 b)))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -3.5e-166) {
tmp = -0.5 * a;
} else {
tmp = 0.5 * b;
}
return tmp;
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-3.5d-166)) then
tmp = (-0.5d0) * a
else
tmp = 0.5d0 * b
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b) {
double tmp;
if (a <= -3.5e-166) {
tmp = -0.5 * a;
} else {
tmp = 0.5 * b;
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if a <= -3.5e-166: tmp = -0.5 * a else: tmp = 0.5 * b return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -3.5e-166) tmp = Float64(-0.5 * a); else tmp = Float64(0.5 * b); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
tmp = 0.0;
if (a <= -3.5e-166)
tmp = -0.5 * a;
else
tmp = 0.5 * b;
end
tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[LessEqual[a, -3.5e-166], N[(-0.5 * a), $MachinePrecision], N[(0.5 * b), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -3.5 \cdot 10^{-166}:\\
\;\;\;\;-0.5 \cdot a\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot b\\
\end{array}
\end{array}
if a < -3.4999999999999999e-166Initial program 100.0%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
Applied rewrites78.8%
Taylor expanded in a around inf
Applied rewrites64.5%
if -3.4999999999999999e-166 < a Initial program 100.0%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
Applied rewrites36.3%
Taylor expanded in a around 0
Applied rewrites35.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* -0.5 a))
assert(a < b);
double code(double a, double b) {
return -0.5 * a;
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (-0.5d0) * a
end function
assert a < b;
public static double code(double a, double b) {
return -0.5 * a;
}
[a, b] = sort([a, b]) def code(a, b): return -0.5 * a
a, b = sort([a, b]) function code(a, b) return Float64(-0.5 * a) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = -0.5 * a;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(-0.5 * a), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
-0.5 \cdot a
\end{array}
Initial program 100.0%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
Applied rewrites52.6%
Taylor expanded in a around inf
Applied rewrites26.9%
herbie shell --seed 2024337
(FPCore (a b)
:name "fabs fraction 2"
:precision binary64
(/ (fabs (- a b)) 2.0))