
(FPCore (x y z) :precision binary64 (+ (* x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
return (x * sin(y)) + (z * cos(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 * sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
return (x * Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z): return (x * math.sin(y)) + (z * math.cos(y))
function code(x, y, z) return Float64(Float64(x * sin(y)) + Float64(z * cos(y))) end
function tmp = code(x, y, z) tmp = (x * sin(y)) + (z * cos(y)); end
code[x_, y_, z_] := N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \sin y + z \cdot \cos y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
return (x * sin(y)) + (z * cos(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 * sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
return (x * Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z): return (x * math.sin(y)) + (z * math.cos(y))
function code(x, y, z) return Float64(Float64(x * sin(y)) + Float64(z * cos(y))) end
function tmp = code(x, y, z) tmp = (x * sin(y)) + (z * cos(y)); end
code[x_, y_, z_] := N[(N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \sin y + z \cdot \cos y
\end{array}
(FPCore (x y z) :precision binary64 (fma (cos y) z (* x (sin y))))
double code(double x, double y, double z) {
return fma(cos(y), z, (x * sin(y)));
}
function code(x, y, z) return fma(cos(y), z, Float64(x * sin(y))) end
code[x_, y_, z_] := N[(N[Cos[y], $MachinePrecision] * z + N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos y, z, x \cdot \sin y\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Final simplification99.8%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* z (cos y))) (t_1 (* x (sin y))))
(if (<= y -6e+57)
t_0
(if (<= y -0.0066)
t_1
(if (<= y 0.019)
(fma (fma (* -0.5 y) z x) y z)
(if (<= y 4.5e+112) t_0 t_1))))))
double code(double x, double y, double z) {
double t_0 = z * cos(y);
double t_1 = x * sin(y);
double tmp;
if (y <= -6e+57) {
tmp = t_0;
} else if (y <= -0.0066) {
tmp = t_1;
} else if (y <= 0.019) {
tmp = fma(fma((-0.5 * y), z, x), y, z);
} else if (y <= 4.5e+112) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(z * cos(y)) t_1 = Float64(x * sin(y)) tmp = 0.0 if (y <= -6e+57) tmp = t_0; elseif (y <= -0.0066) tmp = t_1; elseif (y <= 0.019) tmp = fma(fma(Float64(-0.5 * y), z, x), y, z); elseif (y <= 4.5e+112) tmp = t_0; else tmp = t_1; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x * N[Sin[y], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -6e+57], t$95$0, If[LessEqual[y, -0.0066], t$95$1, If[LessEqual[y, 0.019], N[(N[(N[(-0.5 * y), $MachinePrecision] * z + x), $MachinePrecision] * y + z), $MachinePrecision], If[LessEqual[y, 4.5e+112], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := z \cdot \cos y\\
t_1 := x \cdot \sin y\\
\mathbf{if}\;y \leq -6 \cdot 10^{+57}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq -0.0066:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq 0.019:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.5 \cdot y, z, x\right), y, z\right)\\
\mathbf{elif}\;y \leq 4.5 \cdot 10^{+112}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if y < -5.9999999999999999e57 or 0.0189999999999999995 < y < 4.4999999999999999e112Initial program 99.7%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6450.9
Applied rewrites50.9%
if -5.9999999999999999e57 < y < -0.0066 or 4.4999999999999999e112 < y Initial program 99.6%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower-sin.f6450.5
Applied rewrites50.5%
if -0.0066 < y < 0.0189999999999999995Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
+-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f6499.7
Applied rewrites99.7%
Final simplification74.9%
herbie shell --seed 2024234
(FPCore (x y z)
:name "Diagrams.ThreeD.Transform:aboutX from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ (* x (sin y)) (* z (cos y))))