
(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 8 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 41.0%
lower-log1p.f64100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (/ 1.0 (* (fma x 0.5 1.0) (/ 1.0 x))))
double code(double x) {
return 1.0 / (fma(x, 0.5, 1.0) * (1.0 / x));
}
function code(x) return Float64(1.0 / Float64(fma(x, 0.5, 1.0) * Float64(1.0 / x))) end
code[x_] := N[(1.0 / N[(N[(x * 0.5 + 1.0), $MachinePrecision] * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(x, 0.5, 1\right) \cdot \frac{1}{x}}
\end{array}
Initial program 41.0%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6464.7
Applied rewrites64.7%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f6464.6
Applied rewrites64.6%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6468.3
Applied rewrites68.3%
lift-fma.f64N/A
div-invN/A
lower-*.f64N/A
lower-/.f6468.3
Applied rewrites68.3%
(FPCore (x) :precision binary64 (if (<= (+ x 1.0) 2.0) (fma (fma x (fma x -0.25 0.3333333333333333) -0.5) (* x x) x) 2.0))
double code(double x) {
double tmp;
if ((x + 1.0) <= 2.0) {
tmp = fma(fma(x, fma(x, -0.25, 0.3333333333333333), -0.5), (x * x), x);
} else {
tmp = 2.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(x + 1.0) <= 2.0) tmp = fma(fma(x, fma(x, -0.25, 0.3333333333333333), -0.5), Float64(x * x), x); else tmp = 2.0; end return tmp end
code[x_] := If[LessEqual[N[(x + 1.0), $MachinePrecision], 2.0], N[(N[(x * N[(x * -0.25 + 0.3333333333333333), $MachinePrecision] + -0.5), $MachinePrecision] * N[(x * x), $MachinePrecision] + x), $MachinePrecision], 2.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x + 1 \leq 2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.25, 0.3333333333333333\right), -0.5\right), x \cdot x, x\right)\\
\mathbf{else}:\\
\;\;\;\;2\\
\end{array}
\end{array}
if (+.f64 #s(literal 1 binary64) x) < 2Initial program 7.9%
Taylor expanded in x around 0
+-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
*-lft-identityN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.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
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f643.8
Applied rewrites3.8%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f643.8
Applied rewrites3.8%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6414.4
Applied rewrites14.4%
Taylor expanded in x around inf
Applied rewrites14.4%
Final simplification68.6%
(FPCore (x) :precision binary64 (/ 1.0 (/ (fma x 0.5 1.0) x)))
double code(double x) {
return 1.0 / (fma(x, 0.5, 1.0) / x);
}
function code(x) return Float64(1.0 / Float64(fma(x, 0.5, 1.0) / x)) end
code[x_] := N[(1.0 / N[(N[(x * 0.5 + 1.0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{\mathsf{fma}\left(x, 0.5, 1\right)}{x}}
\end{array}
Initial program 41.0%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6464.7
Applied rewrites64.7%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f6464.6
Applied rewrites64.6%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6468.3
Applied rewrites68.3%
(FPCore (x) :precision binary64 (if (<= (+ x 1.0) 2.0) (fma (fma x 0.3333333333333333 -0.5) (* x x) x) 2.0))
double code(double x) {
double tmp;
if ((x + 1.0) <= 2.0) {
tmp = fma(fma(x, 0.3333333333333333, -0.5), (x * x), x);
} else {
tmp = 2.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(x + 1.0) <= 2.0) tmp = fma(fma(x, 0.3333333333333333, -0.5), Float64(x * x), x); else tmp = 2.0; end return tmp end
code[x_] := If[LessEqual[N[(x + 1.0), $MachinePrecision], 2.0], N[(N[(x * 0.3333333333333333 + -0.5), $MachinePrecision] * N[(x * x), $MachinePrecision] + x), $MachinePrecision], 2.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x + 1 \leq 2:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x, 0.3333333333333333, -0.5\right), x \cdot x, x\right)\\
\mathbf{else}:\\
\;\;\;\;2\\
\end{array}
\end{array}
if (+.f64 #s(literal 1 binary64) x) < 2Initial program 7.9%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6498.9
Applied rewrites98.9%
if 2 < (+.f64 #s(literal 1 binary64) x) Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f643.8
Applied rewrites3.8%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f643.8
Applied rewrites3.8%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6414.4
Applied rewrites14.4%
Taylor expanded in x around inf
Applied rewrites14.4%
Final simplification68.6%
(FPCore (x) :precision binary64 (if (<= (+ x 1.0) 2.0) (* x (fma x -0.5 1.0)) 2.0))
double code(double x) {
double tmp;
if ((x + 1.0) <= 2.0) {
tmp = x * fma(x, -0.5, 1.0);
} else {
tmp = 2.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(x + 1.0) <= 2.0) tmp = Float64(x * fma(x, -0.5, 1.0)); else tmp = 2.0; end return tmp end
code[x_] := If[LessEqual[N[(x + 1.0), $MachinePrecision], 2.0], N[(x * N[(x * -0.5 + 1.0), $MachinePrecision]), $MachinePrecision], 2.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x + 1 \leq 2:\\
\;\;\;\;x \cdot \mathsf{fma}\left(x, -0.5, 1\right)\\
\mathbf{else}:\\
\;\;\;\;2\\
\end{array}
\end{array}
if (+.f64 #s(literal 1 binary64) x) < 2Initial program 7.9%
Taylor expanded in x around 0
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6498.8
Applied rewrites98.8%
lift-*.f64N/A
*-commutativeN/A
distribute-lft1-inN/A
lower-*.f64N/A
lift-*.f64N/A
lower-fma.f6498.8
Applied rewrites98.8%
if 2 < (+.f64 #s(literal 1 binary64) x) Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f643.8
Applied rewrites3.8%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f643.8
Applied rewrites3.8%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6414.4
Applied rewrites14.4%
Taylor expanded in x around inf
Applied rewrites14.4%
Final simplification68.5%
(FPCore (x) :precision binary64 (if (<= (+ x 1.0) 2.0) (* x 0.25) 2.0))
double code(double x) {
double tmp;
if ((x + 1.0) <= 2.0) {
tmp = x * 0.25;
} else {
tmp = 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x + 1.0d0) <= 2.0d0) then
tmp = x * 0.25d0
else
tmp = 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x + 1.0) <= 2.0) {
tmp = x * 0.25;
} else {
tmp = 2.0;
}
return tmp;
}
def code(x): tmp = 0 if (x + 1.0) <= 2.0: tmp = x * 0.25 else: tmp = 2.0 return tmp
function code(x) tmp = 0.0 if (Float64(x + 1.0) <= 2.0) tmp = Float64(x * 0.25); else tmp = 2.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x + 1.0) <= 2.0) tmp = x * 0.25; else tmp = 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(x + 1.0), $MachinePrecision], 2.0], N[(x * 0.25), $MachinePrecision], 2.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x + 1 \leq 2:\\
\;\;\;\;x \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;2\\
\end{array}
\end{array}
if (+.f64 #s(literal 1 binary64) x) < 2Initial program 7.9%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6498.9
Applied rewrites98.9%
lift-fma.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
flip-+N/A
associate-*l/N/A
div-invN/A
lower-fma.f64N/A
Applied rewrites98.9%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f6499.0
Applied rewrites99.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6417.2
Applied rewrites17.2%
if 2 < (+.f64 #s(literal 1 binary64) x) Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f643.8
Applied rewrites3.8%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f643.8
Applied rewrites3.8%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6414.4
Applied rewrites14.4%
Taylor expanded in x around inf
Applied rewrites14.4%
Final simplification16.2%
(FPCore (x) :precision binary64 2.0)
double code(double x) {
return 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0
end function
public static double code(double x) {
return 2.0;
}
def code(x): return 2.0
function code(x) return 2.0 end
function tmp = code(x) tmp = 2.0; end
code[x_] := 2.0
\begin{array}{l}
\\
2
\end{array}
Initial program 41.0%
Taylor expanded in x around 0
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
*-commutativeN/A
associate-*l*N/A
unpow2N/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6464.7
Applied rewrites64.7%
lift-fma.f64N/A
lift-*.f64N/A
flip-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-fma.f64N/A
lower-/.f6464.6
Applied rewrites64.6%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6468.3
Applied rewrites68.3%
Taylor expanded in x around inf
Applied rewrites7.7%
(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 2024214
(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)))