
(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 3 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 (* (+ 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 95.3%
add-sqr-sqrt51.4%
associate-*l*51.5%
prod-diff40.5%
Applied egg-rr40.5%
Taylor expanded in y around 0 52.2%
fma-neg51.5%
*-commutative51.5%
associate-*r*51.4%
add-sqr-sqrt95.3%
difference-of-squares100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (* x x) 1e+297) (- (* x x) (* y y)) (* (- x y) (- x y))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 1e+297) {
tmp = (x * x) - (y * y);
} 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) :: tmp
if ((x * x) <= 1d+297) then
tmp = (x * x) - (y * y)
else
tmp = (x - y) * (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x * x) <= 1e+297) {
tmp = (x * x) - (y * y);
} else {
tmp = (x - y) * (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 1e+297: tmp = (x * x) - (y * y) else: tmp = (x - y) * (x - y) return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 1e+297) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(Float64(x - y) * Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 1e+297) tmp = (x * x) - (y * y); else tmp = (x - y) * (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 1e+297], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[(x - y), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 10^{+297}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(x - y\right) \cdot \left(x - y\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 1e297Initial program 100.0%
if 1e297 < (*.f64 x x) Initial program 80.3%
add-sqr-sqrt80.3%
pow280.3%
difference-of-squares88.5%
sqrt-prod41.0%
add-sqr-sqrt16.4%
sqrt-prod41.0%
sqr-neg41.0%
sqrt-unprod24.6%
add-sqr-sqrt41.0%
sub-neg41.0%
add-sqr-sqrt88.5%
add-sqr-sqrt41.0%
add-sqr-sqrt16.4%
difference-of-squares16.4%
unpow-prod-down16.4%
Applied egg-rr16.4%
unpow216.4%
unpow216.4%
unswap-sqr16.4%
difference-of-squares16.4%
rem-square-sqrt16.4%
rem-square-sqrt16.4%
difference-of-squares16.4%
rem-square-sqrt42.6%
rem-square-sqrt88.5%
Simplified88.5%
Final simplification97.2%
(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 95.3%
add-sqr-sqrt50.8%
pow250.8%
difference-of-squares52.7%
sqrt-prod26.8%
add-sqr-sqrt14.0%
sqrt-prod27.1%
sqr-neg27.1%
sqrt-unprod13.5%
add-sqr-sqrt27.3%
sub-neg27.3%
add-sqr-sqrt52.7%
add-sqr-sqrt26.9%
add-sqr-sqrt13.9%
difference-of-squares13.9%
unpow-prod-down13.9%
Applied egg-rr13.9%
unpow213.9%
unpow213.9%
unswap-sqr13.9%
difference-of-squares13.9%
rem-square-sqrt13.9%
rem-square-sqrt13.9%
difference-of-squares13.9%
rem-square-sqrt27.0%
rem-square-sqrt52.7%
Simplified52.7%
Final simplification52.7%
herbie shell --seed 2024046
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))