
(FPCore (x) :precision binary64 (/ (* 6.0 (- x 1.0)) (+ (+ x 1.0) (* 4.0 (sqrt x)))))
double code(double x) {
return (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * sqrt(x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (6.0d0 * (x - 1.0d0)) / ((x + 1.0d0) + (4.0d0 * sqrt(x)))
end function
public static double code(double x) {
return (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * Math.sqrt(x)));
}
def code(x): return (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * math.sqrt(x)))
function code(x) return Float64(Float64(6.0 * Float64(x - 1.0)) / Float64(Float64(x + 1.0) + Float64(4.0 * sqrt(x)))) end
function tmp = code(x) tmp = (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * sqrt(x))); end
code[x_] := N[(N[(6.0 * N[(x - 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(x + 1.0), $MachinePrecision] + N[(4.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{6 \cdot \left(x - 1\right)}{\left(x + 1\right) + 4 \cdot \sqrt{x}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (* 6.0 (- x 1.0)) (+ (+ x 1.0) (* 4.0 (sqrt x)))))
double code(double x) {
return (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * sqrt(x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (6.0d0 * (x - 1.0d0)) / ((x + 1.0d0) + (4.0d0 * sqrt(x)))
end function
public static double code(double x) {
return (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * Math.sqrt(x)));
}
def code(x): return (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * math.sqrt(x)))
function code(x) return Float64(Float64(6.0 * Float64(x - 1.0)) / Float64(Float64(x + 1.0) + Float64(4.0 * sqrt(x)))) end
function tmp = code(x) tmp = (6.0 * (x - 1.0)) / ((x + 1.0) + (4.0 * sqrt(x))); end
code[x_] := N[(N[(6.0 * N[(x - 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(x + 1.0), $MachinePrecision] + N[(4.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{6 \cdot \left(x - 1\right)}{\left(x + 1\right) + 4 \cdot \sqrt{x}}
\end{array}
(FPCore (x) :precision binary64 (* 6.0 (/ (- 1.0 x) (- (fma -4.0 (sqrt x) -1.0) x))))
double code(double x) {
return 6.0 * ((1.0 - x) / (fma(-4.0, sqrt(x), -1.0) - x));
}
function code(x) return Float64(6.0 * Float64(Float64(1.0 - x) / Float64(fma(-4.0, sqrt(x), -1.0) - x))) end
code[x_] := N[(6.0 * N[(N[(1.0 - x), $MachinePrecision] / N[(N[(-4.0 * N[Sqrt[x], $MachinePrecision] + -1.0), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
6 \cdot \frac{1 - x}{\mathsf{fma}\left(-4, \sqrt{x}, -1\right) - x}
\end{array}
Initial program 99.4%
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (/ (* (- x 1.0) 6.0) (+ (* 4.0 (sqrt x)) (+ x 1.0))) -2.0) (/ (fma x 6.0 -6.0) (fma (sqrt x) 4.0 1.0)) (/ 6.0 (+ (/ 4.0 (sqrt x)) 1.0))))
double code(double x) {
double tmp;
if ((((x - 1.0) * 6.0) / ((4.0 * sqrt(x)) + (x + 1.0))) <= -2.0) {
tmp = fma(x, 6.0, -6.0) / fma(sqrt(x), 4.0, 1.0);
} else {
tmp = 6.0 / ((4.0 / sqrt(x)) + 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(Float64(x - 1.0) * 6.0) / Float64(Float64(4.0 * sqrt(x)) + Float64(x + 1.0))) <= -2.0) tmp = Float64(fma(x, 6.0, -6.0) / fma(sqrt(x), 4.0, 1.0)); else tmp = Float64(6.0 / Float64(Float64(4.0 / sqrt(x)) + 1.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(N[(x - 1.0), $MachinePrecision] * 6.0), $MachinePrecision] / N[(N[(4.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -2.0], N[(N[(x * 6.0 + -6.0), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] * 4.0 + 1.0), $MachinePrecision]), $MachinePrecision], N[(6.0 / N[(N[(4.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\left(x - 1\right) \cdot 6}{4 \cdot \sqrt{x} + \left(x + 1\right)} \leq -2:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, 6, -6\right)}{\mathsf{fma}\left(\sqrt{x}, 4, 1\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{6}{\frac{4}{\sqrt{x}} + 1}\\
\end{array}
\end{array}
if (/.f64 (*.f64 #s(literal 6 binary64) (-.f64 x #s(literal 1 binary64))) (+.f64 (+.f64 x #s(literal 1 binary64)) (*.f64 #s(literal 4 binary64) (sqrt.f64 x)))) < -2Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f6497.6
Applied rewrites97.6%
lift-*.f64N/A
lift--.f64N/A
sub-negN/A
metadata-evalN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f6497.6
Applied rewrites97.6%
if -2 < (/.f64 (*.f64 #s(literal 6 binary64) (-.f64 x #s(literal 1 binary64))) (+.f64 (+.f64 x #s(literal 1 binary64)) (*.f64 #s(literal 4 binary64) (sqrt.f64 x)))) Initial program 98.8%
Taylor expanded in x around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lower-/.f6497.1
Applied rewrites97.1%
Applied rewrites97.1%
Final simplification97.4%
(FPCore (x) :precision binary64 (* (- x 1.0) (/ 6.0 (fma (sqrt x) 4.0 (- x -1.0)))))
double code(double x) {
return (x - 1.0) * (6.0 / fma(sqrt(x), 4.0, (x - -1.0)));
}
function code(x) return Float64(Float64(x - 1.0) * Float64(6.0 / fma(sqrt(x), 4.0, Float64(x - -1.0)))) end
code[x_] := N[(N[(x - 1.0), $MachinePrecision] * N[(6.0 / N[(N[Sqrt[x], $MachinePrecision] * 4.0 + N[(x - -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x - 1\right) \cdot \frac{6}{\mathsf{fma}\left(\sqrt{x}, 4, x - -1\right)}
\end{array}
Initial program 99.4%
lift-/.f64N/A
clear-numN/A
lift-*.f64N/A
associate-/r*N/A
associate-/r/N/A
clear-numN/A
lower-*.f64N/A
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 1.0) (/ -6.0 (fma 4.0 (sqrt x) (- x -1.0))) (* 1.5 (sqrt x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = -6.0 / fma(4.0, sqrt(x), (x - -1.0));
} else {
tmp = 1.5 * sqrt(x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(-6.0 / fma(4.0, sqrt(x), Float64(x - -1.0))); else tmp = Float64(1.5 * sqrt(x)); end return tmp end
code[x_] := If[LessEqual[x, 1.0], N[(-6.0 / N[(4.0 * N[Sqrt[x], $MachinePrecision] + N[(x - -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\frac{-6}{\mathsf{fma}\left(4, \sqrt{x}, x - -1\right)}\\
\mathbf{else}:\\
\;\;\;\;1.5 \cdot \sqrt{x}\\
\end{array}
\end{array}
if x < 1Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites97.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6497.6
lift-+.f64N/A
metadata-evalN/A
sub-negN/A
lift--.f6497.6
Applied rewrites97.6%
if 1 < x Initial program 98.8%
Taylor expanded in x around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lower-/.f6497.1
Applied rewrites97.1%
Taylor expanded in x around 0
Applied rewrites6.8%
(FPCore (x) :precision binary64 (/ (- x 1.0) (fma (fma 4.0 (sqrt x) x) 0.16666666666666666 0.16666666666666666)))
double code(double x) {
return (x - 1.0) / fma(fma(4.0, sqrt(x), x), 0.16666666666666666, 0.16666666666666666);
}
function code(x) return Float64(Float64(x - 1.0) / fma(fma(4.0, sqrt(x), x), 0.16666666666666666, 0.16666666666666666)) end
code[x_] := N[(N[(x - 1.0), $MachinePrecision] / N[(N[(4.0 * N[Sqrt[x], $MachinePrecision] + x), $MachinePrecision] * 0.16666666666666666 + 0.16666666666666666), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - 1}{\mathsf{fma}\left(\mathsf{fma}\left(4, \sqrt{x}, x\right), 0.16666666666666666, 0.16666666666666666\right)}
\end{array}
Initial program 99.4%
lift-/.f64N/A
clear-numN/A
lift-*.f64N/A
associate-/r*N/A
associate-/r/N/A
clear-numN/A
lower-*.f64N/A
Applied rewrites99.9%
lift-*.f64N/A
*-commutativeN/A
lift-/.f64N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lift-fma.f64N/A
lift-*.f64N/A
lift--.f64N/A
associate-+r-N/A
div-subN/A
sub-negN/A
div-invN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
metadata-evalN/A
metadata-eval99.6
Applied rewrites99.6%
(FPCore (x) :precision binary64 (if (<= x 1.0) (/ -6.0 (fma (sqrt x) 4.0 1.0)) (* 1.5 (sqrt x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = -6.0 / fma(sqrt(x), 4.0, 1.0);
} else {
tmp = 1.5 * sqrt(x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(-6.0 / fma(sqrt(x), 4.0, 1.0)); else tmp = Float64(1.5 * sqrt(x)); end return tmp end
code[x_] := If[LessEqual[x, 1.0], N[(-6.0 / N[(N[Sqrt[x], $MachinePrecision] * 4.0 + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\frac{-6}{\mathsf{fma}\left(\sqrt{x}, 4, 1\right)}\\
\mathbf{else}:\\
\;\;\;\;1.5 \cdot \sqrt{x}\\
\end{array}
\end{array}
if x < 1Initial program 100.0%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f6497.5
Applied rewrites97.5%
if 1 < x Initial program 98.8%
Taylor expanded in x around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lower-/.f6497.1
Applied rewrites97.1%
Taylor expanded in x around 0
Applied rewrites6.8%
(FPCore (x) :precision binary64 (/ (fma x 6.0 -6.0) (fma (sqrt x) 4.0 1.0)))
double code(double x) {
return fma(x, 6.0, -6.0) / fma(sqrt(x), 4.0, 1.0);
}
function code(x) return Float64(fma(x, 6.0, -6.0) / fma(sqrt(x), 4.0, 1.0)) end
code[x_] := N[(N[(x * 6.0 + -6.0), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] * 4.0 + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(x, 6, -6\right)}{\mathsf{fma}\left(\sqrt{x}, 4, 1\right)}
\end{array}
Initial program 99.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f6452.6
Applied rewrites52.6%
lift-*.f64N/A
lift--.f64N/A
sub-negN/A
metadata-evalN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f6452.6
Applied rewrites52.6%
(FPCore (x) :precision binary64 (/ (- x 1.0) (fma 0.6666666666666666 (sqrt x) 0.16666666666666666)))
double code(double x) {
return (x - 1.0) / fma(0.6666666666666666, sqrt(x), 0.16666666666666666);
}
function code(x) return Float64(Float64(x - 1.0) / fma(0.6666666666666666, sqrt(x), 0.16666666666666666)) end
code[x_] := N[(N[(x - 1.0), $MachinePrecision] / N[(0.6666666666666666 * N[Sqrt[x], $MachinePrecision] + 0.16666666666666666), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - 1}{\mathsf{fma}\left(0.6666666666666666, \sqrt{x}, 0.16666666666666666\right)}
\end{array}
Initial program 99.4%
lift-/.f64N/A
clear-numN/A
lift-*.f64N/A
associate-/r*N/A
associate-/r/N/A
clear-numN/A
lower-*.f64N/A
Applied rewrites99.9%
lift-*.f64N/A
*-commutativeN/A
lift-/.f64N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lift-fma.f64N/A
lift-*.f64N/A
lift--.f64N/A
associate-+r-N/A
div-subN/A
sub-negN/A
div-invN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
metadata-evalN/A
metadata-eval99.6
Applied rewrites99.6%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f6452.5
Applied rewrites52.5%
(FPCore (x) :precision binary64 (if (<= x 1.0) (/ -1.5 (sqrt x)) (* 1.5 (sqrt x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = -1.5 / sqrt(x);
} else {
tmp = 1.5 * sqrt(x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.0d0) then
tmp = (-1.5d0) / sqrt(x)
else
tmp = 1.5d0 * sqrt(x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = -1.5 / Math.sqrt(x);
} else {
tmp = 1.5 * Math.sqrt(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.0: tmp = -1.5 / math.sqrt(x) else: tmp = 1.5 * math.sqrt(x) return tmp
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(-1.5 / sqrt(x)); else tmp = Float64(1.5 * sqrt(x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.0) tmp = -1.5 / sqrt(x); else tmp = 1.5 * sqrt(x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.0], N[(-1.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[(1.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\frac{-1.5}{\sqrt{x}}\\
\mathbf{else}:\\
\;\;\;\;1.5 \cdot \sqrt{x}\\
\end{array}
\end{array}
if x < 1Initial program 100.0%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f6497.5
Applied rewrites97.5%
Taylor expanded in x around inf
Applied rewrites6.9%
Applied rewrites6.9%
if 1 < x Initial program 98.8%
Taylor expanded in x around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lower-/.f6497.1
Applied rewrites97.1%
Taylor expanded in x around 0
Applied rewrites6.8%
(FPCore (x) :precision binary64 (* 1.5 (sqrt x)))
double code(double x) {
return 1.5 * sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.5d0 * sqrt(x)
end function
public static double code(double x) {
return 1.5 * Math.sqrt(x);
}
def code(x): return 1.5 * math.sqrt(x)
function code(x) return Float64(1.5 * sqrt(x)) end
function tmp = code(x) tmp = 1.5 * sqrt(x); end
code[x_] := N[(1.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1.5 \cdot \sqrt{x}
\end{array}
Initial program 99.4%
Taylor expanded in x around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lower-/.f6449.1
Applied rewrites49.1%
Taylor expanded in x around 0
Applied rewrites4.3%
(FPCore (x) :precision binary64 (/ 6.0 (/ (+ (+ x 1.0) (* 4.0 (sqrt x))) (- x 1.0))))
double code(double x) {
return 6.0 / (((x + 1.0) + (4.0 * sqrt(x))) / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 6.0d0 / (((x + 1.0d0) + (4.0d0 * sqrt(x))) / (x - 1.0d0))
end function
public static double code(double x) {
return 6.0 / (((x + 1.0) + (4.0 * Math.sqrt(x))) / (x - 1.0));
}
def code(x): return 6.0 / (((x + 1.0) + (4.0 * math.sqrt(x))) / (x - 1.0))
function code(x) return Float64(6.0 / Float64(Float64(Float64(x + 1.0) + Float64(4.0 * sqrt(x))) / Float64(x - 1.0))) end
function tmp = code(x) tmp = 6.0 / (((x + 1.0) + (4.0 * sqrt(x))) / (x - 1.0)); end
code[x_] := N[(6.0 / N[(N[(N[(x + 1.0), $MachinePrecision] + N[(4.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{6}{\frac{\left(x + 1\right) + 4 \cdot \sqrt{x}}{x - 1}}
\end{array}
herbie shell --seed 2024288
(FPCore (x)
:name "Data.Approximate.Numerics:blog from approximate-0.2.2.1"
:precision binary64
:alt
(! :herbie-platform default (/ 6 (/ (+ (+ x 1) (* 4 (sqrt x))) (- x 1))))
(/ (* 6.0 (- x 1.0)) (+ (+ x 1.0) (* 4.0 (sqrt x)))))