(FPCore (x) :precision binary64 (+ (+ 0.5 (* 0.4 (cos x))) (* 0.1 (cos (* 2.0 x)))))
double code(double x) { return (0.5 + (0.4 * cos(x))) + (0.1 * cos((2.0 * x))); }
real(8) function code(x) real(8), intent (in) :: x code = (0.5d0 + (0.4d0 * cos(x))) + (0.1d0 * cos((2.0d0 * x))) end function
public static double code(double x) { return (0.5 + (0.4 * Math.cos(x))) + (0.1 * Math.cos((2.0 * x))); }
def code(x): return (0.5 + (0.4 * math.cos(x))) + (0.1 * math.cos((2.0 * x)))
function code(x) return Float64(Float64(0.5 + Float64(0.4 * cos(x))) + Float64(0.1 * cos(Float64(2.0 * x)))) end
function tmp = code(x) tmp = (0.5 + (0.4 * cos(x))) + (0.1 * cos((2.0 * x))); end
code[x_] := N[(N[(0.5 + N[(0.4 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.1 * N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(0.5 + 0.4 \cdot \cos x\right) + 0.1 \cdot \cos \left(2 \cdot x\right) \end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (x) :precision binary64 (+ (+ 0.5 (* 0.4 (cos x))) (* 0.1 (cos (* 2.0 x)))))
double code(double x) { return (0.5 + (0.4 * cos(x))) + (0.1 * cos((2.0 * x))); }
real(8) function code(x) real(8), intent (in) :: x code = (0.5d0 + (0.4d0 * cos(x))) + (0.1d0 * cos((2.0d0 * x))) end function
public static double code(double x) { return (0.5 + (0.4 * Math.cos(x))) + (0.1 * Math.cos((2.0 * x))); }
def code(x): return (0.5 + (0.4 * math.cos(x))) + (0.1 * math.cos((2.0 * x)))
function code(x) return Float64(Float64(0.5 + Float64(0.4 * cos(x))) + Float64(0.1 * cos(Float64(2.0 * x)))) end
function tmp = code(x) tmp = (0.5 + (0.4 * cos(x))) + (0.1 * cos((2.0 * x))); end
code[x_] := N[(N[(0.5 + N[(0.4 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.1 * N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(0.5 + 0.4 \cdot \cos x\right) + 0.1 \cdot \cos \left(2 \cdot x\right) \end{array}
(FPCore (x) :precision binary64 (+ (+ 0.5 (* 0.4 (cos x))) (* 0.1 (cos (* 2.0 x)))))
double code(double x) { return (0.5 + (0.4 * cos(x))) + (0.1 * cos((2.0 * x))); }
real(8) function code(x) real(8), intent (in) :: x code = (0.5d0 + (0.4d0 * cos(x))) + (0.1d0 * cos((2.0d0 * x))) end function
public static double code(double x) { return (0.5 + (0.4 * Math.cos(x))) + (0.1 * Math.cos((2.0 * x))); }
def code(x): return (0.5 + (0.4 * math.cos(x))) + (0.1 * math.cos((2.0 * x)))
function code(x) return Float64(Float64(0.5 + Float64(0.4 * cos(x))) + Float64(0.1 * cos(Float64(2.0 * x)))) end
function tmp = code(x) tmp = (0.5 + (0.4 * cos(x))) + (0.1 * cos((2.0 * x))); end
code[x_] := N[(N[(0.5 + N[(0.4 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.1 * N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(0.5 + 0.4 \cdot \cos x\right) + 0.1 \cdot \cos \left(2 \cdot x\right) \end{array}
Initial program 100.0%
(FPCore (x) :precision binary64 (fma (cos (+ x x)) 0.1 (fma (cos x) 0.4 0.5)))
double code(double x) { return fma(cos((x + x)), 0.1, fma(cos(x), 0.4, 0.5)); }
function code(x) return fma(cos(Float64(x + x)), 0.1, fma(cos(x), 0.4, 0.5)) end
code[x_] := N[(N[Cos[N[(x + x), $MachinePrecision]], $MachinePrecision] * 0.1 + N[(N[Cos[x], $MachinePrecision] * 0.4 + 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(\cos \left(x + x\right), 0.1, \mathsf{fma}\left(\cos x, 0.4, 0.5\right)\right) \end{array}
Initial program 100.0%
lift-+.f64
N/A
+-commutative
N/A
lift-*.f64
N/A
*-commutative
N/A
lower-fma.f64
100.0
lift-+.f64
N/A
+-commutative
N/A
lift-*.f64
N/A
*-commutative
N/A
lower-fma.f64
100.0
Applied rewrites100.0%
lift-cos.f64
N/A
lift-*.f64
N/A
cos-2
N/A
cos-sum
N/A
lower-cos.f64
N/A
lower-+.f64
100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (+ (fma (fma 0.016666666666666666 (* x x) -0.2) (* x x) 0.9) (* 0.1 (cos (* 2.0 x)))))
double code(double x) { return fma(fma(0.016666666666666666, (x * x), -0.2), (x * x), 0.9) + (0.1 * cos((2.0 * x))); }
function code(x) return Float64(fma(fma(0.016666666666666666, Float64(x * x), -0.2), Float64(x * x), 0.9) + Float64(0.1 * cos(Float64(2.0 * x)))) end
code[x_] := N[(N[(N[(0.016666666666666666 * N[(x * x), $MachinePrecision] + -0.2), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.9), $MachinePrecision] + N[(0.1 * N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(0.016666666666666666, x \cdot x, -0.2\right), x \cdot x, 0.9\right) + 0.1 \cdot \cos \left(2 \cdot x\right) \end{array}
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
sub-neg
N/A
metadata-eval
N/A
lower-fma.f64
N/A
unpow2
N/A
lower-*.f64
N/A
unpow2
N/A
lower-*.f64
99.0
Applied rewrites99.0%
(FPCore (x) :precision binary64 (+ (+ 0.5 (* 0.4 (cos x))) (fma (fma 0.06666666666666667 (* x x) -0.2) (* x x) 0.1)))
double code(double x) { return (0.5 + (0.4 * cos(x))) + fma(fma(0.06666666666666667, (x * x), -0.2), (x * x), 0.1); }
function code(x) return Float64(Float64(0.5 + Float64(0.4 * cos(x))) + fma(fma(0.06666666666666667, Float64(x * x), -0.2), Float64(x * x), 0.1)) end
code[x_] := N[(N[(0.5 + N[(0.4 * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(0.06666666666666667 * N[(x * x), $MachinePrecision] + -0.2), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(0.5 + 0.4 \cdot \cos x\right) + \mathsf{fma}\left(\mathsf{fma}\left(0.06666666666666667, x \cdot x, -0.2\right), x \cdot x, 0.1\right) \end{array}
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
sub-neg
N/A
metadata-eval
N/A
lower-fma.f64
N/A
unpow2
N/A
lower-*.f64
N/A
unpow2
N/A
lower-*.f64
99.0
Applied rewrites99.0%
(FPCore (x) :precision binary64 (+ (fma (fma 0.016666666666666666 (* x x) -0.2) (* x x) 0.9) (* 0.1 (fma (fma 0.6666666666666666 (* x x) -2.0) (* x x) 1.0))))
double code(double x) { return fma(fma(0.016666666666666666, (x * x), -0.2), (x * x), 0.9) + (0.1 * fma(fma(0.6666666666666666, (x * x), -2.0), (x * x), 1.0)); }
function code(x) return Float64(fma(fma(0.016666666666666666, Float64(x * x), -0.2), Float64(x * x), 0.9) + Float64(0.1 * fma(fma(0.6666666666666666, Float64(x * x), -2.0), Float64(x * x), 1.0))) end
code[x_] := N[(N[(N[(0.016666666666666666 * N[(x * x), $MachinePrecision] + -0.2), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.9), $MachinePrecision] + N[(0.1 * N[(N[(0.6666666666666666 * N[(x * x), $MachinePrecision] + -2.0), $MachinePrecision] * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(0.016666666666666666, x \cdot x, -0.2\right), x \cdot x, 0.9\right) + 0.1 \cdot \mathsf{fma}\left(\mathsf{fma}\left(0.6666666666666666, x \cdot x, -2\right), x \cdot x, 1\right) \end{array}
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
sub-neg
N/A
metadata-eval
N/A
lower-fma.f64
N/A
unpow2
N/A
lower-*.f64
N/A
unpow2
N/A
lower-*.f64
99.0
Applied rewrites99.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
sub-neg
N/A
metadata-eval
N/A
lower-fma.f64
N/A
unpow2
N/A
lower-*.f64
N/A
unpow2
N/A
lower-*.f64
99.0
Applied rewrites99.0%
(FPCore (x) :precision binary64 (fma (fma 0.08333333333333334 (* x x) -0.4) (* x x) 1.0))
double code(double x) { return fma(fma(0.08333333333333334, (x * x), -0.4), (x * x), 1.0); }
function code(x) return fma(fma(0.08333333333333334, Float64(x * x), -0.4), Float64(x * x), 1.0) end
code[x_] := N[(N[(0.08333333333333334 * N[(x * x), $MachinePrecision] + -0.4), $MachinePrecision] * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(0.08333333333333334, x \cdot x, -0.4\right), x \cdot x, 1\right) \end{array}
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
sub-neg
N/A
metadata-eval
N/A
lower-fma.f64
N/A
unpow2
N/A
lower-*.f64
N/A
unpow2
N/A
lower-*.f64
99.0
Applied rewrites99.0%
(FPCore (x) :precision binary64 (fma (* x x) -0.4 1.0))
double code(double x) { return fma((x * x), -0.4, 1.0); }
function code(x) return fma(Float64(x * x), -0.4, 1.0) end
code[x_] := N[(N[(x * x), $MachinePrecision] * -0.4 + 1.0), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(x \cdot x, -0.4, 1\right) \end{array}
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
unpow2
N/A
lower-*.f64
98.5
Applied rewrites98.5%
(FPCore (x) :precision binary64 1.0)
double code(double x) { return 1.0; }
real(8) function code(x) real(8), intent (in) :: x code = 1.0d0 end function
public static double code(double x) { return 1.0; }
def code(x): return 1.0
function code(x) return 1.0 end
function tmp = code(x) tmp = 1.0; end
code[x_] := 1.0
\begin{array}{l} \\ 1 \end{array}
Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites98.1%
herbie shell --seed 1
(FPCore (x)
:name "0.5 + 0.4 * cos(x) + 0.1 * cos(2 x)"
:precision binary64
:pre (and (<= -1000.0 x) (<= x 1000.0))
(+ (+ 0.5 (* 0.4 (cos x))) (* 0.1 (cos (* 2.0 x)))))