
(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 7 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}
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -0.5) (- (/ p_m (- x)) (* -1.5 (pow (/ p_m x) 3.0))) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p_m 2.0) x)))))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.5) {
tmp = (p_m / -x) - (-1.5 * pow((p_m / x), 3.0));
} else {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p_m * 2.0), x)))));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if ((x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.5) {
tmp = (p_m / -x) - (-1.5 * Math.pow((p_m / x), 3.0));
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p_m * 2.0), x)))));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if (x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.5: tmp = (p_m / -x) - (-1.5 * math.pow((p_m / x), 3.0)) else: tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p_m * 2.0), x))))) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -0.5) tmp = Float64(Float64(p_m / Float64(-x)) - Float64(-1.5 * (Float64(p_m / x) ^ 3.0))); else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p_m * 2.0), x))))); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.5) tmp = (p_m / -x) - (-1.5 * ((p_m / x) ^ 3.0)); else tmp = sqrt((0.5 * (1.0 + (x / hypot((p_m * 2.0), x))))); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.5], N[(N[(p$95$m / (-x)), $MachinePrecision] - N[(-1.5 * N[Power[N[(p$95$m / x), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p$95$m * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p\_m \cdot \left(4 \cdot p\_m\right) + x \cdot x}} \leq -0.5:\\
\;\;\;\;\frac{p\_m}{-x} - -1.5 \cdot {\left(\frac{p\_m}{x}\right)}^{3}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p\_m \cdot 2, x\right)}\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < -0.5Initial program 20.1%
add-log-exp20.1%
+-commutative20.1%
distribute-rgt-in20.1%
metadata-eval20.1%
fma-define20.1%
Applied egg-rr20.1%
Taylor expanded in x around -inf 45.0%
mul-1-neg45.0%
distribute-rgt-out45.0%
metadata-eval45.0%
Simplified45.0%
Taylor expanded in p around 0 53.9%
+-commutative53.9%
distribute-rgt-in53.8%
associate-*l/54.0%
*-lft-identity54.0%
associate-*r/54.0%
associate-*l/54.0%
associate-*r*54.0%
unpow254.0%
unpow354.0%
associate-*r/54.0%
cube-div55.5%
Simplified55.5%
if -0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
add-sqr-sqrt100.0%
hypot-define100.0%
associate-*l*100.0%
sqrt-prod100.0%
metadata-eval100.0%
sqrt-unprod50.8%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification88.0%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= p_m 6.9e-169) (/ p_m (- x)) (if (<= p_m 1.3e-36) (log E) (sqrt 0.5))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (p_m <= 6.9e-169) {
tmp = p_m / -x;
} else if (p_m <= 1.3e-36) {
tmp = log(((double) M_E));
} else {
tmp = sqrt(0.5);
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (p_m <= 6.9e-169) {
tmp = p_m / -x;
} else if (p_m <= 1.3e-36) {
tmp = Math.log(Math.E);
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if p_m <= 6.9e-169: tmp = p_m / -x elif p_m <= 1.3e-36: tmp = math.log(math.e) else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (p_m <= 6.9e-169) tmp = Float64(p_m / Float64(-x)); elseif (p_m <= 1.3e-36) tmp = log(exp(1)); else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (p_m <= 6.9e-169) tmp = p_m / -x; elseif (p_m <= 1.3e-36) tmp = log(2.71828182845904523536); else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[p$95$m, 6.9e-169], N[(p$95$m / (-x)), $MachinePrecision], If[LessEqual[p$95$m, 1.3e-36], N[Log[E], $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;p\_m \leq 6.9 \cdot 10^{-169}:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{elif}\;p\_m \leq 1.3 \cdot 10^{-36}:\\
\;\;\;\;\log e\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 6.90000000000000027e-169Initial program 73.9%
add-log-exp73.9%
+-commutative73.9%
distribute-rgt-in73.9%
metadata-eval73.9%
fma-define73.9%
Applied egg-rr73.9%
Taylor expanded in x around -inf 17.5%
associate-*r/17.5%
neg-mul-117.5%
Simplified17.5%
if 6.90000000000000027e-169 < p < 1.3e-36Initial program 72.2%
add-log-exp72.1%
+-commutative72.1%
distribute-rgt-in72.1%
metadata-eval72.1%
fma-define72.1%
Applied egg-rr72.1%
Taylor expanded in x around inf 53.1%
exp-1-e53.1%
Simplified53.1%
if 1.3e-36 < p Initial program 91.6%
Taylor expanded in x around 0 87.1%
Final simplification40.4%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -3.2e-114) (/ p_m (- x)) (log E)))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -3.2e-114) {
tmp = p_m / -x;
} else {
tmp = log(((double) M_E));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -3.2e-114) {
tmp = p_m / -x;
} else {
tmp = Math.log(Math.E);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -3.2e-114: tmp = p_m / -x else: tmp = math.log(math.e) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -3.2e-114) tmp = Float64(p_m / Float64(-x)); else tmp = log(exp(1)); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -3.2e-114) tmp = p_m / -x; else tmp = log(2.71828182845904523536); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -3.2e-114], N[(p$95$m / (-x)), $MachinePrecision], N[Log[E], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-114}:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;\log e\\
\end{array}
\end{array}
if x < -3.2000000000000002e-114Initial program 51.2%
add-log-exp51.2%
+-commutative51.2%
distribute-rgt-in51.2%
metadata-eval51.2%
fma-define51.2%
Applied egg-rr51.2%
Taylor expanded in x around -inf 36.1%
associate-*r/36.1%
neg-mul-136.1%
Simplified36.1%
if -3.2000000000000002e-114 < x Initial program 99.3%
add-log-exp99.3%
+-commutative99.3%
distribute-rgt-in99.3%
metadata-eval99.3%
fma-define99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 51.9%
exp-1-e51.9%
Simplified51.9%
Final simplification45.0%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -2.2e-131) (/ p_m (- x)) 1.5))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -2.2e-131) {
tmp = p_m / -x;
} else {
tmp = 1.5;
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-2.2d-131)) then
tmp = p_m / -x
else
tmp = 1.5d0
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -2.2e-131) {
tmp = p_m / -x;
} else {
tmp = 1.5;
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -2.2e-131: tmp = p_m / -x else: tmp = 1.5 return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -2.2e-131) tmp = Float64(p_m / Float64(-x)); else tmp = 1.5; end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -2.2e-131) tmp = p_m / -x; else tmp = 1.5; end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -2.2e-131], N[(p$95$m / (-x)), $MachinePrecision], 1.5]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.2 \cdot 10^{-131}:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;1.5\\
\end{array}
\end{array}
if x < -2.2e-131Initial program 53.3%
add-log-exp53.2%
+-commutative53.2%
distribute-rgt-in53.2%
metadata-eval53.2%
fma-define53.2%
Applied egg-rr53.2%
Taylor expanded in x around -inf 34.1%
associate-*r/34.1%
neg-mul-134.1%
Simplified34.1%
if -2.2e-131 < x Initial program 100.0%
Taylor expanded in x around 0 65.9%
Applied egg-rr19.3%
Final simplification26.1%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -5e+20) 0.0 1.5))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -5e+20) {
tmp = 0.0;
} else {
tmp = 1.5;
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d+20)) then
tmp = 0.0d0
else
tmp = 1.5d0
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -5e+20) {
tmp = 0.0;
} else {
tmp = 1.5;
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -5e+20: tmp = 0.0 else: tmp = 1.5 return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -5e+20) tmp = 0.0; else tmp = 1.5; end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -5e+20) tmp = 0.0; else tmp = 1.5; end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -5e+20], 0.0, 1.5]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+20}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;1.5\\
\end{array}
\end{array}
if x < -5e20Initial program 53.6%
Taylor expanded in x around -inf 22.5%
neg-mul-122.5%
Simplified22.5%
Taylor expanded in x around 0 22.5%
if -5e20 < x Initial program 85.0%
Taylor expanded in x around 0 61.0%
Applied egg-rr17.0%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -5e+20) 0.0 0.125))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -5e+20) {
tmp = 0.0;
} else {
tmp = 0.125;
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d+20)) then
tmp = 0.0d0
else
tmp = 0.125d0
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -5e+20) {
tmp = 0.0;
} else {
tmp = 0.125;
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -5e+20: tmp = 0.0 else: tmp = 0.125 return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -5e+20) tmp = 0.0; else tmp = 0.125; end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -5e+20) tmp = 0.0; else tmp = 0.125; end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -5e+20], 0.0, 0.125]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+20}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;0.125\\
\end{array}
\end{array}
if x < -5e20Initial program 53.6%
Taylor expanded in x around -inf 22.5%
neg-mul-122.5%
Simplified22.5%
Taylor expanded in x around 0 22.5%
if -5e20 < x Initial program 85.0%
Taylor expanded in x around 0 61.0%
Applied egg-rr14.8%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 0.0)
p_m = fabs(p);
double code(double p_m, double x) {
return 0.0;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
code = 0.0d0
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
return 0.0;
}
p_m = math.fabs(p) def code(p_m, x): return 0.0
p_m = abs(p) function code(p_m, x) return 0.0 end
p_m = abs(p); function tmp = code(p_m, x) tmp = 0.0; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := 0.0
\begin{array}{l}
p_m = \left|p\right|
\\
0
\end{array}
Initial program 78.5%
Taylor expanded in x around -inf 7.4%
neg-mul-17.4%
Simplified7.4%
Taylor expanded in x around 0 7.4%
(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 2024180
(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))))))))