Data.Colour.CIE:cieLABView from colour-2.3.3, A

Percentage Accurate: 99.9% → 99.9%
Time: 4.3s
Alternatives: 3
Speedup: 4.4×

Specification

?
\[\begin{array}{l} \\ \frac{841}{108} \cdot x + \frac{4}{29} \end{array} \]
(FPCore (x) :precision binary64 (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))
double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((841.0d0 / 108.0d0) * x) + (4.0d0 / 29.0d0)
end function
public static double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
def code(x):
	return ((841.0 / 108.0) * x) + (4.0 / 29.0)
function code(x)
	return Float64(Float64(Float64(841.0 / 108.0) * x) + Float64(4.0 / 29.0))
end
function tmp = code(x)
	tmp = ((841.0 / 108.0) * x) + (4.0 / 29.0);
end
code[x_] := N[(N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision] + N[(4.0 / 29.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{841}{108} \cdot x + \frac{4}{29}
\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 3 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} \\ \frac{841}{108} \cdot x + \frac{4}{29} \end{array} \]
(FPCore (x) :precision binary64 (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))
double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((841.0d0 / 108.0d0) * x) + (4.0d0 / 29.0d0)
end function
public static double code(double x) {
	return ((841.0 / 108.0) * x) + (4.0 / 29.0);
}
def code(x):
	return ((841.0 / 108.0) * x) + (4.0 / 29.0)
function code(x)
	return Float64(Float64(Float64(841.0 / 108.0) * x) + Float64(4.0 / 29.0))
end
function tmp = code(x)
	tmp = ((841.0 / 108.0) * x) + (4.0 / 29.0);
end
code[x_] := N[(N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision] + N[(4.0 / 29.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{841}{108} \cdot x + \frac{4}{29}
\end{array}

Alternative 1: 99.9% accurate, 4.4× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, 7.787037037037037, 0.13793103448275862\right) \end{array} \]
(FPCore (x) :precision binary64 (fma x 7.787037037037037 0.13793103448275862))
double code(double x) {
	return fma(x, 7.787037037037037, 0.13793103448275862);
}
function code(x)
	return fma(x, 7.787037037037037, 0.13793103448275862)
end
code[x_] := N[(x * 7.787037037037037 + 0.13793103448275862), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, 7.787037037037037, 0.13793103448275862\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\frac{841}{108} \cdot x + \frac{4}{29} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\frac{841}{108} \cdot x + \frac{4}{29}} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{\frac{841}{108} \cdot x} + \frac{4}{29} \]
    3. *-commutativeN/A

      \[\leadsto \color{blue}{x \cdot \frac{841}{108}} + \frac{4}{29} \]
    4. lower-fma.f6499.9

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{841}{108}, \frac{4}{29}\right)} \]
    5. lift-/.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{841}{108}}, \frac{4}{29}\right) \]
    6. metadata-eval99.9

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{7.787037037037037}, \frac{4}{29}\right) \]
    7. lift-/.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \frac{841}{108}, \color{blue}{\frac{4}{29}}\right) \]
    8. metadata-eval99.9

      \[\leadsto \mathsf{fma}\left(x, 7.787037037037037, \color{blue}{0.13793103448275862}\right) \]
  4. Applied rewrites99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, 7.787037037037037, 0.13793103448275862\right)} \]
  5. Add Preprocessing

Alternative 2: 97.8% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.017 \lor \neg \left(x \leq 0.018\right):\\ \;\;\;\;7.787037037037037 \cdot x\\ \mathbf{else}:\\ \;\;\;\;0.13793103448275862\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (or (<= x -0.017) (not (<= x 0.018)))
   (* 7.787037037037037 x)
   0.13793103448275862))
double code(double x) {
	double tmp;
	if ((x <= -0.017) || !(x <= 0.018)) {
		tmp = 7.787037037037037 * x;
	} else {
		tmp = 0.13793103448275862;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x <= (-0.017d0)) .or. (.not. (x <= 0.018d0))) then
        tmp = 7.787037037037037d0 * x
    else
        tmp = 0.13793103448275862d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x <= -0.017) || !(x <= 0.018)) {
		tmp = 7.787037037037037 * x;
	} else {
		tmp = 0.13793103448275862;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x <= -0.017) or not (x <= 0.018):
		tmp = 7.787037037037037 * x
	else:
		tmp = 0.13793103448275862
	return tmp
function code(x)
	tmp = 0.0
	if ((x <= -0.017) || !(x <= 0.018))
		tmp = Float64(7.787037037037037 * x);
	else
		tmp = 0.13793103448275862;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x <= -0.017) || ~((x <= 0.018)))
		tmp = 7.787037037037037 * x;
	else
		tmp = 0.13793103448275862;
	end
	tmp_2 = tmp;
end
code[x_] := If[Or[LessEqual[x, -0.017], N[Not[LessEqual[x, 0.018]], $MachinePrecision]], N[(7.787037037037037 * x), $MachinePrecision], 0.13793103448275862]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.017 \lor \neg \left(x \leq 0.018\right):\\
\;\;\;\;7.787037037037037 \cdot x\\

\mathbf{else}:\\
\;\;\;\;0.13793103448275862\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -0.017000000000000001 or 0.0179999999999999986 < x

    1. Initial program 99.8%

      \[\frac{841}{108} \cdot x + \frac{4}{29} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{\frac{841}{108} \cdot x + \frac{4}{29}} \]
      2. unpow1N/A

        \[\leadsto \color{blue}{{\left(\frac{841}{108} \cdot x\right)}^{1}} + \frac{4}{29} \]
      3. metadata-evalN/A

        \[\leadsto {\left(\frac{841}{108} \cdot x\right)}^{\color{blue}{\left(\frac{2}{2}\right)}} + \frac{4}{29} \]
      4. sqrt-pow1N/A

        \[\leadsto \color{blue}{\sqrt{{\left(\frac{841}{108} \cdot x\right)}^{2}}} + \frac{4}{29} \]
      5. pow2N/A

        \[\leadsto \sqrt{\color{blue}{\left(\frac{841}{108} \cdot x\right) \cdot \left(\frac{841}{108} \cdot x\right)}} + \frac{4}{29} \]
      6. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{\left(\frac{841}{108} \cdot x\right)} \cdot \left(\frac{841}{108} \cdot x\right)} + \frac{4}{29} \]
      7. associate-*l*N/A

        \[\leadsto \sqrt{\color{blue}{\frac{841}{108} \cdot \left(x \cdot \left(\frac{841}{108} \cdot x\right)\right)}} + \frac{4}{29} \]
      8. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(x \cdot \left(\frac{841}{108} \cdot x\right)\right) \cdot \frac{841}{108}}} + \frac{4}{29} \]
      9. sqrt-prodN/A

        \[\leadsto \color{blue}{\sqrt{x \cdot \left(\frac{841}{108} \cdot x\right)} \cdot \sqrt{\frac{841}{108}}} + \frac{4}{29} \]
      10. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{x \cdot \left(\frac{841}{108} \cdot x\right)}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right)} \]
      11. lower-sqrt.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\sqrt{x \cdot \left(\frac{841}{108} \cdot x\right)}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      12. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{x \cdot \color{blue}{\left(\frac{841}{108} \cdot x\right)}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      13. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\sqrt{x \cdot \color{blue}{\left(x \cdot \frac{841}{108}\right)}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      14. associate-*r*N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{\left(x \cdot x\right) \cdot \frac{841}{108}}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      15. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{\left(x \cdot x\right) \cdot \frac{841}{108}}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      16. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{\left(x \cdot x\right)} \cdot \frac{841}{108}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      17. lift-/.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot \color{blue}{\frac{841}{108}}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      18. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot \color{blue}{\frac{841}{108}}}, \sqrt{\frac{841}{108}}, \frac{4}{29}\right) \]
      19. lower-sqrt.f6423.4

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot 7.787037037037037}, \color{blue}{\sqrt{\frac{841}{108}}}, \frac{4}{29}\right) \]
      20. lift-/.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot \frac{841}{108}}, \sqrt{\color{blue}{\frac{841}{108}}}, \frac{4}{29}\right) \]
      21. metadata-eval23.4

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot 7.787037037037037}, \sqrt{\color{blue}{7.787037037037037}}, \frac{4}{29}\right) \]
      22. lift-/.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot \frac{841}{108}}, \sqrt{\frac{841}{108}}, \color{blue}{\frac{4}{29}}\right) \]
      23. metadata-eval23.4

        \[\leadsto \mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot 7.787037037037037}, \sqrt{7.787037037037037}, \color{blue}{0.13793103448275862}\right) \]
    4. Applied rewrites23.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{\left(x \cdot x\right) \cdot 7.787037037037037}, \sqrt{7.787037037037037}, 0.13793103448275862\right)} \]
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{x \cdot {\left(\sqrt{\frac{841}{108}}\right)}^{2}} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{{\left(\sqrt{\frac{841}{108}}\right)}^{2} \cdot x} \]
      2. unpow2N/A

        \[\leadsto \color{blue}{\left(\sqrt{\frac{841}{108}} \cdot \sqrt{\frac{841}{108}}\right)} \cdot x \]
      3. rem-square-sqrtN/A

        \[\leadsto \color{blue}{\frac{841}{108}} \cdot x \]
      4. lower-*.f6499.2

        \[\leadsto \color{blue}{7.787037037037037 \cdot x} \]
    7. Applied rewrites99.2%

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

    if -0.017000000000000001 < x < 0.0179999999999999986

    1. Initial program 100.0%

      \[\frac{841}{108} \cdot x + \frac{4}{29} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{4}{29}} \]
    4. Step-by-step derivation
      1. Applied rewrites98.9%

        \[\leadsto \color{blue}{0.13793103448275862} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification99.1%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.017 \lor \neg \left(x \leq 0.018\right):\\ \;\;\;\;7.787037037037037 \cdot x\\ \mathbf{else}:\\ \;\;\;\;0.13793103448275862\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 50.5% accurate, 31.0× speedup?

    \[\begin{array}{l} \\ 0.13793103448275862 \end{array} \]
    (FPCore (x) :precision binary64 0.13793103448275862)
    double code(double x) {
    	return 0.13793103448275862;
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = 0.13793103448275862d0
    end function
    
    public static double code(double x) {
    	return 0.13793103448275862;
    }
    
    def code(x):
    	return 0.13793103448275862
    
    function code(x)
    	return 0.13793103448275862
    end
    
    function tmp = code(x)
    	tmp = 0.13793103448275862;
    end
    
    code[x_] := 0.13793103448275862
    
    \begin{array}{l}
    
    \\
    0.13793103448275862
    \end{array}
    
    Derivation
    1. Initial program 99.9%

      \[\frac{841}{108} \cdot x + \frac{4}{29} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{4}{29}} \]
    4. Step-by-step derivation
      1. Applied rewrites46.4%

        \[\leadsto \color{blue}{0.13793103448275862} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024342 
      (FPCore (x)
        :name "Data.Colour.CIE:cieLABView from colour-2.3.3, A"
        :precision binary64
        (+ (* (/ 841.0 108.0) x) (/ 4.0 29.0)))