
(FPCore (x) :precision binary64 (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
return fmod(exp(x), sqrt(cos(x))) * exp(-x);
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x): return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x) return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
return fmod(exp(x), sqrt(cos(x))) * exp(-x);
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x): return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x) return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x}
(FPCore (x)
:precision binary64
(let* ((t_0 (fma (* x x) -0.25 1.0)))
(if (<= x 0.66)
(* (fmod (exp x) t_0) (- 1.0 x))
(/ (fmod 1.0 t_0) (exp x)))))double code(double x) {
double t_0 = fma((x * x), -0.25, 1.0);
double tmp;
if (x <= 0.66) {
tmp = fmod(exp(x), t_0) * (1.0 - x);
} else {
tmp = fmod(1.0, t_0) / exp(x);
}
return tmp;
}
function code(x) t_0 = fma(Float64(x * x), -0.25, 1.0) tmp = 0.0 if (x <= 0.66) tmp = Float64(rem(exp(x), t_0) * Float64(1.0 - x)); else tmp = Float64(rem(1.0, t_0) / exp(x)); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, If[LessEqual[x, 0.66], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(1.0 - x), $MachinePrecision]), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
t_0 := \mathsf{fma}\left(x \cdot x, -0.25, 1\right)\\
\mathbf{if}\;x \leq 0.66:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod t\_0\right) \cdot \left(1 - x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(1 \bmod t\_0\right)}{e^{x}}\\
\end{array}
if x < 0.660000000000000031Initial program 9.0%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower-pow.f648.6%
Applied rewrites8.6%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f647.6%
Applied rewrites7.6%
Applied rewrites7.6%
if 0.660000000000000031 < x Initial program 9.0%
Taylor expanded in x around 0
Applied rewrites35.3%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower-pow.f6435.3%
Applied rewrites35.3%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
mult-flip-revN/A
lower-/.f6435.3%
Applied rewrites35.3%
(FPCore (x) :precision binary64 (/ (fmod (+ 1.0 x) (fma (* x x) -0.25 1.0)) (exp x)))
double code(double x) {
return fmod((1.0 + x), fma((x * x), -0.25, 1.0)) / exp(x);
}
function code(x) return Float64(rem(Float64(1.0 + x), fma(Float64(x * x), -0.25, 1.0)) / exp(x)) end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]
\frac{\left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right)}{e^{x}}
Initial program 9.0%
Taylor expanded in x around 0
Applied rewrites35.3%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower-pow.f6435.3%
Applied rewrites35.3%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
mult-flip-revN/A
lower-/.f6435.3%
Applied rewrites35.3%
Taylor expanded in x around 0
lower-+.f6438.8%
Applied rewrites38.8%
(FPCore (x) :precision binary64 (/ (fmod 1.0 (fma (* x x) -0.25 1.0)) (exp x)))
double code(double x) {
return fmod(1.0, fma((x * x), -0.25, 1.0)) / exp(x);
}
function code(x) return Float64(rem(1.0, fma(Float64(x * x), -0.25, 1.0)) / exp(x)) end
code[x_] := N[(N[With[{TMP1 = 1.0, TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]
\frac{\left(1 \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right)}{e^{x}}
Initial program 9.0%
Taylor expanded in x around 0
Applied rewrites35.3%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower-pow.f6435.3%
Applied rewrites35.3%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
mult-flip-revN/A
lower-/.f6435.3%
Applied rewrites35.3%
(FPCore (x) :precision binary64 (* (fmod 1.0 (fma (* x x) -0.25 1.0)) (+ 1.0 (* -1.0 x))))
double code(double x) {
return fmod(1.0, fma((x * x), -0.25, 1.0)) * (1.0 + (-1.0 * x));
}
function code(x) return Float64(rem(1.0, fma(Float64(x * x), -0.25, 1.0)) * Float64(1.0 + Float64(-1.0 * x))) end
code[x_] := N[(N[With[{TMP1 = 1.0, TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(1.0 + N[(-1.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(1 \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot \left(1 + -1 \cdot x\right)
Initial program 9.0%
Taylor expanded in x around 0
Applied rewrites35.3%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f64N/A
lower-pow.f6435.3%
Applied rewrites35.3%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-pow.f64N/A
pow2N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
add-flipN/A
metadata-evalN/A
sub-to-mult-revN/A
lift-/.f64N/A
lift--.f64N/A
lift-*.f6433.4%
lift-*.f64N/A
lift--.f64N/A
lift-/.f64N/A
Applied rewrites35.3%
Taylor expanded in x around 0
lower-+.f64N/A
lower-*.f644.6%
Applied rewrites4.6%
herbie shell --seed 2025183
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))