
(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 (+ 1.0 x)))) 0.0) (* (pow x -1.5) 0.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 = pow(x, -1.5) * 0.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 = (x ** (-1.5d0)) * 0.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 = Math.pow(x, -1.5) * 0.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 = math.pow(x, -1.5) * 0.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((x ^ -1.5) * 0.5); else tmp = Float64(1.0 / Float64(x * Float64(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 = (x ^ -1.5) * 0.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[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(1.0 / N[(x * N[(N[(1.0 + x), $MachinePrecision] * N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0Initial program 36.8%
flip--36.8%
clear-num36.8%
inv-pow36.8%
sqrt-pow236.8%
metadata-eval36.8%
inv-pow36.8%
sqrt-pow236.8%
+-commutative36.8%
metadata-eval36.8%
frac-times19.8%
metadata-eval19.8%
add-sqr-sqrt16.6%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt36.8%
+-commutative36.8%
Applied egg-rr36.8%
Taylor expanded in x around inf 65.7%
*-commutative65.7%
Simplified65.7%
*-un-lft-identity65.7%
pow1/265.7%
pow-flip66.5%
metadata-eval66.5%
pow-pow100.0%
metadata-eval100.0%
Applied egg-rr100.0%
*-lft-identity100.0%
Simplified100.0%
if 0.0 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 60.4%
flip--61.1%
clear-num61.1%
inv-pow61.1%
sqrt-pow261.1%
metadata-eval61.1%
inv-pow61.1%
sqrt-pow261.1%
+-commutative61.1%
metadata-eval61.1%
frac-times61.0%
metadata-eval61.0%
add-sqr-sqrt61.1%
frac-times61.2%
metadata-eval61.2%
add-sqr-sqrt62.6%
+-commutative62.6%
Applied egg-rr62.6%
frac-sub98.9%
*-un-lft-identity98.9%
Applied egg-rr98.9%
*-rgt-identity98.9%
associate--l+99.1%
+-inverses99.1%
metadata-eval99.1%
Simplified99.1%
associate-/r/99.3%
/-rgt-identity99.3%
*-commutative99.3%
associate-*r*99.4%
Applied egg-rr99.4%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 0.0) (* (pow x -1.5) 0.5) (/ 1.0 (* (+ 1.0 x) (+ (sqrt x) (* x (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 = pow(x, -1.5) * 0.5;
} else {
tmp = 1.0 / ((1.0 + x) * (sqrt(x) + (x * 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 = (x ** (-1.5d0)) * 0.5d0
else
tmp = 1.0d0 / ((1.0d0 + x) * (sqrt(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((1.0 + x)))) <= 0.0) {
tmp = Math.pow(x, -1.5) * 0.5;
} else {
tmp = 1.0 / ((1.0 + x) * (Math.sqrt(x) + (x * 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 = math.pow(x, -1.5) * 0.5 else: tmp = 1.0 / ((1.0 + x) * (math.sqrt(x) + (x * 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((x ^ -1.5) * 0.5); else tmp = Float64(1.0 / Float64(Float64(1.0 + x) * Float64(sqrt(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((1.0 + x)))) <= 0.0) tmp = (x ^ -1.5) * 0.5; else tmp = 1.0 / ((1.0 + x) * (sqrt(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[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], 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}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(1 + x\right) \cdot \left(\sqrt{x} + x \cdot {\left(1 + x\right)}^{-0.5}\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0Initial program 36.8%
flip--36.8%
clear-num36.8%
inv-pow36.8%
sqrt-pow236.8%
metadata-eval36.8%
inv-pow36.8%
sqrt-pow236.8%
+-commutative36.8%
metadata-eval36.8%
frac-times19.8%
metadata-eval19.8%
add-sqr-sqrt16.6%
frac-times22.4%
metadata-eval22.4%
add-sqr-sqrt36.8%
+-commutative36.8%
Applied egg-rr36.8%
Taylor expanded in x around inf 65.7%
*-commutative65.7%
Simplified65.7%
*-un-lft-identity65.7%
pow1/265.7%
pow-flip66.5%
metadata-eval66.5%
pow-pow100.0%
metadata-eval100.0%
Applied egg-rr100.0%
*-lft-identity100.0%
Simplified100.0%
if 0.0 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 60.4%
flip--61.1%
clear-num61.1%
inv-pow61.1%
sqrt-pow261.1%
metadata-eval61.1%
inv-pow61.1%
sqrt-pow261.1%
+-commutative61.1%
metadata-eval61.1%
frac-times61.0%
metadata-eval61.0%
add-sqr-sqrt61.1%
frac-times61.2%
metadata-eval61.2%
add-sqr-sqrt62.6%
+-commutative62.6%
Applied egg-rr62.6%
frac-sub98.9%
*-un-lft-identity98.9%
Applied egg-rr98.9%
*-rgt-identity98.9%
associate--l+99.1%
+-inverses99.1%
metadata-eval99.1%
Simplified99.1%
associate-/r/99.3%
/-rgt-identity99.3%
distribute-rgt-in99.4%
*-un-lft-identity99.4%
distribute-lft-in99.2%
pow299.2%
Applied egg-rr99.2%
*-rgt-identity99.2%
unpow299.2%
associate-*r*99.1%
distribute-lft-in99.4%
*-commutative99.4%
*-commutative99.4%
distribute-lft-in99.3%
*-commutative99.3%
pow-plus99.1%
metadata-eval99.1%
unpow1/299.1%
Simplified99.1%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ (/ (/ 1.0 x) (+ (pow x -0.5) (pow (+ 1.0 x) -0.5))) (+ 1.0 x)))
double code(double x) {
return ((1.0 / x) / (pow(x, -0.5) + pow((1.0 + x), -0.5))) / (1.0 + x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / x) / ((x ** (-0.5d0)) + ((1.0d0 + x) ** (-0.5d0)))) / (1.0d0 + x)
end function
public static double code(double x) {
return ((1.0 / x) / (Math.pow(x, -0.5) + Math.pow((1.0 + x), -0.5))) / (1.0 + x);
}
def code(x): return ((1.0 / x) / (math.pow(x, -0.5) + math.pow((1.0 + x), -0.5))) / (1.0 + x)
function code(x) return Float64(Float64(Float64(1.0 / x) / Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5))) / Float64(1.0 + x)) end
function tmp = code(x) tmp = ((1.0 / x) / ((x ^ -0.5) + ((1.0 + x) ^ -0.5))) / (1.0 + x); end
code[x_] := N[(N[(N[(1.0 / x), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}}{1 + x}
\end{array}
Initial program 37.8%
flip--37.8%
clear-num37.8%
inv-pow37.8%
sqrt-pow237.8%
metadata-eval37.8%
inv-pow37.8%
sqrt-pow237.8%
+-commutative37.8%
metadata-eval37.8%
frac-times21.6%
metadata-eval21.6%
add-sqr-sqrt18.5%
frac-times24.0%
metadata-eval24.0%
add-sqr-sqrt37.9%
+-commutative37.9%
Applied egg-rr37.9%
frac-sub39.4%
*-un-lft-identity39.4%
Applied egg-rr39.4%
*-rgt-identity39.4%
associate--l+80.2%
+-inverses80.2%
metadata-eval80.2%
Simplified80.2%
associate-/r/80.2%
*-un-lft-identity80.2%
associate-/r*82.2%
frac-times99.5%
*-un-lft-identity99.5%
Applied egg-rr99.5%
*-lft-identity99.5%
associate-/r*99.6%
Simplified99.6%
Final simplification99.6%
(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 37.8%
flip--37.8%
clear-num37.8%
inv-pow37.8%
sqrt-pow237.8%
metadata-eval37.8%
inv-pow37.8%
sqrt-pow237.8%
+-commutative37.8%
metadata-eval37.8%
frac-times21.6%
metadata-eval21.6%
add-sqr-sqrt18.5%
frac-times24.0%
metadata-eval24.0%
add-sqr-sqrt37.9%
+-commutative37.9%
Applied egg-rr37.9%
Taylor expanded in x around inf 65.4%
*-commutative65.4%
Simplified65.4%
*-un-lft-identity65.4%
pow1/265.4%
pow-flip66.2%
metadata-eval66.2%
pow-pow98.2%
metadata-eval98.2%
Applied egg-rr98.2%
*-lft-identity98.2%
Simplified98.2%
Final simplification98.2%
(FPCore (x) :precision binary64 (sqrt (/ 0.25 x)))
double code(double x) {
return sqrt((0.25 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((0.25d0 / x))
end function
public static double code(double x) {
return Math.sqrt((0.25 / x));
}
def code(x): return math.sqrt((0.25 / x))
function code(x) return sqrt(Float64(0.25 / x)) end
function tmp = code(x) tmp = sqrt((0.25 / x)); end
code[x_] := N[Sqrt[N[(0.25 / x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{0.25}{x}}
\end{array}
Initial program 37.8%
flip--37.8%
clear-num37.8%
inv-pow37.8%
sqrt-pow237.8%
metadata-eval37.8%
inv-pow37.8%
sqrt-pow237.8%
+-commutative37.8%
metadata-eval37.8%
frac-times21.6%
metadata-eval21.6%
add-sqr-sqrt18.5%
frac-times24.0%
metadata-eval24.0%
add-sqr-sqrt37.9%
+-commutative37.9%
Applied egg-rr37.9%
Taylor expanded in x around inf 36.9%
unpow1/236.9%
rem-exp-log36.9%
exp-neg36.9%
exp-prod36.9%
distribute-lft-neg-out36.9%
distribute-rgt-neg-in36.9%
metadata-eval36.9%
exp-to-pow36.9%
Simplified36.9%
Taylor expanded in x around 0 5.6%
add-sqr-sqrt5.6%
sqrt-unprod5.6%
clear-num5.6%
clear-num5.6%
frac-times5.6%
metadata-eval5.6%
div-inv5.6%
metadata-eval5.6%
/-rgt-identity5.6%
associate-*r*5.6%
*-un-lft-identity5.6%
associate-/l*5.6%
Applied egg-rr5.6%
associate-/r*5.6%
metadata-eval5.6%
Simplified5.6%
Final simplification5.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 37.8%
flip--37.8%
clear-num37.8%
inv-pow37.8%
sqrt-pow237.8%
metadata-eval37.8%
inv-pow37.8%
sqrt-pow237.8%
+-commutative37.8%
metadata-eval37.8%
frac-times21.6%
metadata-eval21.6%
add-sqr-sqrt18.5%
frac-times24.0%
metadata-eval24.0%
add-sqr-sqrt37.9%
+-commutative37.9%
Applied egg-rr37.9%
Taylor expanded in x around 0 5.6%
unpow1/25.6%
rem-exp-log5.6%
exp-neg5.6%
exp-prod5.6%
distribute-lft-neg-out5.6%
distribute-rgt-neg-in5.6%
metadata-eval5.6%
exp-to-pow5.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 2024050
(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)))))