
(FPCore (x) :precision binary64 (/ (log (- 1.0 x)) (log (+ 1.0 x))))
double code(double x) {
return log((1.0 - x)) / log((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = log((1.0d0 - x)) / log((1.0d0 + x))
end function
public static double code(double x) {
return Math.log((1.0 - x)) / Math.log((1.0 + x));
}
def code(x): return math.log((1.0 - x)) / math.log((1.0 + x))
function code(x) return Float64(log(Float64(1.0 - x)) / log(Float64(1.0 + x))) end
function tmp = code(x) tmp = log((1.0 - x)) / log((1.0 + x)); end
code[x_] := N[(N[Log[N[(1.0 - x), $MachinePrecision]], $MachinePrecision] / N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\log \left(1 - x\right)}{\log \left(1 + x\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (log (- 1.0 x)) (log (+ 1.0 x))))
double code(double x) {
return log((1.0 - x)) / log((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = log((1.0d0 - x)) / log((1.0d0 + x))
end function
public static double code(double x) {
return Math.log((1.0 - x)) / Math.log((1.0 + x));
}
def code(x): return math.log((1.0 - x)) / math.log((1.0 + x))
function code(x) return Float64(log(Float64(1.0 - x)) / log(Float64(1.0 + x))) end
function tmp = code(x) tmp = log((1.0 - x)) / log((1.0 + x)); end
code[x_] := N[(N[Log[N[(1.0 - x), $MachinePrecision]], $MachinePrecision] / N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\log \left(1 - x\right)}{\log \left(1 + x\right)}
\end{array}
(FPCore (x) :precision binary64 (let* ((t_0 (log1p (- x)))) (/ t_0 (- (log1p (* x (- x))) t_0))))
double code(double x) {
double t_0 = log1p(-x);
return t_0 / (log1p((x * -x)) - t_0);
}
public static double code(double x) {
double t_0 = Math.log1p(-x);
return t_0 / (Math.log1p((x * -x)) - t_0);
}
def code(x): t_0 = math.log1p(-x) return t_0 / (math.log1p((x * -x)) - t_0)
function code(x) t_0 = log1p(Float64(-x)) return Float64(t_0 / Float64(log1p(Float64(x * Float64(-x))) - t_0)) end
code[x_] := Block[{t$95$0 = N[Log[1 + (-x)], $MachinePrecision]}, N[(t$95$0 / N[(N[Log[1 + N[(x * (-x)), $MachinePrecision]], $MachinePrecision] - t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{log1p}\left(-x\right)\\
\frac{t\_0}{\mathsf{log1p}\left(x \cdot \left(-x\right)\right) - t\_0}
\end{array}
\end{array}
Initial program 2.7%
lower-log1p.f645.4
Applied rewrites5.4%
flip-+N/A
lift--.f64N/A
log-divN/A
lift-log.f64N/A
lower--.f64N/A
metadata-evalN/A
lift-*.f64N/A
sub-negN/A
lower-log1p.f64N/A
lift-*.f64N/A
distribute-rgt-neg-inN/A
neg-mul-1N/A
lower-*.f64N/A
neg-mul-1N/A
lower-neg.f644.8
lift-log.f64N/A
lift--.f64N/A
sub-negN/A
neg-mul-1N/A
lower-log1p.f64N/A
neg-mul-1N/A
lower-neg.f645.4
Applied rewrites5.4%
sub-negN/A
lift-neg.f64N/A
lift-log1p.f64100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (/ (log1p (- x)) (log1p x)))
double code(double x) {
return log1p(-x) / log1p(x);
}
public static double code(double x) {
return Math.log1p(-x) / Math.log1p(x);
}
def code(x): return math.log1p(-x) / math.log1p(x)
function code(x) return Float64(log1p(Float64(-x)) / log1p(x)) end
code[x_] := N[(N[Log[1 + (-x)], $MachinePrecision] / N[Log[1 + x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{log1p}\left(-x\right)}{\mathsf{log1p}\left(x\right)}
\end{array}
Initial program 2.7%
lower-log1p.f645.4
Applied rewrites5.4%
sub-negN/A
neg-mul-1N/A
lower-log1p.f64N/A
neg-mul-1N/A
lower-neg.f64100.0
Applied rewrites100.0%
(FPCore (x)
:precision binary64
(/
(*
x
(fma
x
(*
(fma (* x x) (fma x 0.16666666666666666 0.1111111111111111) -0.25)
(/ 1.0 (fma x (fma x -0.25 -0.3333333333333333) 0.5)))
-1.0))
(fma (* x x) (fma x (fma x -0.25 0.3333333333333333) -0.5) x)))
double code(double x) {
return (x * fma(x, (fma((x * x), fma(x, 0.16666666666666666, 0.1111111111111111), -0.25) * (1.0 / fma(x, fma(x, -0.25, -0.3333333333333333), 0.5))), -1.0)) / fma((x * x), fma(x, fma(x, -0.25, 0.3333333333333333), -0.5), x);
}
function code(x) return Float64(Float64(x * fma(x, Float64(fma(Float64(x * x), fma(x, 0.16666666666666666, 0.1111111111111111), -0.25) * Float64(1.0 / fma(x, fma(x, -0.25, -0.3333333333333333), 0.5))), -1.0)) / fma(Float64(x * x), fma(x, fma(x, -0.25, 0.3333333333333333), -0.5), x)) end
code[x_] := N[(N[(x * N[(x * N[(N[(N[(x * x), $MachinePrecision] * N[(x * 0.16666666666666666 + 0.1111111111111111), $MachinePrecision] + -0.25), $MachinePrecision] * N[(1.0 / N[(x * N[(x * -0.25 + -0.3333333333333333), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * -0.25 + 0.3333333333333333), $MachinePrecision] + -0.5), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, 0.16666666666666666, 0.1111111111111111\right), -0.25\right) \cdot \frac{1}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.25, -0.3333333333333333\right), 0.5\right)}, -1\right)}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.25, 0.3333333333333333\right), -0.5\right), x\right)}
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f643.9
Applied rewrites3.9%
Taylor expanded in x around 0
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
unpow2N/A
*-rgt-identityN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6499.7
Applied rewrites99.7%
lift-fma.f64N/A
flip-+N/A
div-invN/A
lower-*.f64N/A
Applied rewrites99.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6499.7
Applied rewrites99.7%
(FPCore (x) :precision binary64 (/ (* x (fma x (fma x (fma x -0.25 -0.3333333333333333) -0.5) -1.0)) (fma (* x x) (fma x (fma x -0.25 0.3333333333333333) -0.5) x)))
double code(double x) {
return (x * fma(x, fma(x, fma(x, -0.25, -0.3333333333333333), -0.5), -1.0)) / fma((x * x), fma(x, fma(x, -0.25, 0.3333333333333333), -0.5), x);
}
function code(x) return Float64(Float64(x * fma(x, fma(x, fma(x, -0.25, -0.3333333333333333), -0.5), -1.0)) / fma(Float64(x * x), fma(x, fma(x, -0.25, 0.3333333333333333), -0.5), x)) end
code[x_] := N[(N[(x * N[(x * N[(x * N[(x * -0.25 + -0.3333333333333333), $MachinePrecision] + -0.5), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * -0.25 + 0.3333333333333333), $MachinePrecision] + -0.5), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.25, -0.3333333333333333\right), -0.5\right), -1\right)}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.25, 0.3333333333333333\right), -0.5\right), x\right)}
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f643.9
Applied rewrites3.9%
Taylor expanded in x around 0
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
unpow2N/A
*-rgt-identityN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6499.7
Applied rewrites99.7%
(FPCore (x) :precision binary64 (fma x (fma x (fma x -0.4166666666666667 -0.5) -1.0) -1.0))
double code(double x) {
return fma(x, fma(x, fma(x, -0.4166666666666667, -0.5), -1.0), -1.0);
}
function code(x) return fma(x, fma(x, fma(x, -0.4166666666666667, -0.5), -1.0), -1.0) end
code[x_] := N[(x * N[(x * N[(x * -0.4166666666666667 + -0.5), $MachinePrecision] + -1.0), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, \mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.4166666666666667, -0.5\right), -1\right), -1\right)
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f6499.6
Applied rewrites99.6%
(FPCore (x) :precision binary64 (+ -1.0 (* x (fma x -0.5 -1.0))))
double code(double x) {
return -1.0 + (x * fma(x, -0.5, -1.0));
}
function code(x) return Float64(-1.0 + Float64(x * fma(x, -0.5, -1.0))) end
code[x_] := N[(-1.0 + N[(x * N[(x * -0.5 + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-1 + x \cdot \mathsf{fma}\left(x, -0.5, -1\right)
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f6499.5
Applied rewrites99.5%
lift-fma.f64N/A
lower-+.f64N/A
lower-*.f6499.5
Applied rewrites99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (fma x (fma x -0.5 -1.0) -1.0))
double code(double x) {
return fma(x, fma(x, -0.5, -1.0), -1.0);
}
function code(x) return fma(x, fma(x, -0.5, -1.0), -1.0) end
code[x_] := N[(x * N[(x * -0.5 + -1.0), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, \mathsf{fma}\left(x, -0.5, -1\right), -1\right)
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f6499.5
Applied rewrites99.5%
(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 - x
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
lower--.f6499.2
Applied rewrites99.2%
(FPCore (x) :precision binary64 -1.0)
double code(double x) {
return -1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -1.0d0
end function
public static double code(double x) {
return -1.0;
}
def code(x): return -1.0
function code(x) return -1.0 end
function tmp = code(x) tmp = -1.0; end
code[x_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 2.7%
Taylor expanded in x around 0
Applied rewrites98.4%
(FPCore (x) :precision binary64 (/ (log1p (- x)) (log1p x)))
double code(double x) {
return log1p(-x) / log1p(x);
}
public static double code(double x) {
return Math.log1p(-x) / Math.log1p(x);
}
def code(x): return math.log1p(-x) / math.log1p(x)
function code(x) return Float64(log1p(Float64(-x)) / log1p(x)) end
code[x_] := N[(N[Log[1 + (-x)], $MachinePrecision] / N[Log[1 + x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{log1p}\left(-x\right)}{\mathsf{log1p}\left(x\right)}
\end{array}
herbie shell --seed 2024216
(FPCore (x)
:name "qlog (example 3.10)"
:precision binary64
:pre (<= (fabs x) 1.0)
:alt
(! :herbie-platform default (/ (log1p (- x)) (log1p x)))
(/ (log (- 1.0 x)) (log (+ 1.0 x))))