
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
return (1.0 / x) - (1.0 / tan(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x): return (1.0 / x) - (1.0 / math.tan(x))
function code(x) return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))) end
function tmp = code(x) tmp = (1.0 / x) - (1.0 / tan(x)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} - \frac{1}{\tan x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
return (1.0 / x) - (1.0 / tan(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x): return (1.0 / x) - (1.0 / math.tan(x))
function code(x) return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))) end
function tmp = code(x) tmp = (1.0 / x) - (1.0 / tan(x)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} - \frac{1}{\tan x}
\end{array}
(FPCore (x)
:precision binary64
(/
x
(/
(+
(- 0.1111111111111111 (* (pow x 2.0) 0.007407407407407408))
(* 0.0004938271604938272 (pow x 4.0)))
(+ 0.037037037037037035 (* (pow x 6.0) 1.0973936899862826e-5)))))
double code(double x) {
return x / (((0.1111111111111111 - (pow(x, 2.0) * 0.007407407407407408)) + (0.0004938271604938272 * pow(x, 4.0))) / (0.037037037037037035 + (pow(x, 6.0) * 1.0973936899862826e-5)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x / (((0.1111111111111111d0 - ((x ** 2.0d0) * 0.007407407407407408d0)) + (0.0004938271604938272d0 * (x ** 4.0d0))) / (0.037037037037037035d0 + ((x ** 6.0d0) * 1.0973936899862826d-5)))
end function
public static double code(double x) {
return x / (((0.1111111111111111 - (Math.pow(x, 2.0) * 0.007407407407407408)) + (0.0004938271604938272 * Math.pow(x, 4.0))) / (0.037037037037037035 + (Math.pow(x, 6.0) * 1.0973936899862826e-5)));
}
def code(x): return x / (((0.1111111111111111 - (math.pow(x, 2.0) * 0.007407407407407408)) + (0.0004938271604938272 * math.pow(x, 4.0))) / (0.037037037037037035 + (math.pow(x, 6.0) * 1.0973936899862826e-5)))
function code(x) return Float64(x / Float64(Float64(Float64(0.1111111111111111 - Float64((x ^ 2.0) * 0.007407407407407408)) + Float64(0.0004938271604938272 * (x ^ 4.0))) / Float64(0.037037037037037035 + Float64((x ^ 6.0) * 1.0973936899862826e-5)))) end
function tmp = code(x) tmp = x / (((0.1111111111111111 - ((x ^ 2.0) * 0.007407407407407408)) + (0.0004938271604938272 * (x ^ 4.0))) / (0.037037037037037035 + ((x ^ 6.0) * 1.0973936899862826e-5))); end
code[x_] := N[(x / N[(N[(N[(0.1111111111111111 - N[(N[Power[x, 2.0], $MachinePrecision] * 0.007407407407407408), $MachinePrecision]), $MachinePrecision] + N[(0.0004938271604938272 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(0.037037037037037035 + N[(N[Power[x, 6.0], $MachinePrecision] * 1.0973936899862826e-5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{\frac{\left(0.1111111111111111 - {x}^{2} \cdot 0.007407407407407408\right) + 0.0004938271604938272 \cdot {x}^{4}}{0.037037037037037035 + {x}^{6} \cdot 1.0973936899862826 \cdot 10^{-5}}}
\end{array}
Initial program 6.8%
Taylor expanded in x around 0 99.5%
unpow399.5%
associate-*r*99.5%
distribute-rgt-out99.5%
pow299.5%
Applied egg-rr99.5%
flip3-+98.0%
associate-*r/98.3%
metadata-eval99.5%
+-commutative99.5%
*-commutative99.5%
cube-prod99.5%
unpow299.5%
pow-prod-down99.5%
pow-prod-up99.5%
metadata-eval99.5%
metadata-eval99.5%
+-commutative99.5%
metadata-eval99.5%
Applied egg-rr99.5%
associate-/l*99.9%
associate--r-99.9%
*-commutative99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ 1.0 (+ (/ 3.0 x) (* x -0.2))))
double code(double x) {
return 1.0 / ((3.0 / x) + (x * -0.2));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / ((3.0d0 / x) + (x * (-0.2d0)))
end function
public static double code(double x) {
return 1.0 / ((3.0 / x) + (x * -0.2));
}
def code(x): return 1.0 / ((3.0 / x) + (x * -0.2))
function code(x) return Float64(1.0 / Float64(Float64(3.0 / x) + Float64(x * -0.2))) end
function tmp = code(x) tmp = 1.0 / ((3.0 / x) + (x * -0.2)); end
code[x_] := N[(1.0 / N[(N[(3.0 / x), $MachinePrecision] + N[(x * -0.2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{3}{x} + x \cdot -0.2}
\end{array}
Initial program 6.8%
Taylor expanded in x around 0 99.5%
flip3-+33.4%
clear-num33.4%
clear-num33.4%
flip3-+99.4%
+-commutative99.4%
fma-def99.4%
Applied egg-rr99.4%
Taylor expanded in x around 0 99.3%
fma-def99.3%
associate-*r/99.5%
metadata-eval99.5%
Simplified99.5%
fma-udef99.5%
+-commutative99.5%
*-commutative99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (* x 0.3333333333333333))
double code(double x) {
return x * 0.3333333333333333;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * 0.3333333333333333d0
end function
public static double code(double x) {
return x * 0.3333333333333333;
}
def code(x): return x * 0.3333333333333333
function code(x) return Float64(x * 0.3333333333333333) end
function tmp = code(x) tmp = x * 0.3333333333333333; end
code[x_] := N[(x * 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.3333333333333333
\end{array}
Initial program 6.8%
Taylor expanded in x around 0 98.8%
Final simplification98.8%
(FPCore (x) :precision binary64 (/ x 3.0))
double code(double x) {
return x / 3.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x / 3.0d0
end function
public static double code(double x) {
return x / 3.0;
}
def code(x): return x / 3.0
function code(x) return Float64(x / 3.0) end
function tmp = code(x) tmp = x / 3.0; end
code[x_] := N[(x / 3.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{3}
\end{array}
Initial program 6.8%
Taylor expanded in x around 0 98.8%
add-cbrt-cube36.1%
unpow336.1%
pow1/319.7%
*-commutative19.7%
cube-prod19.7%
metadata-eval19.7%
Applied egg-rr19.7%
unpow1/336.1%
Simplified36.1%
metadata-eval36.1%
cube-prod36.1%
*-commutative36.1%
unpow336.1%
add-cbrt-cube98.8%
add-sqr-sqrt48.1%
pow248.1%
*-commutative48.1%
Applied egg-rr48.1%
unpow248.1%
add-sqr-sqrt98.8%
metadata-eval98.8%
div-inv99.2%
Applied egg-rr99.2%
Final simplification99.2%
(FPCore (x) :precision binary64 (if (< (fabs x) 0.026) (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0))) (- (/ 1.0 x) (/ 1.0 (tan x)))))
double code(double x) {
double tmp;
if (fabs(x) < 0.026) {
tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
} else {
tmp = (1.0 / x) - (1.0 / tan(x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (abs(x) < 0.026d0) then
tmp = (x / 3.0d0) * (1.0d0 + ((x * x) / 15.0d0))
else
tmp = (1.0d0 / x) - (1.0d0 / tan(x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (Math.abs(x) < 0.026) {
tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
} else {
tmp = (1.0 / x) - (1.0 / Math.tan(x));
}
return tmp;
}
def code(x): tmp = 0 if math.fabs(x) < 0.026: tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0)) else: tmp = (1.0 / x) - (1.0 / math.tan(x)) return tmp
function code(x) tmp = 0.0 if (abs(x) < 0.026) tmp = Float64(Float64(x / 3.0) * Float64(1.0 + Float64(Float64(x * x) / 15.0))); else tmp = Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) < 0.026) tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0)); else tmp = (1.0 / x) - (1.0 / tan(x)); end tmp_2 = tmp; end
code[x_] := If[Less[N[Abs[x], $MachinePrecision], 0.026], N[(N[(x / 3.0), $MachinePrecision] * N[(1.0 + N[(N[(x * x), $MachinePrecision] / 15.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| < 0.026:\\
\;\;\;\;\frac{x}{3} \cdot \left(1 + \frac{x \cdot x}{15}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x} - \frac{1}{\tan x}\\
\end{array}
\end{array}
herbie shell --seed 2023305
(FPCore (x)
:name "invcot (example 3.9)"
:precision binary64
:pre (and (< -0.026 x) (< x 0.026))
:herbie-target
(if (< (fabs x) 0.026) (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0))) (- (/ 1.0 x) (/ 1.0 (tan x))))
(- (/ 1.0 x) (/ 1.0 (tan x))))