(FPCore (x) :precision binary64 (- (+ (* x x) 1e+18) (* (* 2.0 1000000000.0) x)))
double code(double x) { return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x); }
real(8) function code(x) real(8), intent (in) :: x code = ((x * x) + 1d+18) - ((2.0d0 * 1000000000.0d0) * x) end function
public static double code(double x) { return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x); }
def code(x): return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x)
function code(x) return Float64(Float64(Float64(x * x) + 1e+18) - Float64(Float64(2.0 * 1000000000.0) * x)) end
function tmp = code(x) tmp = ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x); end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] + 1e+18), $MachinePrecision] - N[(N[(2.0 * 1000000000.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x \end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (x) :precision binary64 (- (+ (* x x) 1e+18) (* (* 2.0 1000000000.0) x)))
double code(double x) { return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x); }
real(8) function code(x) real(8), intent (in) :: x code = ((x * x) + 1d+18) - ((2.0d0 * 1000000000.0d0) * x) end function
public static double code(double x) { return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x); }
def code(x): return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x)
function code(x) return Float64(Float64(Float64(x * x) + 1e+18) - Float64(Float64(2.0 * 1000000000.0) * x)) end
function tmp = code(x) tmp = ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x); end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] + 1e+18), $MachinePrecision] - N[(N[(2.0 * 1000000000.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x \end{array}
(FPCore (x) :precision binary64 (fma x (- x 2000000000.0) 1e+18))
double code(double x) { return fma(x, (x - 2000000000.0), 1e+18); }
function code(x) return fma(x, Float64(x - 2000000000.0), 1e+18) end
code[x_] := N[(x * N[(x - 2000000000.0), $MachinePrecision] + 1e+18), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(x, x - 2000000000, 10^{+18}\right) \end{array}
Initial program 98.0%
lift--.f64
N/A
lift-+.f64
N/A
+-commutative
N/A
associate--l+
N/A
+-commutative
N/A
lift-*.f64
N/A
lift-*.f64
N/A
distribute-rgt-out--
N/A
lower-fma.f64
N/A
lower--.f64
99.4
lift-*.f64
N/A
metadata-eval
99.4
Applied rewrites99.4%
(FPCore (x) :precision binary64 (if (<= x 420000000.0) (fma -2000000000.0 x 1e+18) (* x x)))
double code(double x) { double tmp; if (x <= 420000000.0) { tmp = fma(-2000000000.0, x, 1e+18); } else { tmp = x * x; } return tmp; }
function code(x) tmp = 0.0 if (x <= 420000000.0) tmp = fma(-2000000000.0, x, 1e+18); else tmp = Float64(x * x); end return tmp end
code[x_] := If[LessEqual[x, 420000000.0], N[(-2000000000.0 * x + 1e+18), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 420000000:\\ \;\;\;\;\mathsf{fma}\left(-2000000000, x, 10^{+18}\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array}
if x < 4.2e8
Initial program 99.4%
Taylor expanded in x around 0
+-commutative
N/A
lower-fma.f64
24.5
Applied rewrites24.5%
if 4.2e8 < x
Initial program 97.3%
Taylor expanded in x around inf
unpow2
N/A
lower-*.f64
18.4
Applied rewrites18.4%
(FPCore (x) :precision binary64 1e+18)
double code(double x) { return 1e+18; }
real(8) function code(x) real(8), intent (in) :: x code = 1d+18 end function
public static double code(double x) { return 1e+18; }
def code(x): return 1e+18
function code(x) return 1e+18 end
function tmp = code(x) tmp = 1e+18; end
code[x_] := 1e+18
\begin{array}{l} \\ 10^{+18} \end{array}
Initial program 98.0%
Taylor expanded in x around 0
Applied rewrites17.6%
herbie shell --seed 5
(FPCore (x)
:name "x*x + 1e18 - 2*1e9*x"
:precision binary64
:pre (and (<= 100000000.0 x) (<= x 10000000000.0))
(- (+ (* x x) 1e+18) (* (* 2.0 1000000000.0) x)))