
(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 (* (exp (+ (* 0.34375 (pow x -2.0)) (/ -0.75 x))) (* 0.5 (pow x -1.5))))
double code(double x) {
return exp(((0.34375 * pow(x, -2.0)) + (-0.75 / x))) * (0.5 * pow(x, -1.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = exp(((0.34375d0 * (x ** (-2.0d0))) + ((-0.75d0) / x))) * (0.5d0 * (x ** (-1.5d0)))
end function
public static double code(double x) {
return Math.exp(((0.34375 * Math.pow(x, -2.0)) + (-0.75 / x))) * (0.5 * Math.pow(x, -1.5));
}
def code(x): return math.exp(((0.34375 * math.pow(x, -2.0)) + (-0.75 / x))) * (0.5 * math.pow(x, -1.5))
function code(x) return Float64(exp(Float64(Float64(0.34375 * (x ^ -2.0)) + Float64(-0.75 / x))) * Float64(0.5 * (x ^ -1.5))) end
function tmp = code(x) tmp = exp(((0.34375 * (x ^ -2.0)) + (-0.75 / x))) * (0.5 * (x ^ -1.5)); end
code[x_] := N[(N[Exp[N[(N[(0.34375 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision] + N[(-0.75 / x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
e^{0.34375 \cdot {x}^{-2} + \frac{-0.75}{x}} \cdot \left(0.5 \cdot {x}^{-1.5}\right)
\end{array}
Initial program 38.6%
add-exp-log38.6%
inv-pow38.6%
sqrt-pow232.8%
metadata-eval32.8%
inv-pow32.8%
sqrt-pow238.6%
+-commutative38.6%
metadata-eval38.6%
Applied egg-rr38.6%
Taylor expanded in x around inf 93.1%
associate--l+93.1%
distribute-lft-neg-in93.1%
metadata-eval93.1%
sub-neg93.1%
+-commutative93.1%
log-rec93.1%
unsub-neg93.1%
associate-*r/93.1%
metadata-eval93.1%
associate-*r/93.1%
metadata-eval93.1%
distribute-neg-frac93.1%
metadata-eval93.1%
Simplified93.1%
Taylor expanded in x around 0 93.1%
Simplified99.1%
pow199.1%
Applied egg-rr99.5%
unpow199.5%
associate-*l*99.5%
*-commutative99.5%
associate-*r*99.5%
exp-prod99.5%
prod-exp99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (* (* 0.5 (pow x -1.5)) (exp (/ -0.75 x))))
double code(double x) {
return (0.5 * pow(x, -1.5)) * exp((-0.75 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-1.5d0))) * exp(((-0.75d0) / x))
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -1.5)) * Math.exp((-0.75 / x));
}
def code(x): return (0.5 * math.pow(x, -1.5)) * math.exp((-0.75 / x))
function code(x) return Float64(Float64(0.5 * (x ^ -1.5)) * exp(Float64(-0.75 / x))) end
function tmp = code(x) tmp = (0.5 * (x ^ -1.5)) * exp((-0.75 / x)); end
code[x_] := N[(N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision] * N[Exp[N[(-0.75 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot {x}^{-1.5}\right) \cdot e^{\frac{-0.75}{x}}
\end{array}
Initial program 38.6%
add-exp-log38.6%
inv-pow38.6%
sqrt-pow232.8%
metadata-eval32.8%
inv-pow32.8%
sqrt-pow238.6%
+-commutative38.6%
metadata-eval38.6%
Applied egg-rr38.6%
Taylor expanded in x around inf 92.8%
sub-neg92.8%
log-rec92.8%
unsub-neg92.8%
distribute-lft-neg-in92.8%
metadata-eval92.8%
associate-*r/92.8%
metadata-eval92.8%
distribute-neg-frac92.8%
metadata-eval92.8%
Simplified92.8%
exp-sum92.7%
diff-log93.0%
add-exp-log98.8%
inv-pow98.8%
sqrt-pow198.8%
metadata-eval98.8%
Applied egg-rr98.8%
div-inv98.7%
associate-*l*98.7%
inv-pow98.7%
metadata-eval98.7%
pow-prod-up98.3%
associate-*l*98.3%
*-commutative98.3%
associate-*l*98.3%
pow-prod-up98.7%
metadata-eval98.7%
inv-pow98.7%
div-inv98.8%
pow198.8%
pow-div99.0%
metadata-eval99.0%
Applied egg-rr99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (* (/ 0.5 x) (pow (+ x 1.0) -0.5)))
double code(double x) {
return (0.5 / x) * pow((x + 1.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / x) * ((x + 1.0d0) ** (-0.5d0))
end function
public static double code(double x) {
return (0.5 / x) * Math.pow((x + 1.0), -0.5);
}
def code(x): return (0.5 / x) * math.pow((x + 1.0), -0.5)
function code(x) return Float64(Float64(0.5 / x) * (Float64(x + 1.0) ^ -0.5)) end
function tmp = code(x) tmp = (0.5 / x) * ((x + 1.0) ^ -0.5); end
code[x_] := N[(N[(0.5 / x), $MachinePrecision] * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{x} \cdot {\left(x + 1\right)}^{-0.5}
\end{array}
Initial program 38.6%
frac-sub38.7%
div-inv38.7%
*-un-lft-identity38.7%
+-commutative38.7%
*-rgt-identity38.7%
metadata-eval38.7%
frac-times38.7%
associate-*l/38.7%
*-un-lft-identity38.7%
pow1/238.7%
pow-flip38.7%
+-commutative38.7%
metadata-eval38.7%
Applied egg-rr38.7%
*-commutative38.7%
/-rgt-identity38.7%
times-frac38.7%
*-commutative38.7%
times-frac38.7%
div-sub38.7%
sub-neg38.7%
*-inverses38.7%
metadata-eval38.7%
Simplified38.7%
Taylor expanded in x around inf 97.8%
Final simplification97.8%
(FPCore (x) :precision binary64 (if (<= x 4.6e+153) (/ 1.0 x) 0.0))
double code(double x) {
double tmp;
if (x <= 4.6e+153) {
tmp = 1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.6d+153) then
tmp = 1.0d0 / x
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.6e+153) {
tmp = 1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.6e+153: tmp = 1.0 / x else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 4.6e+153) tmp = Float64(1.0 / x); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.6e+153) tmp = 1.0 / x; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.6e+153], N[(1.0 / x), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.6 \cdot 10^{+153}:\\
\;\;\;\;\frac{1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 4.6000000000000003e153Initial program 9.8%
frac-2neg9.8%
metadata-eval9.8%
frac-sub10.0%
*-un-lft-identity10.0%
+-commutative10.0%
+-commutative10.0%
Applied egg-rr10.0%
Taylor expanded in x around inf 7.9%
mul-1-neg7.9%
Simplified7.9%
Taylor expanded in x around 0 8.1%
if 4.6000000000000003e153 < x Initial program 66.9%
Taylor expanded in x around inf 66.9%
Final simplification37.8%
(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 38.6%
Taylor expanded in x around inf 36.0%
Final simplification36.0%
(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 2024059
(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)))))