
(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 4 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 x) (+ (* (pow x -0.5) (+ 1.0 x)) (sqrt (+ 1.0 x)))))
double code(double x) {
return (1.0 / x) / ((pow(x, -0.5) * (1.0 + x)) + sqrt((1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (((x ** (-0.5d0)) * (1.0d0 + x)) + sqrt((1.0d0 + x)))
end function
public static double code(double x) {
return (1.0 / x) / ((Math.pow(x, -0.5) * (1.0 + x)) + Math.sqrt((1.0 + x)));
}
def code(x): return (1.0 / x) / ((math.pow(x, -0.5) * (1.0 + x)) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64((x ^ -0.5) * Float64(1.0 + x)) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = (1.0 / x) / (((x ^ -0.5) * (1.0 + x)) + sqrt((1.0 + x))); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(N[Power[x, -0.5], $MachinePrecision] * N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{{x}^{-0.5} \cdot \left(1 + x\right) + \sqrt{1 + x}}
\end{array}
Initial program 36.7%
flip--36.7%
clear-num36.7%
pow1/236.7%
pow-flip36.7%
metadata-eval36.7%
inv-pow36.7%
sqrt-pow236.7%
+-commutative36.7%
metadata-eval36.7%
frac-times22.1%
metadata-eval22.1%
add-sqr-sqrt22.2%
frac-times27.9%
metadata-eval27.9%
add-sqr-sqrt37.0%
Applied egg-rr37.0%
frac-sub39.2%
*-un-lft-identity39.2%
Applied egg-rr39.2%
*-rgt-identity39.2%
associate--l+81.4%
+-inverses81.4%
metadata-eval81.4%
Simplified81.4%
expm1-log1p-u81.4%
expm1-udef35.6%
clear-num35.6%
associate-/r*35.6%
+-commutative35.6%
+-commutative35.6%
Applied egg-rr35.6%
expm1-def82.9%
expm1-log1p82.9%
associate-/l/99.5%
*-commutative99.5%
Simplified99.5%
distribute-rgt-in99.5%
pow-plus99.6%
metadata-eval99.6%
pow1/299.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (<= x 85000000.0) (- (pow x -0.5) (pow (+ 1.0 x) -0.5)) (* 0.5 (pow x -1.5))))
double code(double x) {
double tmp;
if (x <= 85000000.0) {
tmp = pow(x, -0.5) - pow((1.0 + x), -0.5);
} else {
tmp = 0.5 * pow(x, -1.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 85000000.0d0) then
tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-0.5d0))
else
tmp = 0.5d0 * (x ** (-1.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 85000000.0) {
tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
} else {
tmp = 0.5 * Math.pow(x, -1.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 85000000.0: tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5) else: tmp = 0.5 * math.pow(x, -1.5) return tmp
function code(x) tmp = 0.0 if (x <= 85000000.0) tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5)); else tmp = Float64(0.5 * (x ^ -1.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 85000000.0) tmp = (x ^ -0.5) - ((1.0 + x) ^ -0.5); else tmp = 0.5 * (x ^ -1.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 85000000.0], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 85000000:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\end{array}
\end{array}
if x < 8.5e7Initial program 79.4%
*-un-lft-identity79.4%
clear-num79.4%
associate-/r/79.4%
prod-diff79.4%
*-un-lft-identity79.4%
fma-neg79.4%
*-un-lft-identity79.4%
pow1/279.4%
pow-flip81.0%
metadata-eval81.0%
pow1/281.0%
pow-flip81.0%
+-commutative81.0%
metadata-eval81.0%
Applied egg-rr81.0%
associate-+l-81.0%
expm1-log1p81.0%
expm1-def71.2%
associate--l-71.2%
fma-udef71.2%
distribute-lft1-in71.2%
metadata-eval71.2%
mul0-lft71.2%
metadata-eval71.2%
expm1-def81.0%
expm1-log1p81.0%
Simplified81.0%
if 8.5e7 < x Initial program 35.0%
flip--35.0%
clear-num35.0%
pow1/235.0%
pow-flip35.0%
metadata-eval35.0%
inv-pow35.0%
sqrt-pow235.0%
+-commutative35.0%
metadata-eval35.0%
frac-times19.7%
metadata-eval19.7%
add-sqr-sqrt19.8%
frac-times25.8%
metadata-eval25.8%
add-sqr-sqrt35.1%
Applied egg-rr35.1%
frac-sub36.8%
*-un-lft-identity36.8%
Applied egg-rr36.8%
*-rgt-identity36.8%
associate--l+80.7%
+-inverses80.7%
metadata-eval80.7%
Simplified80.7%
Taylor expanded in x around inf 63.9%
unpow-163.9%
exp-to-pow61.6%
*-commutative61.6%
exp-prod62.6%
*-commutative62.6%
associate-*r*62.6%
metadata-eval62.6%
*-commutative62.6%
exp-to-pow64.9%
metadata-eval64.9%
pow-sqr65.0%
rem-sqrt-square99.4%
rem-square-sqrt99.0%
fabs-sqr99.0%
rem-square-sqrt99.4%
Simplified99.4%
Final simplification98.7%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (+ (* (pow x -0.5) 1.5) (* 2.0 (sqrt x)))))
double code(double x) {
return (1.0 / x) / ((pow(x, -0.5) * 1.5) + (2.0 * sqrt(x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (((x ** (-0.5d0)) * 1.5d0) + (2.0d0 * sqrt(x)))
end function
public static double code(double x) {
return (1.0 / x) / ((Math.pow(x, -0.5) * 1.5) + (2.0 * Math.sqrt(x)));
}
def code(x): return (1.0 / x) / ((math.pow(x, -0.5) * 1.5) + (2.0 * math.sqrt(x)))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64((x ^ -0.5) * 1.5) + Float64(2.0 * sqrt(x)))) end
function tmp = code(x) tmp = (1.0 / x) / (((x ^ -0.5) * 1.5) + (2.0 * sqrt(x))); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(N[Power[x, -0.5], $MachinePrecision] * 1.5), $MachinePrecision] + N[(2.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{{x}^{-0.5} \cdot 1.5 + 2 \cdot \sqrt{x}}
\end{array}
Initial program 36.7%
flip--36.7%
clear-num36.7%
pow1/236.7%
pow-flip36.7%
metadata-eval36.7%
inv-pow36.7%
sqrt-pow236.7%
+-commutative36.7%
metadata-eval36.7%
frac-times22.1%
metadata-eval22.1%
add-sqr-sqrt22.2%
frac-times27.9%
metadata-eval27.9%
add-sqr-sqrt37.0%
Applied egg-rr37.0%
frac-sub39.2%
*-un-lft-identity39.2%
Applied egg-rr39.2%
*-rgt-identity39.2%
associate--l+81.4%
+-inverses81.4%
metadata-eval81.4%
Simplified81.4%
expm1-log1p-u81.4%
expm1-udef35.6%
clear-num35.6%
associate-/r*35.6%
+-commutative35.6%
+-commutative35.6%
Applied egg-rr35.6%
expm1-def82.9%
expm1-log1p82.9%
associate-/l/99.5%
*-commutative99.5%
Simplified99.5%
Taylor expanded in x around inf 98.3%
+-commutative98.3%
associate-+r+98.3%
distribute-rgt-out98.3%
unpow1/298.3%
rem-exp-log98.3%
exp-neg98.3%
exp-prod98.3%
distribute-lft-neg-out98.3%
distribute-rgt-neg-in98.3%
metadata-eval98.3%
exp-to-pow98.3%
metadata-eval98.3%
Simplified98.3%
Final simplification98.3%
(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 36.7%
flip--36.7%
clear-num36.7%
pow1/236.7%
pow-flip36.7%
metadata-eval36.7%
inv-pow36.7%
sqrt-pow236.7%
+-commutative36.7%
metadata-eval36.7%
frac-times22.1%
metadata-eval22.1%
add-sqr-sqrt22.2%
frac-times27.9%
metadata-eval27.9%
add-sqr-sqrt37.0%
Applied egg-rr37.0%
frac-sub39.2%
*-un-lft-identity39.2%
Applied egg-rr39.2%
*-rgt-identity39.2%
associate--l+81.4%
+-inverses81.4%
metadata-eval81.4%
Simplified81.4%
Taylor expanded in x around inf 62.9%
unpow-162.9%
exp-to-pow60.7%
*-commutative60.7%
exp-prod61.6%
*-commutative61.6%
associate-*r*61.6%
metadata-eval61.6%
*-commutative61.6%
exp-to-pow63.9%
metadata-eval63.9%
pow-sqr64.0%
rem-sqrt-square97.1%
rem-square-sqrt96.7%
fabs-sqr96.7%
rem-square-sqrt97.1%
Simplified97.1%
Final simplification97.1%
(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 2024027
(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)))))