
(FPCore (x y z) :precision binary64 (+ (- (- (* x y) (* y z)) (* y y)) (* y y)))
double code(double x, double y, double z) {
return (((x * y) - (y * z)) - (y * y)) + (y * y);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (((x * y) - (y * z)) - (y * y)) + (y * y)
end function
public static double code(double x, double y, double z) {
return (((x * y) - (y * z)) - (y * y)) + (y * y);
}
def code(x, y, z): return (((x * y) - (y * z)) - (y * y)) + (y * y)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) - Float64(y * z)) - Float64(y * y)) + Float64(y * y)) end
function tmp = code(x, y, z) tmp = (((x * y) - (y * z)) - (y * y)) + (y * y); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y - y \cdot z\right) - y \cdot y\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (- (- (* x y) (* y z)) (* y y)) (* y y)))
double code(double x, double y, double z) {
return (((x * y) - (y * z)) - (y * y)) + (y * y);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (((x * y) - (y * z)) - (y * y)) + (y * y)
end function
public static double code(double x, double y, double z) {
return (((x * y) - (y * z)) - (y * y)) + (y * y);
}
def code(x, y, z): return (((x * y) - (y * z)) - (y * y)) + (y * y)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) - Float64(y * z)) - Float64(y * y)) + Float64(y * y)) end
function tmp = code(x, y, z) tmp = (((x * y) - (y * z)) - (y * y)) + (y * y); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y - y \cdot z\right) - y \cdot y\right) + y \cdot y
\end{array}
(FPCore (x y z) :precision binary64 (fma y x (* y (- z))))
double code(double x, double y, double z) {
return fma(y, x, (y * -z));
}
function code(x, y, z) return fma(y, x, Float64(y * Float64(-z))) end
code[x_, y_, z_] := N[(y * x + N[(y * (-z)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, y \cdot \left(-z\right)\right)
\end{array}
Initial program 59.4%
associate-+l-73.4%
+-inverses99.6%
--rgt-identity99.6%
*-commutative99.6%
distribute-lft-out--100.0%
Simplified100.0%
sub-neg100.0%
distribute-lft-in99.6%
fma-define100.0%
Applied egg-rr100.0%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.05e+37) (not (<= z 5.4e+20))) (* y (- z)) (* y x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.05e+37) || !(z <= 5.4e+20)) {
tmp = y * -z;
} else {
tmp = y * x;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((z <= (-1.05d+37)) .or. (.not. (z <= 5.4d+20))) then
tmp = y * -z
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -1.05e+37) || !(z <= 5.4e+20)) {
tmp = y * -z;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.05e+37) or not (z <= 5.4e+20): tmp = y * -z else: tmp = y * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.05e+37) || !(z <= 5.4e+20)) tmp = Float64(y * Float64(-z)); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.05e+37) || ~((z <= 5.4e+20))) tmp = y * -z; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.05e+37], N[Not[LessEqual[z, 5.4e+20]], $MachinePrecision]], N[(y * (-z)), $MachinePrecision], N[(y * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.05 \cdot 10^{+37} \lor \neg \left(z \leq 5.4 \cdot 10^{+20}\right):\\
\;\;\;\;y \cdot \left(-z\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if z < -1.0500000000000001e37 or 5.4e20 < z Initial program 64.5%
associate-+l-71.2%
+-inverses99.1%
--rgt-identity99.1%
*-commutative99.1%
distribute-lft-out--100.0%
Simplified100.0%
Taylor expanded in x around 0 85.1%
mul-1-neg85.1%
*-commutative85.1%
distribute-rgt-neg-in85.1%
Simplified85.1%
if -1.0500000000000001e37 < z < 5.4e20Initial program 55.0%
associate-+l-75.4%
+-inverses100.0%
--rgt-identity100.0%
*-commutative100.0%
distribute-lft-out--100.0%
Simplified100.0%
Taylor expanded in x around inf 82.4%
*-commutative82.4%
Simplified82.4%
Final simplification83.6%
(FPCore (x y z) :precision binary64 (* y (- x z)))
double code(double x, double y, double z) {
return y * (x - z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y * (x - z)
end function
public static double code(double x, double y, double z) {
return y * (x - z);
}
def code(x, y, z): return y * (x - z)
function code(x, y, z) return Float64(y * Float64(x - z)) end
function tmp = code(x, y, z) tmp = y * (x - z); end
code[x_, y_, z_] := N[(y * N[(x - z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(x - z\right)
\end{array}
Initial program 59.4%
associate-+l-73.4%
+-inverses99.6%
--rgt-identity99.6%
*-commutative99.6%
distribute-lft-out--100.0%
Simplified100.0%
(FPCore (x y z) :precision binary64 (* y x))
double code(double x, double y, double z) {
return y * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y * x
end function
public static double code(double x, double y, double z) {
return y * x;
}
def code(x, y, z): return y * x
function code(x, y, z) return Float64(y * x) end
function tmp = code(x, y, z) tmp = y * x; end
code[x_, y_, z_] := N[(y * x), $MachinePrecision]
\begin{array}{l}
\\
y \cdot x
\end{array}
Initial program 59.4%
associate-+l-73.4%
+-inverses99.6%
--rgt-identity99.6%
*-commutative99.6%
distribute-lft-out--100.0%
Simplified100.0%
Taylor expanded in x around inf 55.9%
*-commutative55.9%
Simplified55.9%
(FPCore (x y z) :precision binary64 (* (- x z) y))
double code(double x, double y, double z) {
return (x - z) * y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x - z) * y
end function
public static double code(double x, double y, double z) {
return (x - z) * y;
}
def code(x, y, z): return (x - z) * y
function code(x, y, z) return Float64(Float64(x - z) * y) end
function tmp = code(x, y, z) tmp = (x - z) * y; end
code[x_, y_, z_] := N[(N[(x - z), $MachinePrecision] * y), $MachinePrecision]
\begin{array}{l}
\\
\left(x - z\right) \cdot y
\end{array}
herbie shell --seed 2024143
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, B"
:precision binary64
:alt
(! :herbie-platform default (* (- x z) y))
(+ (- (- (* x y) (* y z)) (* y y)) (* y y)))