
(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)));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
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]
\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 6 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)));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
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]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(if (<= re -3.6e+22)
(* 0.5 (sqrt (* (- im_m) (/ im_m re))))
(if (<= re 1.9e-18)
(*
0.5
(sqrt
(*
2.0
(+ (/ 1.0 (/ (- 1.0 (* (ratio-of-squares re im_m) 0.5)) im_m)) re))))
(if (<= re 2e+123)
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im_m im_m))) re))))
(sqrt re)))))\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.6 \cdot 10^{+22}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\_m\right) \cdot \frac{im\_m}{re}}\\
\mathbf{elif}\;re \leq 1.9 \cdot 10^{-18}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{1}{\frac{1 - \mathsf{ratio\_of\_squares}\left(re, im\_m\right) \cdot 0.5}{im\_m}} + re\right)}\\
\mathbf{elif}\;re \leq 2 \cdot 10^{+123}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im\_m \cdot im\_m} + re\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.6e22Initial program 7.7%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6446.1
Applied rewrites46.1%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6447.9
Applied rewrites47.9%
if -3.6e22 < re < 1.8999999999999999e-18Initial program 58.0%
lift-sqrt.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow1/2N/A
metadata-evalN/A
pow-negN/A
lower-/.f64N/A
lower-pow.f64N/A
pow2N/A
pow2N/A
+-commutativeN/A
lower-+.f64N/A
pow2N/A
lift-*.f64N/A
pow2N/A
lift-*.f6457.9
Applied rewrites57.9%
Taylor expanded in im around inf
lower-/.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
pow2N/A
pow2N/A
lower-ratio-of-squares.f6447.1
Applied rewrites47.1%
if 1.8999999999999999e-18 < re < 1.99999999999999996e123Initial program 75.2%
if 1.99999999999999996e123 < re Initial program 18.2%
Taylor expanded in re around 0
Applied rewrites14.1%
lift-*.f64N/A
count-2-revN/A
lower-+.f6414.1
Applied rewrites14.1%
Taylor expanded in re around inf
count-2-revN/A
lower-sqrt.f6486.7
Applied rewrites86.7%
Final simplification57.1%
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(if (<= re -3.6e+22)
(* 0.5 (sqrt (* (- im_m) (/ im_m re))))
(if (<= re 1.9e-18)
(* 0.5 (sqrt (* 2.0 (+ im_m re))))
(if (<= re 2e+123)
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im_m im_m))) re))))
(sqrt re)))))im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -3.6e+22) {
tmp = 0.5 * sqrt((-im_m * (im_m / re)));
} else if (re <= 1.9e-18) {
tmp = 0.5 * sqrt((2.0 * (im_m + re)));
} else if (re <= 2e+123) {
tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im_m * im_m))) + re)));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im_m)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-3.6d+22)) then
tmp = 0.5d0 * sqrt((-im_m * (im_m / re)))
else if (re <= 1.9d-18) then
tmp = 0.5d0 * sqrt((2.0d0 * (im_m + re)))
else if (re <= 2d+123) then
tmp = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im_m * im_m))) + re)))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -3.6e+22) {
tmp = 0.5 * Math.sqrt((-im_m * (im_m / re)));
} else if (re <= 1.9e-18) {
tmp = 0.5 * Math.sqrt((2.0 * (im_m + re)));
} else if (re <= 2e+123) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im_m * im_m))) + re)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -3.6e+22: tmp = 0.5 * math.sqrt((-im_m * (im_m / re))) elif re <= 1.9e-18: tmp = 0.5 * math.sqrt((2.0 * (im_m + re))) elif re <= 2e+123: tmp = 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im_m * im_m))) + re))) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -3.6e+22) tmp = Float64(0.5 * sqrt(Float64(Float64(-im_m) * Float64(im_m / re)))); elseif (re <= 1.9e-18) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im_m + re)))); elseif (re <= 2e+123) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im_m * im_m))) + re)))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -3.6e+22) tmp = 0.5 * sqrt((-im_m * (im_m / re))); elseif (re <= 1.9e-18) tmp = 0.5 * sqrt((2.0 * (im_m + re))); elseif (re <= 2e+123) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im_m * im_m))) + re))); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -3.6e+22], N[(0.5 * N[Sqrt[N[((-im$95$m) * N[(im$95$m / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.9e-18], N[(0.5 * N[Sqrt[N[(2.0 * N[(im$95$m + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2e+123], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.6 \cdot 10^{+22}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\_m\right) \cdot \frac{im\_m}{re}}\\
\mathbf{elif}\;re \leq 1.9 \cdot 10^{-18}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im\_m + re\right)}\\
\mathbf{elif}\;re \leq 2 \cdot 10^{+123}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im\_m \cdot im\_m} + re\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.6e22Initial program 7.7%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6446.1
Applied rewrites46.1%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6447.9
Applied rewrites47.9%
if -3.6e22 < re < 1.8999999999999999e-18Initial program 58.0%
Taylor expanded in re around 0
Applied rewrites46.7%
if 1.8999999999999999e-18 < re < 1.99999999999999996e123Initial program 75.2%
if 1.99999999999999996e123 < re Initial program 18.2%
Taylor expanded in re around 0
Applied rewrites14.1%
lift-*.f64N/A
count-2-revN/A
lower-+.f6414.1
Applied rewrites14.1%
Taylor expanded in re around inf
count-2-revN/A
lower-sqrt.f6486.7
Applied rewrites86.7%
Final simplification56.9%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -3.6e+22) (* 0.5 (sqrt (* (- im_m) (/ im_m re)))) (if (<= re 8.2e-17) (* 0.5 (sqrt (* 2.0 (+ im_m re)))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -3.6e+22) {
tmp = 0.5 * sqrt((-im_m * (im_m / re)));
} else if (re <= 8.2e-17) {
tmp = 0.5 * sqrt((2.0 * (im_m + re)));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im_m)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-3.6d+22)) then
tmp = 0.5d0 * sqrt((-im_m * (im_m / re)))
else if (re <= 8.2d-17) then
tmp = 0.5d0 * sqrt((2.0d0 * (im_m + re)))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -3.6e+22) {
tmp = 0.5 * Math.sqrt((-im_m * (im_m / re)));
} else if (re <= 8.2e-17) {
tmp = 0.5 * Math.sqrt((2.0 * (im_m + re)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -3.6e+22: tmp = 0.5 * math.sqrt((-im_m * (im_m / re))) elif re <= 8.2e-17: tmp = 0.5 * math.sqrt((2.0 * (im_m + re))) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -3.6e+22) tmp = Float64(0.5 * sqrt(Float64(Float64(-im_m) * Float64(im_m / re)))); elseif (re <= 8.2e-17) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im_m + re)))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -3.6e+22) tmp = 0.5 * sqrt((-im_m * (im_m / re))); elseif (re <= 8.2e-17) tmp = 0.5 * sqrt((2.0 * (im_m + re))); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -3.6e+22], N[(0.5 * N[Sqrt[N[((-im$95$m) * N[(im$95$m / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8.2e-17], N[(0.5 * N[Sqrt[N[(2.0 * N[(im$95$m + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.6 \cdot 10^{+22}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\_m\right) \cdot \frac{im\_m}{re}}\\
\mathbf{elif}\;re \leq 8.2 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im\_m + re\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.6e22Initial program 7.7%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6446.1
Applied rewrites46.1%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6447.9
Applied rewrites47.9%
if -3.6e22 < re < 8.2000000000000001e-17Initial program 58.0%
Taylor expanded in re around 0
Applied rewrites46.7%
if 8.2000000000000001e-17 < re Initial program 42.8%
Taylor expanded in re around 0
Applied rewrites15.5%
lift-*.f64N/A
count-2-revN/A
lower-+.f6415.5
Applied rewrites15.5%
Taylor expanded in re around inf
count-2-revN/A
lower-sqrt.f6475.8
Applied rewrites75.8%
Final simplification55.2%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -3.9e+211) (* 0.5 (sqrt (* 2.0 (+ (- re) re)))) (if (<= re 6.5e-17) (* 0.5 (sqrt (+ im_m im_m))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -3.9e+211) {
tmp = 0.5 * sqrt((2.0 * (-re + re)));
} else if (re <= 6.5e-17) {
tmp = 0.5 * sqrt((im_m + im_m));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im_m)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-3.9d+211)) then
tmp = 0.5d0 * sqrt((2.0d0 * (-re + re)))
else if (re <= 6.5d-17) then
tmp = 0.5d0 * sqrt((im_m + im_m))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -3.9e+211) {
tmp = 0.5 * Math.sqrt((2.0 * (-re + re)));
} else if (re <= 6.5e-17) {
tmp = 0.5 * Math.sqrt((im_m + im_m));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -3.9e+211: tmp = 0.5 * math.sqrt((2.0 * (-re + re))) elif re <= 6.5e-17: tmp = 0.5 * math.sqrt((im_m + im_m)) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -3.9e+211) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(-re) + re)))); elseif (re <= 6.5e-17) tmp = Float64(0.5 * sqrt(Float64(im_m + im_m))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -3.9e+211) tmp = 0.5 * sqrt((2.0 * (-re + re))); elseif (re <= 6.5e-17) tmp = 0.5 * sqrt((im_m + im_m)); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -3.9e+211], N[(0.5 * N[Sqrt[N[(2.0 * N[((-re) + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.5e-17], N[(0.5 * N[Sqrt[N[(im$95$m + im$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.9 \cdot 10^{+211}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(-re\right) + re\right)}\\
\mathbf{elif}\;re \leq 6.5 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{im\_m + im\_m}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.90000000000000023e211Initial program 2.3%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f6435.1
Applied rewrites35.1%
if -3.90000000000000023e211 < re < 6.4999999999999996e-17Initial program 45.1%
Taylor expanded in re around 0
Applied rewrites38.6%
lift-*.f64N/A
count-2-revN/A
lower-+.f6438.6
Applied rewrites38.6%
if 6.4999999999999996e-17 < re Initial program 42.8%
Taylor expanded in re around 0
Applied rewrites15.5%
lift-*.f64N/A
count-2-revN/A
lower-+.f6415.5
Applied rewrites15.5%
Taylor expanded in re around inf
count-2-revN/A
lower-sqrt.f6475.8
Applied rewrites75.8%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re 6.5e-17) (* 0.5 (sqrt (+ im_m im_m))) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= 6.5e-17) {
tmp = 0.5 * sqrt((im_m + im_m));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im_m)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= 6.5d-17) then
tmp = 0.5d0 * sqrt((im_m + im_m))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= 6.5e-17) {
tmp = 0.5 * Math.sqrt((im_m + im_m));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= 6.5e-17: tmp = 0.5 * math.sqrt((im_m + im_m)) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= 6.5e-17) tmp = Float64(0.5 * sqrt(Float64(im_m + im_m))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= 6.5e-17) tmp = 0.5 * sqrt((im_m + im_m)); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, 6.5e-17], N[(0.5 * N[Sqrt[N[(im$95$m + im$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq 6.5 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{im\_m + im\_m}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 6.4999999999999996e-17Initial program 40.0%
Taylor expanded in re around 0
Applied rewrites34.1%
lift-*.f64N/A
count-2-revN/A
lower-+.f6434.1
Applied rewrites34.1%
if 6.4999999999999996e-17 < re Initial program 42.8%
Taylor expanded in re around 0
Applied rewrites15.5%
lift-*.f64N/A
count-2-revN/A
lower-+.f6415.5
Applied rewrites15.5%
Taylor expanded in re around inf
count-2-revN/A
lower-sqrt.f6475.8
Applied rewrites75.8%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (sqrt re))
im_m = fabs(im);
double code(double re, double im_m) {
return sqrt(re);
}
im_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(re, im_m)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = sqrt(re)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return Math.sqrt(re);
}
im_m = math.fabs(im) def code(re, im_m): return math.sqrt(re)
im_m = abs(im) function code(re, im_m) return sqrt(re) end
im_m = abs(im); function tmp = code(re, im_m) tmp = sqrt(re); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\sqrt{re}
\end{array}
Initial program 40.8%
Taylor expanded in re around 0
Applied rewrites28.9%
lift-*.f64N/A
count-2-revN/A
lower-+.f6428.9
Applied rewrites28.9%
Taylor expanded in re around inf
count-2-revN/A
lower-sqrt.f6427.0
Applied rewrites27.0%
herbie shell --seed 2025065
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(! :herbie-platform default (if (< re 0) (* 1/2 (* (sqrt 2) (sqrt (/ (* im im) (- (modulus re im) re))))) (* 1/2 (sqrt (* 2 (+ (modulus re im) re))))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))