(FPCore (x) :precision binary64 (+ (- (/ 1.0 (sin x)) (/ 1.0 (tan x))) (- 1.0 (cos x))))
double code(double x) { return ((1.0 / sin(x)) - (1.0 / tan(x))) + (1.0 - cos(x)); }
real(8) function code(x) real(8), intent (in) :: x code = ((1.0d0 / sin(x)) - (1.0d0 / tan(x))) + (1.0d0 - cos(x)) end function
public static double code(double x) { return ((1.0 / Math.sin(x)) - (1.0 / Math.tan(x))) + (1.0 - Math.cos(x)); }
def code(x): return ((1.0 / math.sin(x)) - (1.0 / math.tan(x))) + (1.0 - math.cos(x))
function code(x) return Float64(Float64(Float64(1.0 / sin(x)) - Float64(1.0 / tan(x))) + Float64(1.0 - cos(x))) end
function tmp = code(x) tmp = ((1.0 / sin(x)) - (1.0 / tan(x))) + (1.0 - cos(x)); end
code[x_] := N[(N[(N[(1.0 / N[Sin[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(\frac{1}{\sin x} - \frac{1}{\tan x}\right) + \left(1 - \cos x\right) \end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (sin x)) (/ 1.0 (tan x))) (- 1.0 (cos x))))
double code(double x) { return ((1.0 / sin(x)) - (1.0 / tan(x))) + (1.0 - cos(x)); }
real(8) function code(x) real(8), intent (in) :: x code = ((1.0d0 / sin(x)) - (1.0d0 / tan(x))) + (1.0d0 - cos(x)) end function
public static double code(double x) { return ((1.0 / Math.sin(x)) - (1.0 / Math.tan(x))) + (1.0 - Math.cos(x)); }
def code(x): return ((1.0 / math.sin(x)) - (1.0 / math.tan(x))) + (1.0 - math.cos(x))
function code(x) return Float64(Float64(Float64(1.0 / sin(x)) - Float64(1.0 / tan(x))) + Float64(1.0 - cos(x))) end
function tmp = code(x) tmp = ((1.0 / sin(x)) - (1.0 / tan(x))) + (1.0 - cos(x)); end
code[x_] := N[(N[(N[(1.0 / N[Sin[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(\frac{1}{\sin x} - \frac{1}{\tan x}\right) + \left(1 - \cos x\right) \end{array}
(FPCore (x) :precision binary64 (fma (* 0.5 x) x (* 0.5 x)))
double code(double x) { return fma((0.5 * x), x, (0.5 * x)); }
function code(x) return fma(Float64(0.5 * x), x, Float64(0.5 * x)) end
code[x_] := N[(N[(0.5 * x), $MachinePrecision] * x + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(0.5 \cdot x, x, 0.5 \cdot x\right) \end{array}
Initial program 5.7%
Taylor expanded in x around 0
*-commutative
N/A
lower-*.f64
N/A
+-commutative
N/A
lower-fma.f64
100.0
Applied rewrites100.0%
Applied rewrites100.0%
(FPCore (x) :precision binary64 (* (fma 0.5 x 0.5) x))
double code(double x) { return fma(0.5, x, 0.5) * x; }
function code(x) return Float64(fma(0.5, x, 0.5) * x) end
code[x_] := N[(N[(0.5 * x + 0.5), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(0.5, x, 0.5\right) \cdot x \end{array}
Initial program 5.7%
Taylor expanded in x around 0
*-commutative
N/A
lower-*.f64
N/A
+-commutative
N/A
lower-fma.f64
100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (* 0.5 x))
double code(double x) { return 0.5 * x; }
real(8) function code(x) real(8), intent (in) :: x code = 0.5d0 * x end function
public static double code(double x) { return 0.5 * x; }
def code(x): return 0.5 * x
function code(x) return Float64(0.5 * x) end
function tmp = code(x) tmp = 0.5 * x; end
code[x_] := N[(0.5 * x), $MachinePrecision]
\begin{array}{l} \\ 0.5 \cdot x \end{array}
Initial program 5.7%
Taylor expanded in x around 0
lower-*.f64
99.5
Applied rewrites99.5%
herbie shell --seed 1
(FPCore (x)
:name " (1/sin(x))-(1/tan(x)) + (1-cos(x)) "
:precision binary64
:pre (and (<= -1e-9 x) (<= x 0.0))
(+ (- (/ 1.0 (sin x)) (/ 1.0 (tan x))) (- 1.0 (cos x))))