FastMath test2

Percentage Accurate: 99.7% → 99.7%
Time: 2.3s
Alternatives: 1
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \end{array} \]
(FPCore (d1 d2) :precision binary64 (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
real(8) function code(d1, d2)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2):
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2)
	return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0))
end
function tmp = code(d1, d2)
	tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20
\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 1 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: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \end{array} \]
(FPCore (d1 d2) :precision binary64 (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
real(8) function code(d1, d2)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2):
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2)
	return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0))
end
function tmp = code(d1, d2)
	tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20
\end{array}

Alternative 1: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \end{array} \]
(FPCore (d1 d2) :precision binary64 (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
real(8) function code(d1, d2)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2):
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2)
	return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0))
end
function tmp = code(d1, d2)
	tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20
\end{array}
Derivation
  1. Initial program 99.3%

    \[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
  2. Add Preprocessing

Developer Target 1: 100.0% accurate, 2.2× speedup?

\[\begin{array}{l} \\ d1 \cdot \left(30 + d2\right) \end{array} \]
(FPCore (d1 d2) :precision binary64 (* d1 (+ 30.0 d2)))
double code(double d1, double d2) {
	return d1 * (30.0 + d2);
}
real(8) function code(d1, d2)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = d1 * (30.0d0 + d2)
end function
public static double code(double d1, double d2) {
	return d1 * (30.0 + d2);
}
def code(d1, d2):
	return d1 * (30.0 + d2)
function code(d1, d2)
	return Float64(d1 * Float64(30.0 + d2))
end
function tmp = code(d1, d2)
	tmp = d1 * (30.0 + d2);
end
code[d1_, d2_] := N[(d1 * N[(30.0 + d2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot \left(30 + d2\right)
\end{array}

Reproduce

?
herbie shell --seed 2024180 -o setup:simplify
(FPCore (d1 d2)
  :name "FastMath test2"
  :precision binary64

  :alt
  (! :herbie-platform default (* d1 (+ 30 d2)))

  (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))