
(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 8 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 (* x 0.5)))
double code(double x) {
return tan((x * 0.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = tan((x * 0.5d0))
end function
public static double code(double x) {
return Math.tan((x * 0.5));
}
def code(x): return math.tan((x * 0.5))
function code(x) return tan(Float64(x * 0.5)) end
function tmp = code(x) tmp = tan((x * 0.5)); end
code[x_] := N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\tan \left(x \cdot 0.5\right)
\end{array}
Initial program 57.1%
lift-/.f64N/A
lift--.f64N/A
lift-cos.f64N/A
lift-sin.f64N/A
hang-p0-tanN/A
lower-tan.f64N/A
div-invN/A
metadata-evalN/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (x)
:precision binary64
(if (<= x 0.2)
(*
x
(fma
(* x x)
(fma
(* x x)
(fma x (* x 0.00042162698412698415) 0.004166666666666667)
0.041666666666666664)
0.5))
1.0))
double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = x * fma((x * x), fma((x * x), fma(x, (x * 0.00042162698412698415), 0.004166666666666667), 0.041666666666666664), 0.5);
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.2) tmp = Float64(x * fma(Float64(x * x), fma(Float64(x * x), fma(x, Float64(x * 0.00042162698412698415), 0.004166666666666667), 0.041666666666666664), 0.5)); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 0.2], N[(x * N[(N[(x * x), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * 0.00042162698412698415), $MachinePrecision] + 0.004166666666666667), $MachinePrecision] + 0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.2:\\
\;\;\;\;x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot 0.00042162698412698415, 0.004166666666666667\right), 0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 41.6%
Taylor expanded in x around 0
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6463.4
Applied rewrites63.4%
if 0.20000000000000001 < x Initial program 98.4%
Applied rewrites9.2%
lift-pow.f64N/A
pow-base-19.2
Applied rewrites9.2%
(FPCore (x)
:precision binary64
(if (<= x 0.2)
(fma
(fma x (* x 0.004166666666666667) 0.041666666666666664)
(* x (* x x))
(* x 0.5))
1.0))
double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = fma(fma(x, (x * 0.004166666666666667), 0.041666666666666664), (x * (x * x)), (x * 0.5));
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.2) tmp = fma(fma(x, Float64(x * 0.004166666666666667), 0.041666666666666664), Float64(x * Float64(x * x)), Float64(x * 0.5)); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 0.2], N[(N[(x * N[(x * 0.004166666666666667), $MachinePrecision] + 0.041666666666666664), $MachinePrecision] * N[(x * N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x, x \cdot 0.004166666666666667, 0.041666666666666664\right), x \cdot \left(x \cdot x\right), x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 41.6%
Taylor expanded in x around 0
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6463.3
Applied rewrites63.3%
Applied rewrites63.3%
if 0.20000000000000001 < x Initial program 98.4%
Applied rewrites9.2%
lift-pow.f64N/A
pow-base-19.2
Applied rewrites9.2%
(FPCore (x)
:precision binary64
(if (<= x 0.2)
(*
x
(fma (* x x) (fma x (* x 0.004166666666666667) 0.041666666666666664) 0.5))
1.0))
double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = x * fma((x * x), fma(x, (x * 0.004166666666666667), 0.041666666666666664), 0.5);
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.2) tmp = Float64(x * fma(Float64(x * x), fma(x, Float64(x * 0.004166666666666667), 0.041666666666666664), 0.5)); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 0.2], N[(x * N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * 0.004166666666666667), $MachinePrecision] + 0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.2:\\
\;\;\;\;x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot 0.004166666666666667, 0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 41.6%
Taylor expanded in x around 0
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6463.3
Applied rewrites63.3%
if 0.20000000000000001 < x Initial program 98.4%
Applied rewrites9.2%
lift-pow.f64N/A
pow-base-19.2
Applied rewrites9.2%
(FPCore (x) :precision binary64 (if (<= x 0.2) (fma (* x (* x x)) 0.041666666666666664 (* x 0.5)) 1.0))
double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = fma((x * (x * x)), 0.041666666666666664, (x * 0.5));
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.2) tmp = fma(Float64(x * Float64(x * x)), 0.041666666666666664, Float64(x * 0.5)); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 0.2], N[(N[(x * N[(x * x), $MachinePrecision]), $MachinePrecision] * 0.041666666666666664 + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.2:\\
\;\;\;\;\mathsf{fma}\left(x \cdot \left(x \cdot x\right), 0.041666666666666664, x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 41.6%
Taylor expanded in x around 0
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6463.0
Applied rewrites63.0%
Applied rewrites63.0%
if 0.20000000000000001 < x Initial program 98.4%
Applied rewrites9.2%
lift-pow.f64N/A
pow-base-19.2
Applied rewrites9.2%
(FPCore (x) :precision binary64 (if (<= x 0.2) (* x (fma x (* x 0.041666666666666664) 0.5)) 1.0))
double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = x * fma(x, (x * 0.041666666666666664), 0.5);
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.2) tmp = Float64(x * fma(x, Float64(x * 0.041666666666666664), 0.5)); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 0.2], N[(x * N[(x * N[(x * 0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.2:\\
\;\;\;\;x \cdot \mathsf{fma}\left(x, x \cdot 0.041666666666666664, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 41.6%
Taylor expanded in x around 0
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6463.0
Applied rewrites63.0%
if 0.20000000000000001 < x Initial program 98.4%
Applied rewrites9.2%
lift-pow.f64N/A
pow-base-19.2
Applied rewrites9.2%
(FPCore (x) :precision binary64 (if (<= x 0.2) (* x 0.5) 1.0))
double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = x * 0.5;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.2d0) then
tmp = x * 0.5d0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.2) {
tmp = x * 0.5;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.2: tmp = x * 0.5 else: tmp = 1.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.2) tmp = Float64(x * 0.5); else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.2) tmp = x * 0.5; else tmp = 1.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.2], N[(x * 0.5), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.2:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 41.6%
Taylor expanded in x around 0
lower-*.f6462.8
Applied rewrites62.8%
if 0.20000000000000001 < x Initial program 98.4%
Applied rewrites9.2%
lift-pow.f64N/A
pow-base-19.2
Applied rewrites9.2%
Final simplification48.2%
(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 57.1%
Applied rewrites7.6%
lift-pow.f64N/A
pow-base-17.6
Applied rewrites7.6%
(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 2024214
(FPCore (x)
:name "tanhf (example 3.4)"
:precision binary64
:alt
(! :herbie-platform default (tan (/ x 2)))
(/ (- 1.0 (cos x)) (sin x)))