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

Percentage Accurate: 76.8% → 80.0%
Time: 1.7s
Alternatives: 4
Speedup: 0.3×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(x \cdot 0.5\right)\\ \frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sin (* x 0.5)))) (/ (* (* (/ 8.0 3.0) t_0) t_0) (sin x))))
double code(double x) {
	double t_0 = sin((x * 0.5));
	return (((8.0 / 3.0) * t_0) * t_0) / sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = sin((x * 0.5d0))
    code = (((8.0d0 / 3.0d0) * t_0) * t_0) / sin(x)
end function
public static double code(double x) {
	double t_0 = Math.sin((x * 0.5));
	return (((8.0 / 3.0) * t_0) * t_0) / Math.sin(x);
}
def code(x):
	t_0 = math.sin((x * 0.5))
	return (((8.0 / 3.0) * t_0) * t_0) / math.sin(x)
function code(x)
	t_0 = sin(Float64(x * 0.5))
	return Float64(Float64(Float64(Float64(8.0 / 3.0) * t_0) * t_0) / sin(x))
end
function tmp = code(x)
	t_0 = sin((x * 0.5));
	tmp = (((8.0 / 3.0) * t_0) * t_0) / sin(x);
end
code[x_] := Block[{t$95$0 = N[Sin[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(8.0 / 3.0), $MachinePrecision] * t$95$0), $MachinePrecision] * t$95$0), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(x \cdot 0.5\right)\\
\frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x}
\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 4 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: 76.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(x \cdot 0.5\right)\\ \frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sin (* x 0.5)))) (/ (* (* (/ 8.0 3.0) t_0) t_0) (sin x))))
double code(double x) {
	double t_0 = sin((x * 0.5));
	return (((8.0 / 3.0) * t_0) * t_0) / sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = sin((x * 0.5d0))
    code = (((8.0d0 / 3.0d0) * t_0) * t_0) / sin(x)
end function
public static double code(double x) {
	double t_0 = Math.sin((x * 0.5));
	return (((8.0 / 3.0) * t_0) * t_0) / Math.sin(x);
}
def code(x):
	t_0 = math.sin((x * 0.5))
	return (((8.0 / 3.0) * t_0) * t_0) / math.sin(x)
function code(x)
	t_0 = sin(Float64(x * 0.5))
	return Float64(Float64(Float64(Float64(8.0 / 3.0) * t_0) * t_0) / sin(x))
end
function tmp = code(x)
	t_0 = sin((x * 0.5));
	tmp = (((8.0 / 3.0) * t_0) * t_0) / sin(x);
end
code[x_] := Block[{t$95$0 = N[Sin[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(8.0 / 3.0), $MachinePrecision] * t$95$0), $MachinePrecision] * t$95$0), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(x \cdot 0.5\right)\\
\frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x}
\end{array}
\end{array}

Alternative 1: 80.0% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(x \cdot 0.5\right)\\ t_1 := \frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x}\\ \mathbf{if}\;t\_1 \leq -4 \cdot 10^{-159}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_1 \leq 0:\\ \;\;\;\;x \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sin (* x 0.5))) (t_1 (/ (* (* (/ 8.0 3.0) t_0) t_0) (sin x))))
   (if (<= t_1 -4e-159) t_1 (if (<= t_1 0.0) (* x 0.5) t_1))))
double code(double x) {
	double t_0 = sin((x * 0.5));
	double t_1 = (((8.0 / 3.0) * t_0) * t_0) / sin(x);
	double tmp;
	if (t_1 <= -4e-159) {
		tmp = t_1;
	} else if (t_1 <= 0.0) {
		tmp = x * 0.5;
	} else {
		tmp = t_1;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = sin((x * 0.5d0))
    t_1 = (((8.0d0 / 3.0d0) * t_0) * t_0) / sin(x)
    if (t_1 <= (-4d-159)) then
        tmp = t_1
    else if (t_1 <= 0.0d0) then
        tmp = x * 0.5d0
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double x) {
	double t_0 = Math.sin((x * 0.5));
	double t_1 = (((8.0 / 3.0) * t_0) * t_0) / Math.sin(x);
	double tmp;
	if (t_1 <= -4e-159) {
		tmp = t_1;
	} else if (t_1 <= 0.0) {
		tmp = x * 0.5;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(x):
	t_0 = math.sin((x * 0.5))
	t_1 = (((8.0 / 3.0) * t_0) * t_0) / math.sin(x)
	tmp = 0
	if t_1 <= -4e-159:
		tmp = t_1
	elif t_1 <= 0.0:
		tmp = x * 0.5
	else:
		tmp = t_1
	return tmp
function code(x)
	t_0 = sin(Float64(x * 0.5))
	t_1 = Float64(Float64(Float64(Float64(8.0 / 3.0) * t_0) * t_0) / sin(x))
	tmp = 0.0
	if (t_1 <= -4e-159)
		tmp = t_1;
	elseif (t_1 <= 0.0)
		tmp = Float64(x * 0.5);
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(x)
	t_0 = sin((x * 0.5));
	t_1 = (((8.0 / 3.0) * t_0) * t_0) / sin(x);
	tmp = 0.0;
	if (t_1 <= -4e-159)
		tmp = t_1;
	elseif (t_1 <= 0.0)
		tmp = x * 0.5;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[x_] := Block[{t$95$0 = N[Sin[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(8.0 / 3.0), $MachinePrecision] * t$95$0), $MachinePrecision] * t$95$0), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -4e-159], t$95$1, If[LessEqual[t$95$1, 0.0], N[(x * 0.5), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(x \cdot 0.5\right)\\
t_1 := \frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x}\\
\mathbf{if}\;t\_1 \leq -4 \cdot 10^{-159}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_1 \leq 0:\\
\;\;\;\;x \cdot 0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (*.f64 (/.f64 #s(literal 8 binary64) #s(literal 3 binary64)) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 x)) < -3.99999999999999995e-159 or -0.0 < (/.f64 (*.f64 (*.f64 (/.f64 #s(literal 8 binary64) #s(literal 3 binary64)) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 x))

    1. Initial program 98.7%

      \[\frac{\left(\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)\right) \cdot \sin \left(x \cdot 0.5\right)}{\sin x} \]
    2. Add Preprocessing

    if -3.99999999999999995e-159 < (/.f64 (*.f64 (*.f64 (/.f64 #s(literal 8 binary64) #s(literal 3 binary64)) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 x)) < -0.0

    1. Initial program 7.1%

      \[\frac{\left(\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)\right) \cdot \sin \left(x \cdot 0.5\right)}{\sin x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{x \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{1}{18} + \frac{1}{180} \cdot {x}^{2}\right)\right)} \]
    4. Applied rewrites18.8%

      \[\leadsto \color{blue}{\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto x \cdot \color{blue}{\left(\frac{4}{3} + {x}^{2} \cdot \left(\frac{1}{1440} \cdot {x}^{2} - \frac{1}{18}\right)\right)} \]
    6. Applied rewrites20.7%

      \[\leadsto x \cdot \color{blue}{0.5} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 15.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(x \cdot 0.5\right)\\ \mathbf{if}\;\frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x} \leq 5 \cdot 10^{-8}:\\ \;\;\;\;x \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{8}{3}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sin (* x 0.5))))
   (if (<= (/ (* (* (/ 8.0 3.0) t_0) t_0) (sin x)) 5e-8)
     (* x 0.5)
     (/ 8.0 3.0))))
double code(double x) {
	double t_0 = sin((x * 0.5));
	double tmp;
	if (((((8.0 / 3.0) * t_0) * t_0) / sin(x)) <= 5e-8) {
		tmp = x * 0.5;
	} else {
		tmp = 8.0 / 3.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = sin((x * 0.5d0))
    if (((((8.0d0 / 3.0d0) * t_0) * t_0) / sin(x)) <= 5d-8) then
        tmp = x * 0.5d0
    else
        tmp = 8.0d0 / 3.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double t_0 = Math.sin((x * 0.5));
	double tmp;
	if (((((8.0 / 3.0) * t_0) * t_0) / Math.sin(x)) <= 5e-8) {
		tmp = x * 0.5;
	} else {
		tmp = 8.0 / 3.0;
	}
	return tmp;
}
def code(x):
	t_0 = math.sin((x * 0.5))
	tmp = 0
	if ((((8.0 / 3.0) * t_0) * t_0) / math.sin(x)) <= 5e-8:
		tmp = x * 0.5
	else:
		tmp = 8.0 / 3.0
	return tmp
function code(x)
	t_0 = sin(Float64(x * 0.5))
	tmp = 0.0
	if (Float64(Float64(Float64(Float64(8.0 / 3.0) * t_0) * t_0) / sin(x)) <= 5e-8)
		tmp = Float64(x * 0.5);
	else
		tmp = Float64(8.0 / 3.0);
	end
	return tmp
end
function tmp_2 = code(x)
	t_0 = sin((x * 0.5));
	tmp = 0.0;
	if (((((8.0 / 3.0) * t_0) * t_0) / sin(x)) <= 5e-8)
		tmp = x * 0.5;
	else
		tmp = 8.0 / 3.0;
	end
	tmp_2 = tmp;
end
code[x_] := Block[{t$95$0 = N[Sin[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(N[(N[(8.0 / 3.0), $MachinePrecision] * t$95$0), $MachinePrecision] * t$95$0), $MachinePrecision] / N[Sin[x], $MachinePrecision]), $MachinePrecision], 5e-8], N[(x * 0.5), $MachinePrecision], N[(8.0 / 3.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(x \cdot 0.5\right)\\
\mathbf{if}\;\frac{\left(\frac{8}{3} \cdot t\_0\right) \cdot t\_0}{\sin x} \leq 5 \cdot 10^{-8}:\\
\;\;\;\;x \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{8}{3}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (*.f64 (/.f64 #s(literal 8 binary64) #s(literal 3 binary64)) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 x)) < 4.9999999999999998e-8

    1. Initial program 70.8%

      \[\frac{\left(\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)\right) \cdot \sin \left(x \cdot 0.5\right)}{\sin x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{x \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{1}{18} + \frac{1}{180} \cdot {x}^{2}\right)\right)} \]
    4. Applied rewrites16.0%

      \[\leadsto \color{blue}{\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto x \cdot \color{blue}{\left(\frac{4}{3} + {x}^{2} \cdot \left(\frac{1}{1440} \cdot {x}^{2} - \frac{1}{18}\right)\right)} \]
    6. Applied rewrites14.4%

      \[\leadsto x \cdot \color{blue}{0.5} \]

    if 4.9999999999999998e-8 < (/.f64 (*.f64 (*.f64 (/.f64 #s(literal 8 binary64) #s(literal 3 binary64)) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 (*.f64 x #s(literal 1/2 binary64)))) (sin.f64 x))

    1. Initial program 98.9%

      \[\frac{\left(\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)\right) \cdot \sin \left(x \cdot 0.5\right)}{\sin x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{x \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{1}{18} + {x}^{2} \cdot \left(\frac{1}{180} + \frac{17}{30240} \cdot {x}^{2}\right)\right)\right)} \]
    4. Applied rewrites18.2%

      \[\leadsto \color{blue}{\frac{8}{3}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 19.7% accurate, 3.4× speedup?

\[\begin{array}{l} \\ \sin x \end{array} \]
(FPCore (x) :precision binary64 (sin x))
double code(double x) {
	return sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sin(x)
end function
public static double code(double x) {
	return Math.sin(x);
}
def code(x):
	return math.sin(x)
function code(x)
	return sin(x)
end
function tmp = code(x)
	tmp = sin(x);
end
code[x_] := N[Sin[x], $MachinePrecision]
\begin{array}{l}

\\
\sin x
\end{array}
Derivation
  1. Initial program 77.3%

    \[\frac{\left(\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)\right) \cdot \sin \left(x \cdot 0.5\right)}{\sin x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{1}{18} + \frac{1}{180} \cdot {x}^{2}\right)\right)} \]
  4. Applied rewrites15.1%

    \[\leadsto \color{blue}{\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto x \cdot \color{blue}{\left(\frac{4}{3} + \frac{-1}{18} \cdot {x}^{2}\right)} \]
  6. Applied rewrites19.8%

    \[\leadsto \sin x \]
  7. Add Preprocessing

Alternative 4: 11.8% accurate, 57.2× speedup?

\[\begin{array}{l} \\ x \cdot 0.5 \end{array} \]
(FPCore (x) :precision binary64 (* x 0.5))
double code(double x) {
	return x * 0.5;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x * 0.5d0
end function
public static double code(double x) {
	return x * 0.5;
}
def code(x):
	return x * 0.5
function code(x)
	return Float64(x * 0.5)
end
function tmp = code(x)
	tmp = x * 0.5;
end
code[x_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}

\\
x \cdot 0.5
\end{array}
Derivation
  1. Initial program 77.3%

    \[\frac{\left(\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)\right) \cdot \sin \left(x \cdot 0.5\right)}{\sin x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{1}{18} + \frac{1}{180} \cdot {x}^{2}\right)\right)} \]
  4. Applied rewrites15.1%

    \[\leadsto \color{blue}{\frac{8}{3} \cdot \sin \left(x \cdot 0.5\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto x \cdot \color{blue}{\left(\frac{4}{3} + {x}^{2} \cdot \left(\frac{1}{1440} \cdot {x}^{2} - \frac{1}{18}\right)\right)} \]
  6. Applied rewrites12.2%

    \[\leadsto x \cdot \color{blue}{0.5} \]
  7. Add Preprocessing

Developer Target 1: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(x \cdot 0.5\right)\\ \frac{\frac{8 \cdot t\_0}{3}}{\frac{\sin x}{t\_0}} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sin (* x 0.5)))) (/ (/ (* 8.0 t_0) 3.0) (/ (sin x) t_0))))
double code(double x) {
	double t_0 = sin((x * 0.5));
	return ((8.0 * t_0) / 3.0) / (sin(x) / t_0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = sin((x * 0.5d0))
    code = ((8.0d0 * t_0) / 3.0d0) / (sin(x) / t_0)
end function
public static double code(double x) {
	double t_0 = Math.sin((x * 0.5));
	return ((8.0 * t_0) / 3.0) / (Math.sin(x) / t_0);
}
def code(x):
	t_0 = math.sin((x * 0.5))
	return ((8.0 * t_0) / 3.0) / (math.sin(x) / t_0)
function code(x)
	t_0 = sin(Float64(x * 0.5))
	return Float64(Float64(Float64(8.0 * t_0) / 3.0) / Float64(sin(x) / t_0))
end
function tmp = code(x)
	t_0 = sin((x * 0.5));
	tmp = ((8.0 * t_0) / 3.0) / (sin(x) / t_0);
end
code[x_] := Block[{t$95$0 = N[Sin[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(8.0 * t$95$0), $MachinePrecision] / 3.0), $MachinePrecision] / N[(N[Sin[x], $MachinePrecision] / t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(x \cdot 0.5\right)\\
\frac{\frac{8 \cdot t\_0}{3}}{\frac{\sin x}{t\_0}}
\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2024321 
(FPCore (x)
  :name "Graphics.Rasterific.Svg.PathConverter:segmentToBezier from rasterific-svg-0.2.3.1, A"
  :precision binary64
  :pre (TRUE)

  :alt
  (! :herbie-platform default (/ (/ (* 8 (sin (* x 1/2))) 3) (/ (sin x) (sin (* x 1/2)))))

  (/ (* (* (/ 8.0 3.0) (sin (* x 0.5))) (sin (* x 0.5))) (sin x)))