
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 2.0) (pow x 2.0)))
double code(double x, double eps) {
return pow((x + eps), 2.0) - pow(x, 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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((x + eps) ** 2.0d0) - (x ** 2.0d0)
end function
public static double code(double x, double eps) {
return Math.pow((x + eps), 2.0) - Math.pow(x, 2.0);
}
def code(x, eps): return math.pow((x + eps), 2.0) - math.pow(x, 2.0)
function code(x, eps) return Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) end
function tmp = code(x, eps) tmp = ((x + eps) ^ 2.0) - (x ^ 2.0); end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 2.0) (pow x 2.0)))
double code(double x, double eps) {
return pow((x + eps), 2.0) - pow(x, 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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((x + eps) ** 2.0d0) - (x ** 2.0d0)
end function
public static double code(double x, double eps) {
return Math.pow((x + eps), 2.0) - Math.pow(x, 2.0);
}
def code(x, eps): return math.pow((x + eps), 2.0) - math.pow(x, 2.0)
function code(x, eps) return Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) end
function tmp = code(x, eps) tmp = ((x + eps) ^ 2.0) - (x ^ 2.0); end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
(FPCore (x eps) :precision binary64 (- (* eps eps) (- (* (- x x) (+ x x)) (* (+ x x) eps))))
double code(double x, double eps) {
return (eps * eps) - (((x - x) * (x + x)) - ((x + x) * eps));
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (eps * eps) - (((x - x) * (x + x)) - ((x + x) * eps))
end function
public static double code(double x, double eps) {
return (eps * eps) - (((x - x) * (x + x)) - ((x + x) * eps));
}
def code(x, eps): return (eps * eps) - (((x - x) * (x + x)) - ((x + x) * eps))
function code(x, eps) return Float64(Float64(eps * eps) - Float64(Float64(Float64(x - x) * Float64(x + x)) - Float64(Float64(x + x) * eps))) end
function tmp = code(x, eps) tmp = (eps * eps) - (((x - x) * (x + x)) - ((x + x) * eps)); end
code[x_, eps_] := N[(N[(eps * eps), $MachinePrecision] - N[(N[(N[(x - x), $MachinePrecision] * N[(x + x), $MachinePrecision]), $MachinePrecision] - N[(N[(x + x), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\varepsilon \cdot \varepsilon - \left(\left(x - x\right) \cdot \left(x + x\right) - \left(x + x\right) \cdot \varepsilon\right)
Initial program 75.2%
lift--.f64N/A
sub-negate-revN/A
lift-pow.f64N/A
lift-+.f64N/A
sum-square-powN/A
associate--r+N/A
sub-negateN/A
lower--.f64N/A
unpow2N/A
lower-*.f64N/A
lift-pow.f64N/A
associate--r+N/A
lower--.f64N/A
Applied rewrites100.0%
(FPCore (x eps) :precision binary64 (* eps (+ (+ x x) eps)))
double code(double x, double eps) {
return eps * ((x + x) + eps);
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * ((x + x) + eps)
end function
public static double code(double x, double eps) {
return eps * ((x + x) + eps);
}
def code(x, eps): return eps * ((x + x) + eps)
function code(x, eps) return Float64(eps * Float64(Float64(x + x) + eps)) end
function tmp = code(x, eps) tmp = eps * ((x + x) + eps); end
code[x_, eps_] := N[(eps * N[(N[(x + x), $MachinePrecision] + eps), $MachinePrecision]), $MachinePrecision]
\varepsilon \cdot \left(\left(x + x\right) + \varepsilon\right)
Initial program 75.2%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64100.0%
Applied rewrites100.0%
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0%
lift-*.f64N/A
count-2-revN/A
lift-+.f64100.0%
Applied rewrites100.0%
(FPCore (x eps) :precision binary64 (if (<= (- (pow (+ x eps) 2.0) (pow x 2.0)) 0.0) (* (+ eps eps) x) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((pow((x + eps), 2.0) - pow(x, 2.0)) <= 0.0) {
tmp = (eps + eps) * x;
} else {
tmp = eps * eps;
}
return tmp;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((((x + eps) ** 2.0d0) - (x ** 2.0d0)) <= 0.0d0) then
tmp = (eps + eps) * x
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((Math.pow((x + eps), 2.0) - Math.pow(x, 2.0)) <= 0.0) {
tmp = (eps + eps) * x;
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (math.pow((x + eps), 2.0) - math.pow(x, 2.0)) <= 0.0: tmp = (eps + eps) * x else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if (Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = Float64(Float64(eps + eps) * x); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((((x + eps) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = (eps + eps) * x; else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(eps + eps), $MachinePrecision] * x), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;{\left(x + \varepsilon\right)}^{2} - {x}^{2} \leq 0:\\
\;\;\;\;\left(\varepsilon + \varepsilon\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
if (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) < 0.0Initial program 75.2%
Taylor expanded in x around inf
lower-*.f64N/A
lower-*.f6463.8%
Applied rewrites63.8%
lift-*.f64N/A
lift-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
count-2-revN/A
lower-+.f6463.8%
Applied rewrites63.8%
if 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) Initial program 75.2%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64100.0%
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites72.5%
(FPCore (x eps) :precision binary64 (* eps eps))
double code(double x, double eps) {
return eps * eps;
}
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(x, eps)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * eps
end function
public static double code(double x, double eps) {
return eps * eps;
}
def code(x, eps): return eps * eps
function code(x, eps) return Float64(eps * eps) end
function tmp = code(x, eps) tmp = eps * eps; end
code[x_, eps_] := N[(eps * eps), $MachinePrecision]
\varepsilon \cdot \varepsilon
Initial program 75.2%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64100.0%
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites72.5%
herbie shell --seed 2025258
(FPCore (x eps)
:name "ENA, Section 1.4, Exercise 4b, n=2"
:precision binary64
:pre (and (and (<= -1000000000.0 x) (<= x 1000000000.0)) (and (<= -1.0 eps) (<= eps 1.0)))
(- (pow (+ x eps) 2.0) (pow x 2.0)))