
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (if (<= x 7.2e+217) (fma x x (* y (- y))) (* (+ x y) (+ x y))))
double code(double x, double y) {
double tmp;
if (x <= 7.2e+217) {
tmp = fma(x, x, (y * -y));
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= 7.2e+217) tmp = fma(x, x, Float64(y * Float64(-y))); else tmp = Float64(Float64(x + y) * Float64(x + y)); end return tmp end
code[x_, y_] := If[LessEqual[x, 7.2e+217], N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 7.2 \cdot 10^{+217}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x + y\right) \cdot \left(x + y\right)\\
\end{array}
\end{array}
if x < 7.2000000000000004e217Initial program 94.4%
sqr-neg94.4%
cancel-sign-sub94.4%
fma-define98.7%
Simplified98.7%
if 7.2000000000000004e217 < x Initial program 76.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt56.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-prod44.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification98.8%
(FPCore (x y) :precision binary64 (if (<= (* y y) 1e+292) (- (* x x) (* y y)) (- (pow y 2.0))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+292) {
tmp = (x * x) - (y * y);
} else {
tmp = -pow(y, 2.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y * y) <= 1d+292) then
tmp = (x * x) - (y * y)
else
tmp = -(y ** 2.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+292) {
tmp = (x * x) - (y * y);
} else {
tmp = -Math.pow(y, 2.0);
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 1e+292: tmp = (x * x) - (y * y) else: tmp = -math.pow(y, 2.0) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 1e+292) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(-(y ^ 2.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 1e+292) tmp = (x * x) - (y * y); else tmp = -(y ^ 2.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1e+292], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], (-N[Power[y, 2.0], $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 10^{+292}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;-{y}^{2}\\
\end{array}
\end{array}
if (*.f64 y y) < 1e292Initial program 100.0%
if 1e292 < (*.f64 y y) Initial program 69.8%
Taylor expanded in x around 0 85.7%
mul-1-neg85.7%
Simplified85.7%
Final simplification96.5%
(FPCore (x y) :precision binary64 (let* ((t_0 (- (* x x) (* y y)))) (if (<= t_0 4e+298) t_0 (* (+ x y) (+ x y)))))
double code(double x, double y) {
double t_0 = (x * x) - (y * y);
double tmp;
if (t_0 <= 4e+298) {
tmp = t_0;
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = (x * x) - (y * y)
if (t_0 <= 4d+298) then
tmp = t_0
else
tmp = (x + y) * (x + y)
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (x * x) - (y * y);
double tmp;
if (t_0 <= 4e+298) {
tmp = t_0;
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
def code(x, y): t_0 = (x * x) - (y * y) tmp = 0 if t_0 <= 4e+298: tmp = t_0 else: tmp = (x + y) * (x + y) return tmp
function code(x, y) t_0 = Float64(Float64(x * x) - Float64(y * y)) tmp = 0.0 if (t_0 <= 4e+298) tmp = t_0; else tmp = Float64(Float64(x + y) * Float64(x + y)); end return tmp end
function tmp_2 = code(x, y) t_0 = (x * x) - (y * y); tmp = 0.0; if (t_0 <= 4e+298) tmp = t_0; else tmp = (x + y) * (x + y); end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 4e+298], t$95$0, N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot x - y \cdot y\\
\mathbf{if}\;t\_0 \leq 4 \cdot 10^{+298}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\left(x + y\right) \cdot \left(x + y\right)\\
\end{array}
\end{array}
if (-.f64 (*.f64 x x) (*.f64 y y)) < 3.9999999999999998e298Initial program 100.0%
if 3.9999999999999998e298 < (-.f64 (*.f64 x x) (*.f64 y y)) Initial program 78.2%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt49.4%
sqrt-unprod93.1%
sqr-neg93.1%
sqrt-prod44.8%
add-sqr-sqrt88.5%
Applied egg-rr88.5%
Final simplification96.1%
(FPCore (x y) :precision binary64 (* (+ x y) (+ x y)))
double code(double x, double y) {
return (x + y) * (x + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) * (x + y)
end function
public static double code(double x, double y) {
return (x + y) * (x + y);
}
def code(x, y): return (x + y) * (x + y)
function code(x, y) return Float64(Float64(x + y) * Float64(x + y)) end
function tmp = code(x, y) tmp = (x + y) * (x + y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(x + y\right)
\end{array}
Initial program 92.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt49.9%
sqrt-unprod78.4%
sqr-neg78.4%
sqrt-prod28.8%
add-sqr-sqrt60.7%
Applied egg-rr60.7%
Final simplification60.7%
herbie shell --seed 2024079
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))