
(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 11 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 a) (cos b))))))
double code(double r, double a, double b) {
return r * (sin(b) / fma(sin(-b), sin(a), (cos(a) * cos(b))));
}
function code(r, a, b) return Float64(r * Float64(sin(b) / fma(sin(Float64(-b)), sin(a), Float64(cos(a) * cos(b))))) end
code[r_, a_, b_] := N[(r * N[(N[Sin[b], $MachinePrecision] / N[(N[Sin[(-b)], $MachinePrecision] * N[Sin[a], $MachinePrecision] + N[(N[Cos[a], $MachinePrecision] * N[Cos[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
r \cdot \frac{\sin b}{\mathsf{fma}\left(\sin \left(-b\right), \sin a, \cos a \cdot \cos b\right)}
\end{array}
Initial program 78.8%
lift-cos.f64N/A
lift-+.f64N/A
cos-sumN/A
sub-negN/A
+-commutativeN/A
lift-sin.f64N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lift-sin.f64N/A
sin-negN/A
lower-sin.f64N/A
lower-neg.f64N/A
lower-sin.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6499.6
Applied rewrites99.6%
(FPCore (r a b) :precision binary64 (let* ((t_0 (/ (sin b) (cos (+ b a)))) (t_1 (/ (* r (sin b)) (cos b)))) (if (<= t_0 -0.002) t_1 (if (<= t_0 2e-11) (* b (/ r (cos a))) t_1))))
double code(double r, double a, double b) {
double t_0 = sin(b) / cos((b + a));
double t_1 = (r * sin(b)) / cos(b);
double tmp;
if (t_0 <= -0.002) {
tmp = t_1;
} else if (t_0 <= 2e-11) {
tmp = b * (r / cos(a));
} 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((b + a))
t_1 = (r * sin(b)) / cos(b)
if (t_0 <= (-0.002d0)) then
tmp = t_1
else if (t_0 <= 2d-11) then
tmp = b * (r / cos(a))
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((b + a));
double t_1 = (r * Math.sin(b)) / Math.cos(b);
double tmp;
if (t_0 <= -0.002) {
tmp = t_1;
} else if (t_0 <= 2e-11) {
tmp = b * (r / Math.cos(a));
} else {
tmp = t_1;
}
return tmp;
}
def code(r, a, b): t_0 = math.sin(b) / math.cos((b + a)) t_1 = (r * math.sin(b)) / math.cos(b) tmp = 0 if t_0 <= -0.002: tmp = t_1 elif t_0 <= 2e-11: tmp = b * (r / math.cos(a)) else: tmp = t_1 return tmp
function code(r, a, b) t_0 = Float64(sin(b) / cos(Float64(b + a))) t_1 = Float64(Float64(r * sin(b)) / cos(b)) tmp = 0.0 if (t_0 <= -0.002) tmp = t_1; elseif (t_0 <= 2e-11) tmp = Float64(b * Float64(r / cos(a))); else tmp = t_1; end return tmp end
function tmp_2 = code(r, a, b) t_0 = sin(b) / cos((b + a)); t_1 = (r * sin(b)) / cos(b); tmp = 0.0; if (t_0 <= -0.002) tmp = t_1; elseif (t_0 <= 2e-11) tmp = b * (r / cos(a)); 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[(b + a), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(r * N[Sin[b], $MachinePrecision]), $MachinePrecision] / N[Cos[b], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.002], t$95$1, If[LessEqual[t$95$0, 2e-11], N[(b * N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sin b}{\cos \left(b + a\right)}\\
t_1 := \frac{r \cdot \sin b}{\cos b}\\
\mathbf{if}\;t\_0 \leq -0.002:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{-11}:\\
\;\;\;\;b \cdot \frac{r}{\cos a}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) < -2e-3 or 1.99999999999999988e-11 < (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) Initial program 54.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-*.f64N/A
lower-sin.f64N/A
lower-cos.f6453.5
Applied rewrites53.5%
if -2e-3 < (/.f64 (sin.f64 b) (cos.f64 (+.f64 a b))) < 1.99999999999999988e-11Initial program 99.3%
Taylor expanded in b around 0
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
lower-cos.f6499.1
Applied rewrites99.1%
Final simplification76.3%
herbie shell --seed 2024225
(FPCore (r a b)
:name "rsin B (should all be same)"
:precision binary64
(* r (/ (sin b) (cos (+ a b)))))