
(FPCore (x eps) :precision binary64 (- (cos (+ x eps)) (cos x)))
double code(double x, double eps) {
return cos((x + eps)) - cos(x);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos((x + eps)) - cos(x)
end function
public static double code(double x, double eps) {
return Math.cos((x + eps)) - Math.cos(x);
}
def code(x, eps): return math.cos((x + eps)) - math.cos(x)
function code(x, eps) return Float64(cos(Float64(x + eps)) - cos(x)) end
function tmp = code(x, eps) tmp = cos((x + eps)) - cos(x); end
code[x_, eps_] := N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(x + \varepsilon\right) - \cos x
\end{array}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (cos (+ x eps)) (cos x)))
double code(double x, double eps) {
return cos((x + eps)) - cos(x);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos((x + eps)) - cos(x)
end function
public static double code(double x, double eps) {
return Math.cos((x + eps)) - Math.cos(x);
}
def code(x, eps): return math.cos((x + eps)) - math.cos(x)
function code(x, eps) return Float64(cos(Float64(x + eps)) - cos(x)) end
function tmp = code(x, eps) tmp = cos((x + eps)) - cos(x); end
code[x_, eps_] := N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(x + \varepsilon\right) - \cos x
\end{array}
(FPCore (x eps)
:precision binary64
(let* ((t_0
(fma
-0.5
(cos x)
(*
eps
(fma
0.041666666666666664
(* eps (cos x))
(* 0.16666666666666666 (sin x)))))))
(if (<= eps 1.5e-14)
(fma
(fma
(fma
(* eps x)
(fma -0.027777777777777776 (* eps eps) 0.16666666666666666)
(* (fma -0.020833333333333332 (* eps eps) 0.25) (* eps eps)))
x
(* (- (* (* eps eps) 0.16666666666666666) 1.0) eps))
x
(* (- (* (* eps eps) 0.041666666666666664) 0.5) (* eps eps)))
(/
(* eps (- (pow (* eps t_0) 3.0) (pow (sin x) 3.0)))
(fma
eps
(* (sin x) t_0)
(fma (* eps eps) (pow t_0 2.0) (pow (sin x) 2.0)))))))
double code(double x, double eps) {
double t_0 = fma(-0.5, cos(x), (eps * fma(0.041666666666666664, (eps * cos(x)), (0.16666666666666666 * sin(x)))));
double tmp;
if (eps <= 1.5e-14) {
tmp = fma(fma(fma((eps * x), fma(-0.027777777777777776, (eps * eps), 0.16666666666666666), (fma(-0.020833333333333332, (eps * eps), 0.25) * (eps * eps))), x, ((((eps * eps) * 0.16666666666666666) - 1.0) * eps)), x, ((((eps * eps) * 0.041666666666666664) - 0.5) * (eps * eps)));
} else {
tmp = (eps * (pow((eps * t_0), 3.0) - pow(sin(x), 3.0))) / fma(eps, (sin(x) * t_0), fma((eps * eps), pow(t_0, 2.0), pow(sin(x), 2.0)));
}
return tmp;
}
function code(x, eps) t_0 = fma(-0.5, cos(x), Float64(eps * fma(0.041666666666666664, Float64(eps * cos(x)), Float64(0.16666666666666666 * sin(x))))) tmp = 0.0 if (eps <= 1.5e-14) tmp = fma(fma(fma(Float64(eps * x), fma(-0.027777777777777776, Float64(eps * eps), 0.16666666666666666), Float64(fma(-0.020833333333333332, Float64(eps * eps), 0.25) * Float64(eps * eps))), x, Float64(Float64(Float64(Float64(eps * eps) * 0.16666666666666666) - 1.0) * eps)), x, Float64(Float64(Float64(Float64(eps * eps) * 0.041666666666666664) - 0.5) * Float64(eps * eps))); else tmp = Float64(Float64(eps * Float64((Float64(eps * t_0) ^ 3.0) - (sin(x) ^ 3.0))) / fma(eps, Float64(sin(x) * t_0), fma(Float64(eps * eps), (t_0 ^ 2.0), (sin(x) ^ 2.0)))); end return tmp end
code[x_, eps_] := Block[{t$95$0 = N[(-0.5 * N[Cos[x], $MachinePrecision] + N[(eps * N[(0.041666666666666664 * N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(0.16666666666666666 * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[eps, 1.5e-14], N[(N[(N[(N[(eps * x), $MachinePrecision] * N[(-0.027777777777777776 * N[(eps * eps), $MachinePrecision] + 0.16666666666666666), $MachinePrecision] + N[(N[(-0.020833333333333332 * N[(eps * eps), $MachinePrecision] + 0.25), $MachinePrecision] * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x + N[(N[(N[(N[(eps * eps), $MachinePrecision] * 0.16666666666666666), $MachinePrecision] - 1.0), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision] * x + N[(N[(N[(N[(eps * eps), $MachinePrecision] * 0.041666666666666664), $MachinePrecision] - 0.5), $MachinePrecision] * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(eps * N[(N[Power[N[(eps * t$95$0), $MachinePrecision], 3.0], $MachinePrecision] - N[Power[N[Sin[x], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(eps * N[(N[Sin[x], $MachinePrecision] * t$95$0), $MachinePrecision] + N[(N[(eps * eps), $MachinePrecision] * N[Power[t$95$0, 2.0], $MachinePrecision] + N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-0.5, \cos x, \varepsilon \cdot \mathsf{fma}\left(0.041666666666666664, \varepsilon \cdot \cos x, 0.16666666666666666 \cdot \sin x\right)\right)\\
\mathbf{if}\;\varepsilon \leq 1.5 \cdot 10^{-14}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\varepsilon \cdot x, \mathsf{fma}\left(-0.027777777777777776, \varepsilon \cdot \varepsilon, 0.16666666666666666\right), \mathsf{fma}\left(-0.020833333333333332, \varepsilon \cdot \varepsilon, 0.25\right) \cdot \left(\varepsilon \cdot \varepsilon\right)\right), x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 0.16666666666666666 - 1\right) \cdot \varepsilon\right), x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 0.041666666666666664 - 0.5\right) \cdot \left(\varepsilon \cdot \varepsilon\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon \cdot \left({\left(\varepsilon \cdot t\_0\right)}^{3} - {\sin x}^{3}\right)}{\mathsf{fma}\left(\varepsilon, \sin x \cdot t\_0, \mathsf{fma}\left(\varepsilon \cdot \varepsilon, {t\_0}^{2}, {\sin x}^{2}\right)\right)}\\
\end{array}
\end{array}
if eps < 1.4999999999999999e-14Initial program 52.5%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites99.8%
if 1.4999999999999999e-14 < eps Initial program 45.9%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites89.6%
Applied rewrites89.5%
Taylor expanded in x around inf
Applied rewrites89.4%
(FPCore (x eps)
:precision binary64
(if (<= eps 1.5e-14)
(fma
(fma
(fma
(* eps x)
(fma -0.027777777777777776 (* eps eps) 0.16666666666666666)
(* (fma -0.020833333333333332 (* eps eps) 0.25) (* eps eps)))
x
(* (- (* (* eps eps) 0.16666666666666666) 1.0) eps))
x
(* (- (* (* eps eps) 0.041666666666666664) 0.5) (* eps eps)))
(*
(pow eps 4.0)
(fma
-1.0
(/
(fma
-1.0
(/ (fma -1.0 (/ (sin x) eps) (* -0.5 (cos x))) eps)
(* -0.16666666666666666 (sin x)))
eps)
(* 0.041666666666666664 (cos x))))))
double code(double x, double eps) {
double tmp;
if (eps <= 1.5e-14) {
tmp = fma(fma(fma((eps * x), fma(-0.027777777777777776, (eps * eps), 0.16666666666666666), (fma(-0.020833333333333332, (eps * eps), 0.25) * (eps * eps))), x, ((((eps * eps) * 0.16666666666666666) - 1.0) * eps)), x, ((((eps * eps) * 0.041666666666666664) - 0.5) * (eps * eps)));
} else {
tmp = pow(eps, 4.0) * fma(-1.0, (fma(-1.0, (fma(-1.0, (sin(x) / eps), (-0.5 * cos(x))) / eps), (-0.16666666666666666 * sin(x))) / eps), (0.041666666666666664 * cos(x)));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (eps <= 1.5e-14) tmp = fma(fma(fma(Float64(eps * x), fma(-0.027777777777777776, Float64(eps * eps), 0.16666666666666666), Float64(fma(-0.020833333333333332, Float64(eps * eps), 0.25) * Float64(eps * eps))), x, Float64(Float64(Float64(Float64(eps * eps) * 0.16666666666666666) - 1.0) * eps)), x, Float64(Float64(Float64(Float64(eps * eps) * 0.041666666666666664) - 0.5) * Float64(eps * eps))); else tmp = Float64((eps ^ 4.0) * fma(-1.0, Float64(fma(-1.0, Float64(fma(-1.0, Float64(sin(x) / eps), Float64(-0.5 * cos(x))) / eps), Float64(-0.16666666666666666 * sin(x))) / eps), Float64(0.041666666666666664 * cos(x)))); end return tmp end
code[x_, eps_] := If[LessEqual[eps, 1.5e-14], N[(N[(N[(N[(eps * x), $MachinePrecision] * N[(-0.027777777777777776 * N[(eps * eps), $MachinePrecision] + 0.16666666666666666), $MachinePrecision] + N[(N[(-0.020833333333333332 * N[(eps * eps), $MachinePrecision] + 0.25), $MachinePrecision] * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x + N[(N[(N[(N[(eps * eps), $MachinePrecision] * 0.16666666666666666), $MachinePrecision] - 1.0), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision] * x + N[(N[(N[(N[(eps * eps), $MachinePrecision] * 0.041666666666666664), $MachinePrecision] - 0.5), $MachinePrecision] * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[eps, 4.0], $MachinePrecision] * N[(-1.0 * N[(N[(-1.0 * N[(N[(-1.0 * N[(N[Sin[x], $MachinePrecision] / eps), $MachinePrecision] + N[(-0.5 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / eps), $MachinePrecision] + N[(-0.16666666666666666 * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / eps), $MachinePrecision] + N[(0.041666666666666664 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq 1.5 \cdot 10^{-14}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\varepsilon \cdot x, \mathsf{fma}\left(-0.027777777777777776, \varepsilon \cdot \varepsilon, 0.16666666666666666\right), \mathsf{fma}\left(-0.020833333333333332, \varepsilon \cdot \varepsilon, 0.25\right) \cdot \left(\varepsilon \cdot \varepsilon\right)\right), x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 0.16666666666666666 - 1\right) \cdot \varepsilon\right), x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 0.041666666666666664 - 0.5\right) \cdot \left(\varepsilon \cdot \varepsilon\right)\right)\\
\mathbf{else}:\\
\;\;\;\;{\varepsilon}^{4} \cdot \mathsf{fma}\left(-1, \frac{\mathsf{fma}\left(-1, \frac{\mathsf{fma}\left(-1, \frac{\sin x}{\varepsilon}, -0.5 \cdot \cos x\right)}{\varepsilon}, -0.16666666666666666 \cdot \sin x\right)}{\varepsilon}, 0.041666666666666664 \cdot \cos x\right)\\
\end{array}
\end{array}
if eps < 1.4999999999999999e-14Initial program 52.5%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites99.8%
if 1.4999999999999999e-14 < eps Initial program 45.9%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites89.6%
Taylor expanded in x around 0
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites51.9%
Taylor expanded in eps around -inf
lower-*.f64N/A
lower-pow.f64N/A
lower-fma.f64N/A
Applied rewrites89.4%
(FPCore (x eps)
:precision binary64
(*
(-
(*
(fma
(fma (sin x) 0.16666666666666666 (* (* (cos x) eps) 0.041666666666666664))
eps
(* -0.5 (cos x)))
eps)
(sin x))
eps))
double code(double x, double eps) {
return ((fma(fma(sin(x), 0.16666666666666666, ((cos(x) * eps) * 0.041666666666666664)), eps, (-0.5 * cos(x))) * eps) - sin(x)) * eps;
}
function code(x, eps) return Float64(Float64(Float64(fma(fma(sin(x), 0.16666666666666666, Float64(Float64(cos(x) * eps) * 0.041666666666666664)), eps, Float64(-0.5 * cos(x))) * eps) - sin(x)) * eps) end
code[x_, eps_] := N[(N[(N[(N[(N[(N[Sin[x], $MachinePrecision] * 0.16666666666666666 + N[(N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision] * 0.041666666666666664), $MachinePrecision]), $MachinePrecision] * eps + N[(-0.5 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\left(\mathsf{fma}\left(\mathsf{fma}\left(\sin x, 0.16666666666666666, \left(\cos x \cdot \varepsilon\right) \cdot 0.041666666666666664\right), \varepsilon, -0.5 \cdot \cos x\right) \cdot \varepsilon - \sin x\right) \cdot \varepsilon
\end{array}
Initial program 52.3%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.5%
(FPCore (x eps)
:precision binary64
(*
(pow eps 4.0)
(fma
-1.0
(/
(fma
-1.0
(/ (fma -1.0 (/ (sin x) eps) (* -0.5 (cos x))) eps)
(* -0.16666666666666666 (sin x)))
eps)
(* 0.041666666666666664 (cos x)))))
double code(double x, double eps) {
return pow(eps, 4.0) * fma(-1.0, (fma(-1.0, (fma(-1.0, (sin(x) / eps), (-0.5 * cos(x))) / eps), (-0.16666666666666666 * sin(x))) / eps), (0.041666666666666664 * cos(x)));
}
function code(x, eps) return Float64((eps ^ 4.0) * fma(-1.0, Float64(fma(-1.0, Float64(fma(-1.0, Float64(sin(x) / eps), Float64(-0.5 * cos(x))) / eps), Float64(-0.16666666666666666 * sin(x))) / eps), Float64(0.041666666666666664 * cos(x)))) end
code[x_, eps_] := N[(N[Power[eps, 4.0], $MachinePrecision] * N[(-1.0 * N[(N[(-1.0 * N[(N[(-1.0 * N[(N[Sin[x], $MachinePrecision] / eps), $MachinePrecision] + N[(-0.5 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / eps), $MachinePrecision] + N[(-0.16666666666666666 * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / eps), $MachinePrecision] + N[(0.041666666666666664 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\varepsilon}^{4} \cdot \mathsf{fma}\left(-1, \frac{\mathsf{fma}\left(-1, \frac{\mathsf{fma}\left(-1, \frac{\sin x}{\varepsilon}, -0.5 \cdot \cos x\right)}{\varepsilon}, -0.16666666666666666 \cdot \sin x\right)}{\varepsilon}, 0.041666666666666664 \cdot \cos x\right)
\end{array}
Initial program 52.3%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.5%
Taylor expanded in x around 0
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites98.3%
Taylor expanded in eps around -inf
lower-*.f64N/A
lower-pow.f64N/A
lower-fma.f64N/A
Applied rewrites26.0%
herbie shell --seed 2025093
(FPCore (x eps)
:name "2cos (problem 3.3.5)"
:precision binary64
:pre (and (and (and (<= -10000.0 x) (<= x 10000.0)) (< (* 1e-16 (fabs x)) eps)) (< eps (fabs x)))
(- (cos (+ x eps)) (cos x)))