
(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 5 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 59.3%
hang-p0-tanN/A
tan-lowering-tan.f64N/A
div-invN/A
metadata-evalN/A
*-lowering-*.f64100.0
Applied egg-rr100.0%
(FPCore (x)
:precision binary64
(if (<= (/ (- 1.0 (cos x)) (sin x)) 0.0001)
(*
x
(*
(fma x (* x 0.041666666666666664) 0.5)
(/ (* x -0.5) (* x (fma x (* x 0.041666666666666664) -0.5)))))
1.0))
double code(double x) {
double tmp;
if (((1.0 - cos(x)) / sin(x)) <= 0.0001) {
tmp = x * (fma(x, (x * 0.041666666666666664), 0.5) * ((x * -0.5) / (x * fma(x, (x * 0.041666666666666664), -0.5))));
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(1.0 - cos(x)) / sin(x)) <= 0.0001) tmp = Float64(x * Float64(fma(x, Float64(x * 0.041666666666666664), 0.5) * Float64(Float64(x * -0.5) / Float64(x * fma(x, Float64(x * 0.041666666666666664), -0.5))))); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision], 0.0001], N[(x * N[(N[(x * N[(x * 0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision] * N[(N[(x * -0.5), $MachinePrecision] / N[(x * N[(x * N[(x * 0.041666666666666664), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \leq 0.0001:\\
\;\;\;\;x \cdot \left(\mathsf{fma}\left(x, x \cdot 0.041666666666666664, 0.5\right) \cdot \frac{x \cdot -0.5}{x \cdot \mathsf{fma}\left(x, x \cdot 0.041666666666666664, -0.5\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 (-.f64 #s(literal 1 binary64) (cos.f64 x)) (sin.f64 x)) < 1.00000000000000005e-4Initial program 43.6%
Taylor expanded in x around 0
*-lowering-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f6459.8
Simplified59.8%
distribute-lft-inN/A
flip-+N/A
div-invN/A
*-lowering-*.f64N/A
Applied egg-rr38.0%
Taylor expanded in x around 0
Simplified38.2%
associate-*l*N/A
metadata-evalN/A
sub-negN/A
associate-*l*N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
sub-negN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f64N/A
un-div-invN/A
/-lowering-/.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f6459.7
Applied egg-rr59.7%
if 1.00000000000000005e-4 < (/.f64 (-.f64 #s(literal 1 binary64) (cos.f64 x)) (sin.f64 x)) Initial program 98.5%
Applied egg-rr18.1%
pow-base-118.1
Applied egg-rr18.1%
(FPCore (x) :precision binary64 (if (<= x 3.15) (* x (fma x (* x 0.041666666666666664) 0.5)) 1.0))
double code(double x) {
double tmp;
if (x <= 3.15) {
tmp = x * fma(x, (x * 0.041666666666666664), 0.5);
} else {
tmp = 1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 3.15) tmp = Float64(x * fma(x, Float64(x * 0.041666666666666664), 0.5)); else tmp = 1.0; end return tmp end
code[x_] := If[LessEqual[x, 3.15], 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 3.15:\\
\;\;\;\;x \cdot \mathsf{fma}\left(x, x \cdot 0.041666666666666664, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 3.14999999999999991Initial program 40.9%
Taylor expanded in x around 0
*-lowering-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f6462.9
Simplified62.9%
if 3.14999999999999991 < x Initial program 98.3%
Applied egg-rr9.4%
pow-base-19.4
Applied egg-rr9.4%
(FPCore (x) :precision binary64 (if (<= x 1.4) (* x 0.5) 1.0))
double code(double x) {
double tmp;
if (x <= 1.4) {
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 <= 1.4d0) 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 <= 1.4) {
tmp = x * 0.5;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4: tmp = x * 0.5 else: tmp = 1.0 return tmp
function code(x) tmp = 0.0 if (x <= 1.4) tmp = Float64(x * 0.5); else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4) tmp = x * 0.5; else tmp = 1.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4], N[(x * 0.5), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 1.3999999999999999Initial program 40.9%
Taylor expanded in x around 0
*-lowering-*.f6463.0
Simplified63.0%
if 1.3999999999999999 < x Initial program 98.3%
Applied egg-rr9.4%
pow-base-19.4
Applied egg-rr9.4%
Final simplification45.8%
(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 59.3%
Applied egg-rr7.3%
pow-base-17.3
Applied egg-rr7.3%
(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 2024199
(FPCore (x)
:name "tanhf (example 3.4)"
:precision binary64
:alt
(! :herbie-platform default (tan (/ x 2)))
(/ (- 1.0 (cos x)) (sin x)))