
(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 12 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 (cos b) (cos a) (* (/ (- -1.0) (/ -1.0 (sin b))) (sin a))))))
double code(double r, double a, double b) {
return r * (sin(b) / fma(cos(b), cos(a), ((-(-1.0) / (-1.0 / sin(b))) * sin(a))));
}
function code(r, a, b) return Float64(r * Float64(sin(b) / fma(cos(b), cos(a), Float64(Float64(Float64(-(-1.0)) / Float64(-1.0 / sin(b))) * sin(a))))) end
code[r_, a_, b_] := N[(r * N[(N[Sin[b], $MachinePrecision] / N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision] + N[(N[((--1.0) / N[(-1.0 / N[Sin[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Sin[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\sin b}{\mathsf{fma}\left(\cos b, \cos a, \frac{--1}{\frac{-1}{\sin b}} \cdot \sin a\right)}
\end{array}
Initial program 76.8%
lift-cos.f64N/A
lift-+.f64N/A
cos-sumN/A
sub-negN/A
*-commutativeN/A
lower-fma.f64N/A
lower-cos.f64N/A
lower-cos.f64N/A
lift-sin.f64N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f64N/A
lower-sin.f6499.5
Applied rewrites99.5%
remove-double-divN/A
unpow-1N/A
lift-pow.f64N/A
frac-2negN/A
metadata-evalN/A
lower-/.f64N/A
lift-pow.f64N/A
unpow-1N/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6499.5
Applied rewrites99.5%
Final simplification99.5%
(FPCore (r a b)
:precision binary64
(let* ((t_0 (/ (sin b) (cos (+ a b)))))
(if (or (<= t_0 -0.0002) (not (<= t_0 1e-7)))
(* (/ r (cos b)) (sin b))
(* r (* (pow (cos a) -1.0) b)))))
double code(double r, double a, double b) {
double t_0 = sin(b) / cos((a + b));
double tmp;
if ((t_0 <= -0.0002) || !(t_0 <= 1e-7)) {
tmp = (r / cos(b)) * sin(b);
} else {
tmp = r * (pow(cos(a), -1.0) * b);
}
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 = sin(b) / cos((a + b))
if ((t_0 <= (-0.0002d0)) .or. (.not. (t_0 <= 1d-7))) then
tmp = (r / cos(b)) * sin(b)
else
tmp = r * ((cos(a) ** (-1.0d0)) * b)
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 tmp;
if ((t_0 <= -0.0002) || !(t_0 <= 1e-7)) {
tmp = (r / Math.cos(b)) * Math.sin(b);
} else {
tmp = r * (Math.pow(Math.cos(a), -1.0) * b);
}
return tmp;
}
def code(r, a, b): t_0 = math.sin(b) / math.cos((a + b)) tmp = 0 if (t_0 <= -0.0002) or not (t_0 <= 1e-7): tmp = (r / math.cos(b)) * math.sin(b) else: tmp = r * (math.pow(math.cos(a), -1.0) * b) return tmp
function code(r, a, b) t_0 = Float64(sin(b) / cos(Float64(a + b))) tmp = 0.0 if ((t_0 <= -0.0002) || !(t_0 <= 1e-7)) tmp = Float64(Float64(r / cos(b)) * sin(b)); else tmp = Float64(r * Float64((cos(a) ^ -1.0) * b)); end return tmp end
function tmp_2 = code(r, a, b) t_0 = sin(b) / cos((a + b)); tmp = 0.0; if ((t_0 <= -0.0002) || ~((t_0 <= 1e-7))) tmp = (r / cos(b)) * sin(b); else tmp = r * ((cos(a) ^ -1.0) * b); 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]}, If[Or[LessEqual[t$95$0, -0.0002], N[Not[LessEqual[t$95$0, 1e-7]], $MachinePrecision]], N[(N[(r / N[Cos[b], $MachinePrecision]), $MachinePrecision] * N[Sin[b], $MachinePrecision]), $MachinePrecision], N[(r * N[(N[Power[N[Cos[a], $MachinePrecision], -1.0], $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sin b}{\cos \left(a + b\right)}\\
\mathbf{if}\;t\_0 \leq -0.0002 \lor \neg \left(t\_0 \leq 10^{-7}\right):\\
\;\;\;\;\frac{r}{\cos b} \cdot \sin b\\
\mathbf{else}:\\
\;\;\;\;r \cdot \left({\cos a}^{-1} \cdot b\right)\\
\end{array}
\end{array}
if (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) < -2.0000000000000001e-4 or 9.9999999999999995e-8 < (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) Initial program 55.5%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower-cos.f64N/A
lower-sin.f6455.9
Applied rewrites55.9%
if -2.0000000000000001e-4 < (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) < 9.9999999999999995e-8Initial program 99.1%
Taylor expanded in b around 0
lower-/.f64N/A
lower-cos.f6499.1
Applied rewrites99.1%
Applied rewrites99.2%
Final simplification77.0%
(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(r * Float64(sin(b) / fma(sin(b), Float64(-sin(a)), Float64(cos(b) * cos(a))))) end
code[r_, a_, b_] := N[(r * 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]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\sin b}{\mathsf{fma}\left(\sin b, -\sin a, \cos b \cdot \cos a\right)}
\end{array}
Initial program 76.8%
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%
(FPCore (r a b) :precision binary64 (* r (/ (sin b) (fma (cos b) (cos a) (* (- (sin b)) (sin a))))))
double code(double r, double a, double b) {
return r * (sin(b) / fma(cos(b), cos(a), (-sin(b) * sin(a))));
}
function code(r, a, b) return Float64(r * Float64(sin(b) / fma(cos(b), cos(a), Float64(Float64(-sin(b)) * sin(a))))) end
code[r_, a_, b_] := N[(r * N[(N[Sin[b], $MachinePrecision] / N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision] + N[((-N[Sin[b], $MachinePrecision]) * N[Sin[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\sin b}{\mathsf{fma}\left(\cos b, \cos a, \left(-\sin b\right) \cdot \sin a\right)}
\end{array}
Initial program 76.8%
lift-cos.f64N/A
lift-+.f64N/A
cos-sumN/A
sub-negN/A
*-commutativeN/A
lower-fma.f64N/A
lower-cos.f64N/A
lower-cos.f64N/A
lift-sin.f64N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f64N/A
lower-sin.f6499.5
Applied rewrites99.5%
(FPCore (r a b) :precision binary64 (/ (* (sin b) r) (fma (- (sin a)) (sin b) (* (cos b) (cos a)))))
double code(double r, double a, double b) {
return (sin(b) * r) / fma(-sin(a), sin(b), (cos(b) * cos(a)));
}
function code(r, a, b) return Float64(Float64(sin(b) * r) / fma(Float64(-sin(a)), sin(b), Float64(cos(b) * cos(a)))) end
code[r_, a_, b_] := N[(N[(N[Sin[b], $MachinePrecision] * r), $MachinePrecision] / N[((-N[Sin[a], $MachinePrecision]) * N[Sin[b], $MachinePrecision] + N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin b \cdot r}{\mathsf{fma}\left(-\sin a, \sin b, \cos b \cdot \cos a\right)}
\end{array}
Initial program 76.8%
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%
Taylor expanded in r around 0
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-sin.f64N/A
mul-1-negN/A
distribute-lft-neg-inN/A
sin-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sin-negN/A
lower-neg.f64N/A
lower-sin.f64N/A
lower-sin.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6499.5
Applied rewrites99.5%
(FPCore (r a b) :precision binary64 (* (/ r (cos (+ a b))) (sin b)))
double code(double r, double a, double b) {
return (r / cos((a + b))) * sin(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 / cos((a + b))) * sin(b)
end function
public static double code(double r, double a, double b) {
return (r / Math.cos((a + b))) * Math.sin(b);
}
def code(r, a, b): return (r / math.cos((a + b))) * math.sin(b)
function code(r, a, b) return Float64(Float64(r / cos(Float64(a + b))) * sin(b)) end
function tmp = code(r, a, b) tmp = (r / cos((a + b))) * sin(b); end
code[r_, a_, b_] := N[(N[(r / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[b], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{r}{\cos \left(a + b\right)} \cdot \sin b
\end{array}
Initial program 76.8%
lift-*.f64N/A
lift-/.f64N/A
clear-numN/A
associate-/r/N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
associate-*l/N/A
*-lft-identityN/A
lower-/.f6476.8
Applied rewrites76.8%
(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}
Initial program 76.8%
(FPCore (r a b) :precision binary64 (/ (/ r (cos (+ a b))) (/ (fma (* b b) 0.16666666666666666 1.0) b)))
double code(double r, double a, double b) {
return (r / cos((a + b))) / (fma((b * b), 0.16666666666666666, 1.0) / b);
}
function code(r, a, b) return Float64(Float64(r / cos(Float64(a + b))) / Float64(fma(Float64(b * b), 0.16666666666666666, 1.0) / b)) end
code[r_, a_, b_] := N[(N[(r / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(N[(N[(b * b), $MachinePrecision] * 0.16666666666666666 + 1.0), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{r}{\cos \left(a + b\right)}}{\frac{\mathsf{fma}\left(b \cdot b, 0.16666666666666666, 1\right)}{b}}
\end{array}
Initial program 76.8%
lift-*.f64N/A
lift-/.f64N/A
clear-numN/A
un-div-invN/A
div-invN/A
associate-/r*N/A
*-lft-identityN/A
associate-*l/N/A
lower-/.f64N/A
associate-*l/N/A
*-lft-identityN/A
lower-/.f64N/A
inv-powN/A
lower-pow.f6476.6
Applied rewrites76.6%
Taylor expanded in b around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6451.4
Applied rewrites51.4%
(FPCore (r a b) :precision binary64 (/ (/ r (cos a)) (/ (fma (* b b) 0.16666666666666666 1.0) b)))
double code(double r, double a, double b) {
return (r / cos(a)) / (fma((b * b), 0.16666666666666666, 1.0) / b);
}
function code(r, a, b) return Float64(Float64(r / cos(a)) / Float64(fma(Float64(b * b), 0.16666666666666666, 1.0) / b)) end
code[r_, a_, b_] := N[(N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision] / N[(N[(N[(b * b), $MachinePrecision] * 0.16666666666666666 + 1.0), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{r}{\cos a}}{\frac{\mathsf{fma}\left(b \cdot b, 0.16666666666666666, 1\right)}{b}}
\end{array}
Initial program 76.8%
lift-*.f64N/A
lift-/.f64N/A
clear-numN/A
un-div-invN/A
div-invN/A
associate-/r*N/A
*-lft-identityN/A
associate-*l/N/A
lower-/.f64N/A
associate-*l/N/A
*-lft-identityN/A
lower-/.f64N/A
inv-powN/A
lower-pow.f6476.6
Applied rewrites76.6%
Taylor expanded in b around 0
lower-/.f64N/A
lower-cos.f6454.4
Applied rewrites54.4%
Taylor expanded in b around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6451.0
Applied rewrites51.0%
(FPCore (r a b) :precision binary64 (* r (/ (fma (* -0.16666666666666666 (* b b)) b b) (cos (+ a b)))))
double code(double r, double a, double b) {
return r * (fma((-0.16666666666666666 * (b * b)), b, b) / cos((a + b)));
}
function code(r, a, b) return Float64(r * Float64(fma(Float64(-0.16666666666666666 * Float64(b * b)), b, b) / cos(Float64(a + b)))) end
code[r_, a_, b_] := N[(r * N[(N[(N[(-0.16666666666666666 * N[(b * b), $MachinePrecision]), $MachinePrecision] * b + b), $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\mathsf{fma}\left(-0.16666666666666666 \cdot \left(b \cdot b\right), b, b\right)}{\cos \left(a + b\right)}
\end{array}
Initial program 76.8%
Taylor expanded in b around 0
+-commutativeN/A
distribute-lft-inN/A
*-commutativeN/A
associate-*r*N/A
*-rgt-identityN/A
lower-fma.f64N/A
unpow2N/A
cube-unmultN/A
lower-pow.f6450.8
Applied rewrites50.8%
Applied rewrites50.8%
(FPCore (r a b) :precision binary64 (* (/ r (cos a)) b))
double code(double r, double a, double b) {
return (r / 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 / cos(a)) * b
end function
public static double code(double r, double a, double b) {
return (r / Math.cos(a)) * b;
}
def code(r, a, b): return (r / math.cos(a)) * b
function code(r, a, b) return Float64(Float64(r / cos(a)) * b) end
function tmp = code(r, a, b) tmp = (r / cos(a)) * b; end
code[r_, a_, b_] := N[(N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision]
\begin{array}{l}
\\
\frac{r}{\cos a} \cdot b
\end{array}
Initial program 76.8%
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%
Taylor expanded in b around 0
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lower-/.f64N/A
lower-cos.f6450.8
Applied rewrites50.8%
(FPCore (r a b) :precision binary64 (* b r))
double code(double r, double a, double b) {
return 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 = b * r
end function
public static double code(double r, double a, double b) {
return b * r;
}
def code(r, a, b): return b * r
function code(r, a, b) return Float64(b * r) end
function tmp = code(r, a, b) tmp = b * r; end
code[r_, a_, b_] := N[(b * r), $MachinePrecision]
\begin{array}{l}
\\
b \cdot r
\end{array}
Initial program 76.8%
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%
Taylor expanded in b around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
lower-cos.f64N/A
lower-/.f64N/A
lower-cos.f6450.8
Applied rewrites50.8%
Taylor expanded in a around 0
Applied rewrites36.2%
herbie shell --seed 2024323
(FPCore (r a b)
:name "rsin B (should all be same)"
:precision binary64
(* r (/ (sin b) (cos (+ a b)))))