
(FPCore (x) :precision binary64 (/ (- x (sin x)) (- x (tan x))))
double code(double x) {
return (x - sin(x)) / (x - tan(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x - sin(x)) / (x - tan(x))
end function
public static double code(double x) {
return (x - Math.sin(x)) / (x - Math.tan(x));
}
def code(x): return (x - math.sin(x)) / (x - math.tan(x))
function code(x) return Float64(Float64(x - sin(x)) / Float64(x - tan(x))) end
function tmp = code(x) tmp = (x - sin(x)) / (x - tan(x)); end
code[x_] := N[(N[(x - N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[(x - N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - \sin x}{x - \tan x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (- x (sin x)) (- x (tan x))))
double code(double x) {
return (x - sin(x)) / (x - tan(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x - sin(x)) / (x - tan(x))
end function
public static double code(double x) {
return (x - Math.sin(x)) / (x - Math.tan(x));
}
def code(x): return (x - math.sin(x)) / (x - math.tan(x))
function code(x) return Float64(Float64(x - sin(x)) / Float64(x - tan(x))) end
function tmp = code(x) tmp = (x - sin(x)) / (x - tan(x)); end
code[x_] := N[(N[(x - N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[(x - N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - \sin x}{x - \tan x}
\end{array}
(FPCore (x) :precision binary64 (- (+ (* -0.009642857142857142 (pow x 4.0)) (+ (* 0.00024107142857142857 (pow x 6.0)) (* x (* x 0.225)))) 0.5))
double code(double x) {
return ((-0.009642857142857142 * pow(x, 4.0)) + ((0.00024107142857142857 * pow(x, 6.0)) + (x * (x * 0.225)))) - 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((-0.009642857142857142d0) * (x ** 4.0d0)) + ((0.00024107142857142857d0 * (x ** 6.0d0)) + (x * (x * 0.225d0)))) - 0.5d0
end function
public static double code(double x) {
return ((-0.009642857142857142 * Math.pow(x, 4.0)) + ((0.00024107142857142857 * Math.pow(x, 6.0)) + (x * (x * 0.225)))) - 0.5;
}
def code(x): return ((-0.009642857142857142 * math.pow(x, 4.0)) + ((0.00024107142857142857 * math.pow(x, 6.0)) + (x * (x * 0.225)))) - 0.5
function code(x) return Float64(Float64(Float64(-0.009642857142857142 * (x ^ 4.0)) + Float64(Float64(0.00024107142857142857 * (x ^ 6.0)) + Float64(x * Float64(x * 0.225)))) - 0.5) end
function tmp = code(x) tmp = ((-0.009642857142857142 * (x ^ 4.0)) + ((0.00024107142857142857 * (x ^ 6.0)) + (x * (x * 0.225)))) - 0.5; end
code[x_] := N[(N[(N[(-0.009642857142857142 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.00024107142857142857 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(x * N[(x * 0.225), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]
\begin{array}{l}
\\
\left(-0.009642857142857142 \cdot {x}^{4} + \left(0.00024107142857142857 \cdot {x}^{6} + x \cdot \left(x \cdot 0.225\right)\right)\right) - 0.5
\end{array}
Initial program 3.2%
/-rgt-identity3.2%
metadata-eval3.2%
associate-/r/3.2%
metadata-eval3.2%
metadata-eval3.2%
times-frac3.2%
*-commutative3.2%
*-commutative3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
Simplified3.2%
Taylor expanded in x around 0 98.9%
expm1-log1p-u98.9%
expm1-udef98.9%
Applied egg-rr98.9%
expm1-def98.9%
expm1-log1p-u98.9%
unpow298.9%
associate-*r*98.9%
Applied egg-rr98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (- (+ (* -0.009642857142857142 (pow x 4.0)) (* 0.225 (pow x 2.0))) 0.5))
double code(double x) {
return ((-0.009642857142857142 * pow(x, 4.0)) + (0.225 * pow(x, 2.0))) - 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((-0.009642857142857142d0) * (x ** 4.0d0)) + (0.225d0 * (x ** 2.0d0))) - 0.5d0
end function
public static double code(double x) {
return ((-0.009642857142857142 * Math.pow(x, 4.0)) + (0.225 * Math.pow(x, 2.0))) - 0.5;
}
def code(x): return ((-0.009642857142857142 * math.pow(x, 4.0)) + (0.225 * math.pow(x, 2.0))) - 0.5
function code(x) return Float64(Float64(Float64(-0.009642857142857142 * (x ^ 4.0)) + Float64(0.225 * (x ^ 2.0))) - 0.5) end
function tmp = code(x) tmp = ((-0.009642857142857142 * (x ^ 4.0)) + (0.225 * (x ^ 2.0))) - 0.5; end
code[x_] := N[(N[(N[(-0.009642857142857142 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(0.225 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]
\begin{array}{l}
\\
\left(-0.009642857142857142 \cdot {x}^{4} + 0.225 \cdot {x}^{2}\right) - 0.5
\end{array}
Initial program 3.2%
/-rgt-identity3.2%
metadata-eval3.2%
associate-/r/3.2%
metadata-eval3.2%
metadata-eval3.2%
times-frac3.2%
*-commutative3.2%
*-commutative3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
Simplified3.2%
Taylor expanded in x around 0 98.6%
Final simplification98.6%
(FPCore (x) :precision binary64 (- (* 3.0 (log (+ 1.0 (* (pow x 2.0) 0.075)))) 0.5))
double code(double x) {
return (3.0 * log((1.0 + (pow(x, 2.0) * 0.075)))) - 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (3.0d0 * log((1.0d0 + ((x ** 2.0d0) * 0.075d0)))) - 0.5d0
end function
public static double code(double x) {
return (3.0 * Math.log((1.0 + (Math.pow(x, 2.0) * 0.075)))) - 0.5;
}
def code(x): return (3.0 * math.log((1.0 + (math.pow(x, 2.0) * 0.075)))) - 0.5
function code(x) return Float64(Float64(3.0 * log(Float64(1.0 + Float64((x ^ 2.0) * 0.075)))) - 0.5) end
function tmp = code(x) tmp = (3.0 * log((1.0 + ((x ^ 2.0) * 0.075)))) - 0.5; end
code[x_] := N[(N[(3.0 * N[Log[N[(1.0 + N[(N[Power[x, 2.0], $MachinePrecision] * 0.075), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]
\begin{array}{l}
\\
3 \cdot \log \left(1 + {x}^{2} \cdot 0.075\right) - 0.5
\end{array}
Initial program 3.2%
/-rgt-identity3.2%
metadata-eval3.2%
associate-/r/3.2%
metadata-eval3.2%
metadata-eval3.2%
times-frac3.2%
*-commutative3.2%
*-commutative3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
Simplified3.2%
Taylor expanded in x around 0 98.9%
add-log-exp98.8%
fma-def98.8%
+-commutative98.8%
fma-def98.8%
Applied egg-rr98.8%
add-cube-cbrt98.8%
log-prod98.8%
pow298.8%
Applied egg-rr98.8%
log-pow98.8%
distribute-lft1-in98.8%
metadata-eval98.8%
fma-def98.8%
fma-def98.8%
associate-+l+98.8%
+-commutative98.8%
fma-def98.8%
fma-def98.8%
Simplified98.8%
Taylor expanded in x around 0 98.6%
*-commutative98.6%
Simplified98.6%
Final simplification98.6%
(FPCore (x) :precision binary64 (- (* 0.225 (pow x 2.0)) 0.5))
double code(double x) {
return (0.225 * pow(x, 2.0)) - 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.225d0 * (x ** 2.0d0)) - 0.5d0
end function
public static double code(double x) {
return (0.225 * Math.pow(x, 2.0)) - 0.5;
}
def code(x): return (0.225 * math.pow(x, 2.0)) - 0.5
function code(x) return Float64(Float64(0.225 * (x ^ 2.0)) - 0.5) end
function tmp = code(x) tmp = (0.225 * (x ^ 2.0)) - 0.5; end
code[x_] := N[(N[(0.225 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]
\begin{array}{l}
\\
0.225 \cdot {x}^{2} - 0.5
\end{array}
Initial program 3.2%
/-rgt-identity3.2%
metadata-eval3.2%
associate-/r/3.2%
metadata-eval3.2%
metadata-eval3.2%
times-frac3.2%
*-commutative3.2%
*-commutative3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
Simplified3.2%
Taylor expanded in x around 0 98.5%
Final simplification98.5%
(FPCore (x) :precision binary64 -0.5)
double code(double x) {
return -0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -0.5d0
end function
public static double code(double x) {
return -0.5;
}
def code(x): return -0.5
function code(x) return -0.5 end
function tmp = code(x) tmp = -0.5; end
code[x_] := -0.5
\begin{array}{l}
\\
-0.5
\end{array}
Initial program 3.2%
/-rgt-identity3.2%
metadata-eval3.2%
associate-/r/3.2%
metadata-eval3.2%
metadata-eval3.2%
times-frac3.2%
*-commutative3.2%
*-commutative3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
neg-mul-13.2%
neg-sub03.2%
associate-+l-3.2%
neg-sub03.2%
+-commutative3.2%
unsub-neg3.2%
Simplified3.2%
Taylor expanded in x around 0 97.7%
Final simplification97.7%
(FPCore (x)
:precision binary64
(if (<= (fabs x) 1.0)
(+
(+ (+ -0.5 (/ (* 9.0 (pow x 2.0)) 40.0)) (/ (* -27.0 (pow x 4.0)) 2800.0))
(/ (* 27.0 (pow x 6.0)) 112000.0))
(/ (- x (sin x)) (- x (tan x)))))
double code(double x) {
double tmp;
if (fabs(x) <= 1.0) {
tmp = ((-0.5 + ((9.0 * pow(x, 2.0)) / 40.0)) + ((-27.0 * pow(x, 4.0)) / 2800.0)) + ((27.0 * pow(x, 6.0)) / 112000.0);
} else {
tmp = (x - sin(x)) / (x - tan(x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (abs(x) <= 1.0d0) then
tmp = (((-0.5d0) + ((9.0d0 * (x ** 2.0d0)) / 40.0d0)) + (((-27.0d0) * (x ** 4.0d0)) / 2800.0d0)) + ((27.0d0 * (x ** 6.0d0)) / 112000.0d0)
else
tmp = (x - sin(x)) / (x - tan(x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (Math.abs(x) <= 1.0) {
tmp = ((-0.5 + ((9.0 * Math.pow(x, 2.0)) / 40.0)) + ((-27.0 * Math.pow(x, 4.0)) / 2800.0)) + ((27.0 * Math.pow(x, 6.0)) / 112000.0);
} else {
tmp = (x - Math.sin(x)) / (x - Math.tan(x));
}
return tmp;
}
def code(x): tmp = 0 if math.fabs(x) <= 1.0: tmp = ((-0.5 + ((9.0 * math.pow(x, 2.0)) / 40.0)) + ((-27.0 * math.pow(x, 4.0)) / 2800.0)) + ((27.0 * math.pow(x, 6.0)) / 112000.0) else: tmp = (x - math.sin(x)) / (x - math.tan(x)) return tmp
function code(x) tmp = 0.0 if (abs(x) <= 1.0) tmp = Float64(Float64(Float64(-0.5 + Float64(Float64(9.0 * (x ^ 2.0)) / 40.0)) + Float64(Float64(-27.0 * (x ^ 4.0)) / 2800.0)) + Float64(Float64(27.0 * (x ^ 6.0)) / 112000.0)); else tmp = Float64(Float64(x - sin(x)) / Float64(x - tan(x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) <= 1.0) tmp = ((-0.5 + ((9.0 * (x ^ 2.0)) / 40.0)) + ((-27.0 * (x ^ 4.0)) / 2800.0)) + ((27.0 * (x ^ 6.0)) / 112000.0); else tmp = (x - sin(x)) / (x - tan(x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[Abs[x], $MachinePrecision], 1.0], N[(N[(N[(-0.5 + N[(N[(9.0 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] / 40.0), $MachinePrecision]), $MachinePrecision] + N[(N[(-27.0 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] / 2800.0), $MachinePrecision]), $MachinePrecision] + N[(N[(27.0 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] / 112000.0), $MachinePrecision]), $MachinePrecision], N[(N[(x - N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[(x - N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1:\\
\;\;\;\;\left(\left(-0.5 + \frac{9 \cdot {x}^{2}}{40}\right) + \frac{-27 \cdot {x}^{4}}{2800}\right) + \frac{27 \cdot {x}^{6}}{112000}\\
\mathbf{else}:\\
\;\;\;\;\frac{x - \sin x}{x - \tan x}\\
\end{array}
\end{array}
herbie shell --seed 2023333
(FPCore (x)
:name "sintan (problem 3.4.5)"
:precision binary64
:pre (and (<= -100000.0 x) (<= x 100000.0))
:herbie-target
(if (<= (fabs x) 1.0) (+ (+ (+ -0.5 (/ (* 9.0 (pow x 2.0)) 40.0)) (/ (* -27.0 (pow x 4.0)) 2800.0)) (/ (* 27.0 (pow x 6.0)) 112000.0)) (/ (- x (sin x)) (- x (tan x))))
(/ (- x (sin x)) (- x (tan x))))