
(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 3 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 (/ (* x (- (* 0.125 (pow x -2.5)) (* (pow x -1.5) 0.5))) (- -0.5 x)))
double code(double x) {
return (x * ((0.125 * pow(x, -2.5)) - (pow(x, -1.5) * 0.5))) / (-0.5 - x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * ((0.125d0 * (x ** (-2.5d0))) - ((x ** (-1.5d0)) * 0.5d0))) / ((-0.5d0) - x)
end function
public static double code(double x) {
return (x * ((0.125 * Math.pow(x, -2.5)) - (Math.pow(x, -1.5) * 0.5))) / (-0.5 - x);
}
def code(x): return (x * ((0.125 * math.pow(x, -2.5)) - (math.pow(x, -1.5) * 0.5))) / (-0.5 - x)
function code(x) return Float64(Float64(x * Float64(Float64(0.125 * (x ^ -2.5)) - Float64((x ^ -1.5) * 0.5))) / Float64(-0.5 - x)) end
function tmp = code(x) tmp = (x * ((0.125 * (x ^ -2.5)) - ((x ^ -1.5) * 0.5))) / (-0.5 - x); end
code[x_] := N[(N[(x * N[(N[(0.125 * N[Power[x, -2.5], $MachinePrecision]), $MachinePrecision] - N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(-0.5 - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(0.125 \cdot {x}^{-2.5} - {x}^{-1.5} \cdot 0.5\right)}{-0.5 - x}
\end{array}
Initial program 37.6%
frac-2neg37.6%
metadata-eval37.6%
frac-sub37.6%
*-un-lft-identity37.6%
+-commutative37.6%
+-commutative37.6%
Applied egg-rr37.6%
Taylor expanded in x around inf 36.9%
associate-+r+67.9%
distribute-rgt1-in67.9%
metadata-eval67.9%
mul0-lft67.9%
associate--r+67.9%
Simplified67.9%
*-un-lft-identity67.9%
pow-flip69.2%
sqrt-pow198.7%
metadata-eval98.7%
metadata-eval98.7%
Applied egg-rr98.7%
*-lft-identity98.7%
Simplified98.7%
Taylor expanded in x around inf 99.1%
mul-1-neg99.1%
distribute-lft-in99.1%
*-rgt-identity99.1%
distribute-neg-in99.1%
*-commutative99.1%
associate-*r*99.1%
rgt-mult-inverse99.1%
metadata-eval99.1%
metadata-eval99.1%
Simplified99.1%
Final simplification99.1%
(FPCore (x) :precision binary64 (* (pow x -1.5) 0.5))
double code(double x) {
return pow(x, -1.5) * 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-1.5d0)) * 0.5d0
end function
public static double code(double x) {
return Math.pow(x, -1.5) * 0.5;
}
def code(x): return math.pow(x, -1.5) * 0.5
function code(x) return Float64((x ^ -1.5) * 0.5) end
function tmp = code(x) tmp = (x ^ -1.5) * 0.5; end
code[x_] := N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5} \cdot 0.5
\end{array}
Initial program 37.6%
Taylor expanded in x around inf 67.2%
*-un-lft-identity67.9%
pow-flip69.2%
sqrt-pow198.7%
metadata-eval98.7%
metadata-eval98.7%
Applied egg-rr98.3%
*-lft-identity98.7%
Simplified98.3%
Final simplification98.3%
(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 37.6%
frac-2neg37.6%
metadata-eval37.6%
frac-sub37.6%
*-un-lft-identity37.6%
+-commutative37.6%
+-commutative37.6%
Applied egg-rr37.6%
Taylor expanded in x around inf 35.2%
distribute-rgt1-in35.2%
metadata-eval35.2%
mul0-lft35.2%
Simplified35.2%
(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 2024123
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(! :herbie-platform default (/ 1 (+ (* (+ x 1) (sqrt x)) (* x (sqrt (+ x 1))))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))