
(FPCore (x y) :precision binary64 (/ (- x y) (* (* x 2.0) y)))
double code(double x, double y) {
return (x - y) / ((x * 2.0) * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x - y) / ((x * 2.0d0) * y)
end function
public static double code(double x, double y) {
return (x - y) / ((x * 2.0) * y);
}
def code(x, y): return (x - y) / ((x * 2.0) * y)
function code(x, y) return Float64(Float64(x - y) / Float64(Float64(x * 2.0) * y)) end
function tmp = code(x, y) tmp = (x - y) / ((x * 2.0) * y); end
code[x_, y_] := N[(N[(x - y), $MachinePrecision] / N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - y}{\left(x \cdot 2\right) \cdot y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (- x y) (* (* x 2.0) y)))
double code(double x, double y) {
return (x - y) / ((x * 2.0) * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x - y) / ((x * 2.0d0) * y)
end function
public static double code(double x, double y) {
return (x - y) / ((x * 2.0) * y);
}
def code(x, y): return (x - y) / ((x * 2.0) * y)
function code(x, y) return Float64(Float64(x - y) / Float64(Float64(x * 2.0) * y)) end
function tmp = code(x, y) tmp = (x - y) / ((x * 2.0) * y); end
code[x_, y_] := N[(N[(x - y), $MachinePrecision] / N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - y}{\left(x \cdot 2\right) \cdot y}
\end{array}
(FPCore (x y) :precision binary64 (+ (/ 0.5 y) (/ -0.5 x)))
double code(double x, double y) {
return (0.5 / y) + (-0.5 / x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (0.5d0 / y) + ((-0.5d0) / x)
end function
public static double code(double x, double y) {
return (0.5 / y) + (-0.5 / x);
}
def code(x, y): return (0.5 / y) + (-0.5 / x)
function code(x, y) return Float64(Float64(0.5 / y) + Float64(-0.5 / x)) end
function tmp = code(x, y) tmp = (0.5 / y) + (-0.5 / x); end
code[x_, y_] := N[(N[(0.5 / y), $MachinePrecision] + N[(-0.5 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{y} + \frac{-0.5}{x}
\end{array}
Initial program 71.5%
remove-double-neg71.5%
distribute-rgt-neg-out71.5%
distribute-frac-neg271.5%
neg-mul-171.5%
div-sub71.1%
distribute-lft-out--71.1%
neg-mul-171.1%
distribute-frac-neg271.1%
distribute-rgt-neg-out71.1%
remove-double-neg71.1%
cancel-sign-sub-inv71.1%
associate-/r*81.1%
associate-/r*81.1%
*-inverses81.1%
metadata-eval81.1%
metadata-eval81.1%
metadata-eval81.1%
metadata-eval81.1%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (or (<= y -6.2e+161)
(and (not (<= y -1.65e+55)) (or (<= y -0.0066) (not (<= y 0.049)))))
(/ -0.5 x)
(/ 0.5 y)))
double code(double x, double y) {
double tmp;
if ((y <= -6.2e+161) || (!(y <= -1.65e+55) && ((y <= -0.0066) || !(y <= 0.049)))) {
tmp = -0.5 / x;
} else {
tmp = 0.5 / y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-6.2d+161)) .or. (.not. (y <= (-1.65d+55))) .and. (y <= (-0.0066d0)) .or. (.not. (y <= 0.049d0))) then
tmp = (-0.5d0) / x
else
tmp = 0.5d0 / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -6.2e+161) || (!(y <= -1.65e+55) && ((y <= -0.0066) || !(y <= 0.049)))) {
tmp = -0.5 / x;
} else {
tmp = 0.5 / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -6.2e+161) or (not (y <= -1.65e+55) and ((y <= -0.0066) or not (y <= 0.049))): tmp = -0.5 / x else: tmp = 0.5 / y return tmp
function code(x, y) tmp = 0.0 if ((y <= -6.2e+161) || (!(y <= -1.65e+55) && ((y <= -0.0066) || !(y <= 0.049)))) tmp = Float64(-0.5 / x); else tmp = Float64(0.5 / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -6.2e+161) || (~((y <= -1.65e+55)) && ((y <= -0.0066) || ~((y <= 0.049))))) tmp = -0.5 / x; else tmp = 0.5 / y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -6.2e+161], And[N[Not[LessEqual[y, -1.65e+55]], $MachinePrecision], Or[LessEqual[y, -0.0066], N[Not[LessEqual[y, 0.049]], $MachinePrecision]]]], N[(-0.5 / x), $MachinePrecision], N[(0.5 / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.2 \cdot 10^{+161} \lor \neg \left(y \leq -1.65 \cdot 10^{+55}\right) \land \left(y \leq -0.0066 \lor \neg \left(y \leq 0.049\right)\right):\\
\;\;\;\;\frac{-0.5}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{y}\\
\end{array}
\end{array}
if y < -6.20000000000000013e161 or -1.65e55 < y < -0.0066 or 0.049000000000000002 < y Initial program 69.5%
remove-double-neg69.5%
distribute-rgt-neg-out69.5%
distribute-frac-neg269.5%
neg-mul-169.5%
div-sub69.5%
distribute-lft-out--69.5%
neg-mul-169.5%
distribute-frac-neg269.5%
distribute-rgt-neg-out69.5%
remove-double-neg69.5%
cancel-sign-sub-inv69.5%
associate-/r*79.6%
associate-/r*79.6%
*-inverses79.6%
metadata-eval79.6%
metadata-eval79.6%
metadata-eval79.6%
metadata-eval79.6%
Simplified100.0%
Taylor expanded in y around inf 74.8%
if -6.20000000000000013e161 < y < -1.65e55 or -0.0066 < y < 0.049000000000000002Initial program 73.2%
remove-double-neg73.2%
distribute-rgt-neg-out73.2%
distribute-frac-neg273.2%
neg-mul-173.2%
div-sub72.5%
distribute-lft-out--72.5%
neg-mul-172.5%
distribute-frac-neg272.5%
distribute-rgt-neg-out72.5%
remove-double-neg72.5%
cancel-sign-sub-inv72.5%
associate-/r*82.4%
associate-/r*82.4%
*-inverses82.4%
metadata-eval82.4%
metadata-eval82.4%
metadata-eval82.4%
metadata-eval82.4%
Simplified100.0%
Taylor expanded in y around 0 78.6%
Final simplification76.8%
(FPCore (x y) :precision binary64 (/ -0.5 x))
double code(double x, double y) {
return -0.5 / x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (-0.5d0) / x
end function
public static double code(double x, double y) {
return -0.5 / x;
}
def code(x, y): return -0.5 / x
function code(x, y) return Float64(-0.5 / x) end
function tmp = code(x, y) tmp = -0.5 / x; end
code[x_, y_] := N[(-0.5 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-0.5}{x}
\end{array}
Initial program 71.5%
remove-double-neg71.5%
distribute-rgt-neg-out71.5%
distribute-frac-neg271.5%
neg-mul-171.5%
div-sub71.1%
distribute-lft-out--71.1%
neg-mul-171.1%
distribute-frac-neg271.1%
distribute-rgt-neg-out71.1%
remove-double-neg71.1%
cancel-sign-sub-inv71.1%
associate-/r*81.1%
associate-/r*81.1%
*-inverses81.1%
metadata-eval81.1%
metadata-eval81.1%
metadata-eval81.1%
metadata-eval81.1%
Simplified100.0%
Taylor expanded in y around inf 46.7%
Final simplification46.7%
(FPCore (x y) :precision binary64 (- (/ 0.5 y) (/ 0.5 x)))
double code(double x, double y) {
return (0.5 / y) - (0.5 / x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (0.5d0 / y) - (0.5d0 / x)
end function
public static double code(double x, double y) {
return (0.5 / y) - (0.5 / x);
}
def code(x, y): return (0.5 / y) - (0.5 / x)
function code(x, y) return Float64(Float64(0.5 / y) - Float64(0.5 / x)) end
function tmp = code(x, y) tmp = (0.5 / y) - (0.5 / x); end
code[x_, y_] := N[(N[(0.5 / y), $MachinePrecision] - N[(0.5 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{y} - \frac{0.5}{x}
\end{array}
herbie shell --seed 2024100
(FPCore (x y)
:name "Linear.Projection:inversePerspective from linear-1.19.1.3, B"
:precision binary64
:alt
(- (/ 0.5 y) (/ 0.5 x))
(/ (- x y) (* (* x 2.0) y)))