
(FPCore (x) :precision binary64 (sqrt (+ (pow x 2.0) (pow x 2.0))))
double code(double x) {
return sqrt((pow(x, 2.0) + pow(x, 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((x ** 2.0d0) + (x ** 2.0d0)))
end function
public static double code(double x) {
return Math.sqrt((Math.pow(x, 2.0) + Math.pow(x, 2.0)));
}
def code(x): return math.sqrt((math.pow(x, 2.0) + math.pow(x, 2.0)))
function code(x) return sqrt(Float64((x ^ 2.0) + (x ^ 2.0))) end
function tmp = code(x) tmp = sqrt(((x ^ 2.0) + (x ^ 2.0))); end
code[x_] := N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{{x}^{2} + {x}^{2}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (sqrt (+ (pow x 2.0) (pow x 2.0))))
double code(double x) {
return sqrt((pow(x, 2.0) + pow(x, 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(((x ** 2.0d0) + (x ** 2.0d0)))
end function
public static double code(double x) {
return Math.sqrt((Math.pow(x, 2.0) + Math.pow(x, 2.0)));
}
def code(x): return math.sqrt((math.pow(x, 2.0) + math.pow(x, 2.0)))
function code(x) return sqrt(Float64((x ^ 2.0) + (x ^ 2.0))) end
function tmp = code(x) tmp = sqrt(((x ^ 2.0) + (x ^ 2.0))); end
code[x_] := N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{{x}^{2} + {x}^{2}}
\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 51.7%
unpow251.7%
unpow251.7%
hypot-def100.0%
Simplified100.0%
Final simplification100.0%
(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 52.1%
unpow252.1%
unpow252.1%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 29.7%
add-sqr-sqrt28.0%
associate-*r*28.1%
sqrt-prod28.2%
count-228.2%
*-commutative28.2%
sqrt-unprod52.2%
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-rr7.4%
Simplified4.4%
if 2 < x Initial program 50.4%
unpow250.4%
unpow250.4%
hypot-def99.9%
Simplified99.9%
Taylor expanded in x around 0 99.3%
add-sqr-sqrt98.8%
associate-*r*98.8%
sqrt-prod99.4%
count-299.4%
*-commutative99.4%
sqrt-unprod50.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.3%
Applied egg-rr7.3%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
+-inverses0.0%
+-inverses0.0%
flip-+20.3%
*-un-lft-identity20.3%
fma-def20.3%
Applied egg-rr20.3%
Simplified20.3%
Final simplification8.2%
(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 51.7%
unpow251.7%
unpow251.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 46.6%
add-sqr-sqrt45.2%
associate-*r*45.2%
sqrt-prod45.4%
count-245.4%
*-commutative45.4%
sqrt-unprod51.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-rr10.5%
Final simplification10.5%
(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 51.7%
unpow251.7%
unpow251.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in x around 0 46.6%
add-sqr-sqrt45.2%
associate-*r*45.2%
sqrt-prod45.4%
count-245.4%
*-commutative45.4%
sqrt-unprod51.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-rr10.5%
Simplified3.9%
Final simplification3.9%
herbie shell --seed 2023257
(FPCore (x)
:name "sqrt E (should all be same)"
:precision binary64
(sqrt (+ (pow x 2.0) (pow x 2.0))))