
(FPCore (r a b) :precision binary64 (* r (/ (sin b) (cos (+ a b)))))
double code(double r, double a, double b) {
return r * (sin(b) / cos((a + b)));
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
code = r * (sin(b) / cos((a + b)))
end function
public static double code(double r, double a, double b) {
return r * (Math.sin(b) / Math.cos((a + b)));
}
def code(r, a, b): return r * (math.sin(b) / math.cos((a + b)))
function code(r, a, b) return Float64(r * Float64(sin(b) / cos(Float64(a + b)))) end
function tmp = code(r, a, b) tmp = r * (sin(b) / cos((a + b))); end
code[r_, a_, b_] := N[(r * N[(N[Sin[b], $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\sin b}{\cos \left(a + b\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (r a b) :precision binary64 (* r (/ (sin b) (cos (+ a b)))))
double code(double r, double a, double b) {
return r * (sin(b) / cos((a + b)));
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
code = r * (sin(b) / cos((a + b)))
end function
public static double code(double r, double a, double b) {
return r * (Math.sin(b) / Math.cos((a + b)));
}
def code(r, a, b): return r * (math.sin(b) / math.cos((a + b)))
function code(r, a, b) return Float64(r * Float64(sin(b) / cos(Float64(a + b)))) end
function tmp = code(r, a, b) tmp = r * (sin(b) / cos((a + b))); end
code[r_, a_, b_] := N[(r * N[(N[Sin[b], $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\sin b}{\cos \left(a + b\right)}
\end{array}
(FPCore (r a b) :precision binary64 (/ (* r (sin b)) (fma (sin b) (- (sin a)) (* (cos b) (cos a)))))
double code(double r, double a, double b) {
return (r * sin(b)) / fma(sin(b), -sin(a), (cos(b) * cos(a)));
}
function code(r, a, b) return Float64(Float64(r * sin(b)) / fma(sin(b), Float64(-sin(a)), Float64(cos(b) * cos(a)))) end
code[r_, a_, b_] := N[(N[(r * N[Sin[b], $MachinePrecision]), $MachinePrecision] / N[(N[Sin[b], $MachinePrecision] * (-N[Sin[a], $MachinePrecision]) + N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{r \cdot \sin b}{\mathsf{fma}\left(\sin b, -\sin a, \cos b \cdot \cos a\right)}
\end{array}
Initial program 77.4%
lift-*.f64N/A
lift-/.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6477.4
Applied rewrites77.4%
lift-cos.f64N/A
lift-+.f64N/A
+-commutativeN/A
cos-sumN/A
lift-cos.f64N/A
lift-cos.f64N/A
lift-*.f64N/A
unsub-negN/A
lift-sin.f64N/A
lift-sin.f64N/A
distribute-rgt-neg-outN/A
lift-neg.f64N/A
+-commutativeN/A
lift-fma.f6499.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.6
Applied rewrites99.6%
Final simplification99.6%
(FPCore (r a b) :precision binary64 (let* ((t_0 (/ (sin b) (cos (+ a b)))) (t_1 (* (tan b) r))) (if (<= t_0 -0.02) t_1 (if (<= t_0 2e-13) (* (/ r (cos a)) b) t_1))))
double code(double r, double a, double b) {
double t_0 = sin(b) / cos((a + b));
double t_1 = tan(b) * r;
double tmp;
if (t_0 <= -0.02) {
tmp = t_1;
} else if (t_0 <= 2e-13) {
tmp = (r / cos(a)) * b;
} else {
tmp = t_1;
}
return tmp;
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = sin(b) / cos((a + b))
t_1 = tan(b) * r
if (t_0 <= (-0.02d0)) then
tmp = t_1
else if (t_0 <= 2d-13) then
tmp = (r / cos(a)) * b
else
tmp = t_1
end if
code = tmp
end function
public static double code(double r, double a, double b) {
double t_0 = Math.sin(b) / Math.cos((a + b));
double t_1 = Math.tan(b) * r;
double tmp;
if (t_0 <= -0.02) {
tmp = t_1;
} else if (t_0 <= 2e-13) {
tmp = (r / Math.cos(a)) * b;
} else {
tmp = t_1;
}
return tmp;
}
def code(r, a, b): t_0 = math.sin(b) / math.cos((a + b)) t_1 = math.tan(b) * r tmp = 0 if t_0 <= -0.02: tmp = t_1 elif t_0 <= 2e-13: tmp = (r / math.cos(a)) * b else: tmp = t_1 return tmp
function code(r, a, b) t_0 = Float64(sin(b) / cos(Float64(a + b))) t_1 = Float64(tan(b) * r) tmp = 0.0 if (t_0 <= -0.02) tmp = t_1; elseif (t_0 <= 2e-13) tmp = Float64(Float64(r / cos(a)) * b); else tmp = t_1; end return tmp end
function tmp_2 = code(r, a, b) t_0 = sin(b) / cos((a + b)); t_1 = tan(b) * r; tmp = 0.0; if (t_0 <= -0.02) tmp = t_1; elseif (t_0 <= 2e-13) tmp = (r / cos(a)) * b; else tmp = t_1; end tmp_2 = tmp; end
code[r_, a_, b_] := Block[{t$95$0 = N[(N[Sin[b], $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Tan[b], $MachinePrecision] * r), $MachinePrecision]}, If[LessEqual[t$95$0, -0.02], t$95$1, If[LessEqual[t$95$0, 2e-13], N[(N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sin b}{\cos \left(a + b\right)}\\
t_1 := \tan b \cdot r\\
\mathbf{if}\;t\_0 \leq -0.02:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{-13}:\\
\;\;\;\;\frac{r}{\cos a} \cdot b\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) < -0.0200000000000000004 or 2.0000000000000001e-13 < (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) Initial program 54.8%
lift-*.f64N/A
lift-/.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6454.8
Applied rewrites54.8%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-cos.f6454.9
Applied rewrites54.9%
Applied rewrites55.1%
if -0.0200000000000000004 < (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) < 2.0000000000000001e-13Initial program 99.8%
Taylor expanded in b around 0
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lower-/.f64N/A
lower-cos.f6499.7
Applied rewrites99.7%
(FPCore (r a b) :precision binary64 (/ (* r (sin b)) (fma (cos b) (cos a) (* (- (sin a)) (sin b)))))
double code(double r, double a, double b) {
return (r * sin(b)) / fma(cos(b), cos(a), (-sin(a) * sin(b)));
}
function code(r, a, b) return Float64(Float64(r * sin(b)) / fma(cos(b), cos(a), Float64(Float64(-sin(a)) * sin(b)))) end
code[r_, a_, b_] := N[(N[(r * N[Sin[b], $MachinePrecision]), $MachinePrecision] / N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision] + N[((-N[Sin[a], $MachinePrecision]) * N[Sin[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{r \cdot \sin b}{\mathsf{fma}\left(\cos b, \cos a, \left(-\sin a\right) \cdot \sin b\right)}
\end{array}
Initial program 77.4%
lift-*.f64N/A
lift-/.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6477.4
Applied rewrites77.4%
lift-cos.f64N/A
lift-+.f64N/A
+-commutativeN/A
cos-sumN/A
lift-cos.f64N/A
lift-cos.f64N/A
lift-*.f64N/A
unsub-negN/A
lift-sin.f64N/A
lift-sin.f64N/A
distribute-rgt-neg-outN/A
lift-neg.f64N/A
lift-*.f64N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6499.5
Applied rewrites99.5%
Final simplification99.5%
(FPCore (r a b) :precision binary64 (* (/ (sin b) (fma (sin b) (- (sin a)) (* (cos b) (cos a)))) r))
double code(double r, double a, double b) {
return (sin(b) / fma(sin(b), -sin(a), (cos(b) * cos(a)))) * r;
}
function code(r, a, b) return Float64(Float64(sin(b) / fma(sin(b), Float64(-sin(a)), Float64(cos(b) * cos(a)))) * r) end
code[r_, a_, b_] := N[(N[(N[Sin[b], $MachinePrecision] / N[(N[Sin[b], $MachinePrecision] * (-N[Sin[a], $MachinePrecision]) + N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * r), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin b}{\mathsf{fma}\left(\sin b, -\sin a, \cos b \cdot \cos a\right)} \cdot r
\end{array}
Initial program 77.4%
lift-cos.f64N/A
lift-+.f64N/A
cos-sumN/A
sub-negN/A
+-commutativeN/A
lift-sin.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-neg.f64N/A
lower-sin.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6499.5
Applied rewrites99.5%
Final simplification99.5%
(FPCore (r a b) :precision binary64 (* (/ (sin b) (- (* (cos b) (cos a)) (* (sin a) (sin b)))) r))
double code(double r, double a, double b) {
return (sin(b) / ((cos(b) * cos(a)) - (sin(a) * sin(b)))) * r;
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (sin(b) / ((cos(b) * cos(a)) - (sin(a) * sin(b)))) * r
end function
public static double code(double r, double a, double b) {
return (Math.sin(b) / ((Math.cos(b) * Math.cos(a)) - (Math.sin(a) * Math.sin(b)))) * r;
}
def code(r, a, b): return (math.sin(b) / ((math.cos(b) * math.cos(a)) - (math.sin(a) * math.sin(b)))) * r
function code(r, a, b) return Float64(Float64(sin(b) / Float64(Float64(cos(b) * cos(a)) - Float64(sin(a) * sin(b)))) * r) end
function tmp = code(r, a, b) tmp = (sin(b) / ((cos(b) * cos(a)) - (sin(a) * sin(b)))) * r; end
code[r_, a_, b_] := N[(N[(N[Sin[b], $MachinePrecision] / N[(N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[a], $MachinePrecision] * N[Sin[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * r), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin b}{\cos b \cdot \cos a - \sin a \cdot \sin b} \cdot r
\end{array}
Initial program 77.4%
lift-cos.f64N/A
lift-+.f64N/A
cos-sumN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f64N/A
lift-sin.f64N/A
lower-*.f64N/A
lower-sin.f6499.5
Applied rewrites99.5%
Final simplification99.5%
(FPCore (r a b) :precision binary64 (* (/ (sin b) (cos (+ a b))) r))
double code(double r, double a, double b) {
return (sin(b) / cos((a + b))) * r;
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (sin(b) / cos((a + b))) * r
end function
public static double code(double r, double a, double b) {
return (Math.sin(b) / Math.cos((a + b))) * r;
}
def code(r, a, b): return (math.sin(b) / math.cos((a + b))) * r
function code(r, a, b) return Float64(Float64(sin(b) / cos(Float64(a + b))) * r) end
function tmp = code(r, a, b) tmp = (sin(b) / cos((a + b))) * r; end
code[r_, a_, b_] := N[(N[(N[Sin[b], $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * r), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin b}{\cos \left(a + b\right)} \cdot r
\end{array}
Initial program 77.4%
Final simplification77.4%
(FPCore (r a b) :precision binary64 (let* ((t_0 (* (tan b) r))) (if (<= b -6e-6) t_0 (if (<= b 1.04e-7) (* (/ b (cos a)) r) t_0))))
double code(double r, double a, double b) {
double t_0 = tan(b) * r;
double tmp;
if (b <= -6e-6) {
tmp = t_0;
} else if (b <= 1.04e-7) {
tmp = (b / cos(a)) * r;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: t_0
real(8) :: tmp
t_0 = tan(b) * r
if (b <= (-6d-6)) then
tmp = t_0
else if (b <= 1.04d-7) then
tmp = (b / cos(a)) * r
else
tmp = t_0
end if
code = tmp
end function
public static double code(double r, double a, double b) {
double t_0 = Math.tan(b) * r;
double tmp;
if (b <= -6e-6) {
tmp = t_0;
} else if (b <= 1.04e-7) {
tmp = (b / Math.cos(a)) * r;
} else {
tmp = t_0;
}
return tmp;
}
def code(r, a, b): t_0 = math.tan(b) * r tmp = 0 if b <= -6e-6: tmp = t_0 elif b <= 1.04e-7: tmp = (b / math.cos(a)) * r else: tmp = t_0 return tmp
function code(r, a, b) t_0 = Float64(tan(b) * r) tmp = 0.0 if (b <= -6e-6) tmp = t_0; elseif (b <= 1.04e-7) tmp = Float64(Float64(b / cos(a)) * r); else tmp = t_0; end return tmp end
function tmp_2 = code(r, a, b) t_0 = tan(b) * r; tmp = 0.0; if (b <= -6e-6) tmp = t_0; elseif (b <= 1.04e-7) tmp = (b / cos(a)) * r; else tmp = t_0; end tmp_2 = tmp; end
code[r_, a_, b_] := Block[{t$95$0 = N[(N[Tan[b], $MachinePrecision] * r), $MachinePrecision]}, If[LessEqual[b, -6e-6], t$95$0, If[LessEqual[b, 1.04e-7], N[(N[(b / N[Cos[a], $MachinePrecision]), $MachinePrecision] * r), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \tan b \cdot r\\
\mathbf{if}\;b \leq -6 \cdot 10^{-6}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;b \leq 1.04 \cdot 10^{-7}:\\
\;\;\;\;\frac{b}{\cos a} \cdot r\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if b < -6.0000000000000002e-6 or 1.04e-7 < b Initial program 54.8%
lift-*.f64N/A
lift-/.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6454.8
Applied rewrites54.8%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-cos.f6454.9
Applied rewrites54.9%
Applied rewrites55.1%
if -6.0000000000000002e-6 < b < 1.04e-7Initial program 99.8%
Taylor expanded in b around 0
lower-/.f64N/A
lower-cos.f6499.8
Applied rewrites99.8%
Final simplification77.6%
(FPCore (r a b) :precision binary64 (* (tan b) r))
double code(double r, double a, double b) {
return tan(b) * r;
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
code = tan(b) * r
end function
public static double code(double r, double a, double b) {
return Math.tan(b) * r;
}
def code(r, a, b): return math.tan(b) * r
function code(r, a, b) return Float64(tan(b) * r) end
function tmp = code(r, a, b) tmp = tan(b) * r; end
code[r_, a_, b_] := N[(N[Tan[b], $MachinePrecision] * r), $MachinePrecision]
\begin{array}{l}
\\
\tan b \cdot r
\end{array}
Initial program 77.4%
lift-*.f64N/A
lift-/.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6477.4
Applied rewrites77.4%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-cos.f6457.8
Applied rewrites57.8%
Applied rewrites57.9%
(FPCore (r a b) :precision binary64 (* r b))
double code(double r, double a, double b) {
return r * b;
}
real(8) function code(r, a, b)
real(8), intent (in) :: r
real(8), intent (in) :: a
real(8), intent (in) :: b
code = r * b
end function
public static double code(double r, double a, double b) {
return r * b;
}
def code(r, a, b): return r * b
function code(r, a, b) return Float64(r * b) end
function tmp = code(r, a, b) tmp = r * b; end
code[r_, a_, b_] := N[(r * b), $MachinePrecision]
\begin{array}{l}
\\
r \cdot b
\end{array}
Initial program 77.4%
Taylor expanded in b around 0
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lower-/.f64N/A
lower-cos.f6452.8
Applied rewrites52.8%
Taylor expanded in a around 0
Applied rewrites33.0%
Final simplification33.0%
herbie shell --seed 2024235
(FPCore (r a b)
:name "rsin B (should all be same)"
:precision binary64
(* r (/ (sin b) (cos (+ a b)))))