
(FPCore (x) :precision binary64 (let* ((t_0 (* (tan x) (tan x)))) (/ (- 1.0 t_0) (+ 1.0 t_0))))
double code(double x) {
double t_0 = tan(x) * tan(x);
return (1.0 - t_0) / (1.0 + t_0);
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = tan(x) * tan(x)
code = (1.0d0 - t_0) / (1.0d0 + t_0)
end function
public static double code(double x) {
double t_0 = Math.tan(x) * Math.tan(x);
return (1.0 - t_0) / (1.0 + t_0);
}
def code(x): t_0 = math.tan(x) * math.tan(x) return (1.0 - t_0) / (1.0 + t_0)
function code(x) t_0 = Float64(tan(x) * tan(x)) return Float64(Float64(1.0 - t_0) / Float64(1.0 + t_0)) end
function tmp = code(x) t_0 = tan(x) * tan(x); tmp = (1.0 - t_0) / (1.0 + t_0); end
code[x_] := Block[{t$95$0 = N[(N[Tan[x], $MachinePrecision] * N[Tan[x], $MachinePrecision]), $MachinePrecision]}, N[(N[(1.0 - t$95$0), $MachinePrecision] / N[(1.0 + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \tan x \cdot \tan x\\
\frac{1 - t\_0}{1 + t\_0}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (let* ((t_0 (* (tan x) (tan x)))) (/ (- 1.0 t_0) (+ 1.0 t_0))))
double code(double x) {
double t_0 = tan(x) * tan(x);
return (1.0 - t_0) / (1.0 + t_0);
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = tan(x) * tan(x)
code = (1.0d0 - t_0) / (1.0d0 + t_0)
end function
public static double code(double x) {
double t_0 = Math.tan(x) * Math.tan(x);
return (1.0 - t_0) / (1.0 + t_0);
}
def code(x): t_0 = math.tan(x) * math.tan(x) return (1.0 - t_0) / (1.0 + t_0)
function code(x) t_0 = Float64(tan(x) * tan(x)) return Float64(Float64(1.0 - t_0) / Float64(1.0 + t_0)) end
function tmp = code(x) t_0 = tan(x) * tan(x); tmp = (1.0 - t_0) / (1.0 + t_0); end
code[x_] := Block[{t$95$0 = N[(N[Tan[x], $MachinePrecision] * N[Tan[x], $MachinePrecision]), $MachinePrecision]}, N[(N[(1.0 - t$95$0), $MachinePrecision] / N[(1.0 + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \tan x \cdot \tan x\\
\frac{1 - t\_0}{1 + t\_0}
\end{array}
\end{array}
(FPCore (x) :precision binary64 (let* ((t_0 (pow (tan x) 2.0))) (/ (+ -1.0 t_0) (- -1.0 t_0))))
double code(double x) {
double t_0 = pow(tan(x), 2.0);
return (-1.0 + t_0) / (-1.0 - t_0);
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = tan(x) ** 2.0d0
code = ((-1.0d0) + t_0) / ((-1.0d0) - t_0)
end function
public static double code(double x) {
double t_0 = Math.pow(Math.tan(x), 2.0);
return (-1.0 + t_0) / (-1.0 - t_0);
}
def code(x): t_0 = math.pow(math.tan(x), 2.0) return (-1.0 + t_0) / (-1.0 - t_0)
function code(x) t_0 = tan(x) ^ 2.0 return Float64(Float64(-1.0 + t_0) / Float64(-1.0 - t_0)) end
function tmp = code(x) t_0 = tan(x) ^ 2.0; tmp = (-1.0 + t_0) / (-1.0 - t_0); end
code[x_] := Block[{t$95$0 = N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision]}, N[(N[(-1.0 + t$95$0), $MachinePrecision] / N[(-1.0 - t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\tan x}^{2}\\
\frac{-1 + t\_0}{-1 - t\_0}
\end{array}
\end{array}
Initial program 99.6%
div-sub99.4%
*-un-lft-identity99.4%
add-sqr-sqrt99.3%
prod-diff99.3%
Applied egg-rr99.2%
Simplified99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (or (<= (tan x) -1.0) (not (<= (tan x) 1.0))) (/ -1.0 (+ (pow (tan x) 2.0) 1.0)) (pow (/ 1.0 (hypot 1.0 (tan x))) 2.0)))
double code(double x) {
double tmp;
if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) {
tmp = -1.0 / (pow(tan(x), 2.0) + 1.0);
} else {
tmp = pow((1.0 / hypot(1.0, tan(x))), 2.0);
}
return tmp;
}
public static double code(double x) {
double tmp;
if ((Math.tan(x) <= -1.0) || !(Math.tan(x) <= 1.0)) {
tmp = -1.0 / (Math.pow(Math.tan(x), 2.0) + 1.0);
} else {
tmp = Math.pow((1.0 / Math.hypot(1.0, Math.tan(x))), 2.0);
}
return tmp;
}
def code(x): tmp = 0 if (math.tan(x) <= -1.0) or not (math.tan(x) <= 1.0): tmp = -1.0 / (math.pow(math.tan(x), 2.0) + 1.0) else: tmp = math.pow((1.0 / math.hypot(1.0, math.tan(x))), 2.0) return tmp
function code(x) tmp = 0.0 if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) tmp = Float64(-1.0 / Float64((tan(x) ^ 2.0) + 1.0)); else tmp = Float64(1.0 / hypot(1.0, tan(x))) ^ 2.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((tan(x) <= -1.0) || ~((tan(x) <= 1.0))) tmp = -1.0 / ((tan(x) ^ 2.0) + 1.0); else tmp = (1.0 / hypot(1.0, tan(x))) ^ 2.0; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[N[Tan[x], $MachinePrecision], -1.0], N[Not[LessEqual[N[Tan[x], $MachinePrecision], 1.0]], $MachinePrecision]], N[(-1.0 / N[(N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[Power[N[(1.0 / N[Sqrt[1.0 ^ 2 + N[Tan[x], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\tan x \leq -1 \lor \neg \left(\tan x \leq 1\right):\\
\;\;\;\;\frac{-1}{{\tan x}^{2} + 1}\\
\mathbf{else}:\\
\;\;\;\;{\left(\frac{1}{\mathsf{hypot}\left(1, \tan x\right)}\right)}^{2}\\
\end{array}
\end{array}
if (tan.f64 x) < -1 or 1 < (tan.f64 x) Initial program 99.4%
div-sub98.9%
*-un-lft-identity98.9%
add-sqr-sqrt98.6%
prod-diff98.7%
Applied egg-rr98.6%
Simplified99.4%
Taylor expanded in x around 0 1.6%
Applied egg-rr17.0%
if -1 < (tan.f64 x) < 1Initial program 99.7%
div-sub99.6%
*-un-lft-identity99.6%
add-sqr-sqrt99.6%
prod-diff99.6%
Applied egg-rr99.4%
Simplified99.7%
Taylor expanded in x around 0 74.4%
add-sqr-sqrt74.4%
pow274.4%
Applied egg-rr74.4%
Final simplification59.8%
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (pow (tan x) 2.0) 1.0)))
(if (<= (tan x) -1.0)
(/ -1.0 (cbrt (pow t_0 3.0)))
(if (<= (tan x) 1.0)
(pow (/ 1.0 (hypot 1.0 (tan x))) 2.0)
(/ -1.0 t_0)))))
double code(double x) {
double t_0 = pow(tan(x), 2.0) + 1.0;
double tmp;
if (tan(x) <= -1.0) {
tmp = -1.0 / cbrt(pow(t_0, 3.0));
} else if (tan(x) <= 1.0) {
tmp = pow((1.0 / hypot(1.0, tan(x))), 2.0);
} else {
tmp = -1.0 / t_0;
}
return tmp;
}
public static double code(double x) {
double t_0 = Math.pow(Math.tan(x), 2.0) + 1.0;
double tmp;
if (Math.tan(x) <= -1.0) {
tmp = -1.0 / Math.cbrt(Math.pow(t_0, 3.0));
} else if (Math.tan(x) <= 1.0) {
tmp = Math.pow((1.0 / Math.hypot(1.0, Math.tan(x))), 2.0);
} else {
tmp = -1.0 / t_0;
}
return tmp;
}
function code(x) t_0 = Float64((tan(x) ^ 2.0) + 1.0) tmp = 0.0 if (tan(x) <= -1.0) tmp = Float64(-1.0 / cbrt((t_0 ^ 3.0))); elseif (tan(x) <= 1.0) tmp = Float64(1.0 / hypot(1.0, tan(x))) ^ 2.0; else tmp = Float64(-1.0 / t_0); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision] + 1.0), $MachinePrecision]}, If[LessEqual[N[Tan[x], $MachinePrecision], -1.0], N[(-1.0 / N[Power[N[Power[t$95$0, 3.0], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Tan[x], $MachinePrecision], 1.0], N[Power[N[(1.0 / N[Sqrt[1.0 ^ 2 + N[Tan[x], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision], N[(-1.0 / t$95$0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\tan x}^{2} + 1\\
\mathbf{if}\;\tan x \leq -1:\\
\;\;\;\;\frac{-1}{\sqrt[3]{{t\_0}^{3}}}\\
\mathbf{elif}\;\tan x \leq 1:\\
\;\;\;\;{\left(\frac{1}{\mathsf{hypot}\left(1, \tan x\right)}\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{t\_0}\\
\end{array}
\end{array}
if (tan.f64 x) < -1Initial program 99.4%
div-sub99.0%
*-un-lft-identity99.0%
add-sqr-sqrt98.8%
prod-diff98.9%
Applied egg-rr98.6%
Simplified99.4%
Taylor expanded in x around 0 1.6%
Applied egg-rr17.2%
if -1 < (tan.f64 x) < 1Initial program 99.7%
div-sub99.6%
*-un-lft-identity99.6%
add-sqr-sqrt99.6%
prod-diff99.6%
Applied egg-rr99.4%
Simplified99.7%
Taylor expanded in x around 0 74.4%
add-sqr-sqrt74.4%
pow274.4%
Applied egg-rr74.4%
if 1 < (tan.f64 x) Initial program 99.4%
div-sub98.9%
*-un-lft-identity98.9%
add-sqr-sqrt98.5%
prod-diff98.4%
Applied egg-rr98.6%
Simplified99.4%
Taylor expanded in x around 0 1.6%
Applied egg-rr16.9%
Final simplification59.8%
(FPCore (x) :precision binary64 (if (or (<= (tan x) -1.0) (not (<= (tan x) 1.0))) (/ -1.0 (+ (pow (tan x) 2.0) 1.0)) (pow (hypot 1.0 (tan x)) -2.0)))
double code(double x) {
double tmp;
if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) {
tmp = -1.0 / (pow(tan(x), 2.0) + 1.0);
} else {
tmp = pow(hypot(1.0, tan(x)), -2.0);
}
return tmp;
}
public static double code(double x) {
double tmp;
if ((Math.tan(x) <= -1.0) || !(Math.tan(x) <= 1.0)) {
tmp = -1.0 / (Math.pow(Math.tan(x), 2.0) + 1.0);
} else {
tmp = Math.pow(Math.hypot(1.0, Math.tan(x)), -2.0);
}
return tmp;
}
def code(x): tmp = 0 if (math.tan(x) <= -1.0) or not (math.tan(x) <= 1.0): tmp = -1.0 / (math.pow(math.tan(x), 2.0) + 1.0) else: tmp = math.pow(math.hypot(1.0, math.tan(x)), -2.0) return tmp
function code(x) tmp = 0.0 if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) tmp = Float64(-1.0 / Float64((tan(x) ^ 2.0) + 1.0)); else tmp = hypot(1.0, tan(x)) ^ -2.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((tan(x) <= -1.0) || ~((tan(x) <= 1.0))) tmp = -1.0 / ((tan(x) ^ 2.0) + 1.0); else tmp = hypot(1.0, tan(x)) ^ -2.0; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[N[Tan[x], $MachinePrecision], -1.0], N[Not[LessEqual[N[Tan[x], $MachinePrecision], 1.0]], $MachinePrecision]], N[(-1.0 / N[(N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[Power[N[Sqrt[1.0 ^ 2 + N[Tan[x], $MachinePrecision] ^ 2], $MachinePrecision], -2.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\tan x \leq -1 \lor \neg \left(\tan x \leq 1\right):\\
\;\;\;\;\frac{-1}{{\tan x}^{2} + 1}\\
\mathbf{else}:\\
\;\;\;\;{\left(\mathsf{hypot}\left(1, \tan x\right)\right)}^{-2}\\
\end{array}
\end{array}
if (tan.f64 x) < -1 or 1 < (tan.f64 x) Initial program 99.4%
div-sub98.9%
*-un-lft-identity98.9%
add-sqr-sqrt98.6%
prod-diff98.7%
Applied egg-rr98.6%
Simplified99.4%
Taylor expanded in x around 0 1.6%
Applied egg-rr17.0%
if -1 < (tan.f64 x) < 1Initial program 99.7%
div-sub99.6%
*-un-lft-identity99.6%
add-sqr-sqrt99.6%
prod-diff99.6%
Applied egg-rr99.4%
Simplified99.7%
Taylor expanded in x around 0 74.4%
frac-2neg74.4%
metadata-eval74.4%
add-sqr-sqrt74.4%
Applied egg-rr74.4%
unpow-174.4%
unpow-174.4%
pow-sqr74.4%
metadata-eval74.4%
Simplified74.4%
Final simplification59.8%
(FPCore (x) :precision binary64 (if (or (<= (tan x) -1.0) (not (<= (tan x) 1.0))) (/ -1.0 (+ (pow (tan x) 2.0) 1.0)) 1.0))
double code(double x) {
double tmp;
if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) {
tmp = -1.0 / (pow(tan(x), 2.0) + 1.0);
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((tan(x) <= (-1.0d0)) .or. (.not. (tan(x) <= 1.0d0))) then
tmp = (-1.0d0) / ((tan(x) ** 2.0d0) + 1.0d0)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((Math.tan(x) <= -1.0) || !(Math.tan(x) <= 1.0)) {
tmp = -1.0 / (Math.pow(Math.tan(x), 2.0) + 1.0);
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if (math.tan(x) <= -1.0) or not (math.tan(x) <= 1.0): tmp = -1.0 / (math.pow(math.tan(x), 2.0) + 1.0) else: tmp = 1.0 return tmp
function code(x) tmp = 0.0 if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) tmp = Float64(-1.0 / Float64((tan(x) ^ 2.0) + 1.0)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((tan(x) <= -1.0) || ~((tan(x) <= 1.0))) tmp = -1.0 / ((tan(x) ^ 2.0) + 1.0); else tmp = 1.0; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[N[Tan[x], $MachinePrecision], -1.0], N[Not[LessEqual[N[Tan[x], $MachinePrecision], 1.0]], $MachinePrecision]], N[(-1.0 / N[(N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\tan x \leq -1 \lor \neg \left(\tan x \leq 1\right):\\
\;\;\;\;\frac{-1}{{\tan x}^{2} + 1}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (tan.f64 x) < -1 or 1 < (tan.f64 x) Initial program 99.4%
div-sub98.9%
*-un-lft-identity98.9%
add-sqr-sqrt98.6%
prod-diff98.7%
Applied egg-rr98.6%
Simplified99.4%
Taylor expanded in x around 0 1.6%
Applied egg-rr17.0%
if -1 < (tan.f64 x) < 1Initial program 99.7%
Taylor expanded in x around 0 73.9%
Final simplification59.4%
(FPCore (x)
:precision binary64
(let* ((t_0 (pow (tan x) 2.0)))
(if (or (<= (tan x) -1.0) (not (<= (tan x) 1.0)))
(/ -1.0 (+ t_0 1.0))
(/ -1.0 (- -1.0 t_0)))))
double code(double x) {
double t_0 = pow(tan(x), 2.0);
double tmp;
if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) {
tmp = -1.0 / (t_0 + 1.0);
} else {
tmp = -1.0 / (-1.0 - t_0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = tan(x) ** 2.0d0
if ((tan(x) <= (-1.0d0)) .or. (.not. (tan(x) <= 1.0d0))) then
tmp = (-1.0d0) / (t_0 + 1.0d0)
else
tmp = (-1.0d0) / ((-1.0d0) - t_0)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.pow(Math.tan(x), 2.0);
double tmp;
if ((Math.tan(x) <= -1.0) || !(Math.tan(x) <= 1.0)) {
tmp = -1.0 / (t_0 + 1.0);
} else {
tmp = -1.0 / (-1.0 - t_0);
}
return tmp;
}
def code(x): t_0 = math.pow(math.tan(x), 2.0) tmp = 0 if (math.tan(x) <= -1.0) or not (math.tan(x) <= 1.0): tmp = -1.0 / (t_0 + 1.0) else: tmp = -1.0 / (-1.0 - t_0) return tmp
function code(x) t_0 = tan(x) ^ 2.0 tmp = 0.0 if ((tan(x) <= -1.0) || !(tan(x) <= 1.0)) tmp = Float64(-1.0 / Float64(t_0 + 1.0)); else tmp = Float64(-1.0 / Float64(-1.0 - t_0)); end return tmp end
function tmp_2 = code(x) t_0 = tan(x) ^ 2.0; tmp = 0.0; if ((tan(x) <= -1.0) || ~((tan(x) <= 1.0))) tmp = -1.0 / (t_0 + 1.0); else tmp = -1.0 / (-1.0 - t_0); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision]}, If[Or[LessEqual[N[Tan[x], $MachinePrecision], -1.0], N[Not[LessEqual[N[Tan[x], $MachinePrecision], 1.0]], $MachinePrecision]], N[(-1.0 / N[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(-1.0 - t$95$0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\tan x}^{2}\\
\mathbf{if}\;\tan x \leq -1 \lor \neg \left(\tan x \leq 1\right):\\
\;\;\;\;\frac{-1}{t\_0 + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{-1 - t\_0}\\
\end{array}
\end{array}
if (tan.f64 x) < -1 or 1 < (tan.f64 x) Initial program 99.4%
div-sub98.9%
*-un-lft-identity98.9%
add-sqr-sqrt98.6%
prod-diff98.7%
Applied egg-rr98.6%
Simplified99.4%
Taylor expanded in x around 0 1.6%
Applied egg-rr17.0%
if -1 < (tan.f64 x) < 1Initial program 99.7%
div-sub99.6%
*-un-lft-identity99.6%
add-sqr-sqrt99.6%
prod-diff99.6%
Applied egg-rr99.4%
Simplified99.7%
Taylor expanded in x around 0 74.4%
Final simplification59.8%
(FPCore (x) :precision binary64 1.0)
double code(double x) {
return 1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0
end function
public static double code(double x) {
return 1.0;
}
def code(x): return 1.0
function code(x) return 1.0 end
function tmp = code(x) tmp = 1.0; end
code[x_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 99.6%
Taylor expanded in x around 0 55.5%
Final simplification55.5%
herbie shell --seed 2024046
(FPCore (x)
:name "Trigonometry B"
:precision binary64
(/ (- 1.0 (* (tan x) (tan x))) (+ 1.0 (* (tan x) (tan x)))))