
(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 (/ 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}
Initial program 56.3%
hang-p0-tan100.0%
Simplified100.0%
(FPCore (x) :precision binary64 (if (<= x 2.1) (* x 0.5) (+ 1.0 (/ (+ -4.0 (/ 16.0 x)) x))))
double code(double x) {
double tmp;
if (x <= 2.1) {
tmp = x * 0.5;
} else {
tmp = 1.0 + ((-4.0 + (16.0 / x)) / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.1d0) then
tmp = x * 0.5d0
else
tmp = 1.0d0 + (((-4.0d0) + (16.0d0 / x)) / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.1) {
tmp = x * 0.5;
} else {
tmp = 1.0 + ((-4.0 + (16.0 / x)) / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.1: tmp = x * 0.5 else: tmp = 1.0 + ((-4.0 + (16.0 / x)) / x) return tmp
function code(x) tmp = 0.0 if (x <= 2.1) tmp = Float64(x * 0.5); else tmp = Float64(1.0 + Float64(Float64(-4.0 + Float64(16.0 / x)) / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.1) tmp = x * 0.5; else tmp = 1.0 + ((-4.0 + (16.0 / x)) / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.1], N[(x * 0.5), $MachinePrecision], N[(1.0 + N[(N[(-4.0 + N[(16.0 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.1:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{-4 + \frac{16}{x}}{x}\\
\end{array}
\end{array}
if x < 2.10000000000000009Initial program 40.6%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 63.9%
if 2.10000000000000009 < x Initial program 98.7%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 2.5%
expm1-log1p-u2.5%
expm1-undefine2.5%
flip--2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
metadata-eval2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
Applied egg-rr2.3%
Taylor expanded in x around 0 8.4%
Taylor expanded in x around -inf 8.4%
associate-*r/8.4%
sub-neg8.4%
distribute-rgt-in8.4%
metadata-eval8.4%
distribute-rgt-neg-in8.4%
distribute-frac-neg28.4%
associate-*r*8.4%
*-commutative8.4%
mul-1-neg8.4%
distribute-neg-frac28.4%
remove-double-neg8.4%
associate-*r/8.4%
metadata-eval8.4%
Simplified8.4%
Final simplification48.9%
(FPCore (x) :precision binary64 (if (<= x 3.1) (* x 0.5) (- 1.0 (/ 4.0 x))))
double code(double x) {
double tmp;
if (x <= 3.1) {
tmp = x * 0.5;
} else {
tmp = 1.0 - (4.0 / x);
}
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 - (4.0d0 / x)
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 - (4.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.1: tmp = x * 0.5 else: tmp = 1.0 - (4.0 / x) return tmp
function code(x) tmp = 0.0 if (x <= 3.1) tmp = Float64(x * 0.5); else tmp = Float64(1.0 - Float64(4.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.1) tmp = x * 0.5; else tmp = 1.0 - (4.0 / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.1], N[(x * 0.5), $MachinePrecision], N[(1.0 - N[(4.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.1:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;1 - \frac{4}{x}\\
\end{array}
\end{array}
if x < 3.10000000000000009Initial program 40.6%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 63.9%
if 3.10000000000000009 < x Initial program 98.7%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 2.5%
expm1-log1p-u2.5%
expm1-undefine2.5%
flip--2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
metadata-eval2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
Applied egg-rr2.3%
Taylor expanded in x around 0 8.4%
Taylor expanded in x around inf 8.4%
associate-*r/8.4%
metadata-eval8.4%
Simplified8.4%
Final simplification48.9%
(FPCore (x) :precision binary64 (/ x (+ 1.0 (+ (* x 0.5) 1.0))))
double code(double x) {
return x / (1.0 + ((x * 0.5) + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x / (1.0d0 + ((x * 0.5d0) + 1.0d0))
end function
public static double code(double x) {
return x / (1.0 + ((x * 0.5) + 1.0));
}
def code(x): return x / (1.0 + ((x * 0.5) + 1.0))
function code(x) return Float64(x / Float64(1.0 + Float64(Float64(x * 0.5) + 1.0))) end
function tmp = code(x) tmp = x / (1.0 + ((x * 0.5) + 1.0)); end
code[x_] := N[(x / N[(1.0 + N[(N[(x * 0.5), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{1 + \left(x \cdot 0.5 + 1\right)}
\end{array}
Initial program 56.3%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 47.3%
expm1-log1p-u46.5%
expm1-undefine4.7%
flip--4.7%
log1p-undefine4.7%
rem-exp-log4.7%
+-commutative4.7%
log1p-undefine4.7%
rem-exp-log4.7%
+-commutative4.7%
metadata-eval4.7%
log1p-undefine4.7%
rem-exp-log5.4%
+-commutative5.4%
Applied egg-rr5.4%
Taylor expanded in x around 0 50.2%
Final simplification50.2%
(FPCore (x) :precision binary64 (if (<= x 1.95) (* x 0.5) 2.0))
double code(double x) {
double tmp;
if (x <= 1.95) {
tmp = x * 0.5;
} else {
tmp = 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.95d0) then
tmp = x * 0.5d0
else
tmp = 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.95) {
tmp = x * 0.5;
} else {
tmp = 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.95: tmp = x * 0.5 else: tmp = 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 1.95) tmp = Float64(x * 0.5); else tmp = 2.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.95) tmp = x * 0.5; else tmp = 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.95], N[(x * 0.5), $MachinePrecision], 2.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.95:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;2\\
\end{array}
\end{array}
if x < 1.94999999999999996Initial program 40.6%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 63.9%
if 1.94999999999999996 < x Initial program 98.7%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 2.5%
expm1-log1p-u2.5%
expm1-undefine2.5%
flip--2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
metadata-eval2.3%
log1p-undefine2.3%
rem-exp-log2.3%
+-commutative2.3%
Applied egg-rr2.3%
Taylor expanded in x around 0 8.5%
Taylor expanded in x around inf 8.5%
Final simplification49.0%
(FPCore (x) :precision binary64 2.0)
double code(double x) {
return 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0
end function
public static double code(double x) {
return 2.0;
}
def code(x): return 2.0
function code(x) return 2.0 end
function tmp = code(x) tmp = 2.0; end
code[x_] := 2.0
\begin{array}{l}
\\
2
\end{array}
Initial program 56.3%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 47.3%
expm1-log1p-u46.5%
expm1-undefine4.7%
flip--4.7%
log1p-undefine4.7%
rem-exp-log4.7%
+-commutative4.7%
log1p-undefine4.7%
rem-exp-log4.7%
+-commutative4.7%
metadata-eval4.7%
log1p-undefine4.7%
rem-exp-log5.4%
+-commutative5.4%
Applied egg-rr5.4%
Taylor expanded in x around 0 50.2%
Taylor expanded in x around inf 6.7%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 56.3%
hang-p0-tan100.0%
Simplified100.0%
Taylor expanded in x around 0 47.3%
expm1-log1p-u46.5%
expm1-undefine4.7%
log1p-undefine4.7%
rem-exp-log5.5%
+-commutative5.5%
Applied egg-rr5.5%
Taylor expanded in x around 0 4.1%
metadata-eval4.1%
Applied egg-rr4.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 2024149
(FPCore (x)
:name "tanhf (example 3.4)"
:precision binary64
:alt
(! :herbie-platform default (tan (/ x 2)))
(/ (- 1.0 (cos x)) (sin x)))