
(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 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
Applied rewrites99.9%
(FPCore (x) :precision binary64 (if (<= (- (* x (* 3.0 x)) (* x 4.0)) 0.05) (* 3.0 (fma -4.0 x 1.0)) (* x (fma x 9.0 -12.0))))
double code(double x) {
double tmp;
if (((x * (3.0 * x)) - (x * 4.0)) <= 0.05) {
tmp = 3.0 * fma(-4.0, x, 1.0);
} else {
tmp = x * fma(x, 9.0, -12.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x * Float64(3.0 * x)) - Float64(x * 4.0)) <= 0.05) tmp = Float64(3.0 * fma(-4.0, x, 1.0)); else tmp = Float64(x * fma(x, 9.0, -12.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x * N[(3.0 * x), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.05], N[(3.0 * N[(-4.0 * x + 1.0), $MachinePrecision]), $MachinePrecision], N[(x * N[(x * 9.0 + -12.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(3 \cdot x\right) - x \cdot 4 \leq 0.05:\\
\;\;\;\;3 \cdot \mathsf{fma}\left(-4, x, 1\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))) < 0.050000000000000003Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6498.4
Applied rewrites98.4%
if 0.050000000000000003 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.6%
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.f6498.0
Applied rewrites98.0%
Final simplification98.2%
(FPCore (x) :precision binary64 (if (<= (- (* x (* 3.0 x)) (* x 4.0)) 0.05) (fma x -12.0 3.0) (* x (fma x 9.0 -12.0))))
double code(double x) {
double tmp;
if (((x * (3.0 * x)) - (x * 4.0)) <= 0.05) {
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(3.0 * x)) - Float64(x * 4.0)) <= 0.05) 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[(3.0 * x), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.05], 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(3 \cdot x\right) - x \cdot 4 \leq 0.05:\\
\;\;\;\;\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))) < 0.050000000000000003Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6498.3
Applied rewrites98.3%
if 0.050000000000000003 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.6%
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.f6498.0
Applied rewrites98.0%
Final simplification98.2%
(FPCore (x) :precision binary64 (if (<= (- (* x (* 3.0 x)) (* x 4.0)) 0.05) (fma x -12.0 3.0) (* 9.0 (* x x))))
double code(double x) {
double tmp;
if (((x * (3.0 * x)) - (x * 4.0)) <= 0.05) {
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(3.0 * x)) - Float64(x * 4.0)) <= 0.05) 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[(3.0 * x), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.05], 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(3 \cdot x\right) - x \cdot 4 \leq 0.05:\\
\;\;\;\;\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))) < 0.050000000000000003Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6498.3
Applied rewrites98.3%
if 0.050000000000000003 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) Initial program 99.6%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6497.2
Applied rewrites97.2%
Final simplification97.8%
(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.f6455.9
Applied rewrites55.9%
(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
Applied rewrites54.8%
(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 2024212
(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)))