fma_test1

Percentage Accurate: 3.4% → 99.4%
Time: 3.2s
Alternatives: 2
Speedup: 3.8×

Specification

?
\[0.9 \leq t \land t \leq 1.1\]
\[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\ t_1 \cdot t_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \end{array} \end{array} \]
(FPCore (t)
 :precision binary64
 (let* ((t_1 (+ 1.0 (* t 2e-16))))
   (+ (* t_1 t_1) (- -1.0 (* 2.0 (* t 2e-16))))))
double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
real(8) function code(t)
    real(8), intent (in) :: t
    real(8) :: t_1
    t_1 = 1.0d0 + (t * 2d-16)
    code = (t_1 * t_1) + ((-1.0d0) - (2.0d0 * (t * 2d-16)))
end function
public static double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
def code(t):
	t_1 = 1.0 + (t * 2e-16)
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)))
function code(t)
	t_1 = Float64(1.0 + Float64(t * 2e-16))
	return Float64(Float64(t_1 * t_1) + Float64(-1.0 - Float64(2.0 * Float64(t * 2e-16))))
end
function tmp = code(t)
	t_1 = 1.0 + (t * 2e-16);
	tmp = (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * t$95$1), $MachinePrecision] + N[(-1.0 - N[(2.0 * N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\
t_1 \cdot t_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)
\end{array}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 2 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 3.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\ t_1 \cdot t_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \end{array} \end{array} \]
(FPCore (t)
 :precision binary64
 (let* ((t_1 (+ 1.0 (* t 2e-16))))
   (+ (* t_1 t_1) (- -1.0 (* 2.0 (* t 2e-16))))))
double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
real(8) function code(t)
    real(8), intent (in) :: t
    real(8) :: t_1
    t_1 = 1.0d0 + (t * 2d-16)
    code = (t_1 * t_1) + ((-1.0d0) - (2.0d0 * (t * 2d-16)))
end function
public static double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
def code(t):
	t_1 = 1.0 + (t * 2e-16)
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)))
function code(t)
	t_1 = Float64(1.0 + Float64(t * 2e-16))
	return Float64(Float64(t_1 * t_1) + Float64(-1.0 - Float64(2.0 * Float64(t * 2e-16))))
end
function tmp = code(t)
	t_1 = 1.0 + (t * 2e-16);
	tmp = (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * t$95$1), $MachinePrecision] + N[(-1.0 - N[(2.0 * N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\
t_1 \cdot t_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)
\end{array}
\end{array}

Alternative 1: 99.4% accurate, 3.8× speedup?

\[\begin{array}{l} \\ t \cdot \left(t \cdot 4 \cdot 10^{-32}\right) \end{array} \]
(FPCore (t) :precision binary64 (* t (* t 4e-32)))
double code(double t) {
	return t * (t * 4e-32);
}
real(8) function code(t)
    real(8), intent (in) :: t
    code = t * (t * 4d-32)
end function
public static double code(double t) {
	return t * (t * 4e-32);
}
def code(t):
	return t * (t * 4e-32)
function code(t)
	return Float64(t * Float64(t * 4e-32))
end
function tmp = code(t)
	tmp = t * (t * 4e-32);
end
code[t_] := N[(t * N[(t * 4e-32), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
t \cdot \left(t \cdot 4 \cdot 10^{-32}\right)
\end{array}
Derivation
  1. Initial program 3.4%

    \[\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \]
  2. Step-by-step derivation
    1. associate-+r-9.9%

      \[\leadsto \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + -1\right) - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)} \]
    2. sub-neg9.9%

      \[\leadsto \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + -1\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)} \]
    3. +-commutative9.9%

      \[\leadsto \color{blue}{\left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + -1\right)} \]
    4. difference-of-sqr--19.9%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) - 1\right)} \]
    5. +-commutative9.9%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \left(\color{blue}{\left(t \cdot 2 \cdot 10^{-16} + 1\right)} - 1\right) \]
    6. associate--l+3.4%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \color{blue}{\left(t \cdot 2 \cdot 10^{-16} + \left(1 - 1\right)\right)} \]
    7. metadata-eval3.4%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \left(t \cdot 2 \cdot 10^{-16} + \color{blue}{0}\right) \]
    8. +-rgt-identity3.4%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \color{blue}{\left(t \cdot 2 \cdot 10^{-16}\right)} \]
    9. distribute-lft1-in20.8%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(t \cdot 2 \cdot 10^{-16}\right) + t \cdot 2 \cdot 10^{-16}\right)} \]
    10. *-commutative20.8%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\color{blue}{\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right)} + t \cdot 2 \cdot 10^{-16}\right) \]
    11. associate-+r+20.8%

      \[\leadsto \color{blue}{\left(\left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right)\right) + t \cdot 2 \cdot 10^{-16}} \]
    12. +-commutative20.8%

      \[\leadsto \color{blue}{\left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)\right)} + t \cdot 2 \cdot 10^{-16} \]
    13. +-commutative20.8%

      \[\leadsto \color{blue}{t \cdot 2 \cdot 10^{-16} + \left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)\right)} \]
    14. *-rgt-identity20.8%

      \[\leadsto \color{blue}{\left(t \cdot 2 \cdot 10^{-16}\right) \cdot 1} + \left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)\right) \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{t \cdot \left(t \cdot 4 \cdot 10^{-32}\right)} \]
  4. Final simplification99.5%

    \[\leadsto t \cdot \left(t \cdot 4 \cdot 10^{-32}\right) \]

Alternative 2: 99.3% accurate, 3.8× speedup?

\[\begin{array}{l} \\ 4 \cdot 10^{-32} \cdot \left(t \cdot t\right) \end{array} \]
(FPCore (t) :precision binary64 (* 4e-32 (* t t)))
double code(double t) {
	return 4e-32 * (t * t);
}
real(8) function code(t)
    real(8), intent (in) :: t
    code = 4d-32 * (t * t)
end function
public static double code(double t) {
	return 4e-32 * (t * t);
}
def code(t):
	return 4e-32 * (t * t)
function code(t)
	return Float64(4e-32 * Float64(t * t))
end
function tmp = code(t)
	tmp = 4e-32 * (t * t);
end
code[t_] := N[(4e-32 * N[(t * t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
4 \cdot 10^{-32} \cdot \left(t \cdot t\right)
\end{array}
Derivation
  1. Initial program 3.4%

    \[\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \]
  2. Step-by-step derivation
    1. associate-+r-9.9%

      \[\leadsto \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + -1\right) - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)} \]
    2. sub-neg9.9%

      \[\leadsto \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + -1\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)} \]
    3. +-commutative9.9%

      \[\leadsto \color{blue}{\left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + -1\right)} \]
    4. difference-of-sqr--19.9%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) - 1\right)} \]
    5. +-commutative9.9%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \left(\color{blue}{\left(t \cdot 2 \cdot 10^{-16} + 1\right)} - 1\right) \]
    6. associate--l+3.4%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \color{blue}{\left(t \cdot 2 \cdot 10^{-16} + \left(1 - 1\right)\right)} \]
    7. metadata-eval3.4%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \left(t \cdot 2 \cdot 10^{-16} + \color{blue}{0}\right) \]
    8. +-rgt-identity3.4%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) + 1\right) \cdot \color{blue}{\left(t \cdot 2 \cdot 10^{-16}\right)} \]
    9. distribute-lft1-in20.8%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \color{blue}{\left(\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(t \cdot 2 \cdot 10^{-16}\right) + t \cdot 2 \cdot 10^{-16}\right)} \]
    10. *-commutative20.8%

      \[\leadsto \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(\color{blue}{\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right)} + t \cdot 2 \cdot 10^{-16}\right) \]
    11. associate-+r+20.8%

      \[\leadsto \color{blue}{\left(\left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) + \left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right)\right) + t \cdot 2 \cdot 10^{-16}} \]
    12. +-commutative20.8%

      \[\leadsto \color{blue}{\left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)\right)} + t \cdot 2 \cdot 10^{-16} \]
    13. +-commutative20.8%

      \[\leadsto \color{blue}{t \cdot 2 \cdot 10^{-16} + \left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)\right)} \]
    14. *-rgt-identity20.8%

      \[\leadsto \color{blue}{\left(t \cdot 2 \cdot 10^{-16}\right) \cdot 1} + \left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)\right) \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{t \cdot \left(t \cdot 4 \cdot 10^{-32}\right)} \]
  4. Taylor expanded in t around 0 99.4%

    \[\leadsto \color{blue}{4 \cdot 10^{-32} \cdot {t}^{2}} \]
  5. Step-by-step derivation
    1. unpow299.4%

      \[\leadsto 4 \cdot 10^{-32} \cdot \color{blue}{\left(t \cdot t\right)} \]
  6. Simplified99.4%

    \[\leadsto \color{blue}{4 \cdot 10^{-32} \cdot \left(t \cdot t\right)} \]
  7. Final simplification99.4%

    \[\leadsto 4 \cdot 10^{-32} \cdot \left(t \cdot t\right) \]

Developer target: 21.0% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\ \mathsf{fma}\left(t_1, t_1, -1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \end{array} \end{array} \]
(FPCore (t)
 :precision binary64
 (let* ((t_1 (+ 1.0 (* t 2e-16)))) (fma t_1 t_1 (- -1.0 (* 2.0 (* t 2e-16))))))
double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return fma(t_1, t_1, (-1.0 - (2.0 * (t * 2e-16))));
}
function code(t)
	t_1 = Float64(1.0 + Float64(t * 2e-16))
	return fma(t_1, t_1, Float64(-1.0 - Float64(2.0 * Float64(t * 2e-16))))
end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]}, N[(t$95$1 * t$95$1 + N[(-1.0 - N[(2.0 * N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\
\mathsf{fma}\left(t_1, t_1, -1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)
\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2023178 
(FPCore (t)
  :name "fma_test1"
  :precision binary64
  :pre (and (<= 0.9 t) (<= t 1.1))

  :herbie-target
  (fma (+ 1.0 (* t 2e-16)) (+ 1.0 (* t 2e-16)) (- -1.0 (* 2.0 (* t 2e-16))))

  (+ (* (+ 1.0 (* t 2e-16)) (+ 1.0 (* t 2e-16))) (- -1.0 (* 2.0 (* t 2e-16)))))