
(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 4 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 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}
(FPCore (x) :precision binary64 (if (<= x 6.2) (/ 1.0 (+ (* x -0.16666666666666666) (* 2.0 (/ 1.0 x)))) 1.0))
double code(double x) {
double tmp;
if (x <= 6.2) {
tmp = 1.0 / ((x * -0.16666666666666666) + (2.0 * (1.0 / x)));
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.2d0) then
tmp = 1.0d0 / ((x * (-0.16666666666666666d0)) + (2.0d0 * (1.0d0 / x)))
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.2) {
tmp = 1.0 / ((x * -0.16666666666666666) + (2.0 * (1.0 / x)));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.2: tmp = 1.0 / ((x * -0.16666666666666666) + (2.0 * (1.0 / x))) else: tmp = 1.0 return tmp
function code(x) tmp = 0.0 if (x <= 6.2) tmp = Float64(1.0 / Float64(Float64(x * -0.16666666666666666) + Float64(2.0 * Float64(1.0 / x)))); else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.2) tmp = 1.0 / ((x * -0.16666666666666666) + (2.0 * (1.0 / x))); else tmp = 1.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.2], N[(1.0 / N[(N[(x * -0.16666666666666666), $MachinePrecision] + N[(2.0 * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.2:\\
\;\;\;\;\frac{1}{x \cdot -0.16666666666666666 + 2 \cdot \frac{1}{x}}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
(FPCore (x) :precision binary64 (if (<= x 3.1) (* x 0.5) 1.0))
double code(double x) {
double tmp;
if (x <= 3.1) {
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 <= 3.1d0) 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 <= 3.1) {
tmp = x * 0.5;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.1: tmp = x * 0.5 else: tmp = 1.0 return tmp
function code(x) tmp = 0.0 if (x <= 3.1) tmp = Float64(x * 0.5); else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.1) tmp = x * 0.5; else tmp = 1.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.1], N[(x * 0.5), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.1:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
(FPCore (x) :precision binary64 (* x 0.5))
double code(double x) {
return x * 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * 0.5d0
end function
public static double code(double x) {
return x * 0.5;
}
def code(x): return x * 0.5
function code(x) return Float64(x * 0.5) end
function tmp = code(x) tmp = x * 0.5; end
code[x_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
(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 2023341
(FPCore (x)
:name "tanhf (example 3.4)"
:precision binary64
:herbie-target
(tan (/ x 2.0))
(/ (- 1.0 (cos x)) (sin x)))