
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
(FPCore (x y) :precision binary64 (if (<= x -2e+154) (- x) (if (<= x 5e+91) (sqrt (+ (* x x) y)) x)))
double code(double x, double y) {
double tmp;
if (x <= -2e+154) {
tmp = -x;
} else if (x <= 5e+91) {
tmp = sqrt(((x * x) + y));
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2d+154)) then
tmp = -x
else if (x <= 5d+91) then
tmp = sqrt(((x * x) + y))
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2e+154) {
tmp = -x;
} else if (x <= 5e+91) {
tmp = Math.sqrt(((x * x) + y));
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2e+154: tmp = -x elif x <= 5e+91: tmp = math.sqrt(((x * x) + y)) else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -2e+154) tmp = Float64(-x); elseif (x <= 5e+91) tmp = sqrt(Float64(Float64(x * x) + y)); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2e+154) tmp = -x; elseif (x <= 5e+91) tmp = sqrt(((x * x) + y)); else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2e+154], (-x), If[LessEqual[x, 5e+91], N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{+154}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x \leq 5 \cdot 10^{+91}:\\
\;\;\;\;\sqrt{x \cdot x + y}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -2.00000000000000007e154Initial program 6.9%
Taylor expanded in x around -inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -2.00000000000000007e154 < x < 5.0000000000000002e91Initial program 100.0%
if 5.0000000000000002e91 < x Initial program 26.6%
Taylor expanded in x around inf 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -1.35e-73) (- (* y (/ -0.5 x)) x) (if (<= x 2.8e-52) (sqrt y) (+ x (* 0.5 (/ y x))))))
double code(double x, double y) {
double tmp;
if (x <= -1.35e-73) {
tmp = (y * (-0.5 / x)) - x;
} else if (x <= 2.8e-52) {
tmp = sqrt(y);
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.35d-73)) then
tmp = (y * ((-0.5d0) / x)) - x
else if (x <= 2.8d-52) then
tmp = sqrt(y)
else
tmp = x + (0.5d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.35e-73) {
tmp = (y * (-0.5 / x)) - x;
} else if (x <= 2.8e-52) {
tmp = Math.sqrt(y);
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.35e-73: tmp = (y * (-0.5 / x)) - x elif x <= 2.8e-52: tmp = math.sqrt(y) else: tmp = x + (0.5 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -1.35e-73) tmp = Float64(Float64(y * Float64(-0.5 / x)) - x); elseif (x <= 2.8e-52) tmp = sqrt(y); else tmp = Float64(x + Float64(0.5 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.35e-73) tmp = (y * (-0.5 / x)) - x; elseif (x <= 2.8e-52) tmp = sqrt(y); else tmp = x + (0.5 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.35e-73], N[(N[(y * N[(-0.5 / x), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision], If[LessEqual[x, 2.8e-52], N[Sqrt[y], $MachinePrecision], N[(x + N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.35 \cdot 10^{-73}:\\
\;\;\;\;y \cdot \frac{-0.5}{x} - x\\
\mathbf{elif}\;x \leq 2.8 \cdot 10^{-52}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{else}:\\
\;\;\;\;x + 0.5 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -1.34999999999999997e-73Initial program 60.1%
Taylor expanded in x around -inf 78.1%
+-commutative78.1%
mul-1-neg78.1%
unsub-neg78.1%
associate-+r-78.1%
*-commutative78.1%
*-commutative78.1%
unpow278.1%
cube-mult78.1%
times-frac92.4%
associate-*l*92.4%
distribute-lft-out92.4%
associate-*l/92.4%
times-frac92.4%
Simplified92.4%
Taylor expanded in y around 0 92.8%
associate-*r/92.8%
*-commutative92.8%
associate-*r/92.8%
Simplified92.8%
if -1.34999999999999997e-73 < x < 2.79999999999999995e-52Initial program 100.0%
Taylor expanded in x around 0 97.1%
if 2.79999999999999995e-52 < x Initial program 49.3%
Taylor expanded in x around inf 91.4%
Final simplification93.4%
(FPCore (x y) :precision binary64 (if (<= x -2e-310) (- x) (+ x (* 0.5 (/ y x)))))
double code(double x, double y) {
double tmp;
if (x <= -2e-310) {
tmp = -x;
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2d-310)) then
tmp = -x
else
tmp = x + (0.5d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2e-310) {
tmp = -x;
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2e-310: tmp = -x else: tmp = x + (0.5 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -2e-310) tmp = Float64(-x); else tmp = Float64(x + Float64(0.5 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2e-310) tmp = -x; else tmp = x + (0.5 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2e-310], (-x), N[(x + N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;x + 0.5 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -1.999999999999994e-310Initial program 70.5%
Taylor expanded in x around -inf 70.3%
mul-1-neg70.3%
Simplified70.3%
if -1.999999999999994e-310 < x Initial program 63.0%
Taylor expanded in x around inf 68.4%
Final simplification69.3%
(FPCore (x y) :precision binary64 (if (<= x -2e-310) (- (* y (/ -0.5 x)) x) (+ x (* 0.5 (/ y x)))))
double code(double x, double y) {
double tmp;
if (x <= -2e-310) {
tmp = (y * (-0.5 / x)) - x;
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2d-310)) then
tmp = (y * ((-0.5d0) / x)) - x
else
tmp = x + (0.5d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2e-310) {
tmp = (y * (-0.5 / x)) - x;
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2e-310: tmp = (y * (-0.5 / x)) - x else: tmp = x + (0.5 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -2e-310) tmp = Float64(Float64(y * Float64(-0.5 / x)) - x); else tmp = Float64(x + Float64(0.5 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2e-310) tmp = (y * (-0.5 / x)) - x; else tmp = x + (0.5 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2e-310], N[(N[(y * N[(-0.5 / x), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision], N[(x + N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;y \cdot \frac{-0.5}{x} - x\\
\mathbf{else}:\\
\;\;\;\;x + 0.5 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -1.999999999999994e-310Initial program 70.5%
Taylor expanded in x around -inf 57.8%
+-commutative57.8%
mul-1-neg57.8%
unsub-neg57.8%
associate-+r-57.8%
*-commutative57.8%
*-commutative57.8%
unpow257.8%
cube-mult57.8%
times-frac69.3%
associate-*l*69.3%
distribute-lft-out69.3%
associate-*l/69.3%
times-frac69.3%
Simplified69.3%
Taylor expanded in y around 0 70.7%
associate-*r/70.7%
*-commutative70.7%
associate-*r/70.7%
Simplified70.7%
if -1.999999999999994e-310 < x Initial program 63.0%
Taylor expanded in x around inf 68.4%
Final simplification69.5%
(FPCore (x y) :precision binary64 (if (<= x -2e-310) (- x) x))
double code(double x, double y) {
double tmp;
if (x <= -2e-310) {
tmp = -x;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2d-310)) then
tmp = -x
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2e-310) {
tmp = -x;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2e-310: tmp = -x else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -2e-310) tmp = Float64(-x); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2e-310) tmp = -x; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2e-310], (-x), x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1.999999999999994e-310Initial program 70.5%
Taylor expanded in x around -inf 70.3%
mul-1-neg70.3%
Simplified70.3%
if -1.999999999999994e-310 < x Initial program 63.0%
Taylor expanded in x around inf 67.8%
Final simplification69.0%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 66.6%
Taylor expanded in x around inf 35.8%
Final simplification35.8%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (* 0.5 (/ y x)) x)))
(if (< x -1.5097698010472593e+153)
(- t_0)
(if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) t_0))))
double code(double x, double y) {
double t_0 = (0.5 * (y / x)) + x;
double tmp;
if (x < -1.5097698010472593e+153) {
tmp = -t_0;
} else if (x < 5.582399551122541e+57) {
tmp = sqrt(((x * x) + y));
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = (0.5d0 * (y / x)) + x
if (x < (-1.5097698010472593d+153)) then
tmp = -t_0
else if (x < 5.582399551122541d+57) then
tmp = sqrt(((x * x) + y))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (0.5 * (y / x)) + x;
double tmp;
if (x < -1.5097698010472593e+153) {
tmp = -t_0;
} else if (x < 5.582399551122541e+57) {
tmp = Math.sqrt(((x * x) + y));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (0.5 * (y / x)) + x tmp = 0 if x < -1.5097698010472593e+153: tmp = -t_0 elif x < 5.582399551122541e+57: tmp = math.sqrt(((x * x) + y)) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(0.5 * Float64(y / x)) + x) tmp = 0.0 if (x < -1.5097698010472593e+153) tmp = Float64(-t_0); elseif (x < 5.582399551122541e+57) tmp = sqrt(Float64(Float64(x * x) + y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (0.5 * (y / x)) + x; tmp = 0.0; if (x < -1.5097698010472593e+153) tmp = -t_0; elseif (x < 5.582399551122541e+57) tmp = sqrt(((x * x) + y)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]}, If[Less[x, -1.5097698010472593e+153], (-t$95$0), If[Less[x, 5.582399551122541e+57], N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \frac{y}{x} + x\\
\mathbf{if}\;x < -1.5097698010472593 \cdot 10^{+153}:\\
\;\;\;\;-t_0\\
\mathbf{elif}\;x < 5.582399551122541 \cdot 10^{+57}:\\
\;\;\;\;\sqrt{x \cdot x + y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
herbie shell --seed 2023182
(FPCore (x y)
:name "Linear.Quaternion:$clog from linear-1.19.1.3"
:precision binary64
:herbie-target
(if (< x -1.5097698010472593e+153) (- (+ (* 0.5 (/ y x)) x)) (if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) (+ (* 0.5 (/ y x)) x)))
(sqrt (+ (* x x) y)))