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

Percentage Accurate: 99.9% → 99.9%
Time: 9.9s
Alternatives: 3
Speedup: 1.8×

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, 1.8× speedup?

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

\\
7.787037037037037 \cdot x + 0.13793103448275862
\end{array}
Derivation
  1. Initial program 99.9%

    \[\frac{841}{108} \cdot x + \frac{4}{29} \]
  2. Simplified0

    \[\leadsto expr\]
  3. Add Preprocessing
  4. Add Preprocessing

Alternative 2: 97.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.017:\\ \;\;\;\;7.787037037037037 \cdot x\\ \mathbf{elif}\;x \leq 0.018:\\ \;\;\;\;0.13793103448275862\\ \mathbf{else}:\\ \;\;\;\;7.787037037037037 \cdot x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -0.017)
   (* 7.787037037037037 x)
   (if (<= x 0.018) 0.13793103448275862 (* 7.787037037037037 x))))
double code(double x) {
	double tmp;
	if (x <= -0.017) {
		tmp = 7.787037037037037 * x;
	} else if (x <= 0.018) {
		tmp = 0.13793103448275862;
	} else {
		tmp = 7.787037037037037 * x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-0.017d0)) then
        tmp = 7.787037037037037d0 * x
    else if (x <= 0.018d0) then
        tmp = 0.13793103448275862d0
    else
        tmp = 7.787037037037037d0 * x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -0.017) {
		tmp = 7.787037037037037 * x;
	} else if (x <= 0.018) {
		tmp = 0.13793103448275862;
	} else {
		tmp = 7.787037037037037 * x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -0.017:
		tmp = 7.787037037037037 * x
	elif x <= 0.018:
		tmp = 0.13793103448275862
	else:
		tmp = 7.787037037037037 * x
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -0.017)
		tmp = Float64(7.787037037037037 * x);
	elseif (x <= 0.018)
		tmp = 0.13793103448275862;
	else
		tmp = Float64(7.787037037037037 * x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -0.017)
		tmp = 7.787037037037037 * x;
	elseif (x <= 0.018)
		tmp = 0.13793103448275862;
	else
		tmp = 7.787037037037037 * x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -0.017], N[(7.787037037037037 * x), $MachinePrecision], If[LessEqual[x, 0.018], 0.13793103448275862, N[(7.787037037037037 * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.017:\\
\;\;\;\;7.787037037037037 \cdot x\\

\mathbf{elif}\;x \leq 0.018:\\
\;\;\;\;0.13793103448275862\\

\mathbf{else}:\\
\;\;\;\;7.787037037037037 \cdot x\\


\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. Simplified0

      \[\leadsto expr\]
    3. Add Preprocessing
    4. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    5. Simplified0

      \[\leadsto expr\]

    if -0.017000000000000001 < x < 0.0179999999999999986

    1. Initial program 100.0%

      \[\frac{841}{108} \cdot x + \frac{4}{29} \]
    2. Simplified0

      \[\leadsto expr\]
    3. Add Preprocessing
    4. Taylor expanded in x around 0 0

      \[\leadsto expr\]
    5. Simplified0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 50.9% accurate, 9.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. Simplified0

    \[\leadsto expr\]
  3. Add Preprocessing
  4. Taylor expanded in x around 0 0

    \[\leadsto expr\]
  5. Simplified0

    \[\leadsto expr\]
  6. Add Preprocessing

Reproduce

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