
(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 52.7%
hypot-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (+ x (+ (+ x 1.0) -1.0)))
double code(double x) {
return x + ((x + 1.0) + -1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x + ((x + 1.0d0) + (-1.0d0))
end function
public static double code(double x) {
return x + ((x + 1.0) + -1.0);
}
def code(x): return x + ((x + 1.0) + -1.0)
function code(x) return Float64(x + Float64(Float64(x + 1.0) + -1.0)) end
function tmp = code(x) tmp = x + ((x + 1.0) + -1.0); end
code[x_] := N[(x + N[(N[(x + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(\left(x + 1\right) + -1\right)
\end{array}
Initial program 52.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 53.0%
*-commutative53.0%
add-sqr-sqrt51.8%
associate-*r*51.8%
sqrt-prod52.1%
count-252.1%
*-commutative52.1%
sqrt-unprod52.7%
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-+13.6%
Applied egg-rr8.3%
associate-+r+8.3%
associate--l+8.3%
Applied egg-rr8.3%
+-commutative8.3%
associate-+l-11.6%
Applied egg-rr11.6%
Final simplification11.6%
(FPCore (x) :precision binary64 (if (<= x 2.0) 0.0 (+ x -2.0)))
double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.0;
} else {
tmp = x + -2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.0d0) then
tmp = 0.0d0
else
tmp = x + (-2.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.0;
} else {
tmp = x + -2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.0: tmp = 0.0 else: tmp = x + -2.0 return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = 0.0; else tmp = Float64(x + -2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = 0.0; else tmp = x + -2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], 0.0, N[(x + -2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;x + -2\\
\end{array}
\end{array}
if x < 2Initial program 53.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 37.5%
*-commutative37.5%
add-sqr-sqrt36.0%
associate-*r*36.0%
sqrt-prod36.2%
count-236.2%
*-commutative36.2%
sqrt-unprod53.4%
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-+13.7%
Applied egg-rr8.7%
Simplified4.4%
if 2 < x Initial program 50.5%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 99.4%
*-commutative99.4%
add-sqr-sqrt99.1%
associate-*r*99.2%
sqrt-prod99.6%
count-299.6%
*-commutative99.6%
sqrt-unprod50.5%
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-+13.2%
Applied egg-rr20.3%
add-exp-log20.3%
log1p-udef20.3%
expm1-udef20.3%
expm1-log1p-u20.3%
*-un-lft-identity20.3%
fma-def20.3%
Applied egg-rr20.3%
Simplified20.3%
Final simplification8.4%
(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 52.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 53.0%
*-commutative53.0%
add-sqr-sqrt51.8%
associate-*r*51.8%
sqrt-prod52.1%
count-252.1%
*-commutative52.1%
sqrt-unprod52.7%
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-+13.6%
Applied egg-rr11.6%
Final simplification11.6%
(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 52.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 53.0%
*-commutative53.0%
add-sqr-sqrt51.8%
associate-*r*51.8%
sqrt-prod52.1%
count-252.1%
*-commutative52.1%
sqrt-unprod52.7%
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-+13.6%
Applied egg-rr11.6%
Simplified3.8%
Final simplification3.8%
herbie shell --seed 2023278
(FPCore (x)
:name "sqrt A (should all be same)"
:precision binary64
(sqrt (+ (* x x) (* x x))))