
(FPCore (x) :precision binary64 (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))
double code(double x) {
return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 3.0d0 * ((((x * 3.0d0) * x) - (x * 4.0d0)) + 1.0d0)
end function
public static double code(double x) {
return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
def code(x): return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0)
function code(x) return Float64(3.0 * Float64(Float64(Float64(Float64(x * 3.0) * x) - Float64(x * 4.0)) + 1.0)) end
function tmp = code(x) tmp = 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0); end
code[x_] := N[(3.0 * N[(N[(N[(N[(x * 3.0), $MachinePrecision] * x), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))
double code(double x) {
return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 3.0d0 * ((((x * 3.0d0) * x) - (x * 4.0d0)) + 1.0d0)
end function
public static double code(double x) {
return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
def code(x): return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0)
function code(x) return Float64(3.0 * Float64(Float64(Float64(Float64(x * 3.0) * x) - Float64(x * 4.0)) + 1.0)) end
function tmp = code(x) tmp = 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0); end
code[x_] := N[(3.0 * N[(N[(N[(N[(x * 3.0), $MachinePrecision] * x), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right)
\end{array}
(FPCore (x) :precision binary64 (fma (* x 3.0) (fma x 3.0 -4.0) 3.0))
double code(double x) {
return fma((x * 3.0), fma(x, 3.0, -4.0), 3.0);
}
function code(x) return fma(Float64(x * 3.0), fma(x, 3.0, -4.0), 3.0) end
code[x_] := N[(N[(x * 3.0), $MachinePrecision] * N[(x * 3.0 + -4.0), $MachinePrecision] + 3.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x \cdot 3, \mathsf{fma}\left(x, 3, -4\right), 3\right)
\end{array}
Initial program 99.8%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift--.f64N/A
distribute-lft-inN/A
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
distribute-lft-out--N/A
associate-*r*N/A
*-commutativeN/A
lift-*.f64N/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
metadata-eval99.9
Applied egg-rr99.9%
(FPCore (x) :precision binary64 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 5e-7) (fma x -12.0 3.0) (* x (fma x 9.0 -12.0))))
double code(double x) {
double tmp;
if (((x * (x * 3.0)) - (x * 4.0)) <= 5e-7) {
tmp = fma(x, -12.0, 3.0);
} else {
tmp = x * fma(x, 9.0, -12.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 5e-7) tmp = fma(x, -12.0, 3.0); else tmp = Float64(x * fma(x, 9.0, -12.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 5e-7], N[(x * -12.0 + 3.0), $MachinePrecision], N[(x * N[(x * 9.0 + -12.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 5 \cdot 10^{-7}:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \mathsf{fma}\left(x, 9, -12\right)\\
\end{array}
\end{array}
if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 4.99999999999999977e-7Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6499.1
Simplified99.1%
if 4.99999999999999977e-7 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.7%
Taylor expanded in x around inf
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
sub-negN/A
distribute-lft-inN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
associate-*r*N/A
rgt-mult-inverseN/A
metadata-evalN/A
lower-fma.f6499.0
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 5e-7) (fma x -12.0 3.0) (* 9.0 (* x x))))
double code(double x) {
double tmp;
if (((x * (x * 3.0)) - (x * 4.0)) <= 5e-7) {
tmp = fma(x, -12.0, 3.0);
} else {
tmp = 9.0 * (x * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 5e-7) tmp = fma(x, -12.0, 3.0); else tmp = Float64(9.0 * Float64(x * x)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 5e-7], N[(x * -12.0 + 3.0), $MachinePrecision], N[(9.0 * N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 5 \cdot 10^{-7}:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\
\mathbf{else}:\\
\;\;\;\;9 \cdot \left(x \cdot x\right)\\
\end{array}
\end{array}
if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 4.99999999999999977e-7Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6499.1
Simplified99.1%
if 4.99999999999999977e-7 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.7%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6498.6
Simplified98.6%
Final simplification98.8%
(FPCore (x) :precision binary64 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 5e-7) (fma x -12.0 3.0) (* x (* x 9.0))))
double code(double x) {
double tmp;
if (((x * (x * 3.0)) - (x * 4.0)) <= 5e-7) {
tmp = fma(x, -12.0, 3.0);
} else {
tmp = x * (x * 9.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 5e-7) tmp = fma(x, -12.0, 3.0); else tmp = Float64(x * Float64(x * 9.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 5e-7], N[(x * -12.0 + 3.0), $MachinePrecision], N[(x * N[(x * 9.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 5 \cdot 10^{-7}:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(x \cdot 9\right)\\
\end{array}
\end{array}
if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 4.99999999999999977e-7Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6499.1
Simplified99.1%
if 4.99999999999999977e-7 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.7%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6498.6
Simplified98.6%
metadata-evalN/A
swap-sqrN/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
metadata-evalN/A
lower-*.f6498.4
Applied egg-rr98.4%
Final simplification98.8%
(FPCore (x) :precision binary64 (fma x (fma x 9.0 -12.0) 3.0))
double code(double x) {
return fma(x, fma(x, 9.0, -12.0), 3.0);
}
function code(x) return fma(x, fma(x, 9.0, -12.0), 3.0) end
code[x_] := N[(x * N[(x * 9.0 + -12.0), $MachinePrecision] + 3.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0
+-commutativeN/A
sub-negN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
distribute-rgt-inN/A
sub-negN/A
*-commutativeN/A
lower-fma.f64N/A
Simplified99.9%
(FPCore (x) :precision binary64 (fma x -12.0 3.0))
double code(double x) {
return fma(x, -12.0, 3.0);
}
function code(x) return fma(x, -12.0, 3.0) end
code[x_] := N[(x * -12.0 + 3.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, -12, 3\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6453.5
Simplified53.5%
(FPCore (x) :precision binary64 3.0)
double code(double x) {
return 3.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 3.0d0
end function
public static double code(double x) {
return 3.0;
}
def code(x): return 3.0
function code(x) return 3.0 end
function tmp = code(x) tmp = 3.0; end
code[x_] := 3.0
\begin{array}{l}
\\
3
\end{array}
Initial program 99.8%
Taylor expanded in x around 0
Simplified53.1%
(FPCore (x) :precision binary64 (+ 3.0 (- (* (* 9.0 x) x) (* 12.0 x))))
double code(double x) {
return 3.0 + (((9.0 * x) * x) - (12.0 * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 3.0d0 + (((9.0d0 * x) * x) - (12.0d0 * x))
end function
public static double code(double x) {
return 3.0 + (((9.0 * x) * x) - (12.0 * x));
}
def code(x): return 3.0 + (((9.0 * x) * x) - (12.0 * x))
function code(x) return Float64(3.0 + Float64(Float64(Float64(9.0 * x) * x) - Float64(12.0 * x))) end
function tmp = code(x) tmp = 3.0 + (((9.0 * x) * x) - (12.0 * x)); end
code[x_] := N[(3.0 + N[(N[(N[(9.0 * x), $MachinePrecision] * x), $MachinePrecision] - N[(12.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
3 + \left(\left(9 \cdot x\right) \cdot x - 12 \cdot x\right)
\end{array}
herbie shell --seed 2024207
(FPCore (x)
:name "Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, D"
:precision binary64
:alt
(! :herbie-platform default (+ 3 (- (* (* 9 x) x) (* 12 x))))
(* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))