
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
(FPCore (re im) :precision binary64 (if (<= re -4.2e+65) (* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -4.2e+65) {
tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -4.2e+65) {
tmp = 0.5 * Math.sqrt((2.0 * ((im / (re / im)) * -0.5)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.2e+65: tmp = 0.5 * math.sqrt((2.0 * ((im / (re / im)) * -0.5))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.2e+65) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.2e+65) tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.2e+65], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.2 \cdot 10^{+65}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -4.19999999999999983e65Initial program 5.4%
+-commutative5.4%
hypot-def24.6%
Simplified24.6%
Taylor expanded in re around -inf 51.3%
*-commutative51.3%
unpow251.3%
associate-/l*64.0%
Simplified64.0%
if -4.19999999999999983e65 < re Initial program 50.7%
+-commutative50.7%
hypot-def90.5%
Simplified90.5%
Final simplification85.6%
(FPCore (re im)
:precision binary64
(if (<= im -5.2e-89)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im 2e-5)
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (* (sqrt 2.0) (sqrt im))))))
double code(double re, double im) {
double tmp;
if (im <= -5.2e-89) {
tmp = 0.5 * sqrt((2.0 * (re - im)));
} else if (im <= 2e-5) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * (sqrt(2.0) * sqrt(im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-5.2d-89)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
else if (im <= 2d-5) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -5.2e-89) {
tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
} else if (im <= 2e-5) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -5.2e-89: tmp = 0.5 * math.sqrt((2.0 * (re - im))) elif im <= 2e-5: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(im)) return tmp
function code(re, im) tmp = 0.0 if (im <= -5.2e-89) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im)))); elseif (im <= 2e-5) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -5.2e-89) tmp = 0.5 * sqrt((2.0 * (re - im))); elseif (im <= 2e-5) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * (sqrt(2.0) * sqrt(im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -5.2e-89], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 2e-5], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -5.2 \cdot 10^{-89}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 2 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{im}\right)\\
\end{array}
\end{array}
if im < -5.1999999999999997e-89Initial program 34.2%
+-commutative34.2%
hypot-def81.0%
Simplified81.0%
Taylor expanded in im around -inf 64.2%
mul-1-neg64.2%
sub-neg64.2%
Simplified64.2%
if -5.1999999999999997e-89 < im < 2.00000000000000016e-5Initial program 48.2%
+-commutative48.2%
hypot-def74.0%
Simplified74.0%
Taylor expanded in im around 0 50.8%
unpow250.8%
rem-square-sqrt51.8%
Simplified51.8%
if 2.00000000000000016e-5 < im Initial program 42.9%
+-commutative42.9%
hypot-def81.7%
Simplified81.7%
Taylor expanded in re around 0 76.7%
Final simplification62.8%
(FPCore (re im)
:precision binary64
(if (<= im -3.45e-90)
(* 0.5 (sqrt (* 2.0 (- im))))
(if (<= im 5.2e-22)
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (im <= -3.45e-90) {
tmp = 0.5 * sqrt((2.0 * -im));
} else if (im <= 5.2e-22) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-3.45d-90)) then
tmp = 0.5d0 * sqrt((2.0d0 * -im))
else if (im <= 5.2d-22) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -3.45e-90) {
tmp = 0.5 * Math.sqrt((2.0 * -im));
} else if (im <= 5.2e-22) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -3.45e-90: tmp = 0.5 * math.sqrt((2.0 * -im)) elif im <= 5.2e-22: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -3.45e-90) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-im)))); elseif (im <= 5.2e-22) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -3.45e-90) tmp = 0.5 * sqrt((2.0 * -im)); elseif (im <= 5.2e-22) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -3.45e-90], N[(0.5 * N[Sqrt[N[(2.0 * (-im)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5.2e-22], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -3.45 \cdot 10^{-90}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\
\mathbf{elif}\;im \leq 5.2 \cdot 10^{-22}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -3.45000000000000012e-90Initial program 33.8%
+-commutative33.8%
hypot-def80.0%
Simplified80.0%
Taylor expanded in im around -inf 62.3%
mul-1-neg62.3%
Simplified62.3%
if -3.45000000000000012e-90 < im < 5.2e-22Initial program 47.6%
+-commutative47.6%
hypot-def74.4%
Simplified74.4%
Taylor expanded in im around 0 51.7%
unpow251.7%
rem-square-sqrt52.7%
Simplified52.7%
if 5.2e-22 < im Initial program 44.5%
+-commutative44.5%
hypot-def81.8%
Simplified81.8%
Taylor expanded in re around 0 74.9%
Final simplification62.3%
(FPCore (re im)
:precision binary64
(if (<= im -3.4e-89)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im 5.2e-22)
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (im <= -3.4e-89) {
tmp = 0.5 * sqrt((2.0 * (re - im)));
} else if (im <= 5.2e-22) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-3.4d-89)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
else if (im <= 5.2d-22) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -3.4e-89) {
tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
} else if (im <= 5.2e-22) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -3.4e-89: tmp = 0.5 * math.sqrt((2.0 * (re - im))) elif im <= 5.2e-22: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -3.4e-89) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im)))); elseif (im <= 5.2e-22) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -3.4e-89) tmp = 0.5 * sqrt((2.0 * (re - im))); elseif (im <= 5.2e-22) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -3.4e-89], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5.2e-22], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -3.4 \cdot 10^{-89}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 5.2 \cdot 10^{-22}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -3.4e-89Initial program 34.2%
+-commutative34.2%
hypot-def81.0%
Simplified81.0%
Taylor expanded in im around -inf 64.2%
mul-1-neg64.2%
sub-neg64.2%
Simplified64.2%
if -3.4e-89 < im < 5.2e-22Initial program 47.1%
+-commutative47.1%
hypot-def73.7%
Simplified73.7%
Taylor expanded in im around 0 51.2%
unpow251.2%
rem-square-sqrt52.2%
Simplified52.2%
if 5.2e-22 < im Initial program 44.5%
+-commutative44.5%
hypot-def81.8%
Simplified81.8%
Taylor expanded in re around 0 74.9%
Final simplification62.7%
(FPCore (re im) :precision binary64 (if (<= im -3.7e-91) (* 0.5 (sqrt (* 2.0 (- im)))) (if (<= im 1.55e-10) (* 0.5 (* 2.0 (sqrt re))) (* 0.5 (sqrt (* 2.0 im))))))
double code(double re, double im) {
double tmp;
if (im <= -3.7e-91) {
tmp = 0.5 * sqrt((2.0 * -im));
} else if (im <= 1.55e-10) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-3.7d-91)) then
tmp = 0.5d0 * sqrt((2.0d0 * -im))
else if (im <= 1.55d-10) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -3.7e-91) {
tmp = 0.5 * Math.sqrt((2.0 * -im));
} else if (im <= 1.55e-10) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -3.7e-91: tmp = 0.5 * math.sqrt((2.0 * -im)) elif im <= 1.55e-10: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (im <= -3.7e-91) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-im)))); elseif (im <= 1.55e-10) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -3.7e-91) tmp = 0.5 * sqrt((2.0 * -im)); elseif (im <= 1.55e-10) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -3.7e-91], N[(0.5 * N[Sqrt[N[(2.0 * (-im)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.55e-10], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -3.7 \cdot 10^{-91}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\
\mathbf{elif}\;im \leq 1.55 \cdot 10^{-10}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if im < -3.7000000000000002e-91Initial program 33.8%
+-commutative33.8%
hypot-def80.0%
Simplified80.0%
Taylor expanded in im around -inf 62.3%
mul-1-neg62.3%
Simplified62.3%
if -3.7000000000000002e-91 < im < 1.55000000000000008e-10Initial program 48.6%
+-commutative48.6%
hypot-def74.7%
Simplified74.7%
Taylor expanded in im around 0 51.3%
unpow251.3%
rem-square-sqrt52.3%
Simplified52.3%
if 1.55000000000000008e-10 < im Initial program 42.9%
+-commutative42.9%
hypot-def81.7%
Simplified81.7%
Taylor expanded in re around 0 76.0%
Final simplification62.2%
(FPCore (re im) :precision binary64 (if (<= im 5.8e-9) (* 0.5 (* 2.0 (sqrt re))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (im <= 5.8e-9) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 5.8d-9) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 5.8e-9) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 5.8e-9: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (im <= 5.8e-9) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 5.8e-9) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 5.8e-9], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 5.8 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if im < 5.79999999999999982e-9Initial program 42.2%
+-commutative42.2%
hypot-def77.0%
Simplified77.0%
Taylor expanded in im around 0 37.5%
unpow237.5%
rem-square-sqrt38.3%
Simplified38.3%
if 5.79999999999999982e-9 < im Initial program 42.9%
+-commutative42.9%
hypot-def81.7%
Simplified81.7%
Taylor expanded in re around 0 76.0%
Final simplification49.2%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 42.4%
+-commutative42.4%
hypot-def78.4%
Simplified78.4%
Taylor expanded in re around 0 27.7%
Final simplification27.7%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2023196
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))