Graphics.Rasterific.Svg.PathConverter:segmentToBezier from rasterific-svg-0.2.3.1, C

Percentage Accurate: 99.9% → 99.9%
Time: 9.5s
Alternatives: 12
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(x + \sin y\right) + z \cdot \cos y \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
	return (x + sin(y)) + (z * cos(y));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x + sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
	return (x + Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z):
	return (x + math.sin(y)) + (z * math.cos(y))
function code(x, y, z)
	return Float64(Float64(x + sin(y)) + Float64(z * cos(y)))
end
function tmp = code(x, y, z)
	tmp = (x + sin(y)) + (z * cos(y));
end
code[x_, y_, z_] := N[(N[(x + N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x + \sin y\right) + z \cdot \cos y
\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 12 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.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(x + \sin y\right) + z \cdot \cos y \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ x (sin y)) (* z (cos y))))
double code(double x, double y, double z) {
	return (x + sin(y)) + (z * cos(y));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x + sin(y)) + (z * cos(y))
end function
public static double code(double x, double y, double z) {
	return (x + Math.sin(y)) + (z * Math.cos(y));
}
def code(x, y, z):
	return (x + math.sin(y)) + (z * math.cos(y))
function code(x, y, z)
	return Float64(Float64(x + sin(y)) + Float64(z * cos(y)))
end
function tmp = code(x, y, z)
	tmp = (x + sin(y)) + (z * cos(y));
end
code[x_, y_, z_] := N[(N[(x + N[Sin[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Cos[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x + \sin y\right) + z \cdot \cos y
\end{array}

Alternative 1: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\cos y, z, x + \sin y\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma (cos y) z (+ x (sin y))))
double code(double x, double y, double z) {
	return fma(cos(y), z, (x + sin(y)));
}
function code(x, y, z)
	return fma(cos(y), z, Float64(x + sin(y)))
end
code[x_, y_, z_] := N[(N[Cos[y], $MachinePrecision] * z + N[(x + N[Sin[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\cos y, z, x + \sin y\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x + \sin y\right) + z \cdot \cos y \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(x + \sin y\right) + z \cdot \cos y} \]
    2. +-commutativeN/A

      \[\leadsto \color{blue}{z \cdot \cos y + \left(x + \sin y\right)} \]
    3. lift-*.f64N/A

      \[\leadsto \color{blue}{z \cdot \cos y} + \left(x + \sin y\right) \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{\cos y \cdot z} + \left(x + \sin y\right) \]
    5. lower-fma.f6499.9

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos y, z, x + \sin y\right)} \]
  4. Applied rewrites99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos y, z, x + \sin y\right)} \]
  5. Add Preprocessing

Alternative 2: 95.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos y \cdot z\\ t_1 := \mathsf{fma}\left(x, \frac{t\_0}{x}, x\right)\\ \mathbf{if}\;x \leq -2.8 \cdot 10^{-34}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;x \leq 1.8 \cdot 10^{-18}:\\ \;\;\;\;t\_0 + \sin y\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* (cos y) z)) (t_1 (fma x (/ t_0 x) x)))
   (if (<= x -2.8e-34) t_1 (if (<= x 1.8e-18) (+ t_0 (sin y)) t_1))))
double code(double x, double y, double z) {
	double t_0 = cos(y) * z;
	double t_1 = fma(x, (t_0 / x), x);
	double tmp;
	if (x <= -2.8e-34) {
		tmp = t_1;
	} else if (x <= 1.8e-18) {
		tmp = t_0 + sin(y);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(cos(y) * z)
	t_1 = fma(x, Float64(t_0 / x), x)
	tmp = 0.0
	if (x <= -2.8e-34)
		tmp = t_1;
	elseif (x <= 1.8e-18)
		tmp = Float64(t_0 + sin(y));
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[Cos[y], $MachinePrecision] * z), $MachinePrecision]}, Block[{t$95$1 = N[(x * N[(t$95$0 / x), $MachinePrecision] + x), $MachinePrecision]}, If[LessEqual[x, -2.8e-34], t$95$1, If[LessEqual[x, 1.8e-18], N[(t$95$0 + N[Sin[y], $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos y \cdot z\\
t_1 := \mathsf{fma}\left(x, \frac{t\_0}{x}, x\right)\\
\mathbf{if}\;x \leq -2.8 \cdot 10^{-34}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;x \leq 1.8 \cdot 10^{-18}:\\
\;\;\;\;t\_0 + \sin y\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2.79999999999999997e-34 or 1.80000000000000005e-18 < x

    1. Initial program 99.9%

      \[\left(x + \sin y\right) + z \cdot \cos y \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \color{blue}{x + z} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{z + x} \]
      2. lower-+.f6485.3

        \[\leadsto \color{blue}{z + x} \]
    5. Applied rewrites85.3%

      \[\leadsto \color{blue}{z + x} \]
    6. Taylor expanded in x around -inf

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x} - 1\right)\right)} \]
    7. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot \left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x} - 1\right)\right)} \]
      2. sub-negN/A

        \[\leadsto \mathsf{neg}\left(x \cdot \color{blue}{\left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x} + \left(\mathsf{neg}\left(1\right)\right)\right)}\right) \]
      3. metadata-evalN/A

        \[\leadsto \mathsf{neg}\left(x \cdot \left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x} + \color{blue}{-1}\right)\right) \]
      4. distribute-lft-inN/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{\left(x \cdot \left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x}\right) + x \cdot -1\right)}\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{neg}\left(\left(x \cdot \left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x}\right) + \color{blue}{-1 \cdot x}\right)\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(x \cdot \left(-1 \cdot \frac{\sin y + z \cdot \cos y}{x}\right)\right)\right) + \left(\mathsf{neg}\left(-1 \cdot x\right)\right)} \]
      7. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(x \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{\sin y + z \cdot \cos y}{x}\right)\right)}\right)\right) + \left(\mathsf{neg}\left(-1 \cdot x\right)\right) \]
      8. distribute-rgt-neg-outN/A

        \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x \cdot \frac{\sin y + z \cdot \cos y}{x}\right)\right)}\right)\right) + \left(\mathsf{neg}\left(-1 \cdot x\right)\right) \]
      9. remove-double-negN/A

        \[\leadsto \color{blue}{x \cdot \frac{\sin y + z \cdot \cos y}{x}} + \left(\mathsf{neg}\left(-1 \cdot x\right)\right) \]
      10. mul-1-negN/A

        \[\leadsto x \cdot \frac{\sin y + z \cdot \cos y}{x} + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)\right) \]
      11. remove-double-negN/A

        \[\leadsto x \cdot \frac{\sin y + z \cdot \cos y}{x} + \color{blue}{x} \]
      12. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{\sin y + z \cdot \cos y}{x}, x\right)} \]
    8. Applied rewrites99.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{\mathsf{fma}\left(z, \cos y, \sin y\right)}{x}, x\right)} \]
    9. Taylor expanded in z around inf

      \[\leadsto \mathsf{fma}\left(x, \frac{z \cdot \cos y}{x}, x\right) \]
    10. Step-by-step derivation
      1. Applied rewrites97.1%

        \[\leadsto \mathsf{fma}\left(x, \frac{z \cdot \cos y}{x}, x\right) \]

      if -2.79999999999999997e-34 < x < 1.80000000000000005e-18

      1. Initial program 99.9%

        \[\left(x + \sin y\right) + z \cdot \cos y \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\sin y} + z \cdot \cos y \]
      4. Step-by-step derivation
        1. lower-sin.f6493.0

          \[\leadsto \color{blue}{\sin y} + z \cdot \cos y \]
      5. Applied rewrites93.0%

        \[\leadsto \color{blue}{\sin y} + z \cdot \cos y \]
    11. Recombined 2 regimes into one program.
    12. Final simplification95.2%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.8 \cdot 10^{-34}:\\ \;\;\;\;\mathsf{fma}\left(x, \frac{\cos y \cdot z}{x}, x\right)\\ \mathbf{elif}\;x \leq 1.8 \cdot 10^{-18}:\\ \;\;\;\;\cos y \cdot z + \sin y\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, \frac{\cos y \cdot z}{x}, x\right)\\ \end{array} \]
    13. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2024233 
    (FPCore (x y z)
      :name "Graphics.Rasterific.Svg.PathConverter:segmentToBezier from rasterific-svg-0.2.3.1, C"
      :precision binary64
      (+ (+ x (sin y)) (* z (cos y))))