(FPCore (x) :precision binary64 (- (+ (* x x) 1e+18) (* 1000000000.0 x)))
double code(double x) { return ((x * x) + 1e+18) - (1000000000.0 * x); }
real(8) function code(x) real(8), intent (in) :: x code = ((x * x) + 1d+18) - (1000000000.0d0 * x) end function
public static double code(double x) { return ((x * x) + 1e+18) - (1000000000.0 * x); }
def code(x): return ((x * x) + 1e+18) - (1000000000.0 * x)
function code(x) return Float64(Float64(Float64(x * x) + 1e+18) - Float64(1000000000.0 * x)) end
function tmp = code(x) tmp = ((x * x) + 1e+18) - (1000000000.0 * x); end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] + 1e+18), $MachinePrecision] - N[(1000000000.0 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(x \cdot x + 10^{+18}\right) - 1000000000 \cdot x \end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (x) :precision binary64 (- (+ (* x x) 1e+18) (* 1000000000.0 x)))
double code(double x) { return ((x * x) + 1e+18) - (1000000000.0 * x); }
real(8) function code(x) real(8), intent (in) :: x code = ((x * x) + 1d+18) - (1000000000.0d0 * x) end function
public static double code(double x) { return ((x * x) + 1e+18) - (1000000000.0 * x); }
def code(x): return ((x * x) + 1e+18) - (1000000000.0 * x)
function code(x) return Float64(Float64(Float64(x * x) + 1e+18) - Float64(1000000000.0 * x)) end
function tmp = code(x) tmp = ((x * x) + 1e+18) - (1000000000.0 * x); end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] + 1e+18), $MachinePrecision] - N[(1000000000.0 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(x \cdot x + 10^{+18}\right) - 1000000000 \cdot x \end{array}
(FPCore (x) :precision binary64 (fma x (- x 1000000000.0) 1e+18))
double code(double x) { return fma(x, (x - 1000000000.0), 1e+18); }
function code(x) return fma(x, Float64(x - 1000000000.0), 1e+18) end
code[x_] := N[(x * N[(x - 1000000000.0), $MachinePrecision] + 1e+18), $MachinePrecision]
\begin{array}{l} \\ \mathsf{fma}\left(x, x - 1000000000, 10^{+18}\right) \end{array}
Initial program 99.4%
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
100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (if (<= x 620000000.0) (fma -1000000000.0 x 1e+18) (* x x)))
double code(double x) { double tmp; if (x <= 620000000.0) { tmp = fma(-1000000000.0, x, 1e+18); } else { tmp = x * x; } return tmp; }
function code(x) tmp = 0.0 if (x <= 620000000.0) tmp = fma(-1000000000.0, x, 1e+18); else tmp = Float64(x * x); end return tmp end
code[x_] := If[LessEqual[x, 620000000.0], N[(-1000000000.0 * x + 1e+18), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 620000000:\\ \;\;\;\;\mathsf{fma}\left(-1000000000, x, 10^{+18}\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array}
if x < 6.2e8
Initial program 99.4%
Taylor expanded in x around 0
+-commutative
N/A
lower-fma.f64
23.8
Applied rewrites23.8%
if 6.2e8 < x
Initial program 99.4%
Taylor expanded in x around inf
unpow2
N/A
lower-*.f64
21.7
Applied rewrites21.7%
(FPCore (x) :precision binary64 (if (<= x 1000000000.0) 1e+18 (* x x)))
double code(double x) { double tmp; if (x <= 1000000000.0) { tmp = 1e+18; } else { tmp = x * x; } return tmp; }
real(8) function code(x) real(8), intent (in) :: x real(8) :: tmp if (x <= 1000000000.0d0) then tmp = 1d+18 else tmp = x * x end if code = tmp end function
public static double code(double x) { double tmp; if (x <= 1000000000.0) { tmp = 1e+18; } else { tmp = x * x; } return tmp; }
def code(x): tmp = 0 if x <= 1000000000.0: tmp = 1e+18 else: tmp = x * x return tmp
function code(x) tmp = 0.0 if (x <= 1000000000.0) tmp = 1e+18; else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1000000000.0) tmp = 1e+18; else tmp = x * x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1000000000.0], 1e+18, N[(x * x), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 1000000000:\\ \;\;\;\;10^{+18}\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array}
if x < 1e9
Initial program 99.3%
Taylor expanded in x around 0
Applied rewrites21.6%
if 1e9 < x
Initial program 99.4%
Taylor expanded in x around inf
unpow2
N/A
lower-*.f64
21.8
Applied rewrites21.8%
(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 99.4%
Taylor expanded in x around 0
Applied rewrites19.3%
herbie shell --seed 5
(FPCore (x)
:name "x*x + 1e18 - 1e9*x"
:precision binary64
:pre (and (<= 100000000.0 x) (<= x 10000000000.0))
(- (+ (* x x) 1e+18) (* 1000000000.0 x)))