
(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 (fma x x (* y (- y))))
double code(double x, double y) {
return fma(x, x, (y * -y));
}
function code(x, y) return fma(x, x, Float64(y * Float64(-y))) end
code[x_, y_] := N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)
\end{array}
Initial program 91.4%
sqr-neg91.4%
cancel-sign-sub91.4%
fma-def96.1%
Simplified96.1%
Final simplification96.1%
(FPCore (x y) :precision binary64 (if (<= (* y y) 5e+305) (- (* x x) (* y y)) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+305) {
tmp = (x * x) - (y * y);
} else {
tmp = y * -y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y * y) <= 5d+305) then
tmp = (x * x) - (y * y)
else
tmp = y * -y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+305) {
tmp = (x * x) - (y * y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 5e+305: tmp = (x * x) - (y * y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 5e+305) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(y * Float64(-y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 5e+305) tmp = (x * x) - (y * y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 5e+305], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 5 \cdot 10^{+305}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 5.00000000000000009e305Initial program 100.0%
if 5.00000000000000009e305 < (*.f64 y y) Initial program 68.1%
Taylor expanded in x around 0 85.5%
unpow285.5%
mul-1-neg85.5%
distribute-rgt-neg-in85.5%
Simplified85.5%
Final simplification96.1%
(FPCore (x y) :precision binary64 (if (<= y 1.46e-12) (* x x) (* y (- y))))
double code(double x, double y) {
double tmp;
if (y <= 1.46e-12) {
tmp = x * x;
} else {
tmp = y * -y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 1.46d-12) then
tmp = x * x
else
tmp = y * -y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 1.46e-12) {
tmp = x * x;
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.46e-12: tmp = x * x else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (y <= 1.46e-12) tmp = Float64(x * x); else tmp = Float64(y * Float64(-y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.46e-12) tmp = x * x; else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.46e-12], N[(x * x), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.46 \cdot 10^{-12}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if y < 1.46000000000000004e-12Initial program 94.9%
Taylor expanded in x around inf 61.9%
unpow261.9%
Simplified61.9%
if 1.46000000000000004e-12 < y Initial program 80.0%
Taylor expanded in x around 0 75.1%
unpow275.1%
mul-1-neg75.1%
distribute-rgt-neg-in75.1%
Simplified75.1%
Final simplification65.0%
(FPCore (x y) :precision binary64 (* x x))
double code(double x, double y) {
return x * x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * x
end function
public static double code(double x, double y) {
return x * x;
}
def code(x, y): return x * x
function code(x, y) return Float64(x * x) end
function tmp = code(x, y) tmp = x * x; end
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 91.4%
Taylor expanded in x around inf 53.5%
unpow253.5%
Simplified53.5%
Final simplification53.5%
herbie shell --seed 2023279
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))