
(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}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(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 100.0%
(FPCore (x y) :precision binary64 (if (or (<= x 2.45e-36) (and (not (<= x 4.4e+30)) (<= x 1.66e+52))) (* y (- y)) (* x (- x y))))
double code(double x, double y) {
double tmp;
if ((x <= 2.45e-36) || (!(x <= 4.4e+30) && (x <= 1.66e+52))) {
tmp = y * -y;
} else {
tmp = x * (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 <= 2.45d-36) .or. (.not. (x <= 4.4d+30)) .and. (x <= 1.66d+52)) then
tmp = y * -y
else
tmp = x * (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= 2.45e-36) || (!(x <= 4.4e+30) && (x <= 1.66e+52))) {
tmp = y * -y;
} else {
tmp = x * (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= 2.45e-36) or (not (x <= 4.4e+30) and (x <= 1.66e+52)): tmp = y * -y else: tmp = x * (x - y) return tmp
function code(x, y) tmp = 0.0 if ((x <= 2.45e-36) || (!(x <= 4.4e+30) && (x <= 1.66e+52))) tmp = Float64(y * Float64(-y)); else tmp = Float64(x * Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= 2.45e-36) || (~((x <= 4.4e+30)) && (x <= 1.66e+52))) tmp = y * -y; else tmp = x * (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, 2.45e-36], And[N[Not[LessEqual[x, 4.4e+30]], $MachinePrecision], LessEqual[x, 1.66e+52]]], N[(y * (-y)), $MachinePrecision], N[(x * N[(x - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.45 \cdot 10^{-36} \lor \neg \left(x \leq 4.4 \cdot 10^{+30}\right) \land x \leq 1.66 \cdot 10^{+52}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(x - y\right)\\
\end{array}
\end{array}
if x < 2.4499999999999998e-36 or 4.4e30 < x < 1.65999999999999994e52Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 67.3%
Taylor expanded in x around 0 65.7%
neg-mul-165.7%
Simplified65.7%
if 2.4499999999999998e-36 < x < 4.4e30 or 1.65999999999999994e52 < x Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around inf 85.3%
Final simplification71.4%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* x (- x y))))
(if (<= y 4.8e-93)
t_0
(if (<= y 5.5e-67) (* y (- y)) (if (<= y 4.6e-30) t_0 (* y (- x y)))))))
double code(double x, double y) {
double t_0 = x * (x - y);
double tmp;
if (y <= 4.8e-93) {
tmp = t_0;
} else if (y <= 5.5e-67) {
tmp = y * -y;
} else if (y <= 4.6e-30) {
tmp = t_0;
} else {
tmp = 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)
if (y <= 4.8d-93) then
tmp = t_0
else if (y <= 5.5d-67) then
tmp = y * -y
else if (y <= 4.6d-30) then
tmp = t_0
else
tmp = y * (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = x * (x - y);
double tmp;
if (y <= 4.8e-93) {
tmp = t_0;
} else if (y <= 5.5e-67) {
tmp = y * -y;
} else if (y <= 4.6e-30) {
tmp = t_0;
} else {
tmp = y * (x - y);
}
return tmp;
}
def code(x, y): t_0 = x * (x - y) tmp = 0 if y <= 4.8e-93: tmp = t_0 elif y <= 5.5e-67: tmp = y * -y elif y <= 4.6e-30: tmp = t_0 else: tmp = y * (x - y) return tmp
function code(x, y) t_0 = Float64(x * Float64(x - y)) tmp = 0.0 if (y <= 4.8e-93) tmp = t_0; elseif (y <= 5.5e-67) tmp = Float64(y * Float64(-y)); elseif (y <= 4.6e-30) tmp = t_0; else tmp = Float64(y * Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) t_0 = x * (x - y); tmp = 0.0; if (y <= 4.8e-93) tmp = t_0; elseif (y <= 5.5e-67) tmp = y * -y; elseif (y <= 4.6e-30) tmp = t_0; else tmp = y * (x - y); end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(x * N[(x - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, 4.8e-93], t$95$0, If[LessEqual[y, 5.5e-67], N[(y * (-y)), $MachinePrecision], If[LessEqual[y, 4.6e-30], t$95$0, N[(y * N[(x - y), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(x - y\right)\\
\mathbf{if}\;y \leq 4.8 \cdot 10^{-93}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 5.5 \cdot 10^{-67}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{elif}\;y \leq 4.6 \cdot 10^{-30}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x - y\right)\\
\end{array}
\end{array}
if y < 4.8000000000000002e-93 or 5.5000000000000003e-67 < y < 4.59999999999999968e-30Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around inf 67.8%
if 4.8000000000000002e-93 < y < 5.5000000000000003e-67Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 72.9%
Taylor expanded in x around 0 76.1%
neg-mul-176.1%
Simplified76.1%
if 4.59999999999999968e-30 < y Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 81.7%
Final simplification71.4%
(FPCore (x y) :precision binary64 (if (<= x 2.6e+172) (* y (- y)) (* x y)))
double code(double x, double y) {
double tmp;
if (x <= 2.6e+172) {
tmp = y * -y;
} else {
tmp = 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 <= 2.6d+172) then
tmp = y * -y
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 2.6e+172) {
tmp = y * -y;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 2.6e+172: tmp = y * -y else: tmp = x * y return tmp
function code(x, y) tmp = 0.0 if (x <= 2.6e+172) tmp = Float64(y * Float64(-y)); else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 2.6e+172) tmp = y * -y; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 2.6e+172], N[(y * (-y)), $MachinePrecision], N[(x * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.6 \cdot 10^{+172}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < 2.6e172Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 61.3%
Taylor expanded in x around 0 60.0%
neg-mul-160.0%
Simplified60.0%
if 2.6e172 < x Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 19.3%
Taylor expanded in x around inf 14.2%
Final simplification53.0%
(FPCore (x y) :precision binary64 (* x y))
double code(double x, double y) {
return x * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * y
end function
public static double code(double x, double y) {
return x * y;
}
def code(x, y): return x * y
function code(x, y) return Float64(x * y) end
function tmp = code(x, y) tmp = x * y; end
code[x_, y_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y
\end{array}
Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 54.9%
Taylor expanded in x around inf 14.9%
herbie shell --seed 2024110
(FPCore (x y)
:name "Examples.Basics.BasicTests:f1 from sbv-4.4"
:precision binary64
(* (+ x y) (- x y)))