
(FPCore (p r q) :precision binary64 (* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
return (1.0 / 2.0) * ((fabs(p) + fabs(r)) - sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q): return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q) return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0)))))) end
function tmp = code(p, r, q) tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0))))); end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (p r q) :precision binary64 (* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
return (1.0 / 2.0) * ((fabs(p) + fabs(r)) - sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
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(p, r, q)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q
code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q): return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q) return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0)))))) end
function tmp = code(p, r, q) tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0))))); end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (if (<= q_m 1.75e-16) (* -0.5 (- r (fabs r))) (- q_m)))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 1.75e-16) {
tmp = -0.5 * (r - fabs(r));
} else {
tmp = -q_m;
}
return tmp;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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(p, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
real(8) :: tmp
if (q_m <= 1.75d-16) then
tmp = (-0.5d0) * (r - abs(r))
else
tmp = -q_m
end if
code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 1.75e-16) {
tmp = -0.5 * (r - Math.abs(r));
} else {
tmp = -q_m;
}
return tmp;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): tmp = 0 if q_m <= 1.75e-16: tmp = -0.5 * (r - math.fabs(r)) else: tmp = -q_m return tmp
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (q_m <= 1.75e-16) tmp = Float64(-0.5 * Float64(r - abs(r))); else tmp = Float64(-q_m); end return tmp end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
tmp = 0.0;
if (q_m <= 1.75e-16)
tmp = -0.5 * (r - abs(r));
else
tmp = -q_m;
end
tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 1.75e-16], N[(-0.5 * N[(r - N[Abs[r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], (-q$95$m)]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;q\_m \leq 1.75 \cdot 10^{-16}:\\
\;\;\;\;-0.5 \cdot \left(r - \left|r\right|\right)\\
\mathbf{else}:\\
\;\;\;\;-q\_m\\
\end{array}
\end{array}
if q < 1.75000000000000009e-16Initial program 22.1%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites9.6%
Taylor expanded in r around 0
Applied rewrites9.6%
Applied rewrites6.8%
Taylor expanded in p around 0
Applied rewrites21.0%
if 1.75000000000000009e-16 < q Initial program 21.9%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6451.3
Applied rewrites51.3%
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(if (<= q_m 1.65e-59)
(* (- (- r (fabs r)) (- (fabs p) p)) -0.5)
(if (<= q_m 5.5e+83)
(*
(pow 2.0 -1.0)
(/ (fma (+ (+ p (- (fabs r) r)) (fabs p)) r (* -2.0 (* q_m q_m))) r))
(- q_m))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 1.65e-59) {
tmp = ((r - fabs(r)) - (fabs(p) - p)) * -0.5;
} else if (q_m <= 5.5e+83) {
tmp = pow(2.0, -1.0) * (fma(((p + (fabs(r) - r)) + fabs(p)), r, (-2.0 * (q_m * q_m))) / r);
} else {
tmp = -q_m;
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (q_m <= 1.65e-59) tmp = Float64(Float64(Float64(r - abs(r)) - Float64(abs(p) - p)) * -0.5); elseif (q_m <= 5.5e+83) tmp = Float64((2.0 ^ -1.0) * Float64(fma(Float64(Float64(p + Float64(abs(r) - r)) + abs(p)), r, Float64(-2.0 * Float64(q_m * q_m))) / r)); else tmp = Float64(-q_m); end return tmp end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 1.65e-59], N[(N[(N[(r - N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(N[Abs[p], $MachinePrecision] - p), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision], If[LessEqual[q$95$m, 5.5e+83], N[(N[Power[2.0, -1.0], $MachinePrecision] * N[(N[(N[(N[(p + N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] * r + N[(-2.0 * N[(q$95$m * q$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / r), $MachinePrecision]), $MachinePrecision], (-q$95$m)]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;q\_m \leq 1.65 \cdot 10^{-59}:\\
\;\;\;\;\left(\left(r - \left|r\right|\right) - \left(\left|p\right| - p\right)\right) \cdot -0.5\\
\mathbf{elif}\;q\_m \leq 5.5 \cdot 10^{+83}:\\
\;\;\;\;{2}^{-1} \cdot \frac{\mathsf{fma}\left(\left(p + \left(\left|r\right| - r\right)\right) + \left|p\right|, r, -2 \cdot \left(q\_m \cdot q\_m\right)\right)}{r}\\
\mathbf{else}:\\
\;\;\;\;-q\_m\\
\end{array}
\end{array}
if q < 1.64999999999999991e-59Initial program 22.3%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites10.0%
Taylor expanded in r around 0
Applied rewrites10.0%
Applied rewrites32.3%
if 1.64999999999999991e-59 < q < 5.4999999999999996e83Initial program 18.9%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites16.5%
Taylor expanded in r around 0
Applied rewrites24.2%
if 5.4999999999999996e83 < q Initial program 23.8%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6468.8
Applied rewrites68.8%
Final simplification16.8%
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(if (<= q_m 5.5e-16)
(* (- (- r (fabs r)) (- (fabs p) p)) -0.5)
(if (or (<= q_m 4.7e+25) (not (<= q_m 2.05e+82)))
(- q_m)
(* (pow 2.0 -1.0) (* -2.0 (fma q_m (/ q_m r) p))))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 5.5e-16) {
tmp = ((r - fabs(r)) - (fabs(p) - p)) * -0.5;
} else if ((q_m <= 4.7e+25) || !(q_m <= 2.05e+82)) {
tmp = -q_m;
} else {
tmp = pow(2.0, -1.0) * (-2.0 * fma(q_m, (q_m / r), p));
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (q_m <= 5.5e-16) tmp = Float64(Float64(Float64(r - abs(r)) - Float64(abs(p) - p)) * -0.5); elseif ((q_m <= 4.7e+25) || !(q_m <= 2.05e+82)) tmp = Float64(-q_m); else tmp = Float64((2.0 ^ -1.0) * Float64(-2.0 * fma(q_m, Float64(q_m / r), p))); end return tmp end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 5.5e-16], N[(N[(N[(r - N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(N[Abs[p], $MachinePrecision] - p), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision], If[Or[LessEqual[q$95$m, 4.7e+25], N[Not[LessEqual[q$95$m, 2.05e+82]], $MachinePrecision]], (-q$95$m), N[(N[Power[2.0, -1.0], $MachinePrecision] * N[(-2.0 * N[(q$95$m * N[(q$95$m / r), $MachinePrecision] + p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;q\_m \leq 5.5 \cdot 10^{-16}:\\
\;\;\;\;\left(\left(r - \left|r\right|\right) - \left(\left|p\right| - p\right)\right) \cdot -0.5\\
\mathbf{elif}\;q\_m \leq 4.7 \cdot 10^{+25} \lor \neg \left(q\_m \leq 2.05 \cdot 10^{+82}\right):\\
\;\;\;\;-q\_m\\
\mathbf{else}:\\
\;\;\;\;{2}^{-1} \cdot \left(-2 \cdot \mathsf{fma}\left(q\_m, \frac{q\_m}{r}, p\right)\right)\\
\end{array}
\end{array}
if q < 5.49999999999999964e-16Initial program 22.1%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites9.6%
Taylor expanded in r around 0
Applied rewrites9.6%
Applied rewrites31.4%
if 5.49999999999999964e-16 < q < 4.6999999999999998e25 or 2.04999999999999998e82 < q Initial program 22.8%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6458.3
Applied rewrites58.3%
if 4.6999999999999998e25 < q < 2.04999999999999998e82Initial program 18.2%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites18.3%
Applied rewrites2.5%
Taylor expanded in p around inf
Applied rewrites26.3%
Taylor expanded in p around 0
Applied rewrites26.5%
Final simplification15.8%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (if (<= (* 4.0 (pow q_m 2.0)) 5e-111) 0.0 (- q_m)))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if ((4.0 * pow(q_m, 2.0)) <= 5e-111) {
tmp = 0.0;
} else {
tmp = -q_m;
}
return tmp;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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(p, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
real(8) :: tmp
if ((4.0d0 * (q_m ** 2.0d0)) <= 5d-111) then
tmp = 0.0d0
else
tmp = -q_m
end if
code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
double tmp;
if ((4.0 * Math.pow(q_m, 2.0)) <= 5e-111) {
tmp = 0.0;
} else {
tmp = -q_m;
}
return tmp;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): tmp = 0 if (4.0 * math.pow(q_m, 2.0)) <= 5e-111: tmp = 0.0 else: tmp = -q_m return tmp
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (Float64(4.0 * (q_m ^ 2.0)) <= 5e-111) tmp = 0.0; else tmp = Float64(-q_m); end return tmp end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
tmp = 0.0;
if ((4.0 * (q_m ^ 2.0)) <= 5e-111)
tmp = 0.0;
else
tmp = -q_m;
end
tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[N[(4.0 * N[Power[q$95$m, 2.0], $MachinePrecision]), $MachinePrecision], 5e-111], 0.0, (-q$95$m)]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;4 \cdot {q\_m}^{2} \leq 5 \cdot 10^{-111}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;-q\_m\\
\end{array}
\end{array}
if (*.f64 #s(literal 4 binary64) (pow.f64 q #s(literal 2 binary64))) < 5.0000000000000003e-111Initial program 19.7%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites16.0%
Taylor expanded in r around 0
Applied rewrites16.0%
Applied rewrites11.7%
Taylor expanded in p around inf
Applied rewrites35.2%
if 5.0000000000000003e-111 < (*.f64 #s(literal 4 binary64) (pow.f64 q #s(literal 2 binary64))) Initial program 23.6%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6424.0
Applied rewrites24.0%
Final simplification28.4%
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
:precision binary64
(if (<= q_m 5.5e-16)
(* (- (- r (fabs r)) (- (fabs p) p)) -0.5)
(if (or (<= q_m 4.7e+25) (not (<= q_m 2.05e+82)))
(- q_m)
(fma (* p 2.0) 0.5 (/ (* (- q_m) q_m) r)))))q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 5.5e-16) {
tmp = ((r - fabs(r)) - (fabs(p) - p)) * -0.5;
} else if ((q_m <= 4.7e+25) || !(q_m <= 2.05e+82)) {
tmp = -q_m;
} else {
tmp = fma((p * 2.0), 0.5, ((-q_m * q_m) / r));
}
return tmp;
}
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (q_m <= 5.5e-16) tmp = Float64(Float64(Float64(r - abs(r)) - Float64(abs(p) - p)) * -0.5); elseif ((q_m <= 4.7e+25) || !(q_m <= 2.05e+82)) tmp = Float64(-q_m); else tmp = fma(Float64(p * 2.0), 0.5, Float64(Float64(Float64(-q_m) * q_m) / r)); end return tmp end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 5.5e-16], N[(N[(N[(r - N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(N[Abs[p], $MachinePrecision] - p), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision], If[Or[LessEqual[q$95$m, 4.7e+25], N[Not[LessEqual[q$95$m, 2.05e+82]], $MachinePrecision]], (-q$95$m), N[(N[(p * 2.0), $MachinePrecision] * 0.5 + N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / r), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;q\_m \leq 5.5 \cdot 10^{-16}:\\
\;\;\;\;\left(\left(r - \left|r\right|\right) - \left(\left|p\right| - p\right)\right) \cdot -0.5\\
\mathbf{elif}\;q\_m \leq 4.7 \cdot 10^{+25} \lor \neg \left(q\_m \leq 2.05 \cdot 10^{+82}\right):\\
\;\;\;\;-q\_m\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(p \cdot 2, 0.5, \frac{\left(-q\_m\right) \cdot q\_m}{r}\right)\\
\end{array}
\end{array}
if q < 5.49999999999999964e-16Initial program 22.1%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites9.6%
Taylor expanded in r around 0
Applied rewrites9.6%
Applied rewrites31.4%
if 5.49999999999999964e-16 < q < 4.6999999999999998e25 or 2.04999999999999998e82 < q Initial program 22.8%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6458.3
Applied rewrites58.3%
if 4.6999999999999998e25 < q < 2.04999999999999998e82Initial program 18.2%
lift--.f64N/A
lift-+.f64N/A
associate--l+N/A
lift-fabs.f64N/A
rem-sqrt-square-revN/A
sqrt-prodN/A
lower-fma.f64N/A
rem-square-sqrtN/A
sqrt-prodN/A
rem-sqrt-square-revN/A
lift-fabs.f64N/A
lower-sqrt.f64N/A
lift-fabs.f64N/A
rem-sqrt-square-revN/A
sqrt-prodN/A
rem-square-sqrtN/A
rem-square-sqrtN/A
sqrt-prodN/A
rem-sqrt-square-revN/A
lift-fabs.f64N/A
lower-sqrt.f64N/A
lift-fabs.f64N/A
rem-sqrt-square-revN/A
sqrt-prodN/A
rem-square-sqrtN/A
lower--.f649.2
Applied rewrites9.2%
Taylor expanded in r around inf
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
fp-cancel-sub-sign-invN/A
metadata-evalN/A
distribute-rgt1-inN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
rem-square-sqrtN/A
unpow2N/A
distribute-lft-neg-inN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
associate-*r/N/A
mul-1-negN/A
Applied rewrites26.4%
Final simplification15.8%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (if (<= q_m 5.5e-16) (* (- (- r (fabs r)) (- (fabs p) p)) -0.5) (- q_m)))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 5.5e-16) {
tmp = ((r - fabs(r)) - (fabs(p) - p)) * -0.5;
} else {
tmp = -q_m;
}
return tmp;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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(p, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
real(8) :: tmp
if (q_m <= 5.5d-16) then
tmp = ((r - abs(r)) - (abs(p) - p)) * (-0.5d0)
else
tmp = -q_m
end if
code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 5.5e-16) {
tmp = ((r - Math.abs(r)) - (Math.abs(p) - p)) * -0.5;
} else {
tmp = -q_m;
}
return tmp;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): tmp = 0 if q_m <= 5.5e-16: tmp = ((r - math.fabs(r)) - (math.fabs(p) - p)) * -0.5 else: tmp = -q_m return tmp
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (q_m <= 5.5e-16) tmp = Float64(Float64(Float64(r - abs(r)) - Float64(abs(p) - p)) * -0.5); else tmp = Float64(-q_m); end return tmp end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
tmp = 0.0;
if (q_m <= 5.5e-16)
tmp = ((r - abs(r)) - (abs(p) - p)) * -0.5;
else
tmp = -q_m;
end
tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 5.5e-16], N[(N[(N[(r - N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(N[Abs[p], $MachinePrecision] - p), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision], (-q$95$m)]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;q\_m \leq 5.5 \cdot 10^{-16}:\\
\;\;\;\;\left(\left(r - \left|r\right|\right) - \left(\left|p\right| - p\right)\right) \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;-q\_m\\
\end{array}
\end{array}
if q < 5.49999999999999964e-16Initial program 22.1%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites9.6%
Taylor expanded in r around 0
Applied rewrites9.6%
Applied rewrites31.4%
if 5.49999999999999964e-16 < q Initial program 21.9%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6451.3
Applied rewrites51.3%
Final simplification15.5%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (if (<= q_m 9.6e-290) (* (* p 2.0) 0.5) (if (<= q_m 1.4e-55) 0.0 (- q_m))))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 9.6e-290) {
tmp = (p * 2.0) * 0.5;
} else if (q_m <= 1.4e-55) {
tmp = 0.0;
} else {
tmp = -q_m;
}
return tmp;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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(p, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
real(8) :: tmp
if (q_m <= 9.6d-290) then
tmp = (p * 2.0d0) * 0.5d0
else if (q_m <= 1.4d-55) then
tmp = 0.0d0
else
tmp = -q_m
end if
code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
double tmp;
if (q_m <= 9.6e-290) {
tmp = (p * 2.0) * 0.5;
} else if (q_m <= 1.4e-55) {
tmp = 0.0;
} else {
tmp = -q_m;
}
return tmp;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): tmp = 0 if q_m <= 9.6e-290: tmp = (p * 2.0) * 0.5 elif q_m <= 1.4e-55: tmp = 0.0 else: tmp = -q_m return tmp
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) tmp = 0.0 if (q_m <= 9.6e-290) tmp = Float64(Float64(p * 2.0) * 0.5); elseif (q_m <= 1.4e-55) tmp = 0.0; else tmp = Float64(-q_m); end return tmp end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
tmp = 0.0;
if (q_m <= 9.6e-290)
tmp = (p * 2.0) * 0.5;
elseif (q_m <= 1.4e-55)
tmp = 0.0;
else
tmp = -q_m;
end
tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 9.6e-290], N[(N[(p * 2.0), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[q$95$m, 1.4e-55], 0.0, (-q$95$m)]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;q\_m \leq 9.6 \cdot 10^{-290}:\\
\;\;\;\;\left(p \cdot 2\right) \cdot 0.5\\
\mathbf{elif}\;q\_m \leq 1.4 \cdot 10^{-55}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;-q\_m\\
\end{array}
\end{array}
if q < 9.6000000000000002e-290Initial program 24.6%
lift--.f64N/A
lift-+.f64N/A
associate--l+N/A
lift-fabs.f64N/A
rem-sqrt-square-revN/A
sqrt-prodN/A
lower-fma.f64N/A
rem-square-sqrtN/A
sqrt-prodN/A
rem-sqrt-square-revN/A
lift-fabs.f64N/A
lower-sqrt.f64N/A
lift-fabs.f64N/A
rem-sqrt-square-revN/A
sqrt-prodN/A
rem-square-sqrtN/A
rem-square-sqrtN/A
sqrt-prodN/A
rem-sqrt-square-revN/A
lift-fabs.f64N/A
lower-sqrt.f64N/A
lift-fabs.f64N/A
rem-sqrt-square-revN/A
sqrt-prodN/A
rem-square-sqrtN/A
lower--.f649.8
Applied rewrites9.8%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
fp-cancel-sub-sign-invN/A
metadata-evalN/A
distribute-rgt1-inN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
rem-square-sqrtN/A
unpow2N/A
distribute-lft-neg-inN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f6415.2
Applied rewrites15.2%
if 9.6000000000000002e-290 < q < 1.39999999999999992e-55Initial program 14.5%
Taylor expanded in r around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites15.6%
Taylor expanded in r around 0
Applied rewrites15.7%
Applied rewrites11.6%
Taylor expanded in p around inf
Applied rewrites38.3%
if 1.39999999999999992e-55 < q Initial program 21.8%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6447.0
Applied rewrites47.0%
Final simplification28.6%
q_m = (fabs.f64 q) NOTE: p, r, and q_m should be sorted in increasing order before calling this function. (FPCore (p r q_m) :precision binary64 (- q_m))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
return -q_m;
}
q_m = private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
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(p, r, q_m)
use fmin_fmax_functions
real(8), intent (in) :: p
real(8), intent (in) :: r
real(8), intent (in) :: q_m
code = -q_m
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
return -q_m;
}
q_m = math.fabs(q) [p, r, q_m] = sort([p, r, q_m]) def code(p, r, q_m): return -q_m
q_m = abs(q) p, r, q_m = sort([p, r, q_m]) function code(p, r, q_m) return Float64(-q_m) end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp = code(p, r, q_m)
tmp = -q_m;
end
q_m = N[Abs[q], $MachinePrecision] NOTE: p, r, and q_m should be sorted in increasing order before calling this function. code[p_, r_, q$95$m_] := (-q$95$m)
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
-q\_m
\end{array}
Initial program 22.1%
Taylor expanded in q around inf
mul-1-negN/A
lower-neg.f6417.0
Applied rewrites17.0%
herbie shell --seed 2024350
(FPCore (p r q)
:name "1/2(abs(p)+abs(r) - sqrt((p-r)^2 + 4q^2))"
:precision binary64
(* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))