
(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 (/ (- (/ 0.5 x) (/ 0.375 (pow x 2.0))) (sqrt x)))
double code(double x) {
return ((0.5 / x) - (0.375 / pow(x, 2.0))) / sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 / x) - (0.375d0 / (x ** 2.0d0))) / sqrt(x)
end function
public static double code(double x) {
return ((0.5 / x) - (0.375 / Math.pow(x, 2.0))) / Math.sqrt(x);
}
def code(x): return ((0.5 / x) - (0.375 / math.pow(x, 2.0))) / math.sqrt(x)
function code(x) return Float64(Float64(Float64(0.5 / x) - Float64(0.375 / (x ^ 2.0))) / sqrt(x)) end
function tmp = code(x) tmp = ((0.5 / x) - (0.375 / (x ^ 2.0))) / sqrt(x); end
code[x_] := N[(N[(N[(0.5 / x), $MachinePrecision] - N[(0.375 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{x} - \frac{0.375}{{x}^{2}}}{\sqrt{x}}
\end{array}
Initial program 43.2%
frac-2neg43.2%
metadata-eval43.2%
frac-2neg43.2%
metadata-eval43.2%
frac-sub43.3%
fma-neg43.3%
+-commutative43.3%
+-commutative43.3%
Applied egg-rr43.3%
fma-undefine43.3%
distribute-rgt-neg-in43.3%
metadata-eval43.3%
*-rgt-identity43.3%
unsub-neg43.3%
mul-1-neg43.3%
remove-double-neg43.3%
*-lft-identity43.3%
*-lft-identity43.3%
distribute-rgt-neg-out43.3%
*-commutative43.3%
distribute-rgt-neg-in43.3%
remove-double-neg43.3%
associate-/r*43.3%
Simplified43.3%
Taylor expanded in x around inf 99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
Final simplification99.0%
(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 43.2%
frac-2neg43.2%
metadata-eval43.2%
frac-2neg43.2%
metadata-eval43.2%
frac-sub43.3%
fma-neg43.3%
+-commutative43.3%
+-commutative43.3%
Applied egg-rr43.3%
fma-undefine43.3%
distribute-rgt-neg-in43.3%
metadata-eval43.3%
*-rgt-identity43.3%
unsub-neg43.3%
mul-1-neg43.3%
remove-double-neg43.3%
*-lft-identity43.3%
*-lft-identity43.3%
distribute-rgt-neg-out43.3%
*-commutative43.3%
distribute-rgt-neg-in43.3%
remove-double-neg43.3%
associate-/r*43.3%
Simplified43.3%
Taylor expanded in x around inf 42.6%
associate-*r/42.6%
metadata-eval42.6%
Simplified42.6%
associate--r-98.3%
metadata-eval98.3%
+-lft-identity98.3%
*-un-lft-identity98.3%
add-sqr-sqrt98.1%
times-frac98.1%
pow1/298.1%
sqrt-pow198.1%
metadata-eval98.1%
pow1/298.1%
sqrt-pow198.1%
metadata-eval98.1%
Applied egg-rr98.1%
associate-*l/98.0%
*-lft-identity98.0%
associate-/l/98.1%
pow-plus98.2%
metadata-eval98.2%
Simplified98.2%
associate-/l/97.3%
pow-prod-up97.4%
metadata-eval97.4%
metadata-eval97.4%
pow-div97.4%
pow197.4%
associate-/l*98.4%
*-commutative98.4%
associate-*l/98.4%
pow198.4%
pow-div98.6%
metadata-eval98.6%
Applied egg-rr98.6%
Final simplification98.6%
(FPCore (x) :precision binary64 (pow x -0.5))
double code(double x) {
return pow(x, -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-0.5d0)
end function
public static double code(double x) {
return Math.pow(x, -0.5);
}
def code(x): return math.pow(x, -0.5)
function code(x) return x ^ -0.5 end
function tmp = code(x) tmp = x ^ -0.5; end
code[x_] := N[Power[x, -0.5], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5}
\end{array}
Initial program 43.2%
frac-2neg43.2%
metadata-eval43.2%
frac-2neg43.2%
metadata-eval43.2%
frac-sub43.3%
fma-neg43.3%
+-commutative43.3%
+-commutative43.3%
Applied egg-rr43.3%
fma-undefine43.3%
distribute-rgt-neg-in43.3%
metadata-eval43.3%
*-rgt-identity43.3%
unsub-neg43.3%
mul-1-neg43.3%
remove-double-neg43.3%
*-lft-identity43.3%
*-lft-identity43.3%
distribute-rgt-neg-out43.3%
*-commutative43.3%
distribute-rgt-neg-in43.3%
remove-double-neg43.3%
associate-/r*43.3%
Simplified43.3%
Taylor expanded in x around 0 5.5%
add05.5%
inv-pow5.5%
sqrt-pow25.5%
metadata-eval5.5%
Applied egg-rr5.5%
add05.5%
Simplified5.5%
Final simplification5.5%
(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 2024034
(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)))))