
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
double code(double x) {
return sqrt((x + 1.0)) - sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((x + 1.0d0)) - sqrt(x)
end function
public static double code(double x) {
return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
def code(x): return math.sqrt((x + 1.0)) - math.sqrt(x)
function code(x) return Float64(sqrt(Float64(x + 1.0)) - sqrt(x)) end
function tmp = code(x) tmp = sqrt((x + 1.0)) - sqrt(x); end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x + 1} - \sqrt{x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
double code(double x) {
return sqrt((x + 1.0)) - sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((x + 1.0d0)) - sqrt(x)
end function
public static double code(double x) {
return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
def code(x): return math.sqrt((x + 1.0)) - math.sqrt(x)
function code(x) return Float64(sqrt(Float64(x + 1.0)) - sqrt(x)) end
function tmp = code(x) tmp = sqrt((x + 1.0)) - sqrt(x); end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x + 1} - \sqrt{x}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (sqrt (+ 1.0 x))))
(if (<= (- t_0 (sqrt x)) 0.0)
(* (sqrt (/ 1.0 x)) 0.5)
(/ (+ x (- 1.0 x)) (+ (sqrt x) t_0)))))
double code(double x) {
double t_0 = sqrt((1.0 + x));
double tmp;
if ((t_0 - sqrt(x)) <= 0.0) {
tmp = sqrt((1.0 / x)) * 0.5;
} else {
tmp = (x + (1.0 - x)) / (sqrt(x) + t_0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((1.0d0 + x))
if ((t_0 - sqrt(x)) <= 0.0d0) then
tmp = sqrt((1.0d0 / x)) * 0.5d0
else
tmp = (x + (1.0d0 - x)) / (sqrt(x) + t_0)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 + x));
double tmp;
if ((t_0 - Math.sqrt(x)) <= 0.0) {
tmp = Math.sqrt((1.0 / x)) * 0.5;
} else {
tmp = (x + (1.0 - x)) / (Math.sqrt(x) + t_0);
}
return tmp;
}
def code(x): t_0 = math.sqrt((1.0 + x)) tmp = 0 if (t_0 - math.sqrt(x)) <= 0.0: tmp = math.sqrt((1.0 / x)) * 0.5 else: tmp = (x + (1.0 - x)) / (math.sqrt(x) + t_0) return tmp
function code(x) t_0 = sqrt(Float64(1.0 + x)) tmp = 0.0 if (Float64(t_0 - sqrt(x)) <= 0.0) tmp = Float64(sqrt(Float64(1.0 / x)) * 0.5); else tmp = Float64(Float64(x + Float64(1.0 - x)) / Float64(sqrt(x) + t_0)); end return tmp end
function tmp_2 = code(x) t_0 = sqrt((1.0 + x)); tmp = 0.0; if ((t_0 - sqrt(x)) <= 0.0) tmp = sqrt((1.0 / x)) * 0.5; else tmp = (x + (1.0 - x)) / (sqrt(x) + t_0); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(t$95$0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(x + N[(1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{1 + x}\\
\mathbf{if}\;t\_0 - \sqrt{x} \leq 0:\\
\;\;\;\;\sqrt{\frac{1}{x}} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{x + \left(1 - x\right)}{\sqrt{x} + t\_0}\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 x #s(literal 1 binary64))) (sqrt.f64 x)) < 0.0Initial program 3.9%
Taylor expanded in x around inf
*-lowering-*.f64N/A
sqrt-lowering-sqrt.f64N/A
/-lowering-/.f6499.8%
Simplified99.8%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 x #s(literal 1 binary64))) (sqrt.f64 x)) Initial program 59.0%
flip--N/A
/-lowering-/.f64N/A
rem-square-sqrtN/A
rem-square-sqrtN/A
associate--l+N/A
metadata-evalN/A
*-rgt-identityN/A
+-lowering-+.f64N/A
metadata-evalN/A
*-rgt-identityN/A
--lowering--.f64N/A
+-lowering-+.f64N/A
sqrt-lowering-sqrt.f64N/A
+-lowering-+.f64N/A
sqrt-lowering-sqrt.f6499.2%
Applied egg-rr99.2%
Final simplification99.8%
(FPCore (x) :precision binary64 (/ (fma -0.125 (sqrt (/ 1.0 x)) (fma 0.0625 (sqrt (/ 1.0 (* x (* x x)))) (* (sqrt x) 0.5))) x))
double code(double x) {
return fma(-0.125, sqrt((1.0 / x)), fma(0.0625, sqrt((1.0 / (x * (x * x)))), (sqrt(x) * 0.5))) / x;
}
function code(x) return Float64(fma(-0.125, sqrt(Float64(1.0 / x)), fma(0.0625, sqrt(Float64(1.0 / Float64(x * Float64(x * x)))), Float64(sqrt(x) * 0.5))) / x) end
code[x_] := N[(N[(-0.125 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] + N[(0.0625 * N[Sqrt[N[(1.0 / N[(x * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + N[(N[Sqrt[x], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-0.125, \sqrt{\frac{1}{x}}, \mathsf{fma}\left(0.0625, \sqrt{\frac{1}{x \cdot \left(x \cdot x\right)}}, \sqrt{x} \cdot 0.5\right)\right)}{x}
\end{array}
Initial program 6.9%
Taylor expanded in x around inf
/-lowering-/.f64N/A
accelerator-lowering-fma.f64N/A
sqrt-lowering-sqrt.f64N/A
/-lowering-/.f64N/A
accelerator-lowering-fma.f64N/A
sqrt-lowering-sqrt.f64N/A
/-lowering-/.f64N/A
cube-multN/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
sqrt-lowering-sqrt.f6499.3%
Simplified99.3%
(FPCore (x) :precision binary64 (/ (fma (sqrt x) 0.5 (/ -0.125 (sqrt x))) x))
double code(double x) {
return fma(sqrt(x), 0.5, (-0.125 / sqrt(x))) / x;
}
function code(x) return Float64(fma(sqrt(x), 0.5, Float64(-0.125 / sqrt(x))) / x) end
code[x_] := N[(N[(N[Sqrt[x], $MachinePrecision] * 0.5 + N[(-0.125 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(\sqrt{x}, 0.5, \frac{-0.125}{\sqrt{x}}\right)}{x}
\end{array}
Initial program 6.9%
Taylor expanded in x around inf
/-lowering-/.f64N/A
accelerator-lowering-fma.f64N/A
sqrt-lowering-sqrt.f64N/A
/-lowering-/.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
sqrt-lowering-sqrt.f6498.7%
Simplified98.7%
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
sqrt-lowering-sqrt.f64N/A
sqrt-divN/A
metadata-evalN/A
un-div-invN/A
/-lowering-/.f64N/A
sqrt-lowering-sqrt.f6498.7%
Applied egg-rr98.7%
(FPCore (x) :precision binary64 (* (sqrt (/ 1.0 x)) 0.5))
double code(double x) {
return sqrt((1.0 / x)) * 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((1.0d0 / x)) * 0.5d0
end function
public static double code(double x) {
return Math.sqrt((1.0 / x)) * 0.5;
}
def code(x): return math.sqrt((1.0 / x)) * 0.5
function code(x) return Float64(sqrt(Float64(1.0 / x)) * 0.5) end
function tmp = code(x) tmp = sqrt((1.0 / x)) * 0.5; end
code[x_] := N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{1}{x}} \cdot 0.5
\end{array}
Initial program 6.9%
Taylor expanded in x around inf
*-lowering-*.f64N/A
sqrt-lowering-sqrt.f64N/A
/-lowering-/.f6497.8%
Simplified97.8%
Final simplification97.8%
(FPCore (x) :precision binary64 (/ 0.5 (sqrt x)))
double code(double x) {
return 0.5 / sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 / sqrt(x)
end function
public static double code(double x) {
return 0.5 / Math.sqrt(x);
}
def code(x): return 0.5 / math.sqrt(x)
function code(x) return Float64(0.5 / sqrt(x)) end
function tmp = code(x) tmp = 0.5 / sqrt(x); end
code[x_] := N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{\sqrt{x}}
\end{array}
Initial program 6.9%
Taylor expanded in x around inf
*-lowering-*.f64N/A
sqrt-lowering-sqrt.f64N/A
/-lowering-/.f6497.8%
Simplified97.8%
sqrt-divN/A
metadata-evalN/A
un-div-invN/A
/-lowering-/.f64N/A
sqrt-lowering-sqrt.f6497.6%
Applied egg-rr97.6%
(FPCore (x) :precision binary64 (fma x 0.5 (- 0.0 (sqrt x))))
double code(double x) {
return fma(x, 0.5, (0.0 - sqrt(x)));
}
function code(x) return fma(x, 0.5, Float64(0.0 - sqrt(x))) end
code[x_] := N[(x * 0.5 + N[(0.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 0.5, 0 - \sqrt{x}\right)
\end{array}
Initial program 6.9%
Taylor expanded in x around 0
+-commutativeN/A
associate--l+N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
--lowering--.f64N/A
sqrt-lowering-sqrt.f644.5%
Simplified4.5%
Taylor expanded in x around inf
mul-1-negN/A
neg-sub0N/A
--lowering--.f64N/A
sqrt-lowering-sqrt.f644.5%
Simplified4.5%
(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 6.9%
Taylor expanded in x around inf
sqrt-lowering-sqrt.f643.8%
Simplified3.8%
+-inverses3.8%
Applied egg-rr3.8%
(FPCore (x) :precision binary64 (/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x))))
double code(double x) {
return 1.0 / (sqrt((x + 1.0)) + sqrt(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (sqrt((x + 1.0d0)) + sqrt(x))
end function
public static double code(double x) {
return 1.0 / (Math.sqrt((x + 1.0)) + Math.sqrt(x));
}
def code(x): return 1.0 / (math.sqrt((x + 1.0)) + math.sqrt(x))
function code(x) return Float64(1.0 / Float64(sqrt(Float64(x + 1.0)) + sqrt(x))) end
function tmp = code(x) tmp = 1.0 / (sqrt((x + 1.0)) + sqrt(x)); end
code[x_] := N[(1.0 / N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x + 1} + \sqrt{x}}
\end{array}
(FPCore (x) :precision binary64 (* 0.5 (pow x -0.5)))
double code(double x) {
return 0.5 * pow(x, -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * (x ** (-0.5d0))
end function
public static double code(double x) {
return 0.5 * Math.pow(x, -0.5);
}
def code(x): return 0.5 * math.pow(x, -0.5)
function code(x) return Float64(0.5 * (x ^ -0.5)) end
function tmp = code(x) tmp = 0.5 * (x ^ -0.5); end
code[x_] := N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-0.5}
\end{array}
herbie shell --seed 2024193
(FPCore (x)
:name "2sqrt (example 3.1)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(! :herbie-platform default (/ 1 (+ (sqrt (+ x 1)) (sqrt x))))
:alt
(! :herbie-platform default (* 1/2 (pow x -1/2)))
(- (sqrt (+ x 1.0)) (sqrt x)))