
(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 6 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 (* -0.1111111111111111 (/ x (- (* 0.022222222222222223 (* x x)) 0.3333333333333333))))
double code(double x) {
return -0.1111111111111111 * (x / ((0.022222222222222223 * (x * x)) - 0.3333333333333333));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-0.1111111111111111d0) * (x / ((0.022222222222222223d0 * (x * x)) - 0.3333333333333333d0))
end function
public static double code(double x) {
return -0.1111111111111111 * (x / ((0.022222222222222223 * (x * x)) - 0.3333333333333333));
}
def code(x): return -0.1111111111111111 * (x / ((0.022222222222222223 * (x * x)) - 0.3333333333333333))
function code(x) return Float64(-0.1111111111111111 * Float64(x / Float64(Float64(0.022222222222222223 * Float64(x * x)) - 0.3333333333333333))) end
function tmp = code(x) tmp = -0.1111111111111111 * (x / ((0.022222222222222223 * (x * x)) - 0.3333333333333333)); end
code[x_] := N[(-0.1111111111111111 * N[(x / N[(N[(0.022222222222222223 * N[(x * x), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.1111111111111111 \cdot \frac{x}{0.022222222222222223 \cdot \left(x \cdot x\right) - 0.3333333333333333}
\end{array}
Initial program 6.3%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.4
Applied rewrites99.4%
Applied rewrites99.7%
Taylor expanded in x around 0
Applied rewrites99.7%
(FPCore (x) :precision binary64 (* (fma (* x x) 0.022222222222222223 0.3333333333333333) x))
double code(double x) {
return fma((x * x), 0.022222222222222223, 0.3333333333333333) * x;
}
function code(x) return Float64(fma(Float64(x * x), 0.022222222222222223, 0.3333333333333333) * x) end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] * 0.022222222222222223 + 0.3333333333333333), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x
\end{array}
Initial program 6.3%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.4
Applied rewrites99.4%
(FPCore (x) :precision binary64 (* -0.1111111111111111 (/ x -0.3333333333333333)))
double code(double x) {
return -0.1111111111111111 * (x / -0.3333333333333333);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-0.1111111111111111d0) * (x / (-0.3333333333333333d0))
end function
public static double code(double x) {
return -0.1111111111111111 * (x / -0.3333333333333333);
}
def code(x): return -0.1111111111111111 * (x / -0.3333333333333333)
function code(x) return Float64(-0.1111111111111111 * Float64(x / -0.3333333333333333)) end
function tmp = code(x) tmp = -0.1111111111111111 * (x / -0.3333333333333333); end
code[x_] := N[(-0.1111111111111111 * N[(x / -0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.1111111111111111 \cdot \frac{x}{-0.3333333333333333}
\end{array}
Initial program 6.3%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.4
Applied rewrites99.4%
Applied rewrites99.7%
Taylor expanded in x around 0
Applied rewrites99.7%
Taylor expanded in x around 0
Applied rewrites99.2%
(FPCore (x) :precision binary64 (* 0.037037037037037035 (/ x 0.1111111111111111)))
double code(double x) {
return 0.037037037037037035 * (x / 0.1111111111111111);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.037037037037037035d0 * (x / 0.1111111111111111d0)
end function
public static double code(double x) {
return 0.037037037037037035 * (x / 0.1111111111111111);
}
def code(x): return 0.037037037037037035 * (x / 0.1111111111111111)
function code(x) return Float64(0.037037037037037035 * Float64(x / 0.1111111111111111)) end
function tmp = code(x) tmp = 0.037037037037037035 * (x / 0.1111111111111111); end
code[x_] := N[(0.037037037037037035 * N[(x / 0.1111111111111111), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.037037037037037035 \cdot \frac{x}{0.1111111111111111}
\end{array}
Initial program 6.3%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.4
Applied rewrites99.4%
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites99.1%
Taylor expanded in x around 0
Applied rewrites99.1%
(FPCore (x) :precision binary64 (* -0.1111111111111111 (* -3.0 x)))
double code(double x) {
return -0.1111111111111111 * (-3.0 * x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-0.1111111111111111d0) * ((-3.0d0) * x)
end function
public static double code(double x) {
return -0.1111111111111111 * (-3.0 * x);
}
def code(x): return -0.1111111111111111 * (-3.0 * x)
function code(x) return Float64(-0.1111111111111111 * Float64(-3.0 * x)) end
function tmp = code(x) tmp = -0.1111111111111111 * (-3.0 * x); end
code[x_] := N[(-0.1111111111111111 * N[(-3.0 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.1111111111111111 \cdot \left(-3 \cdot x\right)
\end{array}
Initial program 6.3%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.4
Applied rewrites99.4%
Applied rewrites99.7%
Taylor expanded in x around 0
Applied rewrites99.7%
Taylor expanded in x around 0
Applied rewrites99.0%
(FPCore (x) :precision binary64 (* 0.3333333333333333 x))
double code(double x) {
return 0.3333333333333333 * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.3333333333333333d0 * x
end function
public static double code(double x) {
return 0.3333333333333333 * x;
}
def code(x): return 0.3333333333333333 * x
function code(x) return Float64(0.3333333333333333 * x) end
function tmp = code(x) tmp = 0.3333333333333333 * x; end
code[x_] := N[(0.3333333333333333 * x), $MachinePrecision]
\begin{array}{l}
\\
0.3333333333333333 \cdot x
\end{array}
Initial program 6.3%
Taylor expanded in x around 0
lower-*.f6499.0
Applied rewrites99.0%
(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 2024339
(FPCore (x)
:name "invcot (example 3.9)"
:precision binary64
:pre (and (< -0.026 x) (< x 0.026))
:alt
(! :herbie-platform default (if (< (fabs x) 13/500) (* (/ x 3) (+ 1 (/ (* x x) 15))) (- (/ 1 x) (/ 1 (tan x)))))
(- (/ 1.0 x) (/ 1.0 (tan x))))