
(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 (/ 1.0 (* (+ 1.0 x) (+ (sqrt x) (* x (pow (+ 1.0 x) -0.5))))))
double code(double x) {
return 1.0 / ((1.0 + x) * (sqrt(x) + (x * pow((1.0 + x), -0.5))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / ((1.0d0 + x) * (sqrt(x) + (x * ((1.0d0 + x) ** (-0.5d0)))))
end function
public static double code(double x) {
return 1.0 / ((1.0 + x) * (Math.sqrt(x) + (x * Math.pow((1.0 + x), -0.5))));
}
def code(x): return 1.0 / ((1.0 + x) * (math.sqrt(x) + (x * math.pow((1.0 + x), -0.5))))
function code(x) return Float64(1.0 / Float64(Float64(1.0 + x) * Float64(sqrt(x) + Float64(x * (Float64(1.0 + x) ^ -0.5))))) end
function tmp = code(x) tmp = 1.0 / ((1.0 + x) * (sqrt(x) + (x * ((1.0 + x) ^ -0.5)))); end
code[x_] := N[(1.0 / N[(N[(1.0 + x), $MachinePrecision] * N[(N[Sqrt[x], $MachinePrecision] + N[(x * N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(1 + x\right) \cdot \left(\sqrt{x} + x \cdot {\left(1 + x\right)}^{-0.5}\right)}
\end{array}
Initial program 40.0%
flip--40.1%
clear-num40.1%
inv-pow40.1%
sqrt-pow240.1%
metadata-eval40.1%
pow1/240.1%
pow-flip40.1%
+-commutative40.1%
metadata-eval40.1%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt18.0%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt40.1%
Applied egg-rr40.1%
frac-sub40.8%
*-un-lft-identity40.8%
Applied egg-rr40.8%
*-rgt-identity40.8%
associate--l+85.3%
+-inverses85.3%
metadata-eval85.3%
Simplified85.3%
associate-/r/85.3%
/-rgt-identity85.3%
distribute-rgt-in85.3%
*-un-lft-identity85.3%
distribute-lft-in85.3%
+-commutative85.3%
+-commutative85.3%
pow285.3%
Applied egg-rr85.3%
distribute-lft-in85.3%
*-commutative85.3%
unpow285.3%
distribute-rgt1-in85.3%
associate-*r*98.8%
distribute-rgt-in98.8%
+-commutative98.8%
pow-plus98.9%
metadata-eval98.9%
unpow1/298.9%
*-commutative98.9%
+-commutative98.9%
Simplified98.9%
Final simplification98.9%
(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 40.0%
Taylor expanded in x around inf 67.3%
pow167.3%
*-commutative67.3%
pow-flip68.8%
sqrt-pow198.0%
metadata-eval98.0%
metadata-eval98.0%
Applied egg-rr98.0%
unpow198.0%
*-commutative98.0%
Simplified98.0%
Final simplification98.0%
(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 40.0%
Taylor expanded in x around 0 5.6%
inv-pow5.6%
sqrt-pow15.6%
metadata-eval5.6%
*-un-lft-identity5.6%
Applied egg-rr5.6%
*-lft-identity5.6%
Simplified5.6%
Final simplification5.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 2024095
(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)))))