
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (* (+ 1.0 x) (+ (pow x -0.5) (pow (+ 1.0 x) -0.5)))))
double code(double x) {
return (1.0 / x) / ((1.0 + x) * (pow(x, -0.5) + pow((1.0 + x), -0.5)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / ((1.0d0 + x) * ((x ** (-0.5d0)) + ((1.0d0 + x) ** (-0.5d0))))
end function
public static double code(double x) {
return (1.0 / x) / ((1.0 + x) * (Math.pow(x, -0.5) + Math.pow((1.0 + x), -0.5)));
}
def code(x): return (1.0 / x) / ((1.0 + x) * (math.pow(x, -0.5) + math.pow((1.0 + x), -0.5)))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64(1.0 + x) * Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5)))) end
function tmp = code(x) tmp = (1.0 / x) / ((1.0 + x) * ((x ^ -0.5) + ((1.0 + x) ^ -0.5))); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] * N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}
\end{array}
Initial program 33.0%
flip--33.0%
div-inv33.0%
frac-times19.1%
metadata-eval19.1%
add-sqr-sqrt17.0%
frac-times24.5%
metadata-eval24.5%
add-sqr-sqrt33.1%
+-commutative33.1%
inv-pow33.1%
sqrt-pow233.1%
metadata-eval33.1%
pow1/233.1%
pow-flip33.1%
+-commutative33.1%
metadata-eval33.1%
Applied egg-rr33.1%
frac-sub34.5%
div-inv34.5%
*-un-lft-identity34.5%
Applied egg-rr34.5%
*-rgt-identity34.5%
associate--l+80.5%
+-inverses80.5%
metadata-eval80.5%
associate-*r/80.5%
metadata-eval80.5%
associate-/r*81.2%
Simplified81.2%
*-commutative81.2%
frac-times99.6%
*-un-lft-identity99.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (+ (* 2.0 (sqrt x)) (* (sqrt (/ 1.0 x)) 1.5))))
double code(double x) {
return (1.0 / x) / ((2.0 * sqrt(x)) + (sqrt((1.0 / x)) * 1.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / ((2.0d0 * sqrt(x)) + (sqrt((1.0d0 / x)) * 1.5d0))
end function
public static double code(double x) {
return (1.0 / x) / ((2.0 * Math.sqrt(x)) + (Math.sqrt((1.0 / x)) * 1.5));
}
def code(x): return (1.0 / x) / ((2.0 * math.sqrt(x)) + (math.sqrt((1.0 / x)) * 1.5))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64(2.0 * sqrt(x)) + Float64(sqrt(Float64(1.0 / x)) * 1.5))) end
function tmp = code(x) tmp = (1.0 / x) / ((2.0 * sqrt(x)) + (sqrt((1.0 / x)) * 1.5)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(2.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 1.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{2 \cdot \sqrt{x} + \sqrt{\frac{1}{x}} \cdot 1.5}
\end{array}
Initial program 33.0%
flip--33.0%
div-inv33.0%
frac-times19.1%
metadata-eval19.1%
add-sqr-sqrt17.0%
frac-times24.5%
metadata-eval24.5%
add-sqr-sqrt33.1%
+-commutative33.1%
inv-pow33.1%
sqrt-pow233.1%
metadata-eval33.1%
pow1/233.1%
pow-flip33.1%
+-commutative33.1%
metadata-eval33.1%
Applied egg-rr33.1%
frac-sub34.5%
div-inv34.5%
*-un-lft-identity34.5%
Applied egg-rr34.5%
*-rgt-identity34.5%
associate--l+80.5%
+-inverses80.5%
metadata-eval80.5%
associate-*r/80.5%
metadata-eval80.5%
associate-/r*81.2%
Simplified81.2%
*-commutative81.2%
frac-times99.6%
*-un-lft-identity99.6%
Applied egg-rr99.6%
Taylor expanded in x around inf 99.4%
+-commutative99.4%
associate-+l+99.4%
distribute-rgt-out99.4%
metadata-eval99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x) :precision binary64 (* 0.5 (pow x -1.5)))
double code(double x) {
return 0.5 * pow(x, -1.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * (x ** (-1.5d0))
end function
public static double code(double x) {
return 0.5 * Math.pow(x, -1.5);
}
def code(x): return 0.5 * math.pow(x, -1.5)
function code(x) return Float64(0.5 * (x ^ -1.5)) end
function tmp = code(x) tmp = 0.5 * (x ^ -1.5); end
code[x_] := N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-1.5}
\end{array}
Initial program 33.0%
flip--33.0%
div-inv33.0%
frac-times19.1%
metadata-eval19.1%
add-sqr-sqrt17.0%
frac-times24.5%
metadata-eval24.5%
add-sqr-sqrt33.1%
+-commutative33.1%
inv-pow33.1%
sqrt-pow233.1%
metadata-eval33.1%
pow1/233.1%
pow-flip33.1%
+-commutative33.1%
metadata-eval33.1%
Applied egg-rr33.1%
frac-sub34.5%
div-inv34.5%
*-un-lft-identity34.5%
Applied egg-rr34.5%
*-rgt-identity34.5%
associate--l+80.5%
+-inverses80.5%
metadata-eval80.5%
associate-*r/80.5%
metadata-eval80.5%
associate-/r*81.2%
Simplified81.2%
Taylor expanded in x around inf 63.2%
exp-to-pow60.5%
*-commutative60.5%
exp-neg61.6%
distribute-lft-neg-in61.6%
metadata-eval61.6%
*-commutative61.6%
exp-to-pow64.3%
metadata-eval64.3%
pow-sqr64.3%
rem-sqrt-square99.0%
rem-square-sqrt98.5%
fabs-sqr98.5%
rem-square-sqrt99.0%
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 33.0%
Taylor expanded in x around inf 31.6%
Final simplification31.6%
(FPCore (x) :precision binary64 (/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0))))))
double code(double x) {
return 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (((x + 1.0d0) * sqrt(x)) + (x * sqrt((x + 1.0d0))))
end function
public static double code(double x) {
return 1.0 / (((x + 1.0) * Math.sqrt(x)) + (x * Math.sqrt((x + 1.0))));
}
def code(x): return 1.0 / (((x + 1.0) * math.sqrt(x)) + (x * math.sqrt((x + 1.0))))
function code(x) return Float64(1.0 / Float64(Float64(Float64(x + 1.0) * sqrt(x)) + Float64(x * sqrt(Float64(x + 1.0))))) end
function tmp = code(x) tmp = 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0)))); end
code[x_] := N[(1.0 / N[(N[(N[(x + 1.0), $MachinePrecision] * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(x * N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}}
\end{array}
herbie shell --seed 2024039
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:herbie-target
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))