
(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) 2.0))
assert(a < b);
double code(double a, double b) {
return (b - a) / 2.0;
}
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) / 2.0d0
end function
assert a < b;
public static double code(double a, double b) {
return (b - a) / 2.0;
}
[a, b] = sort([a, b]) def code(a, b): return (b - a) / 2.0
a, b = sort([a, b]) function code(a, b) return Float64(Float64(b - a) / 2.0) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = (b - a) / 2.0;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(b - a), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\frac{b - a}{2}
\end{array}
Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in b around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
rem-square-sqrt46.5%
fabs-sqr46.5%
rem-square-sqrt47.4%
Simplified47.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -4.5e-111) (* a -0.5) (/ b 2.0)))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -4.5e-111) {
tmp = a * -0.5;
} else {
tmp = b / 2.0;
}
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 <= (-4.5d-111)) then
tmp = a * (-0.5d0)
else
tmp = b / 2.0d0
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b) {
double tmp;
if (a <= -4.5e-111) {
tmp = a * -0.5;
} else {
tmp = b / 2.0;
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if a <= -4.5e-111: tmp = a * -0.5 else: tmp = b / 2.0 return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -4.5e-111) tmp = Float64(a * -0.5); else tmp = Float64(b / 2.0); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
tmp = 0.0;
if (a <= -4.5e-111)
tmp = a * -0.5;
else
tmp = b / 2.0;
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, -4.5e-111], N[(a * -0.5), $MachinePrecision], N[(b / 2.0), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -4.5 \cdot 10^{-111}:\\
\;\;\;\;a \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{2}\\
\end{array}
\end{array}
if a < -4.49999999999999994e-111Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in b around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
rem-square-sqrt71.7%
fabs-sqr71.7%
rem-square-sqrt72.5%
Simplified72.5%
Taylor expanded in b around 0 57.3%
if -4.49999999999999994e-111 < a Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in b around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
rem-square-sqrt31.8%
fabs-sqr31.8%
rem-square-sqrt32.9%
Simplified32.9%
Taylor expanded in b around inf 31.7%
Final simplification41.1%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* a -0.5))
assert(a < b);
double code(double a, double b) {
return 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 = a * (-0.5d0)
end function
assert a < b;
public static double code(double a, double b) {
return a * -0.5;
}
[a, b] = sort([a, b]) def code(a, b): return a * -0.5
a, b = sort([a, b]) function code(a, b) return Float64(a * -0.5) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = a * -0.5;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(a * -0.5), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
a \cdot -0.5
\end{array}
Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in b around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
rem-square-sqrt46.5%
fabs-sqr46.5%
rem-square-sqrt47.4%
Simplified47.4%
Taylor expanded in b around 0 23.6%
Final simplification23.6%
herbie shell --seed 2024154
(FPCore (a b)
:name "fabs fraction 2"
:precision binary64
(/ (fabs (- a b)) 2.0))