
(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 7 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 (sin y) x (* (cos y) z)))
double code(double x, double y, double z) {
return fma(sin(y), x, (cos(y) * z));
}
function code(x, y, z) return fma(sin(y), x, Float64(cos(y) * z)) end
code[x_, y_, z_] := N[(N[Sin[y], $MachinePrecision] * x + N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin y, x, \cos y \cdot z\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* (cos y) z)) (t_1 (* (sin y) x)))
(if (<= y -3e+98)
t_0
(if (<= y -60000000000.0)
t_1
(if (<= y 2.5e-12)
(fma (fma (* z y) -0.5 x) y z)
(if (<= y 3.1e+121) t_1 t_0))))))
double code(double x, double y, double z) {
double t_0 = cos(y) * z;
double t_1 = sin(y) * x;
double tmp;
if (y <= -3e+98) {
tmp = t_0;
} else if (y <= -60000000000.0) {
tmp = t_1;
} else if (y <= 2.5e-12) {
tmp = fma(fma((z * y), -0.5, x), y, z);
} else if (y <= 3.1e+121) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(cos(y) * z) t_1 = Float64(sin(y) * x) tmp = 0.0 if (y <= -3e+98) tmp = t_0; elseif (y <= -60000000000.0) tmp = t_1; elseif (y <= 2.5e-12) tmp = fma(fma(Float64(z * y), -0.5, x), y, z); elseif (y <= 3.1e+121) tmp = t_1; else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[y], $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[y, -3e+98], t$95$0, If[LessEqual[y, -60000000000.0], t$95$1, If[LessEqual[y, 2.5e-12], N[(N[(N[(z * y), $MachinePrecision] * -0.5 + x), $MachinePrecision] * y + z), $MachinePrecision], If[LessEqual[y, 3.1e+121], t$95$1, t$95$0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos y \cdot z\\
t_1 := \sin y \cdot x\\
\mathbf{if}\;y \leq -3 \cdot 10^{+98}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq -60000000000:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq 2.5 \cdot 10^{-12}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(z \cdot y, -0.5, x\right), y, z\right)\\
\mathbf{elif}\;y \leq 3.1 \cdot 10^{+121}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -3.0000000000000001e98 or 3.10000000000000008e121 < y Initial program 99.7%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6462.0
Applied rewrites62.0%
if -3.0000000000000001e98 < y < -6e10 or 2.49999999999999985e-12 < y < 3.10000000000000008e121Initial program 99.6%
lift-+.f64N/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f6465.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f6465.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f6465.5
Applied rewrites65.5%
Taylor expanded in x around -inf
associate-*r*N/A
*-commutativeN/A
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
*-lft-identityN/A
lower-*.f64N/A
lower-sin.f6468.8
Applied rewrites68.8%
if -6e10 < y < 2.49999999999999985e-12Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6499.3
Applied rewrites99.3%
Final simplification82.1%
(FPCore (x y z) :precision binary64 (if (or (<= z -2.3e+80) (not (<= z 7.5e+121))) (* (cos y) z) (fma (sin y) x (* 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2.3e+80) || !(z <= 7.5e+121)) {
tmp = cos(y) * z;
} else {
tmp = fma(sin(y), x, (1.0 * z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((z <= -2.3e+80) || !(z <= 7.5e+121)) tmp = Float64(cos(y) * z); else tmp = fma(sin(y), x, Float64(1.0 * z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[z, -2.3e+80], N[Not[LessEqual[z, 7.5e+121]], $MachinePrecision]], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision], N[(N[Sin[y], $MachinePrecision] * x + N[(1.0 * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.3 \cdot 10^{+80} \lor \neg \left(z \leq 7.5 \cdot 10^{+121}\right):\\
\;\;\;\;\cos y \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sin y, x, 1 \cdot z\right)\\
\end{array}
\end{array}
if z < -2.30000000000000004e80 or 7.49999999999999965e121 < z Initial program 99.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6490.1
Applied rewrites90.1%
if -2.30000000000000004e80 < z < 7.49999999999999965e121Initial program 99.8%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in y around 0
Applied rewrites86.0%
Final simplification87.5%
(FPCore (x y z)
:precision binary64
(if (or (<= y -0.9) (not (<= y 520.0)))
(* (cos y) z)
(+
(*
(fma
(* (fma 0.008333333333333333 (* y y) -0.16666666666666666) x)
(* y y)
x)
y)
(* (fma (* -0.5 y) y 1.0) z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -0.9) || !(y <= 520.0)) {
tmp = cos(y) * z;
} else {
tmp = (fma((fma(0.008333333333333333, (y * y), -0.16666666666666666) * x), (y * y), x) * y) + (fma((-0.5 * y), y, 1.0) * z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -0.9) || !(y <= 520.0)) tmp = Float64(cos(y) * z); else tmp = Float64(Float64(fma(Float64(fma(0.008333333333333333, Float64(y * y), -0.16666666666666666) * x), Float64(y * y), x) * y) + Float64(fma(Float64(-0.5 * y), y, 1.0) * z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -0.9], N[Not[LessEqual[y, 520.0]], $MachinePrecision]], N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision], N[(N[(N[(N[(N[(0.008333333333333333 * N[(y * y), $MachinePrecision] + -0.16666666666666666), $MachinePrecision] * x), $MachinePrecision] * N[(y * y), $MachinePrecision] + x), $MachinePrecision] * y), $MachinePrecision] + N[(N[(N[(-0.5 * y), $MachinePrecision] * y + 1.0), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -0.9 \lor \neg \left(y \leq 520\right):\\
\;\;\;\;\cos y \cdot z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.008333333333333333, y \cdot y, -0.16666666666666666\right) \cdot x, y \cdot y, x\right) \cdot y + \mathsf{fma}\left(-0.5 \cdot y, y, 1\right) \cdot z\\
\end{array}
\end{array}
if y < -0.900000000000000022 or 520 < y Initial program 99.6%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6452.9
Applied rewrites52.9%
if -0.900000000000000022 < y < 520Initial program 100.0%
Taylor expanded in y around 0
associate-*r*N/A
distribute-rgt1-inN/A
lower-*.f64N/A
unpow2N/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f6499.7
Applied rewrites99.7%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6498.7
Applied rewrites98.7%
Taylor expanded in x around 0
Applied rewrites98.7%
Final simplification76.7%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.25e-66) (not (<= z 4.4e-201))) (* 1.0 z) (* x y)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.25e-66) || !(z <= 4.4e-201)) {
tmp = 1.0 * z;
} else {
tmp = x * y;
}
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.25d-66)) .or. (.not. (z <= 4.4d-201))) then
tmp = 1.0d0 * z
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -1.25e-66) || !(z <= 4.4e-201)) {
tmp = 1.0 * z;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.25e-66) or not (z <= 4.4e-201): tmp = 1.0 * z else: tmp = x * y return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.25e-66) || !(z <= 4.4e-201)) tmp = Float64(1.0 * z); else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.25e-66) || ~((z <= 4.4e-201))) tmp = 1.0 * z; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.25e-66], N[Not[LessEqual[z, 4.4e-201]], $MachinePrecision]], N[(1.0 * z), $MachinePrecision], N[(x * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.25 \cdot 10^{-66} \lor \neg \left(z \leq 4.4 \cdot 10^{-201}\right):\\
\;\;\;\;1 \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if z < -1.2499999999999999e-66 or 4.4e-201 < z Initial program 99.8%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in z around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-sin.f64N/A
lower-cos.f6498.3
Applied rewrites98.3%
Taylor expanded in y around 0
Applied rewrites46.4%
if -1.2499999999999999e-66 < z < 4.4e-201Initial program 99.8%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6455.3
Applied rewrites55.3%
Taylor expanded in x around inf
Applied rewrites40.4%
Final simplification44.5%
(FPCore (x y z) :precision binary64 (fma y x z))
double code(double x, double y, double z) {
return fma(y, x, z);
}
function code(x, y, z) return fma(y, x, z) end
code[x_, y_, z_] := N[(y * x + z), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, z\right)
\end{array}
Initial program 99.8%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6453.3
Applied rewrites53.3%
Final simplification53.3%
(FPCore (x y z) :precision binary64 (* x y))
double code(double x, double y, double z) {
return x * 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
end function
public static double code(double x, double y, double z) {
return x * y;
}
def code(x, y, z): return x * y
function code(x, y, z) return Float64(x * y) end
function tmp = code(x, y, z) tmp = x * y; end
code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y
\end{array}
Initial program 99.8%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6453.3
Applied rewrites53.3%
Taylor expanded in x around inf
Applied rewrites19.2%
Final simplification19.2%
herbie shell --seed 2024326
(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))))