
(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)
use fmin_fmax_functions
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]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
Herbie found 5 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)
use fmin_fmax_functions
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]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.0 (sqrt (* (fabs im) 2.0))))
(t_1
(*
0.5
(sqrt
(*
2.0
(- (sqrt (+ (* re re) (* (fabs im) (fabs im)))) re))))))
(if (<= t_1 2e+55)
t_0
(if (<= t_1 1e+76)
(*
(sqrt
(* (- (sqrt (fma (fabs im) (fabs im) (* re re))) re) 2.0))
0.5)
t_0))))double code(double re, double im) {
double t_0 = 0.0 * sqrt((fabs(im) * 2.0));
double t_1 = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (fabs(im) * fabs(im)))) - re)));
double tmp;
if (t_1 <= 2e+55) {
tmp = t_0;
} else if (t_1 <= 1e+76) {
tmp = sqrt(((sqrt(fma(fabs(im), fabs(im), (re * re))) - re) * 2.0)) * 0.5;
} else {
tmp = t_0;
}
return tmp;
}
function code(re, im) t_0 = Float64(0.0 * sqrt(Float64(abs(im) * 2.0))) t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(abs(im) * abs(im)))) - re)))) tmp = 0.0 if (t_1 <= 2e+55) tmp = t_0; elseif (t_1 <= 1e+76) tmp = Float64(sqrt(Float64(Float64(sqrt(fma(abs(im), abs(im), Float64(re * re))) - re) * 2.0)) * 0.5); else tmp = t_0; end return tmp end
code[re_, im_] := Block[{t$95$0 = N[(0.0 * N[Sqrt[N[(N[Abs[im], $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(N[Abs[im], $MachinePrecision] * N[Abs[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 2e+55], t$95$0, If[LessEqual[t$95$1, 1e+76], N[(N[Sqrt[N[(N[(N[Sqrt[N[(N[Abs[im], $MachinePrecision] * N[Abs[im], $MachinePrecision] + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
t_0 := 0 \cdot \sqrt{\left|im\right| \cdot 2}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \left|im\right| \cdot \left|im\right|} - re\right)}\\
\mathbf{if}\;t\_1 \leq 2 \cdot 10^{+55}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq 10^{+76}:\\
\;\;\;\;\sqrt{\left(\sqrt{\mathsf{fma}\left(\left|im\right|, \left|im\right|, re \cdot re\right)} - re\right) \cdot 2} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
if (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 2e55 or 1e76 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) Initial program 28.4%
Taylor expanded in im around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6427.9%
Applied rewrites27.9%
Taylor expanded in re around 0
Applied rewrites26.9%
Taylor expanded in undef-var around zero
Applied rewrites3.3%
if 2e55 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 1e76Initial program 28.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6428.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6428.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6428.4%
Applied rewrites28.4%
(FPCore (re im) :precision binary64 (if (<= re -3.9e+220) (* 0.5 (sqrt (* -4.0 re))) (* 0.0 (sqrt (* (fabs im) 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -3.9e+220) {
tmp = 0.5 * sqrt((-4.0 * re));
} else {
tmp = 0.0 * sqrt((fabs(im) * 2.0));
}
return tmp;
}
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-3.9d+220)) then
tmp = 0.5d0 * sqrt(((-4.0d0) * re))
else
tmp = 0.0d0 * sqrt((abs(im) * 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.9e+220) {
tmp = 0.5 * Math.sqrt((-4.0 * re));
} else {
tmp = 0.0 * Math.sqrt((Math.abs(im) * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.9e+220: tmp = 0.5 * math.sqrt((-4.0 * re)) else: tmp = 0.0 * math.sqrt((math.fabs(im) * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.9e+220) tmp = Float64(0.5 * sqrt(Float64(-4.0 * re))); else tmp = Float64(0.0 * sqrt(Float64(abs(im) * 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.9e+220) tmp = 0.5 * sqrt((-4.0 * re)); else tmp = 0.0 * sqrt((abs(im) * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.9e+220], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.0 * N[Sqrt[N[(N[Abs[im], $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;re \leq -3.9 \cdot 10^{+220}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\
\mathbf{else}:\\
\;\;\;\;0 \cdot \sqrt{\left|im\right| \cdot 2}\\
\end{array}
if re < -3.9000000000000002e220Initial program 28.4%
Taylor expanded in re around -inf
lower-*.f6413.6%
Applied rewrites13.6%
if -3.9000000000000002e220 < re Initial program 28.4%
Taylor expanded in im around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6427.9%
Applied rewrites27.9%
Taylor expanded in re around 0
Applied rewrites26.9%
Taylor expanded in undef-var around zero
Applied rewrites3.3%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (* -4.0 re))))
(if (<= re -3.9e+220)
(* 0.5 t_0)
(if (<= re -1.3e-298)
(* 0.0 t_0)
(* 0.5 (sqrt (* 2.0 (fabs im))))))))double code(double re, double im) {
double t_0 = sqrt((-4.0 * re));
double tmp;
if (re <= -3.9e+220) {
tmp = 0.5 * t_0;
} else if (re <= -1.3e-298) {
tmp = 0.0 * t_0;
} else {
tmp = 0.5 * sqrt((2.0 * fabs(im)));
}
return tmp;
}
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((-4.0d0) * re))
if (re <= (-3.9d+220)) then
tmp = 0.5d0 * t_0
else if (re <= (-1.3d-298)) then
tmp = 0.0d0 * t_0
else
tmp = 0.5d0 * sqrt((2.0d0 * abs(im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt((-4.0 * re));
double tmp;
if (re <= -3.9e+220) {
tmp = 0.5 * t_0;
} else if (re <= -1.3e-298) {
tmp = 0.0 * t_0;
} else {
tmp = 0.5 * Math.sqrt((2.0 * Math.abs(im)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt((-4.0 * re)) tmp = 0 if re <= -3.9e+220: tmp = 0.5 * t_0 elif re <= -1.3e-298: tmp = 0.0 * t_0 else: tmp = 0.5 * math.sqrt((2.0 * math.fabs(im))) return tmp
function code(re, im) t_0 = sqrt(Float64(-4.0 * re)) tmp = 0.0 if (re <= -3.9e+220) tmp = Float64(0.5 * t_0); elseif (re <= -1.3e-298) tmp = Float64(0.0 * t_0); else tmp = Float64(0.5 * sqrt(Float64(2.0 * abs(im)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt((-4.0 * re)); tmp = 0.0; if (re <= -3.9e+220) tmp = 0.5 * t_0; elseif (re <= -1.3e-298) tmp = 0.0 * t_0; else tmp = 0.5 * sqrt((2.0 * abs(im))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[re, -3.9e+220], N[(0.5 * t$95$0), $MachinePrecision], If[LessEqual[re, -1.3e-298], N[(0.0 * t$95$0), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[Abs[im], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
t_0 := \sqrt{-4 \cdot re}\\
\mathbf{if}\;re \leq -3.9 \cdot 10^{+220}:\\
\;\;\;\;0.5 \cdot t\_0\\
\mathbf{elif}\;re \leq -1.3 \cdot 10^{-298}:\\
\;\;\;\;0 \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left|im\right|}\\
\end{array}
if re < -3.9000000000000002e220Initial program 28.4%
Taylor expanded in re around -inf
lower-*.f6413.6%
Applied rewrites13.6%
if -3.9000000000000002e220 < re < -1.2999999999999999e-298Initial program 28.4%
Taylor expanded in re around -inf
lower-*.f6413.6%
Applied rewrites13.6%
Taylor expanded in undef-var around zero
Applied rewrites25.3%
if -1.2999999999999999e-298 < re Initial program 28.4%
Taylor expanded in re around inf
lower-/.f64N/A
lower-pow.f6423.0%
Applied rewrites23.0%
Taylor expanded in im around inf
lower-*.f6426.9%
Applied rewrites26.9%
(FPCore (re im) :precision binary64 (if (<= re -2.2e+62) (* 0.5 (sqrt (* -4.0 re))) (* 0.5 (sqrt (* 2.0 (fabs im))))))
double code(double re, double im) {
double tmp;
if (re <= -2.2e+62) {
tmp = 0.5 * sqrt((-4.0 * re));
} else {
tmp = 0.5 * sqrt((2.0 * fabs(im)));
}
return tmp;
}
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-2.2d+62)) then
tmp = 0.5d0 * sqrt(((-4.0d0) * re))
else
tmp = 0.5d0 * sqrt((2.0d0 * abs(im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.2e+62) {
tmp = 0.5 * Math.sqrt((-4.0 * re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * Math.abs(im)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.2e+62: tmp = 0.5 * math.sqrt((-4.0 * re)) else: tmp = 0.5 * math.sqrt((2.0 * math.fabs(im))) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.2e+62) tmp = Float64(0.5 * sqrt(Float64(-4.0 * re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * abs(im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.2e+62) tmp = 0.5 * sqrt((-4.0 * re)); else tmp = 0.5 * sqrt((2.0 * abs(im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.2e+62], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[Abs[im], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;re \leq -2.2 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left|im\right|}\\
\end{array}
if re < -2.2000000000000001e62Initial program 28.4%
Taylor expanded in re around -inf
lower-*.f6413.6%
Applied rewrites13.6%
if -2.2000000000000001e62 < re Initial program 28.4%
Taylor expanded in re around inf
lower-/.f64N/A
lower-pow.f6423.0%
Applied rewrites23.0%
Taylor expanded in im around inf
lower-*.f6426.9%
Applied rewrites26.9%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* -4.0 re))))
double code(double re, double im) {
return 0.5 * sqrt((-4.0 * re));
}
real(8) function code(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt(((-4.0d0) * re))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((-4.0 * re));
}
def code(re, im): return 0.5 * math.sqrt((-4.0 * re))
function code(re, im) return Float64(0.5 * sqrt(Float64(-4.0 * re))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((-4.0 * re)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
0.5 \cdot \sqrt{-4 \cdot re}
Initial program 28.4%
Taylor expanded in re around -inf
lower-*.f6413.6%
Applied rewrites13.6%
herbie shell --seed 2025313 -o setup:search
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))