
(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 (/ 1.0 (/ 1.0 (/ x (/ 1.0 (- 0.3333333333333333 (* x (* x -0.022222222222222223))))))))
double code(double x) {
return 1.0 / (1.0 / (x / (1.0 / (0.3333333333333333 - (x * (x * -0.022222222222222223))))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (1.0d0 / (x / (1.0d0 / (0.3333333333333333d0 - (x * (x * (-0.022222222222222223d0)))))))
end function
public static double code(double x) {
return 1.0 / (1.0 / (x / (1.0 / (0.3333333333333333 - (x * (x * -0.022222222222222223))))));
}
def code(x): return 1.0 / (1.0 / (x / (1.0 / (0.3333333333333333 - (x * (x * -0.022222222222222223))))))
function code(x) return Float64(1.0 / Float64(1.0 / Float64(x / Float64(1.0 / Float64(0.3333333333333333 - Float64(x * Float64(x * -0.022222222222222223))))))) end
function tmp = code(x) tmp = 1.0 / (1.0 / (x / (1.0 / (0.3333333333333333 - (x * (x * -0.022222222222222223)))))); end
code[x_] := N[(1.0 / N[(1.0 / N[(x / N[(1.0 / N[(0.3333333333333333 - N[(x * N[(x * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{1}{\frac{x}{\frac{1}{0.3333333333333333 - x \cdot \left(x \cdot -0.022222222222222223\right)}}}}
\end{array}
Initial program 6.9%
Taylor expanded in x around 0
*-lowering-*.f64N/A
metadata-evalN/A
cancel-sign-sub-invN/A
--lowering--.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6499.2%
Simplified99.2%
*-commutativeN/A
flip--N/A
associate-*l/N/A
clear-numN/A
/-lowering-/.f64N/A
/-lowering-/.f64N/A
+-lowering-+.f64N/A
associate-*l*N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
Applied egg-rr99.2%
associate-/r*N/A
clear-numN/A
/-lowering-/.f64N/A
/-lowering-/.f64N/A
clear-numN/A
*-commutativeN/A
metadata-evalN/A
swap-sqrN/A
associate-*r*N/A
associate-*r*N/A
metadata-evalN/A
flip--N/A
/-lowering-/.f64N/A
Applied egg-rr99.5%
(FPCore (x) :precision binary64 (* x (- 0.3333333333333333 (* (* x x) (+ -0.022222222222222223 (* x (* x -0.0021164021164021165)))))))
double code(double x) {
return x * (0.3333333333333333 - ((x * x) * (-0.022222222222222223 + (x * (x * -0.0021164021164021165)))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * (0.3333333333333333d0 - ((x * x) * ((-0.022222222222222223d0) + (x * (x * (-0.0021164021164021165d0))))))
end function
public static double code(double x) {
return x * (0.3333333333333333 - ((x * x) * (-0.022222222222222223 + (x * (x * -0.0021164021164021165)))));
}
def code(x): return x * (0.3333333333333333 - ((x * x) * (-0.022222222222222223 + (x * (x * -0.0021164021164021165)))))
function code(x) return Float64(x * Float64(0.3333333333333333 - Float64(Float64(x * x) * Float64(-0.022222222222222223 + Float64(x * Float64(x * -0.0021164021164021165)))))) end
function tmp = code(x) tmp = x * (0.3333333333333333 - ((x * x) * (-0.022222222222222223 + (x * (x * -0.0021164021164021165))))); end
code[x_] := N[(x * N[(0.3333333333333333 - N[(N[(x * x), $MachinePrecision] * N[(-0.022222222222222223 + N[(x * N[(x * -0.0021164021164021165), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(0.3333333333333333 - \left(x \cdot x\right) \cdot \left(-0.022222222222222223 + x \cdot \left(x \cdot -0.0021164021164021165\right)\right)\right)
\end{array}
Initial program 6.9%
Taylor expanded in x around 0
*-lowering-*.f64N/A
+-commutativeN/A
distribute-lft-inN/A
*-commutativeN/A
associate-+r+N/A
*-commutativeN/A
associate-*l*N/A
metadata-evalN/A
cancel-sign-sub-invN/A
associate-*l*N/A
*-commutativeN/A
associate-+l-N/A
*-commutativeN/A
distribute-lft-out--N/A
--lowering--.f64N/A
Simplified99.4%
(FPCore (x) :precision binary64 (/ (* x 0.1111111111111111) (+ 0.3333333333333333 (* x (* x -0.022222222222222223)))))
double code(double x) {
return (x * 0.1111111111111111) / (0.3333333333333333 + (x * (x * -0.022222222222222223)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * 0.1111111111111111d0) / (0.3333333333333333d0 + (x * (x * (-0.022222222222222223d0))))
end function
public static double code(double x) {
return (x * 0.1111111111111111) / (0.3333333333333333 + (x * (x * -0.022222222222222223)));
}
def code(x): return (x * 0.1111111111111111) / (0.3333333333333333 + (x * (x * -0.022222222222222223)))
function code(x) return Float64(Float64(x * 0.1111111111111111) / Float64(0.3333333333333333 + Float64(x * Float64(x * -0.022222222222222223)))) end
function tmp = code(x) tmp = (x * 0.1111111111111111) / (0.3333333333333333 + (x * (x * -0.022222222222222223))); end
code[x_] := N[(N[(x * 0.1111111111111111), $MachinePrecision] / N[(0.3333333333333333 + N[(x * N[(x * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot 0.1111111111111111}{0.3333333333333333 + x \cdot \left(x \cdot -0.022222222222222223\right)}
\end{array}
Initial program 6.9%
Taylor expanded in x around 0
*-lowering-*.f64N/A
metadata-evalN/A
cancel-sign-sub-invN/A
--lowering--.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6499.2%
Simplified99.2%
*-commutativeN/A
flip--N/A
associate-*l/N/A
/-lowering-/.f64N/A
*-lowering-*.f64N/A
--lowering--.f64N/A
metadata-evalN/A
*-commutativeN/A
*-commutativeN/A
swap-sqrN/A
*-lowering-*.f64N/A
metadata-evalN/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
+-lowering-+.f64N/A
associate-*l*N/A
Applied egg-rr99.3%
Taylor expanded in x around 0
*-commutativeN/A
*-lowering-*.f6499.3%
Simplified99.3%
(FPCore (x) :precision binary64 (* x (- 0.3333333333333333 (* -0.022222222222222223 (* x x)))))
double code(double x) {
return x * (0.3333333333333333 - (-0.022222222222222223 * (x * x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * (0.3333333333333333d0 - ((-0.022222222222222223d0) * (x * x)))
end function
public static double code(double x) {
return x * (0.3333333333333333 - (-0.022222222222222223 * (x * x)));
}
def code(x): return x * (0.3333333333333333 - (-0.022222222222222223 * (x * x)))
function code(x) return Float64(x * Float64(0.3333333333333333 - Float64(-0.022222222222222223 * Float64(x * x)))) end
function tmp = code(x) tmp = x * (0.3333333333333333 - (-0.022222222222222223 * (x * x))); end
code[x_] := N[(x * N[(0.3333333333333333 - N[(-0.022222222222222223 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(0.3333333333333333 - -0.022222222222222223 \cdot \left(x \cdot x\right)\right)
\end{array}
Initial program 6.9%
Taylor expanded in x around 0
*-lowering-*.f64N/A
metadata-evalN/A
cancel-sign-sub-invN/A
--lowering--.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6499.2%
Simplified99.2%
Final simplification99.2%
(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.9%
Taylor expanded in x around 0
*-lowering-*.f64N/A
metadata-evalN/A
cancel-sign-sub-invN/A
--lowering--.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6499.2%
Simplified99.2%
*-commutativeN/A
flip--N/A
associate-*l/N/A
clear-numN/A
/-lowering-/.f64N/A
/-lowering-/.f64N/A
+-lowering-+.f64N/A
associate-*l*N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
Applied egg-rr99.2%
Taylor expanded in x around 0
/-lowering-/.f6498.6%
Simplified98.6%
clear-numN/A
/-lowering-/.f6499.0%
Applied egg-rr99.0%
(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.9%
Taylor expanded in x around 0
*-lowering-*.f6498.5%
Simplified98.5%
Final simplification98.5%
(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 2024141
(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))))