(FPCore (x) :precision binary64 (- (sqrt (+ x 9.0)) (sqrt x)))
double code(double x) { return sqrt((x + 9.0)) - sqrt(x); }
real(8) function code(x) real(8), intent (in) :: x code = sqrt((x + 9.0d0)) - sqrt(x) end function
public static double code(double x) { return Math.sqrt((x + 9.0)) - Math.sqrt(x); }
def code(x): return math.sqrt((x + 9.0)) - math.sqrt(x)
function code(x) return Float64(sqrt(Float64(x + 9.0)) - sqrt(x)) end
function tmp = code(x) tmp = sqrt((x + 9.0)) - sqrt(x); end
code[x_] := N[(N[Sqrt[N[(x + 9.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \sqrt{x + 9} - \sqrt{x} \end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (x) :precision binary64 (- (sqrt (+ x 9.0)) (sqrt x)))
double code(double x) { return sqrt((x + 9.0)) - sqrt(x); }
real(8) function code(x) real(8), intent (in) :: x code = sqrt((x + 9.0d0)) - sqrt(x) end function
public static double code(double x) { return Math.sqrt((x + 9.0)) - Math.sqrt(x); }
def code(x): return math.sqrt((x + 9.0)) - math.sqrt(x)
function code(x) return Float64(sqrt(Float64(x + 9.0)) - sqrt(x)) end
function tmp = code(x) tmp = sqrt((x + 9.0)) - sqrt(x); end
code[x_] := N[(N[Sqrt[N[(x + 9.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \sqrt{x + 9} - \sqrt{x} \end{array}
(FPCore (x) :precision binary64 (/ 9.0 (+ (sqrt x) (sqrt (+ 9.0 x)))))
double code(double x) { return 9.0 / (sqrt(x) + sqrt((9.0 + x))); }
real(8) function code(x) real(8), intent (in) :: x code = 9.0d0 / (sqrt(x) + sqrt((9.0d0 + x))) end function
public static double code(double x) { return 9.0 / (Math.sqrt(x) + Math.sqrt((9.0 + x))); }
def code(x): return 9.0 / (math.sqrt(x) + math.sqrt((9.0 + x)))
function code(x) return Float64(9.0 / Float64(sqrt(x) + sqrt(Float64(9.0 + x)))) end
function tmp = code(x) tmp = 9.0 / (sqrt(x) + sqrt((9.0 + x))); end
code[x_] := N[(9.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(9.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \frac{9}{\sqrt{x} + \sqrt{9 + x}} \end{array}
Initial program 52.9%
lift--.f64
N/A
flip--
N/A
lower-/.f64
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
lower--.f64
N/A
lift-+.f64
N/A
+-commutative
N/A
lower-+.f64
N/A
+-commutative
N/A
lower-+.f64
54.5
lift-+.f64
N/A
+-commutative
N/A
lower-+.f64
54.5
Applied rewrites54.5%
Taylor expanded in x around 0
Applied rewrites99.8%
(FPCore (x) :precision binary64 (if (<= (- (sqrt (+ x 9.0)) (sqrt x)) 0.001) (* (sqrt (pow x -1.0)) 4.5) (- (fma (fma -0.004629629629629629 x 0.16666666666666666) x 3.0) (sqrt x))))
double code(double x) { double tmp; if ((sqrt((x + 9.0)) - sqrt(x)) <= 0.001) { tmp = sqrt(pow(x, -1.0)) * 4.5; } else { tmp = fma(fma(-0.004629629629629629, x, 0.16666666666666666), x, 3.0) - sqrt(x); } return tmp; }
function code(x) tmp = 0.0 if (Float64(sqrt(Float64(x + 9.0)) - sqrt(x)) <= 0.001) tmp = Float64(sqrt((x ^ -1.0)) * 4.5); else tmp = Float64(fma(fma(-0.004629629629629629, x, 0.16666666666666666), x, 3.0) - sqrt(x)); end return tmp end
code[x_] := If[LessEqual[N[(N[Sqrt[N[(x + 9.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], 0.001], N[(N[Sqrt[N[Power[x, -1.0], $MachinePrecision]], $MachinePrecision] * 4.5), $MachinePrecision], N[(N[(N[(-0.004629629629629629 * x + 0.16666666666666666), $MachinePrecision] * x + 3.0), $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{x + 9} - \sqrt{x} \leq 0.001:\\ \;\;\;\;\sqrt{{x}^{-1}} \cdot 4.5\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.004629629629629629, x, 0.16666666666666666\right), x, 3\right) - \sqrt{x}\\ \end{array} \end{array}
if (-.f64 (sqrt.f64 (+.f64 x #s(literal 9 binary64))) (sqrt.f64 x)) < 1e-3
Initial program 5.8%
Taylor expanded in x around inf
*-commutative
N/A
lower-*.f64
N/A
lower-sqrt.f64
N/A
lower-/.f64
98.7
Applied rewrites98.7%
if 1e-3 < (-.f64 (sqrt.f64 (+.f64 x #s(literal 9 binary64))) (sqrt.f64 x))
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
+-commutative
N/A
lower-fma.f64
98.2
Applied rewrites98.2%
Final simplification98.4%
(FPCore (x) :precision binary64 (let* ((t_0 (- (sqrt (+ x 9.0)) (sqrt x)))) (if (<= t_0 1e-5) (/ (* 4.5 (sqrt x)) x) t_0)))
double code(double x) { double t_0 = sqrt((x + 9.0)) - sqrt(x); double tmp; if (t_0 <= 1e-5) { tmp = (4.5 * sqrt(x)) / x; } else { tmp = t_0; } return tmp; }
real(8) function code(x) real(8), intent (in) :: x real(8) :: t_0 real(8) :: tmp t_0 = sqrt((x + 9.0d0)) - sqrt(x) if (t_0 <= 1d-5) then tmp = (4.5d0 * sqrt(x)) / x else tmp = t_0 end if code = tmp end function
public static double code(double x) { double t_0 = Math.sqrt((x + 9.0)) - Math.sqrt(x); double tmp; if (t_0 <= 1e-5) { tmp = (4.5 * Math.sqrt(x)) / x; } else { tmp = t_0; } return tmp; }
def code(x): t_0 = math.sqrt((x + 9.0)) - math.sqrt(x) tmp = 0 if t_0 <= 1e-5: tmp = (4.5 * math.sqrt(x)) / x else: tmp = t_0 return tmp
function code(x) t_0 = Float64(sqrt(Float64(x + 9.0)) - sqrt(x)) tmp = 0.0 if (t_0 <= 1e-5) tmp = Float64(Float64(4.5 * sqrt(x)) / x); else tmp = t_0; end return tmp end
function tmp_2 = code(x) t_0 = sqrt((x + 9.0)) - sqrt(x); tmp = 0.0; if (t_0 <= 1e-5) tmp = (4.5 * sqrt(x)) / x; else tmp = t_0; end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[Sqrt[N[(x + 9.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 1e-5], N[(N[(4.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], t$95$0]]
\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{x + 9} - \sqrt{x}\\ \mathbf{if}\;t\_0 \leq 10^{-5}:\\ \;\;\;\;\frac{4.5 \cdot \sqrt{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array}
if (-.f64 (sqrt.f64 (+.f64 x #s(literal 9 binary64))) (sqrt.f64 x)) < 1.00000000000000008e-5
Initial program 5.3%
Taylor expanded in x around inf
lower-/.f64
N/A
lower-fma.f64
N/A
lower-sqrt.f64
N/A
lower-/.f64
N/A
lower-*.f64
N/A
lower-sqrt.f64
99.5
Applied rewrites99.5%
Taylor expanded in x around -inf
Applied rewrites99.0%
if 1.00000000000000008e-5 < (-.f64 (sqrt.f64 (+.f64 x #s(literal 9 binary64))) (sqrt.f64 x))
Initial program 99.7%
(FPCore (x) :precision binary64 (if (<= (- (sqrt (+ x 9.0)) (sqrt x)) 0.001) (/ (* 4.5 (sqrt x)) x) (- (fma (fma -0.004629629629629629 x 0.16666666666666666) x 3.0) (sqrt x))))
double code(double x) { double tmp; if ((sqrt((x + 9.0)) - sqrt(x)) <= 0.001) { tmp = (4.5 * sqrt(x)) / x; } else { tmp = fma(fma(-0.004629629629629629, x, 0.16666666666666666), x, 3.0) - sqrt(x); } return tmp; }
function code(x) tmp = 0.0 if (Float64(sqrt(Float64(x + 9.0)) - sqrt(x)) <= 0.001) tmp = Float64(Float64(4.5 * sqrt(x)) / x); else tmp = Float64(fma(fma(-0.004629629629629629, x, 0.16666666666666666), x, 3.0) - sqrt(x)); end return tmp end
code[x_] := If[LessEqual[N[(N[Sqrt[N[(x + 9.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], 0.001], N[(N[(4.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(N[(-0.004629629629629629 * x + 0.16666666666666666), $MachinePrecision] * x + 3.0), $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{x + 9} - \sqrt{x} \leq 0.001:\\ \;\;\;\;\frac{4.5 \cdot \sqrt{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(-0.004629629629629629, x, 0.16666666666666666\right), x, 3\right) - \sqrt{x}\\ \end{array} \end{array}
if (-.f64 (sqrt.f64 (+.f64 x #s(literal 9 binary64))) (sqrt.f64 x)) < 1e-3
Initial program 5.8%
Taylor expanded in x around inf
lower-/.f64
N/A
lower-fma.f64
N/A
lower-sqrt.f64
N/A
lower-/.f64
N/A
lower-*.f64
N/A
lower-sqrt.f64
99.5
Applied rewrites99.5%
Taylor expanded in x around -inf
Applied rewrites98.7%
if 1e-3 < (-.f64 (sqrt.f64 (+.f64 x #s(literal 9 binary64))) (sqrt.f64 x))
Initial program 100.0%
Taylor expanded in x around 0
+-commutative
N/A
*-commutative
N/A
lower-fma.f64
N/A
+-commutative
N/A
lower-fma.f64
98.2
Applied rewrites98.2%
(FPCore (x) :precision binary64 (/ 9.0 (+ 3.0 (sqrt x))))
double code(double x) { return 9.0 / (3.0 + sqrt(x)); }
real(8) function code(x) real(8), intent (in) :: x code = 9.0d0 / (3.0d0 + sqrt(x)) end function
public static double code(double x) { return 9.0 / (3.0 + Math.sqrt(x)); }
def code(x): return 9.0 / (3.0 + math.sqrt(x))
function code(x) return Float64(9.0 / Float64(3.0 + sqrt(x))) end
function tmp = code(x) tmp = 9.0 / (3.0 + sqrt(x)); end
code[x_] := N[(9.0 / N[(3.0 + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \frac{9}{3 + \sqrt{x}} \end{array}
Initial program 52.9%
lift--.f64
N/A
flip--
N/A
lower-/.f64
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
lift-sqrt.f64
N/A
lift-sqrt.f64
N/A
rem-square-sqrt
N/A
lower--.f64
N/A
lift-+.f64
N/A
+-commutative
N/A
lower-+.f64
N/A
+-commutative
N/A
lower-+.f64
54.5
lift-+.f64
N/A
+-commutative
N/A
lower-+.f64
54.5
Applied rewrites54.5%
Taylor expanded in x around 0
lower-/.f64
N/A
lower-+.f64
N/A
lower-sqrt.f64
58.0
Applied rewrites58.0%
(FPCore (x) :precision binary64 (fma 0.16666666666666666 x (- 3.0 (sqrt x))))
double code(double x) { return fma(0.16666666666666666, x, (3.0 - sqrt(x))); }
function code(x) return fma(0.16666666666666666, x, Float64(3.0 - sqrt(x))) end
code[x_] := N[(0.16666666666666666 * x + N[(3.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(0.16666666666666666, x, 3 - \sqrt{x}\right) \end{array}
Initial program 52.9%
Taylor expanded in x around 0
+-commutative
N/A
associate--l+
N/A
lower-fma.f64
N/A
lower--.f64
N/A
lower-sqrt.f64
51.1
Applied rewrites51.1%
(FPCore (x) :precision binary64 (- 3.0 (sqrt x)))
double code(double x) { return 3.0 - sqrt(x); }
real(8) function code(x) real(8), intent (in) :: x code = 3.0d0 - sqrt(x) end function
public static double code(double x) { return 3.0 - Math.sqrt(x); }
def code(x): return 3.0 - math.sqrt(x)
function code(x) return Float64(3.0 - sqrt(x)) end
function tmp = code(x) tmp = 3.0 - sqrt(x); end
code[x_] := N[(3.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ 3 - \sqrt{x} \end{array}
Initial program 52.9%
Taylor expanded in x around 0
Applied rewrites49.4%
herbie shell --seed 1
(FPCore (x)
:name "sqrt(x + 9) - sqrt(x)"
:precision binary64
:pre (and (<= 0.0 x) (<= x 1.79e+308))
(- (sqrt (+ x 9.0)) (sqrt x)))