
(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 5 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 (+ 1.0 x)))) 0.0) (* 0.5 (pow x -1.5)) (/ (/ (/ -1.0 x) (- -1.0 x)) (+ (pow x -0.5) (pow (+ 1.0 x) -0.5)))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = ((-1.0 / x) / (-1.0 - x)) / (pow(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((1.0d0 + x)))) <= 0.0d0) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = (((-1.0d0) / x) / ((-1.0d0) - x)) / ((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((1.0 + x)))) <= 0.0) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = ((-1.0 / x) / (-1.0 - x)) / (Math.pow(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((1.0 + x)))) <= 0.0: tmp = 0.5 * math.pow(x, -1.5) else: tmp = ((-1.0 / x) / (-1.0 - x)) / (math.pow(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(1.0 + x)))) <= 0.0) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64(Float64(Float64(-1.0 / x) / Float64(-1.0 - x)) / Float64((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((1.0 + x)))) <= 0.0) tmp = 0.5 * (x ^ -1.5); else tmp = ((-1.0 / x) / (-1.0 - x)) / ((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[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(N[(N[(-1.0 / x), $MachinePrecision] / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\frac{-1}{x}}{-1 - x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}\\
\end{array}
\end{array}
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0Initial program 36.9%
flip--36.9%
clear-num36.9%
pow1/236.9%
pow-flip36.9%
metadata-eval36.9%
inv-pow36.9%
sqrt-pow236.9%
+-commutative36.9%
metadata-eval36.9%
frac-times19.3%
metadata-eval19.3%
add-sqr-sqrt18.4%
frac-times23.2%
metadata-eval23.2%
add-sqr-sqrt36.9%
Applied egg-rr36.9%
frac-2neg36.9%
metadata-eval36.9%
frac-sub36.9%
Applied egg-rr36.9%
*-lft-identity36.9%
neg-mul-136.9%
*-commutative36.9%
/-rgt-identity36.9%
neg-mul-136.9%
*-rgt-identity36.9%
distribute-rgt-neg-in36.9%
distribute-lft-neg-in36.9%
/-rgt-identity36.9%
Simplified83.7%
Taylor expanded in x around inf 66.0%
unpow-166.0%
exp-to-pow63.6%
*-commutative63.6%
exp-prod64.8%
*-commutative64.8%
associate-*r*64.8%
metadata-eval64.8%
*-commutative64.8%
exp-to-pow67.3%
metadata-eval67.3%
pow-sqr67.4%
rem-sqrt-square100.0%
rem-square-sqrt99.5%
fabs-sqr99.5%
rem-square-sqrt100.0%
Simplified100.0%
if 0.0 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 57.2%
flip--57.7%
clear-num57.7%
pow1/257.7%
pow-flip57.7%
metadata-eval57.7%
inv-pow57.7%
sqrt-pow257.7%
+-commutative57.7%
metadata-eval57.7%
frac-times57.4%
metadata-eval57.4%
add-sqr-sqrt58.1%
frac-times58.1%
metadata-eval58.1%
add-sqr-sqrt59.1%
Applied egg-rr59.1%
frac-2neg59.1%
metadata-eval59.1%
frac-sub99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
neg-mul-199.3%
*-commutative99.3%
/-rgt-identity99.3%
neg-mul-199.3%
*-rgt-identity99.3%
distribute-rgt-neg-in99.3%
distribute-lft-neg-in99.3%
/-rgt-identity99.3%
Simplified99.3%
clear-num99.4%
div-inv99.4%
*-un-lft-identity99.4%
times-frac99.4%
metadata-eval99.4%
+-commutative99.4%
Applied egg-rr99.4%
add-sqr-sqrt0.0%
sqrt-unprod1.7%
frac-times1.7%
metadata-eval1.7%
metadata-eval1.7%
frac-times1.7%
sqrt-unprod1.7%
add-sqr-sqrt1.7%
expm1-log1p-u1.7%
expm1-udef2.5%
Applied egg-rr35.5%
expm1-def99.5%
expm1-log1p99.5%
Simplified99.5%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 0.0) (* 0.5 (pow x -1.5)) (/ -1.0 (* (+ (pow x -0.5) (pow (+ 1.0 x) -0.5)) (* x (- -1.0 x))))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = -1.0 / ((pow(x, -0.5) + pow((1.0 + x), -0.5)) * (x * (-1.0 - x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) + ((-1.0d0) / sqrt((1.0d0 + x)))) <= 0.0d0) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = (-1.0d0) / (((x ** (-0.5d0)) + ((1.0d0 + x) ** (-0.5d0))) * (x * ((-1.0d0) - x)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) + (-1.0 / Math.sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = -1.0 / ((Math.pow(x, -0.5) + Math.pow((1.0 + x), -0.5)) * (x * (-1.0 - x)));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 0.0: tmp = 0.5 * math.pow(x, -1.5) else: tmp = -1.0 / ((math.pow(x, -0.5) + math.pow((1.0 + x), -0.5)) * (x * (-1.0 - x))) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(1.0 + x)))) <= 0.0) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64(-1.0 / Float64(Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5)) * Float64(x * Float64(-1.0 - x)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) tmp = 0.5 * (x ^ -1.5); else tmp = -1.0 / (((x ^ -0.5) + ((1.0 + x) ^ -0.5)) * (x * (-1.0 - x))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision] * N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x \cdot \left(-1 - x\right)\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0Initial program 36.9%
flip--36.9%
clear-num36.9%
pow1/236.9%
pow-flip36.9%
metadata-eval36.9%
inv-pow36.9%
sqrt-pow236.9%
+-commutative36.9%
metadata-eval36.9%
frac-times19.3%
metadata-eval19.3%
add-sqr-sqrt18.4%
frac-times23.2%
metadata-eval23.2%
add-sqr-sqrt36.9%
Applied egg-rr36.9%
frac-2neg36.9%
metadata-eval36.9%
frac-sub36.9%
Applied egg-rr36.9%
*-lft-identity36.9%
neg-mul-136.9%
*-commutative36.9%
/-rgt-identity36.9%
neg-mul-136.9%
*-rgt-identity36.9%
distribute-rgt-neg-in36.9%
distribute-lft-neg-in36.9%
/-rgt-identity36.9%
Simplified83.7%
Taylor expanded in x around inf 66.0%
unpow-166.0%
exp-to-pow63.6%
*-commutative63.6%
exp-prod64.8%
*-commutative64.8%
associate-*r*64.8%
metadata-eval64.8%
*-commutative64.8%
exp-to-pow67.3%
metadata-eval67.3%
pow-sqr67.4%
rem-sqrt-square100.0%
rem-square-sqrt99.5%
fabs-sqr99.5%
rem-square-sqrt100.0%
Simplified100.0%
if 0.0 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 57.2%
flip--57.7%
clear-num57.7%
pow1/257.7%
pow-flip57.7%
metadata-eval57.7%
inv-pow57.7%
sqrt-pow257.7%
+-commutative57.7%
metadata-eval57.7%
frac-times57.4%
metadata-eval57.4%
add-sqr-sqrt58.1%
frac-times58.1%
metadata-eval58.1%
add-sqr-sqrt59.1%
Applied egg-rr59.1%
frac-2neg59.1%
metadata-eval59.1%
frac-sub99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
neg-mul-199.3%
*-commutative99.3%
/-rgt-identity99.3%
neg-mul-199.3%
*-rgt-identity99.3%
distribute-rgt-neg-in99.3%
distribute-lft-neg-in99.3%
/-rgt-identity99.3%
Simplified99.3%
expm1-log1p-u99.3%
expm1-udef42.4%
clear-num42.4%
associate-/l/42.4%
+-commutative42.4%
Applied egg-rr42.4%
expm1-def99.3%
expm1-log1p99.4%
Simplified99.4%
Final simplification100.0%
(FPCore (x) :precision binary64 (- (sqrt (/ 1.0 x))))
double code(double x) {
return -sqrt((1.0 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = -sqrt((1.0d0 / x))
end function
public static double code(double x) {
return -Math.sqrt((1.0 / x));
}
def code(x): return -math.sqrt((1.0 / x))
function code(x) return Float64(-sqrt(Float64(1.0 / x))) end
function tmp = code(x) tmp = -sqrt((1.0 / x)); end
code[x_] := (-N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision])
\begin{array}{l}
\\
-\sqrt{\frac{1}{x}}
\end{array}
Initial program 37.9%
add-exp-log7.2%
log-rec7.2%
pow1/27.2%
log-pow7.2%
+-commutative7.2%
log1p-udef7.2%
Applied egg-rr7.2%
distribute-lft-neg-in7.2%
metadata-eval7.2%
exp-prod6.8%
Simplified6.8%
Taylor expanded in x around inf 3.2%
mul-1-neg3.2%
Simplified3.2%
Final simplification3.2%
(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 37.9%
flip--37.9%
clear-num38.0%
pow1/238.0%
pow-flip38.0%
metadata-eval38.0%
inv-pow38.0%
sqrt-pow238.0%
+-commutative38.0%
metadata-eval38.0%
frac-times21.2%
metadata-eval21.2%
add-sqr-sqrt20.4%
frac-times24.9%
metadata-eval24.9%
add-sqr-sqrt38.0%
Applied egg-rr38.0%
frac-2neg38.0%
metadata-eval38.0%
frac-sub40.1%
Applied egg-rr40.1%
*-lft-identity40.1%
neg-mul-140.1%
*-commutative40.1%
/-rgt-identity40.1%
neg-mul-140.1%
*-rgt-identity40.1%
distribute-rgt-neg-in40.1%
distribute-lft-neg-in40.1%
/-rgt-identity40.1%
Simplified84.5%
Taylor expanded in x around inf 65.8%
unpow-165.8%
exp-to-pow63.5%
*-commutative63.5%
exp-prod64.6%
*-commutative64.6%
associate-*r*64.6%
metadata-eval64.6%
*-commutative64.6%
exp-to-pow67.0%
metadata-eval67.0%
pow-sqr67.1%
rem-sqrt-square98.0%
rem-square-sqrt97.5%
fabs-sqr97.5%
rem-square-sqrt98.0%
Simplified98.0%
Final simplification98.0%
(FPCore (x) :precision binary64 -1.0)
double code(double x) {
return -1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -1.0d0
end function
public static double code(double x) {
return -1.0;
}
def code(x): return -1.0
function code(x) return -1.0 end
function tmp = code(x) tmp = -1.0; end
code[x_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 37.9%
Taylor expanded in x around 0 2.5%
Taylor expanded in x around inf 2.5%
Final simplification2.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 2024026
(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)))))