
(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 6 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 (+ -12.0 (* 9.0 x)) x 3.0))
double code(double x) {
return fma((-12.0 + (9.0 * x)), x, 3.0);
}
function code(x) return fma(Float64(-12.0 + Float64(9.0 * x)), x, 3.0) end
code[x_] := N[(N[(-12.0 + N[(9.0 * x), $MachinePrecision]), $MachinePrecision] * x + 3.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-12 + 9 \cdot x, x, 3\right)
\end{array}
Initial program 99.5%
Taylor expanded in x around 0
+-commutativeN/A
distribute-rgt-out--N/A
associate-*r*N/A
unpow2N/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
distribute-lft-neg-outN/A
remove-double-negN/A
associate-*r*N/A
unpow2N/A
distribute-rgt-out--N/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
Applied rewrites99.9%
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= (- (* (* x 3.0) x) (* 4.0 x)) 0.0005) (fma -12.0 x 3.0) (* (fma 9.0 x -12.0) x)))
double code(double x) {
double tmp;
if ((((x * 3.0) * x) - (4.0 * x)) <= 0.0005) {
tmp = fma(-12.0, x, 3.0);
} else {
tmp = fma(9.0, x, -12.0) * x;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(Float64(x * 3.0) * x) - Float64(4.0 * x)) <= 0.0005) tmp = fma(-12.0, x, 3.0); else tmp = Float64(fma(9.0, x, -12.0) * x); end return tmp end
code[x_] := If[LessEqual[N[(N[(N[(x * 3.0), $MachinePrecision] * x), $MachinePrecision] - N[(4.0 * x), $MachinePrecision]), $MachinePrecision], 0.0005], N[(-12.0 * x + 3.0), $MachinePrecision], N[(N[(9.0 * x + -12.0), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x \cdot 3\right) \cdot x - 4 \cdot x \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(-12, x, 3\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(9, x, -12\right) \cdot x\\
\end{array}
\end{array}
if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6499.6
Applied rewrites99.6%
if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.0%
Taylor expanded in x around inf
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
sub-negN/A
distribute-rgt-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
associate-*l*N/A
lft-mult-inverseN/A
metadata-evalN/A
lower-fma.f6499.7
Applied rewrites99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= (- (* (* x 3.0) x) (* 4.0 x)) 0.0005) (fma -12.0 x 3.0) (* (* 9.0 x) x)))
double code(double x) {
double tmp;
if ((((x * 3.0) * x) - (4.0 * x)) <= 0.0005) {
tmp = fma(-12.0, x, 3.0);
} else {
tmp = (9.0 * x) * x;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(Float64(x * 3.0) * x) - Float64(4.0 * x)) <= 0.0005) tmp = fma(-12.0, x, 3.0); else tmp = Float64(Float64(9.0 * x) * x); end return tmp end
code[x_] := If[LessEqual[N[(N[(N[(x * 3.0), $MachinePrecision] * x), $MachinePrecision] - N[(4.0 * x), $MachinePrecision]), $MachinePrecision], 0.0005], N[(-12.0 * x + 3.0), $MachinePrecision], N[(N[(9.0 * x), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x \cdot 3\right) \cdot x - 4 \cdot x \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(-12, x, 3\right)\\
\mathbf{else}:\\
\;\;\;\;\left(9 \cdot x\right) \cdot x\\
\end{array}
\end{array}
if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6499.6
Applied rewrites99.6%
if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.3
Applied rewrites99.3%
Applied rewrites99.3%
Final simplification99.5%
(FPCore (x) :precision binary64 (fma (fma 9.0 x -12.0) x 3.0))
double code(double x) {
return fma(fma(9.0, x, -12.0), x, 3.0);
}
function code(x) return fma(fma(9.0, x, -12.0), x, 3.0) end
code[x_] := N[(N[(9.0 * x + -12.0), $MachinePrecision] * x + 3.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(9, x, -12\right), x, 3\right)
\end{array}
Initial program 99.5%
Taylor expanded in x around 0
+-commutativeN/A
distribute-rgt-out--N/A
associate-*r*N/A
unpow2N/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
distribute-lft-neg-outN/A
remove-double-negN/A
associate-*r*N/A
unpow2N/A
distribute-rgt-out--N/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
Applied rewrites99.9%
(FPCore (x) :precision binary64 (fma -12.0 x 3.0))
double code(double x) {
return fma(-12.0, x, 3.0);
}
function code(x) return fma(-12.0, x, 3.0) end
code[x_] := N[(-12.0 * x + 3.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-12, x, 3\right)
\end{array}
Initial program 99.5%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6452.5
Applied rewrites52.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.5%
Taylor expanded in x around 0
Applied rewrites52.2%
(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 2024295
(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)))