
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}
Initial program 79.0%
(FPCore (p x) :precision binary64 (let* ((t_0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x)))))) (if (<= t_0 0.004) (+ 1.0 t_0) t_0)))
double code(double p, double x) {
double t_0 = x / sqrt((((4.0 * p) * p) + (x * x)));
double tmp;
if (t_0 <= 0.004) {
tmp = 1.0 + t_0;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = x / sqrt((((4.0d0 * p) * p) + (x * x)))
if (t_0 <= 0.004d0) then
tmp = 1.0d0 + t_0
else
tmp = t_0
end if
code = tmp
end function
public static double code(double p, double x) {
double t_0 = x / Math.sqrt((((4.0 * p) * p) + (x * x)));
double tmp;
if (t_0 <= 0.004) {
tmp = 1.0 + t_0;
} else {
tmp = t_0;
}
return tmp;
}
def code(p, x): t_0 = x / math.sqrt((((4.0 * p) * p) + (x * x))) tmp = 0 if t_0 <= 0.004: tmp = 1.0 + t_0 else: tmp = t_0 return tmp
function code(p, x) t_0 = Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x)))) tmp = 0.0 if (t_0 <= 0.004) tmp = Float64(1.0 + t_0); else tmp = t_0; end return tmp end
function tmp_2 = code(p, x) t_0 = x / sqrt((((4.0 * p) * p) + (x * x))); tmp = 0.0; if (t_0 <= 0.004) tmp = 1.0 + t_0; else tmp = t_0; end tmp_2 = tmp; end
code[p_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 0.004], N[(1.0 + t$95$0), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\\
\mathbf{if}\;t\_0 \leq 0.004:\\
\;\;\;\;1 + t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < 0.0040000000000000001Initial program 72.5%
Taylor expanded in p around 0
Applied rewrites19.4%
if 0.0040000000000000001 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
Taylor expanded in p around 0
Applied rewrites99.2%
(FPCore (p x) :precision binary64 (let* ((t_0 (* (* 4.0 p) p)) (t_1 (/ x (sqrt (+ t_0 (* x x)))))) (if (<= t_1 5e-143) (* 0.5 t_0) t_1)))
double code(double p, double x) {
double t_0 = (4.0 * p) * p;
double t_1 = x / sqrt((t_0 + (x * x)));
double tmp;
if (t_1 <= 5e-143) {
tmp = 0.5 * t_0;
} else {
tmp = t_1;
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = (4.0d0 * p) * p
t_1 = x / sqrt((t_0 + (x * x)))
if (t_1 <= 5d-143) then
tmp = 0.5d0 * t_0
else
tmp = t_1
end if
code = tmp
end function
public static double code(double p, double x) {
double t_0 = (4.0 * p) * p;
double t_1 = x / Math.sqrt((t_0 + (x * x)));
double tmp;
if (t_1 <= 5e-143) {
tmp = 0.5 * t_0;
} else {
tmp = t_1;
}
return tmp;
}
def code(p, x): t_0 = (4.0 * p) * p t_1 = x / math.sqrt((t_0 + (x * x))) tmp = 0 if t_1 <= 5e-143: tmp = 0.5 * t_0 else: tmp = t_1 return tmp
function code(p, x) t_0 = Float64(Float64(4.0 * p) * p) t_1 = Float64(x / sqrt(Float64(t_0 + Float64(x * x)))) tmp = 0.0 if (t_1 <= 5e-143) tmp = Float64(0.5 * t_0); else tmp = t_1; end return tmp end
function tmp_2 = code(p, x) t_0 = (4.0 * p) * p; t_1 = x / sqrt((t_0 + (x * x))); tmp = 0.0; if (t_1 <= 5e-143) tmp = 0.5 * t_0; else tmp = t_1; end tmp_2 = tmp; end
code[p_, x_] := Block[{t$95$0 = N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision]}, Block[{t$95$1 = N[(x / N[Sqrt[N[(t$95$0 + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 5e-143], N[(0.5 * t$95$0), $MachinePrecision], t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(4 \cdot p\right) \cdot p\\
t_1 := \frac{x}{\sqrt{t\_0 + x \cdot x}}\\
\mathbf{if}\;t\_1 \leq 5 \cdot 10^{-143}:\\
\;\;\;\;0.5 \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < 5.0000000000000002e-143Initial program 69.4%
Taylor expanded in p around 0
Applied rewrites19.9%
Taylor expanded in p around 0
Applied rewrites4.0%
Taylor expanded in p around 0
Applied rewrites9.9%
if 5.0000000000000002e-143 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
Taylor expanded in p around 0
Applied rewrites76.2%
(FPCore (p x) :precision binary64 (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x)))))))
double code(double p, double x) {
return 0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))));
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = 0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))
end function
public static double code(double p, double x) {
return 0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))));
}
def code(p, x): return 0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))
function code(p, x) return Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x)))))) end
function tmp = code(p, x) tmp = 0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))); end
code[p_, x_] := N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)
\end{array}
Initial program 79.0%
Taylor expanded in p around 0
Applied rewrites38.5%
(FPCore (p x) :precision binary64 (let* ((t_0 (* (* 4.0 p) p))) (if (<= x -2.6e+25) t_0 (sqrt (sqrt (+ t_0 (* x x)))))))
double code(double p, double x) {
double t_0 = (4.0 * p) * p;
double tmp;
if (x <= -2.6e+25) {
tmp = t_0;
} else {
tmp = sqrt(sqrt((t_0 + (x * x))));
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = (4.0d0 * p) * p
if (x <= (-2.6d+25)) then
tmp = t_0
else
tmp = sqrt(sqrt((t_0 + (x * x))))
end if
code = tmp
end function
public static double code(double p, double x) {
double t_0 = (4.0 * p) * p;
double tmp;
if (x <= -2.6e+25) {
tmp = t_0;
} else {
tmp = Math.sqrt(Math.sqrt((t_0 + (x * x))));
}
return tmp;
}
def code(p, x): t_0 = (4.0 * p) * p tmp = 0 if x <= -2.6e+25: tmp = t_0 else: tmp = math.sqrt(math.sqrt((t_0 + (x * x)))) return tmp
function code(p, x) t_0 = Float64(Float64(4.0 * p) * p) tmp = 0.0 if (x <= -2.6e+25) tmp = t_0; else tmp = sqrt(sqrt(Float64(t_0 + Float64(x * x)))); end return tmp end
function tmp_2 = code(p, x) t_0 = (4.0 * p) * p; tmp = 0.0; if (x <= -2.6e+25) tmp = t_0; else tmp = sqrt(sqrt((t_0 + (x * x)))); end tmp_2 = tmp; end
code[p_, x_] := Block[{t$95$0 = N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision]}, If[LessEqual[x, -2.6e+25], t$95$0, N[Sqrt[N[Sqrt[N[(t$95$0 + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(4 \cdot p\right) \cdot p\\
\mathbf{if}\;x \leq -2.6 \cdot 10^{+25}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\sqrt{t\_0 + x \cdot x}}\\
\end{array}
\end{array}
if x < -2.5999999999999998e25Initial program 47.6%
Taylor expanded in p around 0
Applied rewrites26.8%
Taylor expanded in p around 0
Applied rewrites4.7%
Taylor expanded in p around 0
Applied rewrites23.5%
if -2.5999999999999998e25 < x Initial program 86.6%
Taylor expanded in p around 0
Applied rewrites6.9%
(FPCore (p x) :precision binary64 (* 0.5 (* (* 4.0 p) p)))
double code(double p, double x) {
return 0.5 * ((4.0 * p) * p);
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = 0.5d0 * ((4.0d0 * p) * p)
end function
public static double code(double p, double x) {
return 0.5 * ((4.0 * p) * p);
}
def code(p, x): return 0.5 * ((4.0 * p) * p)
function code(p, x) return Float64(0.5 * Float64(Float64(4.0 * p) * p)) end
function tmp = code(p, x) tmp = 0.5 * ((4.0 * p) * p); end
code[p_, x_] := N[(0.5 * N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(\left(4 \cdot p\right) \cdot p\right)
\end{array}
Initial program 79.0%
Taylor expanded in p around 0
Applied rewrites38.5%
Taylor expanded in p around 0
Applied rewrites3.9%
Taylor expanded in p around 0
Applied rewrites8.2%
(FPCore (p x) :precision binary64 (* (* 4.0 p) p))
double code(double p, double x) {
return (4.0 * p) * p;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = (4.0d0 * p) * p
end function
public static double code(double p, double x) {
return (4.0 * p) * p;
}
def code(p, x): return (4.0 * p) * p
function code(p, x) return Float64(Float64(4.0 * p) * p) end
function tmp = code(p, x) tmp = (4.0 * p) * p; end
code[p_, x_] := N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision]
\begin{array}{l}
\\
\left(4 \cdot p\right) \cdot p
\end{array}
Initial program 79.0%
Taylor expanded in p around 0
Applied rewrites19.3%
Taylor expanded in p around 0
Applied rewrites3.9%
Taylor expanded in p around 0
Applied rewrites8.2%
(FPCore (p x) :precision binary64 (* 4.0 p))
double code(double p, double x) {
return 4.0 * p;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = 4.0d0 * p
end function
public static double code(double p, double x) {
return 4.0 * p;
}
def code(p, x): return 4.0 * p
function code(p, x) return Float64(4.0 * p) end
function tmp = code(p, x) tmp = 4.0 * p; end
code[p_, x_] := N[(4.0 * p), $MachinePrecision]
\begin{array}{l}
\\
4 \cdot p
\end{array}
Initial program 79.0%
Taylor expanded in p around 0
Applied rewrites19.3%
Taylor expanded in p around 0
Applied rewrites3.9%
(FPCore (p x) :precision binary64 (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x))))))
double code(double p, double x) {
return sqrt((0.5 + (copysign(0.5, x) / hypot(1.0, ((2.0 * p) / x)))));
}
public static double code(double p, double x) {
return Math.sqrt((0.5 + (Math.copySign(0.5, x) / Math.hypot(1.0, ((2.0 * p) / x)))));
}
def code(p, x): return math.sqrt((0.5 + (math.copysign(0.5, x) / math.hypot(1.0, ((2.0 * p) / x)))))
function code(p, x) return sqrt(Float64(0.5 + Float64(copysign(0.5, x) / hypot(1.0, Float64(Float64(2.0 * p) / x))))) end
function tmp = code(p, x) tmp = sqrt((0.5 + ((sign(x) * abs(0.5)) / hypot(1.0, ((2.0 * p) / x))))); end
code[p_, x_] := N[Sqrt[N[(0.5 + N[(N[With[{TMP1 = Abs[0.5], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] / N[Sqrt[1.0 ^ 2 + N[(N[(2.0 * p), $MachinePrecision] / x), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}
\end{array}
herbie shell --seed 2024321
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:alt
(! :herbie-platform default (sqrt (+ 1/2 (/ (copysign 1/2 x) (hypot 1 (/ (* 2 p) x))))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))