
(FPCore (x) :precision binary64 (sqrt (+ (* x x) (* x x))))
double code(double x) {
return sqrt(((x * x) + (x * x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((x * x) + (x * x)))
end function
public static double code(double x) {
return Math.sqrt(((x * x) + (x * x)));
}
def code(x): return math.sqrt(((x * x) + (x * x)))
function code(x) return sqrt(Float64(Float64(x * x) + Float64(x * x))) end
function tmp = code(x) tmp = sqrt(((x * x) + (x * x))); end
code[x_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + x \cdot x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (sqrt (+ (* x x) (* x x))))
double code(double x) {
return sqrt(((x * x) + (x * x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((x * x) + (x * x)))
end function
public static double code(double x) {
return Math.sqrt(((x * x) + (x * x)));
}
def code(x): return math.sqrt(((x * x) + (x * x)))
function code(x) return sqrt(Float64(Float64(x * x) + Float64(x * x))) end
function tmp = code(x) tmp = sqrt(((x * x) + (x * x))); end
code[x_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + x \cdot x}
\end{array}
(FPCore (x) :precision binary64 (hypot x x))
double code(double x) {
return hypot(x, x);
}
public static double code(double x) {
return Math.hypot(x, x);
}
def code(x): return math.hypot(x, x)
function code(x) return hypot(x, x) end
function tmp = code(x) tmp = hypot(x, x); end
code[x_] := N[Sqrt[x ^ 2 + x ^ 2], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{hypot}\left(x, x\right)
\end{array}
Initial program 58.6%
hypot-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (* x x) 2.0) (* x x) (+ x -2.0)))
double code(double x) {
double tmp;
if ((x * x) <= 2.0) {
tmp = x * x;
} else {
tmp = x + -2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x * x) <= 2.0d0) then
tmp = x * x
else
tmp = x + (-2.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x * x) <= 2.0) {
tmp = x * x;
} else {
tmp = x + -2.0;
}
return tmp;
}
def code(x): tmp = 0 if (x * x) <= 2.0: tmp = x * x else: tmp = x + -2.0 return tmp
function code(x) tmp = 0.0 if (Float64(x * x) <= 2.0) tmp = Float64(x * x); else tmp = Float64(x + -2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x * x) <= 2.0) tmp = x * x; else tmp = x + -2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(x * x), $MachinePrecision], 2.0], N[(x * x), $MachinePrecision], N[(x + -2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 2:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;x + -2\\
\end{array}
\end{array}
if (*.f64 x x) < 2Initial program 60.4%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.9%
sqrt-unprod7.1%
add-sqr-sqrt7.1%
*-un-lft-identity7.1%
fma-def7.1%
Applied egg-rr7.1%
Simplified7.1%
if 2 < (*.f64 x x) Initial program 56.6%
Taylor expanded in x around 0 49.2%
add-sqr-sqrt48.6%
associate-*r*48.7%
sqrt-prod48.9%
count-248.9%
*-commutative48.9%
sqrt-unprod56.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+14.0%
Applied egg-rr10.4%
add-exp-log10.0%
log1p-udef10.0%
expm1-udef10.0%
expm1-log1p-u10.4%
*-un-lft-identity10.4%
fma-def10.4%
Applied egg-rr10.4%
Simplified10.4%
Final simplification8.7%
(FPCore (x) :precision binary64 (* x x))
double code(double x) {
return x * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * x
end function
public static double code(double x) {
return x * x;
}
def code(x): return x * x
function code(x) return Float64(x * x) end
function tmp = code(x) tmp = x * x; end
code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 58.6%
flip-+0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+6.7%
sqrt-unprod7.0%
add-sqr-sqrt7.0%
*-un-lft-identity7.0%
fma-def7.0%
Applied egg-rr7.0%
Simplified7.1%
Final simplification7.1%
(FPCore (x) :precision binary64 (+ x x))
double code(double x) {
return x + x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x + x
end function
public static double code(double x) {
return x + x;
}
def code(x): return x + x
function code(x) return Float64(x + x) end
function tmp = code(x) tmp = x + x; end
code[x_] := N[(x + x), $MachinePrecision]
\begin{array}{l}
\\
x + x
\end{array}
Initial program 58.6%
Taylor expanded in x around 0 49.2%
Simplified11.0%
Final simplification11.0%
(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 58.6%
Taylor expanded in x around 0 49.2%
add-sqr-sqrt47.9%
associate-*r*47.9%
sqrt-prod48.1%
count-248.1%
*-commutative48.1%
sqrt-unprod58.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+14.4%
Applied egg-rr11.0%
Simplified3.8%
Final simplification3.8%
herbie shell --seed 2023222
(FPCore (x)
:name "sqrt A (should all be same)"
:precision binary64
(sqrt (+ (* x x) (* x x))))