
(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 6 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
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))) 0.0)
(* (pow x -1.5) 0.5)
(/
1.0
(/
(pow (+ 1.0 x) 0.5)
(/ (- (+ 1.0 x) x) (+ x (pow (* x (+ 1.0 x)) 0.5)))))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)))) <= 0.0) {
tmp = pow(x, -1.5) * 0.5;
} else {
tmp = 1.0 / (pow((1.0 + x), 0.5) / (((1.0 + x) - x) / (x + pow((x * (1.0 + x)), 0.5))));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))) <= 0.0d0) then
tmp = (x ** (-1.5d0)) * 0.5d0
else
tmp = 1.0d0 / (((1.0d0 + x) ** 0.5d0) / (((1.0d0 + x) - x) / (x + ((x * (1.0d0 + x)) ** 0.5d0))))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)))) <= 0.0) {
tmp = Math.pow(x, -1.5) * 0.5;
} else {
tmp = 1.0 / (Math.pow((1.0 + x), 0.5) / (((1.0 + x) - x) / (x + Math.pow((x * (1.0 + x)), 0.5))));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))) <= 0.0: tmp = math.pow(x, -1.5) * 0.5 else: tmp = 1.0 / (math.pow((1.0 + x), 0.5) / (((1.0 + x) - x) / (x + math.pow((x * (1.0 + x)), 0.5)))) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) <= 0.0) tmp = Float64((x ^ -1.5) * 0.5); else tmp = Float64(1.0 / Float64((Float64(1.0 + x) ^ 0.5) / Float64(Float64(Float64(1.0 + x) - x) / Float64(x + (Float64(x * Float64(1.0 + x)) ^ 0.5))))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)))) <= 0.0) tmp = (x ^ -1.5) * 0.5; else tmp = 1.0 / (((1.0 + x) ^ 0.5) / (((1.0 + x) - x) / (x + ((x * (1.0 + x)) ^ 0.5)))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(1.0 / N[(N[Power[N[(1.0 + x), $MachinePrecision], 0.5], $MachinePrecision] / N[(N[(N[(1.0 + x), $MachinePrecision] - x), $MachinePrecision] / N[(x + N[Power[N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{{\left(1 + x\right)}^{0.5}}{\frac{\left(1 + x\right) - x}{x + {\left(x \cdot \left(1 + x\right)\right)}^{0.5}}}}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) < 0.0Initial program 38.1%
Taylor expanded in x around inf 0
Simplified0
Applied egg-rr0
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 59.1%
Applied egg-rr0
(FPCore (x)
:precision binary64
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))) 0.0)
(* (pow x -1.5) 0.5)
(/
(/ (- (+ 1.0 x) x) (+ x (pow (* x (+ 1.0 x)) 0.5)))
(pow (+ 1.0 x) 0.5))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)))) <= 0.0) {
tmp = pow(x, -1.5) * 0.5;
} else {
tmp = (((1.0 + x) - x) / (x + pow((x * (1.0 + x)), 0.5))) / pow((1.0 + x), 0.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))) <= 0.0d0) then
tmp = (x ** (-1.5d0)) * 0.5d0
else
tmp = (((1.0d0 + x) - x) / (x + ((x * (1.0d0 + x)) ** 0.5d0))) / ((1.0d0 + x) ** 0.5d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)))) <= 0.0) {
tmp = Math.pow(x, -1.5) * 0.5;
} else {
tmp = (((1.0 + x) - x) / (x + Math.pow((x * (1.0 + x)), 0.5))) / Math.pow((1.0 + x), 0.5);
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))) <= 0.0: tmp = math.pow(x, -1.5) * 0.5 else: tmp = (((1.0 + x) - x) / (x + math.pow((x * (1.0 + x)), 0.5))) / math.pow((1.0 + x), 0.5) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) <= 0.0) tmp = Float64((x ^ -1.5) * 0.5); else tmp = Float64(Float64(Float64(Float64(1.0 + x) - x) / Float64(x + (Float64(x * Float64(1.0 + x)) ^ 0.5))) / (Float64(1.0 + x) ^ 0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)))) <= 0.0) tmp = (x ^ -1.5) * 0.5; else tmp = (((1.0 + x) - x) / (x + ((x * (1.0 + x)) ^ 0.5))) / ((1.0 + x) ^ 0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(N[(1.0 + x), $MachinePrecision] - x), $MachinePrecision] / N[(x + N[Power[N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Power[N[(1.0 + x), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\left(1 + x\right) - x}{x + {\left(x \cdot \left(1 + x\right)\right)}^{0.5}}}{{\left(1 + x\right)}^{0.5}}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) < 0.0Initial program 38.1%
Taylor expanded in x around inf 0
Simplified0
Applied egg-rr0
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 59.1%
Applied egg-rr0
(FPCore (x)
:precision binary64
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))) 0.0)
(* (pow x -1.5) 0.5)
(*
(/ (- (+ 1.0 x) x) (+ x (pow (* x (+ 1.0 x)) 0.5)))
(pow (+ 1.0 x) -0.5))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)))) <= 0.0) {
tmp = pow(x, -1.5) * 0.5;
} else {
tmp = (((1.0 + x) - x) / (x + pow((x * (1.0 + x)), 0.5))) * pow((1.0 + x), -0.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))) <= 0.0d0) then
tmp = (x ** (-1.5d0)) * 0.5d0
else
tmp = (((1.0d0 + x) - x) / (x + ((x * (1.0d0 + x)) ** 0.5d0))) * ((1.0d0 + x) ** (-0.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)))) <= 0.0) {
tmp = Math.pow(x, -1.5) * 0.5;
} else {
tmp = (((1.0 + x) - x) / (x + Math.pow((x * (1.0 + x)), 0.5))) * Math.pow((1.0 + x), -0.5);
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))) <= 0.0: tmp = math.pow(x, -1.5) * 0.5 else: tmp = (((1.0 + x) - x) / (x + math.pow((x * (1.0 + x)), 0.5))) * math.pow((1.0 + x), -0.5) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) <= 0.0) tmp = Float64((x ^ -1.5) * 0.5); else tmp = Float64(Float64(Float64(Float64(1.0 + x) - x) / Float64(x + (Float64(x * Float64(1.0 + x)) ^ 0.5))) * (Float64(1.0 + x) ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)))) <= 0.0) tmp = (x ^ -1.5) * 0.5; else tmp = (((1.0 + x) - x) / (x + ((x * (1.0 + x)) ^ 0.5))) * ((1.0 + x) ^ -0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(N[(1.0 + x), $MachinePrecision] - x), $MachinePrecision] / N[(x + N[Power[N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(1 + x\right) - x}{x + {\left(x \cdot \left(1 + x\right)\right)}^{0.5}} \cdot {\left(1 + x\right)}^{-0.5}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) < 0.0Initial program 38.1%
Taylor expanded in x around inf 0
Simplified0
Applied egg-rr0
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 59.1%
Applied egg-rr0
(FPCore (x) :precision binary64 (/ (* (pow x -1.5) (/ (+ x -1.0) x)) 2.0))
double code(double x) {
return (pow(x, -1.5) * ((x + -1.0) / x)) / 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x ** (-1.5d0)) * ((x + (-1.0d0)) / x)) / 2.0d0
end function
public static double code(double x) {
return (Math.pow(x, -1.5) * ((x + -1.0) / x)) / 2.0;
}
def code(x): return (math.pow(x, -1.5) * ((x + -1.0) / x)) / 2.0
function code(x) return Float64(Float64((x ^ -1.5) * Float64(Float64(x + -1.0) / x)) / 2.0) end
function tmp = code(x) tmp = ((x ^ -1.5) * ((x + -1.0) / x)) / 2.0; end
code[x_] := N[(N[(N[Power[x, -1.5], $MachinePrecision] * N[(N[(x + -1.0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-1.5} \cdot \frac{x + -1}{x}}{2}
\end{array}
Initial program 39.1%
Taylor expanded in x around inf 0
Simplified0
Applied egg-rr0
Applied egg-rr0
(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 39.1%
Taylor expanded in x around inf 0
Simplified0
Applied egg-rr0
(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 39.1%
Taylor expanded in x around inf 0
Simplified0
Applied egg-rr0
(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 2024111
(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)))))