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

Percentage Accurate: 99.9% → 99.7%
Time: 3.2s
Alternatives: 4
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 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: 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.7% accurate, 0.8× speedup?

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

\\
\mathsf{fma}\left(x, \frac{x}{-7.787037037037037 \cdot x} \cdot -60.63794581618656, \frac{-0.019024970273483946}{-0.13793103448275862}\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[\frac{841}{108} \cdot x + \frac{4}{29} \]
  2. Add Preprocessing
  3. Applied rewrites99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, -60.63794581618656 \cdot \frac{x}{\mathsf{fma}\left(-7.787037037037037, x, 0.13793103448275862\right)}, \frac{-0.019024970273483946}{\mathsf{fma}\left(x, 7.787037037037037, -0.13793103448275862\right)}\right)} \]
  4. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(x, \frac{-707281}{11664} \cdot \frac{x}{\mathsf{fma}\left(\frac{-841}{108}, x, \frac{4}{29}\right)}, \frac{\frac{-16}{841}}{\color{blue}{\frac{-4}{29}}}\right) \]
  5. Step-by-step derivation
    1. Applied rewrites97.4%

      \[\leadsto \mathsf{fma}\left(x, -60.63794581618656 \cdot \frac{x}{\mathsf{fma}\left(-7.787037037037037, x, 0.13793103448275862\right)}, \frac{-0.019024970273483946}{\color{blue}{-0.13793103448275862}}\right) \]
    2. Taylor expanded in x around inf

      \[\leadsto \mathsf{fma}\left(x, \frac{-707281}{11664} \cdot \frac{x}{\color{blue}{\frac{-841}{108} \cdot x}}, \frac{\frac{-16}{841}}{\frac{-4}{29}}\right) \]
    3. Step-by-step derivation
      1. lower-*.f6499.9

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

      \[\leadsto \mathsf{fma}\left(x, -60.63794581618656 \cdot \frac{x}{\color{blue}{-7.787037037037037 \cdot x}}, \frac{-0.019024970273483946}{-0.13793103448275862}\right) \]
    5. Final simplification99.9%

      \[\leadsto \mathsf{fma}\left(x, \frac{x}{-7.787037037037037 \cdot x} \cdot -60.63794581618656, \frac{-0.019024970273483946}{-0.13793103448275862}\right) \]
    6. Add Preprocessing

    Alternative 2: 97.7% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{841}{108} \cdot x\\ \mathbf{if}\;t\_0 \leq -10:\\ \;\;\;\;7.787037037037037 \cdot x\\ \mathbf{elif}\;t\_0 \leq 0.04:\\ \;\;\;\;0.13793103448275862\\ \mathbf{else}:\\ \;\;\;\;7.787037037037037 \cdot x\\ \end{array} \end{array} \]
    (FPCore (x)
     :precision binary64
     (let* ((t_0 (* (/ 841.0 108.0) x)))
       (if (<= t_0 -10.0)
         (* 7.787037037037037 x)
         (if (<= t_0 0.04) 0.13793103448275862 (* 7.787037037037037 x)))))
    double code(double x) {
    	double t_0 = (841.0 / 108.0) * x;
    	double tmp;
    	if (t_0 <= -10.0) {
    		tmp = 7.787037037037037 * x;
    	} else if (t_0 <= 0.04) {
    		tmp = 0.13793103448275862;
    	} else {
    		tmp = 7.787037037037037 * x;
    	}
    	return tmp;
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        real(8) :: t_0
        real(8) :: tmp
        t_0 = (841.0d0 / 108.0d0) * x
        if (t_0 <= (-10.0d0)) then
            tmp = 7.787037037037037d0 * x
        else if (t_0 <= 0.04d0) then
            tmp = 0.13793103448275862d0
        else
            tmp = 7.787037037037037d0 * x
        end if
        code = tmp
    end function
    
    public static double code(double x) {
    	double t_0 = (841.0 / 108.0) * x;
    	double tmp;
    	if (t_0 <= -10.0) {
    		tmp = 7.787037037037037 * x;
    	} else if (t_0 <= 0.04) {
    		tmp = 0.13793103448275862;
    	} else {
    		tmp = 7.787037037037037 * x;
    	}
    	return tmp;
    }
    
    def code(x):
    	t_0 = (841.0 / 108.0) * x
    	tmp = 0
    	if t_0 <= -10.0:
    		tmp = 7.787037037037037 * x
    	elif t_0 <= 0.04:
    		tmp = 0.13793103448275862
    	else:
    		tmp = 7.787037037037037 * x
    	return tmp
    
    function code(x)
    	t_0 = Float64(Float64(841.0 / 108.0) * x)
    	tmp = 0.0
    	if (t_0 <= -10.0)
    		tmp = Float64(7.787037037037037 * x);
    	elseif (t_0 <= 0.04)
    		tmp = 0.13793103448275862;
    	else
    		tmp = Float64(7.787037037037037 * x);
    	end
    	return tmp
    end
    
    function tmp_2 = code(x)
    	t_0 = (841.0 / 108.0) * x;
    	tmp = 0.0;
    	if (t_0 <= -10.0)
    		tmp = 7.787037037037037 * x;
    	elseif (t_0 <= 0.04)
    		tmp = 0.13793103448275862;
    	else
    		tmp = 7.787037037037037 * x;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_] := Block[{t$95$0 = N[(N[(841.0 / 108.0), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[t$95$0, -10.0], N[(7.787037037037037 * x), $MachinePrecision], If[LessEqual[t$95$0, 0.04], 0.13793103448275862, N[(7.787037037037037 * x), $MachinePrecision]]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \frac{841}{108} \cdot x\\
    \mathbf{if}\;t\_0 \leq -10:\\
    \;\;\;\;7.787037037037037 \cdot x\\
    
    \mathbf{elif}\;t\_0 \leq 0.04:\\
    \;\;\;\;0.13793103448275862\\
    
    \mathbf{else}:\\
    \;\;\;\;7.787037037037037 \cdot x\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (*.f64 (/.f64 #s(literal 841 binary64) #s(literal 108 binary64)) x) < -10 or 0.0400000000000000008 < (*.f64 (/.f64 #s(literal 841 binary64) #s(literal 108 binary64)) x)

      1. Initial program 99.7%

        \[\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. flip3-+N/A

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

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

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

          \[\leadsto \frac{\color{blue}{-1}}{\mathsf{neg}\left(\frac{\left(\frac{841}{108} \cdot x\right) \cdot \left(\frac{841}{108} \cdot x\right) + \left(\frac{4}{29} \cdot \frac{4}{29} - \left(\frac{841}{108} \cdot x\right) \cdot \frac{4}{29}\right)}{{\left(\frac{841}{108} \cdot x\right)}^{3} + {\left(\frac{4}{29}\right)}^{3}}\right)} \]
        6. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{-1}{\mathsf{neg}\left(\frac{\left(\frac{841}{108} \cdot x\right) \cdot \left(\frac{841}{108} \cdot x\right) + \left(\frac{4}{29} \cdot \frac{4}{29} - \left(\frac{841}{108} \cdot x\right) \cdot \frac{4}{29}\right)}{{\left(\frac{841}{108} \cdot x\right)}^{3} + {\left(\frac{4}{29}\right)}^{3}}\right)}} \]
        7. clear-numN/A

          \[\leadsto \frac{-1}{\mathsf{neg}\left(\color{blue}{\frac{1}{\frac{{\left(\frac{841}{108} \cdot x\right)}^{3} + {\left(\frac{4}{29}\right)}^{3}}{\left(\frac{841}{108} \cdot x\right) \cdot \left(\frac{841}{108} \cdot x\right) + \left(\frac{4}{29} \cdot \frac{4}{29} - \left(\frac{841}{108} \cdot x\right) \cdot \frac{4}{29}\right)}}}\right)} \]
      4. Applied rewrites99.4%

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

        \[\leadsto \frac{-1}{\color{blue}{\frac{\frac{-108}{841}}{x}}} \]
      6. Step-by-step derivation
        1. lower-/.f6497.6

          \[\leadsto \frac{-1}{\color{blue}{\frac{-0.12841854934601665}{x}}} \]
      7. Applied rewrites97.6%

        \[\leadsto \frac{-1}{\color{blue}{\frac{-0.12841854934601665}{x}}} \]
      8. Taylor expanded in x around inf

        \[\leadsto \color{blue}{\frac{841}{108} \cdot x} \]
      9. Step-by-step derivation
        1. lower-*.f6498.0

          \[\leadsto \color{blue}{7.787037037037037 \cdot x} \]
      10. Applied rewrites98.0%

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

      if -10 < (*.f64 (/.f64 #s(literal 841 binary64) #s(literal 108 binary64)) x) < 0.0400000000000000008

      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 rewrites96.7%

          \[\leadsto \color{blue}{0.13793103448275862} \]
      5. Recombined 2 regimes into one program.
      6. Add Preprocessing

      Alternative 3: 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.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. 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 4: 49.9% 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.8%

        \[\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 rewrites49.8%

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

        Reproduce

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