
(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 47.9%
unpow247.9%
unpow247.9%
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 50.2%
unpow250.2%
unpow250.2%
hypot-def100.0%
Simplified100.0%
hypot-udef50.2%
pow1/250.2%
distribute-lft-out50.2%
unpow-prod-down37.9%
pow1/237.9%
Applied egg-rr37.9%
unpow1/237.8%
Simplified37.8%
sqrt-unprod50.2%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+13.4%
sqrt-unprod7.7%
add-sqr-sqrt9.2%
Applied egg-rr9.2%
Simplified4.4%
if 2 < x Initial program 40.0%
unpow240.0%
unpow240.0%
hypot-def100.0%
Simplified100.0%
hypot-udef40.0%
pow1/240.0%
distribute-lft-out40.0%
unpow-prod-down99.4%
pow1/299.4%
Applied egg-rr99.4%
unpow1/299.4%
Simplified99.4%
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 simplification7.9%
(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 47.9%
unpow247.9%
unpow247.9%
hypot-def100.0%
Simplified100.0%
hypot-udef47.9%
pow1/247.9%
distribute-lft-out47.9%
unpow-prod-down51.3%
pow1/251.3%
Applied egg-rr51.3%
unpow1/251.3%
Simplified51.3%
sqrt-unprod47.9%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+13.0%
sqrt-unprod10.5%
add-sqr-sqrt11.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 47.9%
unpow247.9%
unpow247.9%
hypot-def100.0%
Simplified100.0%
hypot-udef47.9%
pow1/247.9%
distribute-lft-out47.9%
unpow-prod-down51.3%
pow1/251.3%
Applied egg-rr51.3%
unpow1/251.3%
Simplified51.3%
sqrt-unprod47.9%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
associate-*r/0.0%
distribute-lft-out--0.0%
difference-of-squares0.0%
associate-*r/0.0%
+-inverses0.0%
+-inverses0.0%
flip-+13.0%
sqrt-unprod10.5%
add-sqr-sqrt11.6%
Applied egg-rr11.6%
Simplified3.9%
Final simplification3.9%
herbie shell --seed 2023215
(FPCore (x)
:name "sqrt E (should all be same)"
:precision binary64
(sqrt (+ (pow x 2.0) (pow x 2.0))))