Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, E

Percentage Accurate: 99.7% → 99.8%
Time: 2.1s
Alternatives: 4
Speedup: 1.4×

Specification

?
\[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (* (* 3.0 (- 2.0 (* x 3.0))) x))
double code(double x) {
	return (3.0 * (2.0 - (x * 3.0))) * x;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (3.0d0 * (2.0d0 - (x * 3.0d0))) * x
end function
public static double code(double x) {
	return (3.0 * (2.0 - (x * 3.0))) * x;
}
def code(x):
	return (3.0 * (2.0 - (x * 3.0))) * x
function code(x)
	return Float64(Float64(3.0 * Float64(2.0 - Float64(x * 3.0))) * x)
end
function tmp = code(x)
	tmp = (3.0 * (2.0 - (x * 3.0))) * x;
end
code[x_] := N[(N[(3.0 * N[(2.0 - N[(x * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((3) * ((2) - (x * (3)))) * x
END code
\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x

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.7% accurate, 1.0× speedup?

\[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (* (* 3.0 (- 2.0 (* x 3.0))) x))
double code(double x) {
	return (3.0 * (2.0 - (x * 3.0))) * x;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (3.0d0 * (2.0d0 - (x * 3.0d0))) * x
end function
public static double code(double x) {
	return (3.0 * (2.0 - (x * 3.0))) * x;
}
def code(x):
	return (3.0 * (2.0 - (x * 3.0))) * x
function code(x)
	return Float64(Float64(3.0 * Float64(2.0 - Float64(x * 3.0))) * x)
end
function tmp = code(x)
	tmp = (3.0 * (2.0 - (x * 3.0))) * x;
end
code[x_] := N[(N[(3.0 * N[(2.0 - N[(x * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((3) * ((2) - (x * (3)))) * x
END code
\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x

Alternative 1: 99.8% accurate, 1.1× speedup?

\[\mathsf{fma}\left(6, x, \left(x \cdot x\right) \cdot -9\right) \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (fma 6.0 x (* (* x x) -9.0)))
double code(double x) {
	return fma(6.0, x, ((x * x) * -9.0));
}
function code(x)
	return fma(6.0, x, Float64(Float64(x * x) * -9.0))
end
code[x_] := N[(6.0 * x + N[(N[(x * x), $MachinePrecision] * -9.0), $MachinePrecision]), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	((6) * x) + ((x * x) * (-9))
END code
\mathsf{fma}\left(6, x, \left(x \cdot x\right) \cdot -9\right)
Derivation
  1. Initial program 99.7%

    \[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
  2. Step-by-step derivation
    1. Applied rewrites99.8%

      \[\leadsto \mathsf{fma}\left(6, x, \left(x \cdot x\right) \cdot -9\right) \]
    2. Add Preprocessing

    Alternative 2: 99.8% accurate, 1.4× speedup?

    \[\mathsf{fma}\left(-9, x, 6\right) \cdot x \]
    (FPCore (x)
      :precision binary64
      :pre TRUE
      (* (fma -9.0 x 6.0) x))
    double code(double x) {
    	return fma(-9.0, x, 6.0) * x;
    }
    
    function code(x)
    	return Float64(fma(-9.0, x, 6.0) * x)
    end
    
    code[x_] := N[(N[(-9.0 * x + 6.0), $MachinePrecision] * x), $MachinePrecision]
    
    f(x):
    	x in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x: real): real =
    	(((-9) * x) + (6)) * x
    END code
    \mathsf{fma}\left(-9, x, 6\right) \cdot x
    
    Derivation
    1. Initial program 99.7%

      \[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
    2. Step-by-step derivation
      1. Applied rewrites99.8%

        \[\leadsto \mathsf{fma}\left(-9, x, 6\right) \cdot x \]
      2. Add Preprocessing

      Alternative 3: 97.8% accurate, 0.6× speedup?

      \[\begin{array}{l} \mathbf{if}\;\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \leq -2000:\\ \;\;\;\;\left(-9 \cdot x\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;6 \cdot x\\ \end{array} \]
      (FPCore (x)
        :precision binary64
        :pre TRUE
        (if (<= (* (* 3.0 (- 2.0 (* x 3.0))) x) -2000.0)
        (* (* -9.0 x) x)
        (* 6.0 x)))
      double code(double x) {
      	double tmp;
      	if (((3.0 * (2.0 - (x * 3.0))) * x) <= -2000.0) {
      		tmp = (-9.0 * x) * x;
      	} else {
      		tmp = 6.0 * x;
      	}
      	return tmp;
      }
      
      real(8) function code(x)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          real(8) :: tmp
          if (((3.0d0 * (2.0d0 - (x * 3.0d0))) * x) <= (-2000.0d0)) then
              tmp = ((-9.0d0) * x) * x
          else
              tmp = 6.0d0 * x
          end if
          code = tmp
      end function
      
      public static double code(double x) {
      	double tmp;
      	if (((3.0 * (2.0 - (x * 3.0))) * x) <= -2000.0) {
      		tmp = (-9.0 * x) * x;
      	} else {
      		tmp = 6.0 * x;
      	}
      	return tmp;
      }
      
      def code(x):
      	tmp = 0
      	if ((3.0 * (2.0 - (x * 3.0))) * x) <= -2000.0:
      		tmp = (-9.0 * x) * x
      	else:
      		tmp = 6.0 * x
      	return tmp
      
      function code(x)
      	tmp = 0.0
      	if (Float64(Float64(3.0 * Float64(2.0 - Float64(x * 3.0))) * x) <= -2000.0)
      		tmp = Float64(Float64(-9.0 * x) * x);
      	else
      		tmp = Float64(6.0 * x);
      	end
      	return tmp
      end
      
      function tmp_2 = code(x)
      	tmp = 0.0;
      	if (((3.0 * (2.0 - (x * 3.0))) * x) <= -2000.0)
      		tmp = (-9.0 * x) * x;
      	else
      		tmp = 6.0 * x;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_] := If[LessEqual[N[(N[(3.0 * N[(2.0 - N[(x * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision], -2000.0], N[(N[(-9.0 * x), $MachinePrecision] * x), $MachinePrecision], N[(6.0 * x), $MachinePrecision]]
      
      f(x):
      	x in [-inf, +inf]
      code: THEORY
      BEGIN
      f(x: real): real =
      	LET tmp = IF ((((3) * ((2) - (x * (3)))) * x) <= (-2000)) THEN (((-9) * x) * x) ELSE ((6) * x) ENDIF IN
      	tmp
      END code
      \begin{array}{l}
      \mathbf{if}\;\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \leq -2000:\\
      \;\;\;\;\left(-9 \cdot x\right) \cdot x\\
      
      \mathbf{else}:\\
      \;\;\;\;6 \cdot x\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if (*.f64 (*.f64 #s(literal 3 binary64) (-.f64 #s(literal 2 binary64) (*.f64 x #s(literal 3 binary64)))) x) < -2e3

        1. Initial program 99.7%

          \[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
        2. Taylor expanded in x around inf

          \[\leadsto \left(-9 \cdot x\right) \cdot x \]
        3. Step-by-step derivation
          1. Applied rewrites51.6%

            \[\leadsto \left(-9 \cdot x\right) \cdot x \]

          if -2e3 < (*.f64 (*.f64 #s(literal 3 binary64) (-.f64 #s(literal 2 binary64) (*.f64 x #s(literal 3 binary64)))) x)

          1. Initial program 99.7%

            \[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
          2. Taylor expanded in x around 0

            \[\leadsto 6 \cdot x \]
          3. Step-by-step derivation
            1. Applied rewrites51.1%

              \[\leadsto 6 \cdot x \]
          4. Recombined 2 regimes into one program.
          5. Add Preprocessing

          Alternative 4: 51.1% accurate, 3.1× speedup?

          \[6 \cdot x \]
          (FPCore (x)
            :precision binary64
            :pre TRUE
            (* 6.0 x))
          double code(double x) {
          	return 6.0 * x;
          }
          
          real(8) function code(x)
          use fmin_fmax_functions
              real(8), intent (in) :: x
              code = 6.0d0 * x
          end function
          
          public static double code(double x) {
          	return 6.0 * x;
          }
          
          def code(x):
          	return 6.0 * x
          
          function code(x)
          	return Float64(6.0 * x)
          end
          
          function tmp = code(x)
          	tmp = 6.0 * x;
          end
          
          code[x_] := N[(6.0 * x), $MachinePrecision]
          
          f(x):
          	x in [-inf, +inf]
          code: THEORY
          BEGIN
          f(x: real): real =
          	(6) * x
          END code
          6 \cdot x
          
          Derivation
          1. Initial program 99.7%

            \[\left(3 \cdot \left(2 - x \cdot 3\right)\right) \cdot x \]
          2. Taylor expanded in x around 0

            \[\leadsto 6 \cdot x \]
          3. Step-by-step derivation
            1. Applied rewrites51.1%

              \[\leadsto 6 \cdot x \]
            2. Add Preprocessing

            Reproduce

            ?
            herbie shell --seed 2026092 
            (FPCore (x)
              :name "Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, E"
              :precision binary64
              (* (* 3.0 (- 2.0 (* x 3.0))) x))