
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (sin x)))
double code(double x) {
return (1.0 - cos(x)) / sin(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 - cos(x)) / sin(x)
end function
public static double code(double x) {
return (1.0 - Math.cos(x)) / Math.sin(x);
}
def code(x): return (1.0 - math.cos(x)) / math.sin(x)
function code(x) return Float64(Float64(1.0 - cos(x)) / sin(x)) end
function tmp = code(x) tmp = (1.0 - cos(x)) / sin(x); end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 - \cos x}{\sin x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (sin x)))
double code(double x) {
return (1.0 - cos(x)) / sin(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 - cos(x)) / sin(x)
end function
public static double code(double x) {
return (1.0 - Math.cos(x)) / Math.sin(x);
}
def code(x): return (1.0 - math.cos(x)) / math.sin(x)
function code(x) return Float64(Float64(1.0 - cos(x)) / sin(x)) end
function tmp = code(x) tmp = (1.0 - cos(x)) / sin(x); end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 - \cos x}{\sin x}
\end{array}
(FPCore (x) :precision binary64 (tan (* 0.5 x)))
double code(double x) {
return tan((0.5 * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = tan((0.5d0 * x))
end function
public static double code(double x) {
return Math.tan((0.5 * x));
}
def code(x): return math.tan((0.5 * x))
function code(x) return tan(Float64(0.5 * x)) end
function tmp = code(x) tmp = tan((0.5 * x)); end
code[x_] := N[Tan[N[(0.5 * x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\tan \left(0.5 \cdot x\right)
\end{array}
Initial program 47.6%
lift-/.f64N/A
lift--.f64N/A
lift-cos.f64N/A
lift-sin.f64N/A
hang-p0-tanN/A
lower-tan.f64N/A
clear-numN/A
associate-/r/N/A
metadata-evalN/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (x)
:precision binary64
(if (<= x 10.2)
(*
(+
(/ (* x x) (fma (fma -0.002857142857142857 (* x x) -2.4) (* x x) 24.0))
0.5)
x)
1.0))
double code(double x) {
double tmp;
if (x <= 10.2) {
tmp = (((x * x) / fma(fma(-0.002857142857142857, (x * x), -2.4), (x * x), 24.0)) + 0.5) * x;
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 10.2) tmp = Float64(Float64(Float64(Float64(x * x) / fma(fma(-0.002857142857142857, Float64(x * x), -2.4), Float64(x * x), 24.0)) + 0.5) * x); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 10.2], N[(N[(N[(N[(x * x), $MachinePrecision] / N[(N[(-0.002857142857142857 * N[(x * x), $MachinePrecision] + -2.4), $MachinePrecision] * N[(x * x), $MachinePrecision] + 24.0), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 10.2:\\
\;\;\;\;\left(\frac{x \cdot x}{\mathsf{fma}\left(\mathsf{fma}\left(-0.002857142857142857, x \cdot x, -2.4\right), x \cdot x, 24\right)} + 0.5\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 10.199999999999999Initial program 32.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6472.1
Applied rewrites72.1%
Applied rewrites72.1%
Taylor expanded in x around 0
Applied rewrites72.2%
Applied rewrites72.2%
if 10.199999999999999 < x Initial program 98.6%
Applied rewrites11.5%
lift-pow.f64N/A
pow-base-111.5
Applied rewrites11.5%
(FPCore (x) :precision binary64 (if (<= x 14.0) (* (+ (/ (* x x) (fma (* x x) -2.4 24.0)) 0.5) x) 1.0))
double code(double x) {
double tmp;
if (x <= 14.0) {
tmp = (((x * x) / fma((x * x), -2.4, 24.0)) + 0.5) * x;
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 14.0) tmp = Float64(Float64(Float64(Float64(x * x) / fma(Float64(x * x), -2.4, 24.0)) + 0.5) * x); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 14.0], N[(N[(N[(N[(x * x), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] * -2.4 + 24.0), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 14:\\
\;\;\;\;\left(\frac{x \cdot x}{\mathsf{fma}\left(x \cdot x, -2.4, 24\right)} + 0.5\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 14Initial program 32.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6472.1
Applied rewrites72.1%
Applied rewrites72.1%
Taylor expanded in x around 0
Applied rewrites72.1%
Applied rewrites72.1%
if 14 < x Initial program 98.6%
Applied rewrites11.5%
lift-pow.f64N/A
pow-base-111.5
Applied rewrites11.5%
(FPCore (x)
:precision binary64
(if (<= x 3.2)
(*
(fma (fma 0.004166666666666667 (* x x) 0.041666666666666664) (* x x) 0.5)
x)
1.0))
double code(double x) {
double tmp;
if (x <= 3.2) {
tmp = fma(fma(0.004166666666666667, (x * x), 0.041666666666666664), (x * x), 0.5) * x;
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 3.2) tmp = Float64(fma(fma(0.004166666666666667, Float64(x * x), 0.041666666666666664), Float64(x * x), 0.5) * x); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 3.2], N[(N[(N[(0.004166666666666667 * N[(x * x), $MachinePrecision] + 0.041666666666666664), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.004166666666666667, x \cdot x, 0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 3.2000000000000002Initial program 32.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6472.1
Applied rewrites72.1%
if 3.2000000000000002 < x Initial program 98.6%
Applied rewrites11.5%
lift-pow.f64N/A
pow-base-111.5
Applied rewrites11.5%
(FPCore (x) :precision binary64 (if (<= x 3.2) (* (fma (* x x) 0.041666666666666664 0.5) x) 1.0))
double code(double x) {
double tmp;
if (x <= 3.2) {
tmp = fma((x * x), 0.041666666666666664, 0.5) * x;
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 3.2) tmp = Float64(fma(Float64(x * x), 0.041666666666666664, 0.5) * x); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 3.2], N[(N[(N[(x * x), $MachinePrecision] * 0.041666666666666664 + 0.5), $MachinePrecision] * x), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.2:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, 0.041666666666666664, 0.5\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 3.2000000000000002Initial program 32.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6472.1
Applied rewrites72.1%
if 3.2000000000000002 < x Initial program 98.6%
Applied rewrites11.5%
lift-pow.f64N/A
pow-base-111.5
Applied rewrites11.5%
(FPCore (x) :precision binary64 (if (<= x 1.4) (* 0.5 x) 1.0))
double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 0.5 * x;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.4d0) then
tmp = 0.5d0 * x
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 0.5 * x;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4: tmp = 0.5 * x else: tmp = 1.0 return tmp
function code(x) tmp = 0.0 if (x <= 1.4) tmp = Float64(0.5 * x); else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4) tmp = 0.5 * x; else tmp = 1.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4], N[(0.5 * x), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 1.3999999999999999Initial program 31.7%
Taylor expanded in x around 0
lower-*.f6472.6
Applied rewrites72.6%
if 1.3999999999999999 < x Initial program 98.6%
Applied rewrites11.7%
lift-pow.f64N/A
pow-base-111.7
Applied rewrites11.7%
(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 47.6%
Applied rewrites7.1%
lift-pow.f64N/A
pow-base-17.1
Applied rewrites7.1%
(FPCore (x) :precision binary64 (tan (/ x 2.0)))
double code(double x) {
return tan((x / 2.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = tan((x / 2.0d0))
end function
public static double code(double x) {
return Math.tan((x / 2.0));
}
def code(x): return math.tan((x / 2.0))
function code(x) return tan(Float64(x / 2.0)) end
function tmp = code(x) tmp = tan((x / 2.0)); end
code[x_] := N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\tan \left(\frac{x}{2}\right)
\end{array}
herbie shell --seed 2024308
(FPCore (x)
:name "tanhf (example 3.4)"
:precision binary64
:alt
(! :herbie-platform default (tan (/ x 2)))
(/ (- 1.0 (cos x)) (sin x)))