
(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 (* (* (pow x -1.5) (+ (/ 1.0 x) -1.0)) -0.5))
double code(double x) {
return (pow(x, -1.5) * ((1.0 / x) + -1.0)) * -0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x ** (-1.5d0)) * ((1.0d0 / x) + (-1.0d0))) * (-0.5d0)
end function
public static double code(double x) {
return (Math.pow(x, -1.5) * ((1.0 / x) + -1.0)) * -0.5;
}
def code(x): return (math.pow(x, -1.5) * ((1.0 / x) + -1.0)) * -0.5
function code(x) return Float64(Float64((x ^ -1.5) * Float64(Float64(1.0 / x) + -1.0)) * -0.5) end
function tmp = code(x) tmp = ((x ^ -1.5) * ((1.0 / x) + -1.0)) * -0.5; end
code[x_] := N[(N[(N[Power[x, -1.5], $MachinePrecision] * N[(N[(1.0 / x), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]
\begin{array}{l}
\\
\left({x}^{-1.5} \cdot \left(\frac{1}{x} + -1\right)\right) \cdot -0.5
\end{array}
Initial program 34.7%
Taylor expanded in x around inf 79.6%
Taylor expanded in x around inf 98.5%
metadata-eval98.5%
cancel-sign-sub-inv98.5%
distribute-lft-out--98.5%
associate-/l*98.5%
div-sub98.5%
unpow1/298.5%
rem-exp-log94.2%
exp-neg94.2%
exp-prod94.2%
distribute-lft-neg-out94.2%
distribute-rgt-neg-in94.2%
metadata-eval94.2%
exp-to-pow98.6%
*-lft-identity98.6%
associate-*l/98.4%
unpow-198.4%
metadata-eval98.4%
pow-sqr98.0%
Simplified98.8%
*-commutative98.8%
pow-flip98.8%
sqrt-pow198.8%
metadata-eval98.8%
metadata-eval98.8%
Applied egg-rr98.8%
div-inv98.8%
fma-neg98.8%
Applied egg-rr98.8%
fma-undefine98.8%
*-commutative98.8%
neg-mul-198.8%
distribute-rgt-out98.8%
Simplified98.8%
(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 34.7%
Taylor expanded in x around inf 61.6%
*-commutative61.6%
pow-flip61.9%
sqrt-pow198.7%
metadata-eval98.7%
metadata-eval98.7%
Applied egg-rr98.7%
(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 34.7%
expm1-log1p-u34.7%
expm1-undefine4.8%
inv-pow4.8%
sqrt-pow24.8%
metadata-eval4.8%
Applied egg-rr4.8%
log1p-undefine4.8%
rem-exp-log4.8%
+-commutative4.8%
associate--l+27.1%
metadata-eval27.1%
+-rgt-identity27.1%
Simplified27.1%
Taylor expanded in x around 0 5.5%
unpow1/25.5%
rem-exp-log5.5%
exp-neg5.5%
exp-prod5.5%
distribute-lft-neg-out5.5%
distribute-rgt-neg-in5.5%
metadata-eval5.5%
exp-to-pow5.5%
Simplified5.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 2024096
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))