
(FPCore (x) :precision binary64 (* (cos x) (exp (* 10.0 (* x x)))))
double code(double x) {
return cos(x) * exp((10.0 * (x * 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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = cos(x) * exp((10.0d0 * (x * x)))
end function
public static double code(double x) {
return Math.cos(x) * Math.exp((10.0 * (x * x)));
}
def code(x): return math.cos(x) * math.exp((10.0 * (x * x)))
function code(x) return Float64(cos(x) * exp(Float64(10.0 * Float64(x * x)))) end
function tmp = code(x) tmp = cos(x) * exp((10.0 * (x * x))); end
code[x_] := N[(N[Cos[x], $MachinePrecision] * N[Exp[N[(10.0 * N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot e^{10 \cdot \left(x \cdot x\right)}
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (* (cos x) (exp (* 10.0 (* x x)))))
double code(double x) {
return cos(x) * exp((10.0 * (x * 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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = cos(x) * exp((10.0d0 * (x * x)))
end function
public static double code(double x) {
return Math.cos(x) * Math.exp((10.0 * (x * x)));
}
def code(x): return math.cos(x) * math.exp((10.0 * (x * x)))
function code(x) return Float64(cos(x) * exp(Float64(10.0 * Float64(x * x)))) end
function tmp = code(x) tmp = cos(x) * exp((10.0 * (x * x))); end
code[x_] := N[(N[Cos[x], $MachinePrecision] * N[Exp[N[(10.0 * N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot e^{10 \cdot \left(x \cdot x\right)}
\end{array}
(FPCore (x) :precision binary64 (* (fma (sin x) (sin (fma PI 0.5 (/ PI 2.0))) (* (sin (* PI 0.5)) (cos x))) (pow (pow (pow (exp 10.0) 2.0) x) (/ (* -1.0 x) -2.0))))
double code(double x) {
return fma(sin(x), sin(fma(((double) M_PI), 0.5, (((double) M_PI) / 2.0))), (sin((((double) M_PI) * 0.5)) * cos(x))) * pow(pow(pow(exp(10.0), 2.0), x), ((-1.0 * x) / -2.0));
}
function code(x) return Float64(fma(sin(x), sin(fma(pi, 0.5, Float64(pi / 2.0))), Float64(sin(Float64(pi * 0.5)) * cos(x))) * (((exp(10.0) ^ 2.0) ^ x) ^ Float64(Float64(-1.0 * x) / -2.0))) end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] * N[Sin[N[(Pi * 0.5 + N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + N[(N[Sin[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[Power[N[Power[N[Exp[10.0], $MachinePrecision], 2.0], $MachinePrecision], x], $MachinePrecision], N[(N[(-1.0 * x), $MachinePrecision] / -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin x, \sin \left(\mathsf{fma}\left(\pi, 0.5, \frac{\pi}{2}\right)\right), \sin \left(\pi \cdot 0.5\right) \cdot \cos x\right) \cdot {\left({\left({\left(e^{10}\right)}^{2}\right)}^{x}\right)}^{\left(\frac{-1 \cdot x}{-2}\right)}
\end{array}
Initial program 94.5%
lift-cos.f64N/A
sin-+PI/2-revN/A
sin-sumN/A
lower-fma.f64N/A
lower-sin.f64N/A
lower-cos.f64N/A
lower-/.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lift-cos.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-PI.f6494.5
Applied rewrites94.5%
Taylor expanded in x around inf
Applied rewrites99.2%
lift-exp.f64N/A
lift-pow.f64N/A
pow-expN/A
lower-pow.f64N/A
*-commutativeN/A
pow-expN/A
sqr-powN/A
pow-expN/A
*-commutativeN/A
pow-expN/A
pow-powN/A
associate-/l*N/A
pow-expN/A
*-commutativeN/A
pow-expN/A
pow-powN/A
associate-/l*N/A
lift-/.f64N/A
lift-*.f64N/A
lift-/.f64N/A
Applied rewrites99.4%
(FPCore (x) :precision binary64 (* (fma (sin x) (sin (fma PI 0.5 (/ PI 2.0))) (* (sin (* PI 0.5)) (cos x))) (pow (pow (exp 10.0) x) x)))
double code(double x) {
return fma(sin(x), sin(fma(((double) M_PI), 0.5, (((double) M_PI) / 2.0))), (sin((((double) M_PI) * 0.5)) * cos(x))) * pow(pow(exp(10.0), x), x);
}
function code(x) return Float64(fma(sin(x), sin(fma(pi, 0.5, Float64(pi / 2.0))), Float64(sin(Float64(pi * 0.5)) * cos(x))) * ((exp(10.0) ^ x) ^ x)) end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] * N[Sin[N[(Pi * 0.5 + N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + N[(N[Sin[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[Power[N[Exp[10.0], $MachinePrecision], x], $MachinePrecision], x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin x, \sin \left(\mathsf{fma}\left(\pi, 0.5, \frac{\pi}{2}\right)\right), \sin \left(\pi \cdot 0.5\right) \cdot \cos x\right) \cdot {\left({\left(e^{10}\right)}^{x}\right)}^{x}
\end{array}
Initial program 94.5%
lift-cos.f64N/A
sin-+PI/2-revN/A
sin-sumN/A
lower-fma.f64N/A
lower-sin.f64N/A
lower-cos.f64N/A
lower-/.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lift-cos.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-PI.f6494.5
Applied rewrites94.5%
Taylor expanded in x around inf
Applied rewrites99.2%
(FPCore (x) :precision binary64 (* (fma (sin x) (cos (* PI 0.5)) (* (sin (* PI 0.5)) (cos x))) (pow (pow (exp 10.0) x) x)))
double code(double x) {
return fma(sin(x), cos((((double) M_PI) * 0.5)), (sin((((double) M_PI) * 0.5)) * cos(x))) * pow(pow(exp(10.0), x), x);
}
function code(x) return Float64(fma(sin(x), cos(Float64(pi * 0.5)), Float64(sin(Float64(pi * 0.5)) * cos(x))) * ((exp(10.0) ^ x) ^ x)) end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] * N[Cos[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision] + N[(N[Sin[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[Power[N[Exp[10.0], $MachinePrecision], x], $MachinePrecision], x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin x, \cos \left(\pi \cdot 0.5\right), \sin \left(\pi \cdot 0.5\right) \cdot \cos x\right) \cdot {\left({\left(e^{10}\right)}^{x}\right)}^{x}
\end{array}
Initial program 94.5%
lift-cos.f64N/A
sin-+PI/2-revN/A
sin-sumN/A
lower-fma.f64N/A
lower-sin.f64N/A
lower-cos.f64N/A
lower-/.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lift-cos.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-PI.f6494.5
Applied rewrites94.5%
Taylor expanded in x around inf
Applied rewrites99.2%
lift-sin.f64N/A
lift-PI.f64N/A
lift-fma.f64N/A
lift-PI.f64N/A
lift-/.f64N/A
sin-+PI/2N/A
*-commutativeN/A
lower-cos.f64N/A
*-commutativeN/A
lift-*.f64N/A
lift-PI.f6498.8
Applied rewrites98.8%
(FPCore (x)
:precision binary64
(let* ((t_0 (sin (* PI 0.5)))
(t_1 (* t_0 (cos x)))
(t_2 (* (sin (fma PI 0.5 (/ PI 2.0))) (sin x))))
(*
(/
(fma (pow (cos x) 3.0) (pow t_0 3.0) (pow t_2 3.0))
(fma t_1 t_1 (- (* t_2 t_2) (* t_1 t_2))))
(pow (pow (exp 10.0) x) x))))
double code(double x) {
double t_0 = sin((((double) M_PI) * 0.5));
double t_1 = t_0 * cos(x);
double t_2 = sin(fma(((double) M_PI), 0.5, (((double) M_PI) / 2.0))) * sin(x);
return (fma(pow(cos(x), 3.0), pow(t_0, 3.0), pow(t_2, 3.0)) / fma(t_1, t_1, ((t_2 * t_2) - (t_1 * t_2)))) * pow(pow(exp(10.0), x), x);
}
function code(x) t_0 = sin(Float64(pi * 0.5)) t_1 = Float64(t_0 * cos(x)) t_2 = Float64(sin(fma(pi, 0.5, Float64(pi / 2.0))) * sin(x)) return Float64(Float64(fma((cos(x) ^ 3.0), (t_0 ^ 3.0), (t_2 ^ 3.0)) / fma(t_1, t_1, Float64(Float64(t_2 * t_2) - Float64(t_1 * t_2)))) * ((exp(10.0) ^ x) ^ x)) end
code[x_] := Block[{t$95$0 = N[Sin[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[Cos[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[N[(Pi * 0.5 + N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[Power[N[Cos[x], $MachinePrecision], 3.0], $MachinePrecision] * N[Power[t$95$0, 3.0], $MachinePrecision] + N[Power[t$95$2, 3.0], $MachinePrecision]), $MachinePrecision] / N[(t$95$1 * t$95$1 + N[(N[(t$95$2 * t$95$2), $MachinePrecision] - N[(t$95$1 * t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[Power[N[Exp[10.0], $MachinePrecision], x], $MachinePrecision], x], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\pi \cdot 0.5\right)\\
t_1 := t\_0 \cdot \cos x\\
t_2 := \sin \left(\mathsf{fma}\left(\pi, 0.5, \frac{\pi}{2}\right)\right) \cdot \sin x\\
\frac{\mathsf{fma}\left({\cos x}^{3}, {t\_0}^{3}, {t\_2}^{3}\right)}{\mathsf{fma}\left(t\_1, t\_1, t\_2 \cdot t\_2 - t\_1 \cdot t\_2\right)} \cdot {\left({\left(e^{10}\right)}^{x}\right)}^{x}
\end{array}
\end{array}
Initial program 94.5%
lift-cos.f64N/A
sin-+PI/2-revN/A
sin-sumN/A
lower-fma.f64N/A
lower-sin.f64N/A
lower-cos.f64N/A
lower-/.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lift-cos.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-PI.f6494.5
Applied rewrites94.5%
Taylor expanded in x around inf
Applied rewrites99.2%
Applied rewrites99.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (* (sin (fma PI 0.5 (/ PI 2.0))) (sin x)))
(t_1 (sin (* PI 0.5)))
(t_2 (* t_1 (cos x))))
(*
(/
(fma (* (pow (cos x) 2.0) (pow (cos x) 1.0)) (pow t_1 3.0) (pow t_0 3.0))
(fma t_2 t_2 (- (* t_0 t_0) (* t_2 t_0))))
(pow (pow (exp 10.0) x) x))))
double code(double x) {
double t_0 = sin(fma(((double) M_PI), 0.5, (((double) M_PI) / 2.0))) * sin(x);
double t_1 = sin((((double) M_PI) * 0.5));
double t_2 = t_1 * cos(x);
return (fma((pow(cos(x), 2.0) * pow(cos(x), 1.0)), pow(t_1, 3.0), pow(t_0, 3.0)) / fma(t_2, t_2, ((t_0 * t_0) - (t_2 * t_0)))) * pow(pow(exp(10.0), x), x);
}
function code(x) t_0 = Float64(sin(fma(pi, 0.5, Float64(pi / 2.0))) * sin(x)) t_1 = sin(Float64(pi * 0.5)) t_2 = Float64(t_1 * cos(x)) return Float64(Float64(fma(Float64((cos(x) ^ 2.0) * (cos(x) ^ 1.0)), (t_1 ^ 3.0), (t_0 ^ 3.0)) / fma(t_2, t_2, Float64(Float64(t_0 * t_0) - Float64(t_2 * t_0)))) * ((exp(10.0) ^ x) ^ x)) end
code[x_] := Block[{t$95$0 = N[(N[Sin[N[(Pi * 0.5 + N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sin[N[(Pi * 0.5), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 * N[Cos[x], $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[(N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision] * N[Power[N[Cos[x], $MachinePrecision], 1.0], $MachinePrecision]), $MachinePrecision] * N[Power[t$95$1, 3.0], $MachinePrecision] + N[Power[t$95$0, 3.0], $MachinePrecision]), $MachinePrecision] / N[(t$95$2 * t$95$2 + N[(N[(t$95$0 * t$95$0), $MachinePrecision] - N[(t$95$2 * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[Power[N[Exp[10.0], $MachinePrecision], x], $MachinePrecision], x], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\mathsf{fma}\left(\pi, 0.5, \frac{\pi}{2}\right)\right) \cdot \sin x\\
t_1 := \sin \left(\pi \cdot 0.5\right)\\
t_2 := t\_1 \cdot \cos x\\
\frac{\mathsf{fma}\left({\cos x}^{2} \cdot {\cos x}^{1}, {t\_1}^{3}, {t\_0}^{3}\right)}{\mathsf{fma}\left(t\_2, t\_2, t\_0 \cdot t\_0 - t\_2 \cdot t\_0\right)} \cdot {\left({\left(e^{10}\right)}^{x}\right)}^{x}
\end{array}
\end{array}
Initial program 94.5%
lift-cos.f64N/A
sin-+PI/2-revN/A
sin-sumN/A
lower-fma.f64N/A
lower-sin.f64N/A
lower-cos.f64N/A
lower-/.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lift-cos.f64N/A
lower-sin.f64N/A
lower-/.f64N/A
lower-PI.f6494.5
Applied rewrites94.5%
Taylor expanded in x around inf
Applied rewrites99.2%
Applied rewrites99.0%
lift-cos.f64N/A
lift-pow.f64N/A
metadata-evalN/A
unpow-prod-upN/A
metadata-evalN/A
lower-*.f64N/A
lower-pow.f64N/A
lift-cos.f64N/A
metadata-evalN/A
lower-pow.f64N/A
lift-cos.f6498.9
Applied rewrites98.9%
herbie shell --seed 2025093
(FPCore (x)
:name "ENA, Section 1.4, Exercise 1"
:precision binary64
:pre (and (<= 1.99 x) (<= x 2.01))
(* (cos x) (exp (* 10.0 (* x x)))))