
(FPCore (x) :precision binary64 (log (+ 1.0 x)))
double code(double x) {
return log((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = log((1.0d0 + x))
end function
public static double code(double x) {
return Math.log((1.0 + x));
}
def code(x): return math.log((1.0 + x))
function code(x) return log(Float64(1.0 + x)) end
function tmp = code(x) tmp = log((1.0 + x)); end
code[x_] := N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + x\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (log (+ 1.0 x)))
double code(double x) {
return log((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = log((1.0d0 + x))
end function
public static double code(double x) {
return Math.log((1.0 + x));
}
def code(x): return math.log((1.0 + x))
function code(x) return log(Float64(1.0 + x)) end
function tmp = code(x) tmp = log((1.0 + x)); end
code[x_] := N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + x\right)
\end{array}
(FPCore (x) :precision binary64 (log1p x))
double code(double x) {
return log1p(x);
}
public static double code(double x) {
return Math.log1p(x);
}
def code(x): return math.log1p(x)
function code(x) return log1p(x) end
code[x_] := N[Log[1 + x], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(x\right)
\end{array}
Initial program 42.2%
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f64100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (/ x (/ 1.0 (/ 1.0 (fma 0.5 x 1.0)))))
double code(double x) {
return x / (1.0 / (1.0 / fma(0.5, x, 1.0)));
}
function code(x) return Float64(x / Float64(1.0 / Float64(1.0 / fma(0.5, x, 1.0)))) end
code[x_] := N[(x / N[(1.0 / N[(1.0 / N[(0.5 * x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{\frac{1}{\frac{1}{\mathsf{fma}\left(0.5, x, 1\right)}}}
\end{array}
Initial program 42.2%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6463.1
Applied rewrites63.1%
Applied rewrites63.1%
Taylor expanded in x around 0
Applied rewrites68.0%
Applied rewrites68.0%
(FPCore (x) :precision binary64 (if (<= (+ 1.0 x) 2.0) (* (fma (fma 0.3333333333333333 x -0.5) x 1.0) x) (/ x (* 0.5 x))))
double code(double x) {
double tmp;
if ((1.0 + x) <= 2.0) {
tmp = fma(fma(0.3333333333333333, x, -0.5), x, 1.0) * x;
} else {
tmp = x / (0.5 * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(1.0 + x) <= 2.0) tmp = Float64(fma(fma(0.3333333333333333, x, -0.5), x, 1.0) * x); else tmp = Float64(x / Float64(0.5 * x)); end return tmp end
code[x_] := If[LessEqual[N[(1.0 + x), $MachinePrecision], 2.0], N[(N[(N[(0.3333333333333333 * x + -0.5), $MachinePrecision] * x + 1.0), $MachinePrecision] * x), $MachinePrecision], N[(x / N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 + x \leq 2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, x, -0.5\right), x, 1\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{0.5 \cdot x}\\
\end{array}
\end{array}
if (+.f64 #s(literal 1 binary64) x) < 2Initial program 8.7%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f6499.2
Applied rewrites99.2%
if 2 < (+.f64 #s(literal 1 binary64) x) Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f640.7
Applied rewrites0.7%
Applied rewrites0.7%
Taylor expanded in x around 0
Applied rewrites14.4%
Taylor expanded in x around inf
Applied rewrites14.4%
(FPCore (x) :precision binary64 (if (<= (+ 1.0 x) 2.0) (* (fma -0.5 x 1.0) x) (/ x (* 0.5 x))))
double code(double x) {
double tmp;
if ((1.0 + x) <= 2.0) {
tmp = fma(-0.5, x, 1.0) * x;
} else {
tmp = x / (0.5 * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(1.0 + x) <= 2.0) tmp = Float64(fma(-0.5, x, 1.0) * x); else tmp = Float64(x / Float64(0.5 * x)); end return tmp end
code[x_] := If[LessEqual[N[(1.0 + x), $MachinePrecision], 2.0], N[(N[(-0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], N[(x / N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 + x \leq 2:\\
\;\;\;\;\mathsf{fma}\left(-0.5, x, 1\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{0.5 \cdot x}\\
\end{array}
\end{array}
if (+.f64 #s(literal 1 binary64) x) < 2Initial program 8.7%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6499.1
Applied rewrites99.1%
if 2 < (+.f64 #s(literal 1 binary64) x) Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f640.7
Applied rewrites0.7%
Applied rewrites0.7%
Taylor expanded in x around 0
Applied rewrites14.4%
Taylor expanded in x around inf
Applied rewrites14.4%
(FPCore (x) :precision binary64 (/ x (fma 0.5 x 1.0)))
double code(double x) {
return x / fma(0.5, x, 1.0);
}
function code(x) return Float64(x / fma(0.5, x, 1.0)) end
code[x_] := N[(x / N[(0.5 * x + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{\mathsf{fma}\left(0.5, x, 1\right)}
\end{array}
Initial program 42.2%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6463.1
Applied rewrites63.1%
Applied rewrites63.1%
Taylor expanded in x around 0
Applied rewrites68.0%
(FPCore (x) :precision binary64 (* 1.0 x))
double code(double x) {
return 1.0 * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 * x
end function
public static double code(double x) {
return 1.0 * x;
}
def code(x): return 1.0 * x
function code(x) return Float64(1.0 * x) end
function tmp = code(x) tmp = 1.0 * x; end
code[x_] := N[(1.0 * x), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot x
\end{array}
Initial program 42.2%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6463.0
Applied rewrites63.0%
Taylor expanded in x around 0
Applied rewrites63.9%
(FPCore (x) :precision binary64 (if (== (+ 1.0 x) 1.0) x (/ (* x (log (+ 1.0 x))) (- (+ 1.0 x) 1.0))))
double code(double x) {
double tmp;
if ((1.0 + x) == 1.0) {
tmp = x;
} else {
tmp = (x * log((1.0 + x))) / ((1.0 + x) - 1.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((1.0d0 + x) == 1.0d0) then
tmp = x
else
tmp = (x * log((1.0d0 + x))) / ((1.0d0 + x) - 1.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((1.0 + x) == 1.0) {
tmp = x;
} else {
tmp = (x * Math.log((1.0 + x))) / ((1.0 + x) - 1.0);
}
return tmp;
}
def code(x): tmp = 0 if (1.0 + x) == 1.0: tmp = x else: tmp = (x * math.log((1.0 + x))) / ((1.0 + x) - 1.0) return tmp
function code(x) tmp = 0.0 if (Float64(1.0 + x) == 1.0) tmp = x; else tmp = Float64(Float64(x * log(Float64(1.0 + x))) / Float64(Float64(1.0 + x) - 1.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((1.0 + x) == 1.0) tmp = x; else tmp = (x * log((1.0 + x))) / ((1.0 + x) - 1.0); end tmp_2 = tmp; end
code[x_] := If[Equal[N[(1.0 + x), $MachinePrecision], 1.0], x, N[(N[(x * N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 + x = 1:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;\frac{x \cdot \log \left(1 + x\right)}{\left(1 + x\right) - 1}\\
\end{array}
\end{array}
herbie shell --seed 2024295
(FPCore (x)
:name "ln(1 + x)"
:precision binary64
:alt
(! :herbie-platform default (if (== (+ 1 x) 1) x (/ (* x (log (+ 1 x))) (- (+ 1 x) 1))))
(log (+ 1.0 x)))