
(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(Float64(r * 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[(N[(r * N[Sin[b], $MachinePrecision]), $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{r \cdot \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(Float64(r * 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[(N[(r * N[Sin[b], $MachinePrecision]), $MachinePrecision] / N[Cos[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{r \cdot \sin b}{\cos \left(a + b\right)}
\end{array}
(FPCore (r a b) :precision binary64 (* (/ r (fma (sin (- b)) (sin a) (* (cos a) (cos b)))) (sin b)))
double code(double r, double a, double b) {
return (r / fma(sin(-b), sin(a), (cos(a) * cos(b)))) * sin(b);
}
function code(r, a, b) return Float64(Float64(r / fma(sin(Float64(-b)), sin(a), Float64(cos(a) * cos(b)))) * sin(b)) end
code[r_, a_, b_] := N[(N[(r / N[(N[Sin[(-b)], $MachinePrecision] * N[Sin[a], $MachinePrecision] + N[(N[Cos[a], $MachinePrecision] * N[Cos[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Sin[b], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{r}{\mathsf{fma}\left(\sin \left(-b\right), \sin a, \cos a \cdot \cos b\right)} \cdot \sin b
\end{array}
Initial program 78.2%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6478.3
lift-+.f64N/A
+-commutativeN/A
lower-+.f6478.3
Applied rewrites78.3%
lift-cos.f64N/A
lift-+.f64N/A
cos-sumN/A
lift-cos.f64N/A
lift-cos.f64N/A
*-commutativeN/A
lift-*.f64N/A
lift-sin.f64N/A
lift-sin.f64N/A
cancel-sign-sub-invN/A
lift-sin.f64N/A
sin-negN/A
lift-neg.f64N/A
lift-sin.f64N/A
+-commutativeN/A
lift-fma.f6499.5
Applied rewrites99.5%
(FPCore (r a b) :precision binary64 (* (sin b) (/ r (- (* (cos a) (cos b)) (* (sin a) (sin b))))))
double code(double r, double a, double b) {
return sin(b) * (r / ((cos(a) * cos(b)) - (sin(a) * 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 = sin(b) * (r / ((cos(a) * cos(b)) - (sin(a) * sin(b))))
end function
public static double code(double r, double a, double b) {
return Math.sin(b) * (r / ((Math.cos(a) * Math.cos(b)) - (Math.sin(a) * Math.sin(b))));
}
def code(r, a, b): return math.sin(b) * (r / ((math.cos(a) * math.cos(b)) - (math.sin(a) * math.sin(b))))
function code(r, a, b) return Float64(sin(b) * Float64(r / Float64(Float64(cos(a) * cos(b)) - Float64(sin(a) * sin(b))))) end
function tmp = code(r, a, b) tmp = sin(b) * (r / ((cos(a) * cos(b)) - (sin(a) * sin(b)))); end
code[r_, a_, b_] := N[(N[Sin[b], $MachinePrecision] * N[(r / N[(N[(N[Cos[a], $MachinePrecision] * N[Cos[b], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[a], $MachinePrecision] * N[Sin[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin b \cdot \frac{r}{\cos a \cdot \cos b - \sin a \cdot \sin b}
\end{array}
Initial program 78.2%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6478.3
lift-+.f64N/A
+-commutativeN/A
lower-+.f6478.3
Applied rewrites78.3%
lift-cos.f64N/A
lift-+.f64N/A
+-commutativeN/A
cos-sumN/A
lift-cos.f64N/A
lift-cos.f64N/A
lift-*.f64N/A
lower--.f64N/A
lift-sin.f64N/A
lift-sin.f64N/A
*-commutativeN/A
lower-*.f6499.5
Applied rewrites99.5%
Final simplification99.5%
(FPCore (r a b) :precision binary64 (* (sin b) (/ r (fma (cos b) (cos a) (* (sin a) (sin b))))))
double code(double r, double a, double b) {
return sin(b) * (r / fma(cos(b), cos(a), (sin(a) * sin(b))));
}
function code(r, a, b) return Float64(sin(b) * Float64(r / fma(cos(b), cos(a), Float64(sin(a) * sin(b))))) end
code[r_, a_, b_] := N[(N[Sin[b], $MachinePrecision] * N[(r / N[(N[Cos[b], $MachinePrecision] * N[Cos[a], $MachinePrecision] + N[(N[Sin[a], $MachinePrecision] * N[Sin[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin b \cdot \frac{r}{\mathsf{fma}\left(\cos b, \cos a, \sin a \cdot \sin b\right)}
\end{array}
Initial program 78.2%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6478.3
lift-+.f64N/A
+-commutativeN/A
lower-+.f6478.3
Applied rewrites78.3%
lift-cos.f64N/A
lift-+.f64N/A
+-commutativeN/A
cos-sumN/A
lift-cos.f64N/A
lift-cos.f64N/A
lift-*.f64N/A
lower--.f64N/A
lift-sin.f64N/A
lift-sin.f64N/A
*-commutativeN/A
lower-*.f6499.5
Applied rewrites99.5%
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
cancel-sign-sub-invN/A
neg-mul-1N/A
remove-double-divN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
pow-prod-downN/A
sqr-negN/A
neg-mul-1N/A
lift-/.f64N/A
div-invN/A
lift-/.f64N/A
neg-mul-1N/A
lift-/.f64N/A
div-invN/A
lift-/.f64N/A
pow-prod-downN/A
Applied rewrites78.4%
Final simplification78.4%
(FPCore (r a b) :precision binary64 (if (<= b -6.5e-5) (/ (* r (sin b)) (cos b)) (if (<= b 2e-20) (* b (/ r (cos a))) (* (sin b) (/ r (cos b))))))
double code(double r, double a, double b) {
double tmp;
if (b <= -6.5e-5) {
tmp = (r * sin(b)) / cos(b);
} else if (b <= 2e-20) {
tmp = b * (r / cos(a));
} else {
tmp = sin(b) * (r / cos(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) :: tmp
if (b <= (-6.5d-5)) then
tmp = (r * sin(b)) / cos(b)
else if (b <= 2d-20) then
tmp = b * (r / cos(a))
else
tmp = sin(b) * (r / cos(b))
end if
code = tmp
end function
public static double code(double r, double a, double b) {
double tmp;
if (b <= -6.5e-5) {
tmp = (r * Math.sin(b)) / Math.cos(b);
} else if (b <= 2e-20) {
tmp = b * (r / Math.cos(a));
} else {
tmp = Math.sin(b) * (r / Math.cos(b));
}
return tmp;
}
def code(r, a, b): tmp = 0 if b <= -6.5e-5: tmp = (r * math.sin(b)) / math.cos(b) elif b <= 2e-20: tmp = b * (r / math.cos(a)) else: tmp = math.sin(b) * (r / math.cos(b)) return tmp
function code(r, a, b) tmp = 0.0 if (b <= -6.5e-5) tmp = Float64(Float64(r * sin(b)) / cos(b)); elseif (b <= 2e-20) tmp = Float64(b * Float64(r / cos(a))); else tmp = Float64(sin(b) * Float64(r / cos(b))); end return tmp end
function tmp_2 = code(r, a, b) tmp = 0.0; if (b <= -6.5e-5) tmp = (r * sin(b)) / cos(b); elseif (b <= 2e-20) tmp = b * (r / cos(a)); else tmp = sin(b) * (r / cos(b)); end tmp_2 = tmp; end
code[r_, a_, b_] := If[LessEqual[b, -6.5e-5], N[(N[(r * N[Sin[b], $MachinePrecision]), $MachinePrecision] / N[Cos[b], $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2e-20], N[(b * N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sin[b], $MachinePrecision] * N[(r / N[Cos[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -6.5 \cdot 10^{-5}:\\
\;\;\;\;\frac{r \cdot \sin b}{\cos b}\\
\mathbf{elif}\;b \leq 2 \cdot 10^{-20}:\\
\;\;\;\;b \cdot \frac{r}{\cos a}\\
\mathbf{else}:\\
\;\;\;\;\sin b \cdot \frac{r}{\cos b}\\
\end{array}
\end{array}
if b < -6.49999999999999943e-5Initial program 61.1%
Taylor expanded in a around 0
lower-cos.f6461.0
Applied rewrites61.0%
if -6.49999999999999943e-5 < b < 1.99999999999999989e-20Initial program 99.7%
Taylor expanded in b around 0
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6499.7
Applied rewrites99.7%
Applied rewrites99.8%
if 1.99999999999999989e-20 < b Initial program 60.3%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-cos.f6461.3
Applied rewrites61.3%
(FPCore (r a b) :precision binary64 (let* ((t_0 (* (sin b) (/ r (cos b))))) (if (<= b -6.5e-5) t_0 (if (<= b 2e-20) (* b (/ r (cos a))) t_0))))
double code(double r, double a, double b) {
double t_0 = sin(b) * (r / cos(b));
double tmp;
if (b <= -6.5e-5) {
tmp = t_0;
} else if (b <= 2e-20) {
tmp = b * (r / cos(a));
} 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 = sin(b) * (r / cos(b))
if (b <= (-6.5d-5)) then
tmp = t_0
else if (b <= 2d-20) then
tmp = b * (r / cos(a))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double r, double a, double b) {
double t_0 = Math.sin(b) * (r / Math.cos(b));
double tmp;
if (b <= -6.5e-5) {
tmp = t_0;
} else if (b <= 2e-20) {
tmp = b * (r / Math.cos(a));
} else {
tmp = t_0;
}
return tmp;
}
def code(r, a, b): t_0 = math.sin(b) * (r / math.cos(b)) tmp = 0 if b <= -6.5e-5: tmp = t_0 elif b <= 2e-20: tmp = b * (r / math.cos(a)) else: tmp = t_0 return tmp
function code(r, a, b) t_0 = Float64(sin(b) * Float64(r / cos(b))) tmp = 0.0 if (b <= -6.5e-5) tmp = t_0; elseif (b <= 2e-20) tmp = Float64(b * Float64(r / cos(a))); else tmp = t_0; end return tmp end
function tmp_2 = code(r, a, b) t_0 = sin(b) * (r / cos(b)); tmp = 0.0; if (b <= -6.5e-5) tmp = t_0; elseif (b <= 2e-20) tmp = b * (r / cos(a)); else tmp = t_0; end tmp_2 = tmp; end
code[r_, a_, b_] := Block[{t$95$0 = N[(N[Sin[b], $MachinePrecision] * N[(r / N[Cos[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -6.5e-5], t$95$0, If[LessEqual[b, 2e-20], N[(b * N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin b \cdot \frac{r}{\cos b}\\
\mathbf{if}\;b \leq -6.5 \cdot 10^{-5}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;b \leq 2 \cdot 10^{-20}:\\
\;\;\;\;b \cdot \frac{r}{\cos a}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if b < -6.49999999999999943e-5 or 1.99999999999999989e-20 < b Initial program 60.7%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-cos.f6461.2
Applied rewrites61.2%
if -6.49999999999999943e-5 < b < 1.99999999999999989e-20Initial program 99.7%
Taylor expanded in b around 0
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6499.7
Applied rewrites99.7%
Applied rewrites99.8%
(FPCore (r a b) :precision binary64 (* (sin b) (/ r (cos (+ b a)))))
double code(double r, double a, double b) {
return sin(b) * (r / cos((b + a)));
}
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) * (r / cos((b + a)))
end function
public static double code(double r, double a, double b) {
return Math.sin(b) * (r / Math.cos((b + a)));
}
def code(r, a, b): return math.sin(b) * (r / math.cos((b + a)))
function code(r, a, b) return Float64(sin(b) * Float64(r / cos(Float64(b + a)))) end
function tmp = code(r, a, b) tmp = sin(b) * (r / cos((b + a))); end
code[r_, a_, b_] := N[(N[Sin[b], $MachinePrecision] * N[(r / N[Cos[N[(b + a), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin b \cdot \frac{r}{\cos \left(b + a\right)}
\end{array}
Initial program 78.2%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6478.3
lift-+.f64N/A
+-commutativeN/A
lower-+.f6478.3
Applied rewrites78.3%
Final simplification78.3%
(FPCore (r a b)
:precision binary64
(let* ((t_0 (* (sin b) (/ r 1.0))))
(if (<= b -5.8e+20)
t_0
(if (<= b 5500000000000.0) (* b (/ r (cos a))) t_0))))
double code(double r, double a, double b) {
double t_0 = sin(b) * (r / 1.0);
double tmp;
if (b <= -5.8e+20) {
tmp = t_0;
} else if (b <= 5500000000000.0) {
tmp = b * (r / cos(a));
} 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 = sin(b) * (r / 1.0d0)
if (b <= (-5.8d+20)) then
tmp = t_0
else if (b <= 5500000000000.0d0) then
tmp = b * (r / cos(a))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double r, double a, double b) {
double t_0 = Math.sin(b) * (r / 1.0);
double tmp;
if (b <= -5.8e+20) {
tmp = t_0;
} else if (b <= 5500000000000.0) {
tmp = b * (r / Math.cos(a));
} else {
tmp = t_0;
}
return tmp;
}
def code(r, a, b): t_0 = math.sin(b) * (r / 1.0) tmp = 0 if b <= -5.8e+20: tmp = t_0 elif b <= 5500000000000.0: tmp = b * (r / math.cos(a)) else: tmp = t_0 return tmp
function code(r, a, b) t_0 = Float64(sin(b) * Float64(r / 1.0)) tmp = 0.0 if (b <= -5.8e+20) tmp = t_0; elseif (b <= 5500000000000.0) tmp = Float64(b * Float64(r / cos(a))); else tmp = t_0; end return tmp end
function tmp_2 = code(r, a, b) t_0 = sin(b) * (r / 1.0); tmp = 0.0; if (b <= -5.8e+20) tmp = t_0; elseif (b <= 5500000000000.0) tmp = b * (r / cos(a)); else tmp = t_0; end tmp_2 = tmp; end
code[r_, a_, b_] := Block[{t$95$0 = N[(N[Sin[b], $MachinePrecision] * N[(r / 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -5.8e+20], t$95$0, If[LessEqual[b, 5500000000000.0], N[(b * N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin b \cdot \frac{r}{1}\\
\mathbf{if}\;b \leq -5.8 \cdot 10^{+20}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;b \leq 5500000000000:\\
\;\;\;\;b \cdot \frac{r}{\cos a}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if b < -5.8e20 or 5.5e12 < b Initial program 58.7%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6458.7
lift-+.f64N/A
+-commutativeN/A
lower-+.f6458.7
Applied rewrites58.7%
Taylor expanded in b around 0
lower-cos.f6410.4
Applied rewrites10.4%
Taylor expanded in a around 0
Applied rewrites11.0%
if -5.8e20 < b < 5.5e12Initial program 99.7%
Taylor expanded in b around 0
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6496.7
Applied rewrites96.7%
Applied rewrites96.9%
Final simplification51.9%
(FPCore (r a b) :precision binary64 (* b (/ r (cos a))))
double code(double r, double a, double b) {
return b * (r / cos(a));
}
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 / cos(a))
end function
public static double code(double r, double a, double b) {
return b * (r / Math.cos(a));
}
def code(r, a, b): return b * (r / math.cos(a))
function code(r, a, b) return Float64(b * Float64(r / cos(a))) end
function tmp = code(r, a, b) tmp = b * (r / cos(a)); end
code[r_, a_, b_] := N[(b * N[(r / N[Cos[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
b \cdot \frac{r}{\cos a}
\end{array}
Initial program 78.2%
Taylor expanded in b around 0
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6447.8
Applied rewrites47.8%
Applied rewrites47.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 78.2%
Taylor expanded in b around 0
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f6447.8
Applied rewrites47.8%
Taylor expanded in a around 0
Applied rewrites34.4%
herbie shell --seed 2024233
(FPCore (r a b)
:name "rsin A (should all be same)"
:precision binary64
(/ (* r (sin b)) (cos (+ a b))))