
(FPCore (k n) :precision binary64 (* (/ 1.0 (sqrt k)) (pow (* (* 2.0 (PI)) n) (/ (- 1.0 k) 2.0))))
\begin{array}{l}
\\
\frac{1}{\sqrt{k}} \cdot {\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot n\right)}^{\left(\frac{1 - k}{2}\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (k n) :precision binary64 (* (/ 1.0 (sqrt k)) (pow (* (* 2.0 (PI)) n) (/ (- 1.0 k) 2.0))))
\begin{array}{l}
\\
\frac{1}{\sqrt{k}} \cdot {\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot n\right)}^{\left(\frac{1 - k}{2}\right)}
\end{array}
(FPCore (k n) :precision binary64 (let* ((t_0 (* (* 2.0 n) (PI)))) (/ (sqrt t_0) (* (sqrt k) (sqrt (pow t_0 k))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(2 \cdot n\right) \cdot \mathsf{PI}\left(\right)\\
\frac{\sqrt{t\_0}}{\sqrt{k} \cdot \sqrt{{t\_0}^{k}}}
\end{array}
\end{array}
Initial program 99.5%
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
lift-PI.f64N/A
add-sqr-sqrtN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lift-PI.f64N/A
lower-sqrt.f64N/A
lift-PI.f64N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
Applied rewrites99.7%
lift-*.f64N/A
*-rgt-identity99.7
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.7
lift-pow.f64N/A
pow-to-expN/A
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
lift-PI.f64N/A
lift-/.f64N/A
Applied rewrites99.7%
(FPCore (k n)
:precision binary64
(let* ((t_0 (* n (PI)))
(t_1
(* (pow (sqrt k) -1.0) (pow (* (* 2.0 (PI)) n) (/ (- 1.0 k) 2.0)))))
(if (<= t_1 0.0)
(sqrt 0.0)
(if (<= t_1 5e+219)
(/ (sqrt (* (PI) (* n 2.0))) (sqrt k))
(/
(* (* (sqrt (sqrt t_0)) (sqrt (sqrt (* k (* t_0 k))))) (sqrt 2.0))
k)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := n \cdot \mathsf{PI}\left(\right)\\
t_1 := {\left(\sqrt{k}\right)}^{-1} \cdot {\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot n\right)}^{\left(\frac{1 - k}{2}\right)}\\
\mathbf{if}\;t\_1 \leq 0:\\
\;\;\;\;\sqrt{0}\\
\mathbf{elif}\;t\_1 \leq 5 \cdot 10^{+219}:\\
\;\;\;\;\frac{\sqrt{\mathsf{PI}\left(\right) \cdot \left(n \cdot 2\right)}}{\sqrt{k}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(\sqrt{\sqrt{t\_0}} \cdot \sqrt{\sqrt{k \cdot \left(t\_0 \cdot k\right)}}\right) \cdot \sqrt{2}}{k}\\
\end{array}
\end{array}
if (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) < 0.0Initial program 100.0%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f643.2
Applied rewrites3.2%
Applied rewrites3.2%
Applied rewrites100.0%
if 0.0 < (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) < 5e219Initial program 99.0%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f6480.9
Applied rewrites80.9%
Applied rewrites97.1%
if 5e219 < (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) Initial program 99.9%
Taylor expanded in k around 0
lower-/.f64N/A
Applied rewrites68.0%
Taylor expanded in k around 0
Applied rewrites7.5%
Applied rewrites26.3%
Final simplification77.5%
(FPCore (k n)
:precision binary64
(if (<=
(* (pow (sqrt k) -1.0) (pow (* (* 2.0 (PI)) n) (/ (- 1.0 k) 2.0)))
0.0)
(sqrt 0.0)
(/ (* (sqrt (* (* 2.0 n) (PI))) (sqrt k)) k)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\left(\sqrt{k}\right)}^{-1} \cdot {\left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot n\right)}^{\left(\frac{1 - k}{2}\right)} \leq 0:\\
\;\;\;\;\sqrt{0}\\
\mathbf{else}:\\
\;\;\;\;\frac{\sqrt{\left(2 \cdot n\right) \cdot \mathsf{PI}\left(\right)} \cdot \sqrt{k}}{k}\\
\end{array}
\end{array}
if (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) < 0.0Initial program 100.0%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f643.2
Applied rewrites3.2%
Applied rewrites3.2%
Applied rewrites100.0%
if 0.0 < (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) Initial program 99.3%
Taylor expanded in k around 0
lower-/.f64N/A
Applied rewrites72.0%
Taylor expanded in k around 0
Applied rewrites49.8%
Applied rewrites65.2%
Final simplification72.1%
(FPCore (k n)
:precision binary64
(let* ((t_0 (* 2.0 (PI))))
(if (<= (* (pow (sqrt k) -1.0) (pow (* t_0 n) (/ (- 1.0 k) 2.0))) 5e+149)
(sqrt (* t_0 (/ n k)))
(/ (sqrt (* (* (* n (PI)) k) 2.0)) k))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := 2 \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;{\left(\sqrt{k}\right)}^{-1} \cdot {\left(t\_0 \cdot n\right)}^{\left(\frac{1 - k}{2}\right)} \leq 5 \cdot 10^{+149}:\\
\;\;\;\;\sqrt{t\_0 \cdot \frac{n}{k}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\sqrt{\left(\left(n \cdot \mathsf{PI}\left(\right)\right) \cdot k\right) \cdot 2}}{k}\\
\end{array}
\end{array}
if (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) < 4.9999999999999999e149Initial program 99.4%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f6467.0
Applied rewrites67.0%
Applied rewrites67.1%
Applied rewrites67.1%
if 4.9999999999999999e149 < (*.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 k)) (pow.f64 (*.f64 (*.f64 #s(literal 2 binary64) (PI.f64)) n) (/.f64 (-.f64 #s(literal 1 binary64) k) #s(literal 2 binary64)))) Initial program 99.6%
Taylor expanded in k around 0
lower-/.f64N/A
Applied rewrites75.5%
Taylor expanded in k around 0
Applied rewrites29.2%
Applied rewrites29.4%
Final simplification53.0%
(FPCore (k n) :precision binary64 (/ (pow (* (PI) (* n 2.0)) (fma -0.5 k 0.5)) (sqrt k)))
\begin{array}{l}
\\
\frac{{\left(\mathsf{PI}\left(\right) \cdot \left(n \cdot 2\right)\right)}^{\left(\mathsf{fma}\left(-0.5, k, 0.5\right)\right)}}{\sqrt{k}}
\end{array}
Initial program 99.5%
Taylor expanded in k around inf
sinh-+-cosh-revN/A
+-commutativeN/A
distribute-rgt-out--N/A
fp-cancel-sub-signN/A
mul-1-negN/A
distribute-rgt-inN/A
distribute-rgt-out--N/A
fp-cancel-sub-signN/A
Applied rewrites99.5%
lift-*.f64N/A
lift-/.f64N/A
associate-*l/N/A
*-lft-identityN/A
lower-/.f6499.5
Applied rewrites99.5%
(FPCore (k n) :precision binary64 (if (<= k 2.25e+176) (/ (* (sqrt (* (* 2.0 n) (PI))) (sqrt k)) k) (sqrt (/ (fma (* k n) (PI) (* (* n (PI)) k)) (* k k)))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;k \leq 2.25 \cdot 10^{+176}:\\
\;\;\;\;\frac{\sqrt{\left(2 \cdot n\right) \cdot \mathsf{PI}\left(\right)} \cdot \sqrt{k}}{k}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\frac{\mathsf{fma}\left(k \cdot n, \mathsf{PI}\left(\right), \left(n \cdot \mathsf{PI}\left(\right)\right) \cdot k\right)}{k \cdot k}}\\
\end{array}
\end{array}
if k < 2.25000000000000002e176Initial program 99.3%
Taylor expanded in k around 0
lower-/.f64N/A
Applied rewrites57.7%
Taylor expanded in k around 0
Applied rewrites49.2%
Applied rewrites64.4%
if 2.25000000000000002e176 < k Initial program 100.0%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f642.5
Applied rewrites2.5%
Applied rewrites2.5%
Applied rewrites17.6%
(FPCore (k n) :precision binary64 (/ (* (sqrt (* (* 2.0 n) (PI))) (sqrt k)) k))
\begin{array}{l}
\\
\frac{\sqrt{\left(2 \cdot n\right) \cdot \mathsf{PI}\left(\right)} \cdot \sqrt{k}}{k}
\end{array}
Initial program 99.5%
Taylor expanded in k around 0
lower-/.f64N/A
Applied rewrites57.9%
Taylor expanded in k around 0
Applied rewrites40.4%
Applied rewrites52.9%
(FPCore (k n) :precision binary64 (/ (sqrt (* (PI) (* n 2.0))) (sqrt k)))
\begin{array}{l}
\\
\frac{\sqrt{\mathsf{PI}\left(\right) \cdot \left(n \cdot 2\right)}}{\sqrt{k}}
\end{array}
Initial program 99.5%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f6443.0
Applied rewrites43.0%
Applied rewrites52.9%
(FPCore (k n) :precision binary64 (* (sqrt n) (sqrt (/ (* 2.0 (PI)) k))))
\begin{array}{l}
\\
\sqrt{n} \cdot \sqrt{\frac{2 \cdot \mathsf{PI}\left(\right)}{k}}
\end{array}
Initial program 99.5%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f6443.0
Applied rewrites43.0%
Applied rewrites43.1%
Applied rewrites52.9%
(FPCore (k n) :precision binary64 (sqrt (* (* 2.0 (PI)) (/ n k))))
\begin{array}{l}
\\
\sqrt{\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{n}{k}}
\end{array}
Initial program 99.5%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f6443.0
Applied rewrites43.0%
Applied rewrites43.1%
Applied rewrites43.1%
(FPCore (k n) :precision binary64 (sqrt (* (/ 2.0 k) n)))
double code(double k, double n) {
return sqrt(((2.0 / k) * n));
}
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(k, n)
use fmin_fmax_functions
real(8), intent (in) :: k
real(8), intent (in) :: n
code = sqrt(((2.0d0 / k) * n))
end function
public static double code(double k, double n) {
return Math.sqrt(((2.0 / k) * n));
}
def code(k, n): return math.sqrt(((2.0 / k) * n))
function code(k, n) return sqrt(Float64(Float64(2.0 / k) * n)) end
function tmp = code(k, n) tmp = sqrt(((2.0 / k) * n)); end
code[k_, n_] := N[Sqrt[N[(N[(2.0 / k), $MachinePrecision] * n), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{2}{k} \cdot n}
\end{array}
Initial program 99.5%
Taylor expanded in k around 0
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-PI.f6443.0
Applied rewrites43.0%
Applied rewrites43.1%
Applied rewrites43.1%
Applied rewrites9.9%
herbie shell --seed 2024346
(FPCore (k n)
:name "Migdal et al, Equation (51)"
:precision binary64
(* (/ 1.0 (sqrt k)) (pow (* (* 2.0 (PI)) n) (/ (- 1.0 k) 2.0))))