Numeric.SpecFunctions:choose from math-functions-0.1.5.2

Percentage Accurate: 84.8% → 96.9%
Time: 1.7s
Alternatives: 5
Speedup: 1.1×

Specification

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

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 5 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: 84.8% accurate, 1.0× speedup?

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

Alternative 1: 96.9% accurate, 0.4× speedup?

\[\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 2.604772325003076 \cdot 10^{-139}:\\ \;\;\;\;\mathsf{fma}\left(y \cdot \left|x\right|, \frac{1}{z}, \left|x\right|\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\left|x\right|, \frac{y}{z}, \left|x\right|\right)\\ \end{array} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (*
 (copysign 1.0 x)
 (if (<= (fabs x) 2.604772325003076e-139)
   (fma (* y (fabs x)) (/ 1.0 z) (fabs x))
   (fma (fabs x) (/ y z) (fabs x)))))
double code(double x, double y, double z) {
	double tmp;
	if (fabs(x) <= 2.604772325003076e-139) {
		tmp = fma((y * fabs(x)), (1.0 / z), fabs(x));
	} else {
		tmp = fma(fabs(x), (y / z), fabs(x));
	}
	return copysign(1.0, x) * tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (abs(x) <= 2.604772325003076e-139)
		tmp = fma(Float64(y * abs(x)), Float64(1.0 / z), abs(x));
	else
		tmp = fma(abs(x), Float64(y / z), abs(x));
	end
	return Float64(copysign(1.0, x) * tmp)
end
code[x_, y_, z_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 2.604772325003076e-139], N[(N[(y * N[Abs[x], $MachinePrecision]), $MachinePrecision] * N[(1.0 / z), $MachinePrecision] + N[Abs[x], $MachinePrecision]), $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * N[(y / z), $MachinePrecision] + N[Abs[x], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 2.604772325003076 \cdot 10^{-139}:\\
\;\;\;\;\mathsf{fma}\left(y \cdot \left|x\right|, \frac{1}{z}, \left|x\right|\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left|x\right|, \frac{y}{z}, \left|x\right|\right)\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 2.6047723250030761e-139

    1. Initial program 84.8%

      \[\frac{x \cdot \left(y + z\right)}{z} \]
    2. Step-by-step derivation
      1. Applied rewrites94.0%

        \[\leadsto \mathsf{fma}\left(y \cdot x, \frac{1}{z}, x\right) \]

      if 2.6047723250030761e-139 < x

      1. Initial program 84.8%

        \[\frac{x \cdot \left(y + z\right)}{z} \]
      2. Step-by-step derivation
        1. Applied rewrites95.5%

          \[\leadsto \mathsf{fma}\left(x, \frac{y}{z}, x\right) \]
      3. Recombined 2 regimes into one program.
      4. Add Preprocessing

      Alternative 2: 95.5% accurate, 1.1× speedup?

      \[\mathsf{fma}\left(x, \frac{y}{z}, x\right) \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (fma x (/ y z) x))
      double code(double x, double y, double z) {
      	return fma(x, (y / z), x);
      }
      
      function code(x, y, z)
      	return fma(x, Float64(y / z), x)
      end
      
      code[x_, y_, z_] := N[(x * N[(y / z), $MachinePrecision] + x), $MachinePrecision]
      
      f(x, y, z):
      	x in [-inf, +inf],
      	y in [-inf, +inf],
      	z in [-inf, +inf]
      code: THEORY
      BEGIN
      f(x, y, z: real): real =
      	(x * (y / z)) + x
      END code
      \mathsf{fma}\left(x, \frac{y}{z}, x\right)
      
      Derivation
      1. Initial program 84.8%

        \[\frac{x \cdot \left(y + z\right)}{z} \]
      2. Step-by-step derivation
        1. Applied rewrites95.5%

          \[\leadsto \mathsf{fma}\left(x, \frac{y}{z}, x\right) \]
        2. Add Preprocessing

        Alternative 3: 73.4% accurate, 0.7× speedup?

        \[\begin{array}{l} t_0 := \frac{x \cdot y}{z}\\ \mathbf{if}\;y \leq -4.4496981415615434 \cdot 10^{-17}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 3.727054120548146 \cdot 10^{+47}:\\ \;\;\;\;x \cdot 1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
        (FPCore (x y z)
          :precision binary64
          :pre TRUE
          (let* ((t_0 (/ (* x y) z)))
          (if (<= y -4.4496981415615434e-17)
            t_0
            (if (<= y 3.727054120548146e+47) (* x 1.0) t_0))))
        double code(double x, double y, double z) {
        	double t_0 = (x * y) / z;
        	double tmp;
        	if (y <= -4.4496981415615434e-17) {
        		tmp = t_0;
        	} else if (y <= 3.727054120548146e+47) {
        		tmp = x * 1.0;
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        real(8) function code(x, y, z)
        use fmin_fmax_functions
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8), intent (in) :: z
            real(8) :: t_0
            real(8) :: tmp
            t_0 = (x * y) / z
            if (y <= (-4.4496981415615434d-17)) then
                tmp = t_0
            else if (y <= 3.727054120548146d+47) then
                tmp = x * 1.0d0
            else
                tmp = t_0
            end if
            code = tmp
        end function
        
        public static double code(double x, double y, double z) {
        	double t_0 = (x * y) / z;
        	double tmp;
        	if (y <= -4.4496981415615434e-17) {
        		tmp = t_0;
        	} else if (y <= 3.727054120548146e+47) {
        		tmp = x * 1.0;
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        def code(x, y, z):
        	t_0 = (x * y) / z
        	tmp = 0
        	if y <= -4.4496981415615434e-17:
        		tmp = t_0
        	elif y <= 3.727054120548146e+47:
        		tmp = x * 1.0
        	else:
        		tmp = t_0
        	return tmp
        
        function code(x, y, z)
        	t_0 = Float64(Float64(x * y) / z)
        	tmp = 0.0
        	if (y <= -4.4496981415615434e-17)
        		tmp = t_0;
        	elseif (y <= 3.727054120548146e+47)
        		tmp = Float64(x * 1.0);
        	else
        		tmp = t_0;
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y, z)
        	t_0 = (x * y) / z;
        	tmp = 0.0;
        	if (y <= -4.4496981415615434e-17)
        		tmp = t_0;
        	elseif (y <= 3.727054120548146e+47)
        		tmp = x * 1.0;
        	else
        		tmp = t_0;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * y), $MachinePrecision] / z), $MachinePrecision]}, If[LessEqual[y, -4.4496981415615434e-17], t$95$0, If[LessEqual[y, 3.727054120548146e+47], N[(x * 1.0), $MachinePrecision], t$95$0]]]
        
        f(x, y, z):
        	x in [-inf, +inf],
        	y in [-inf, +inf],
        	z in [-inf, +inf]
        code: THEORY
        BEGIN
        f(x, y, z: real): real =
        	LET t_0 = ((x * y) / z) IN
        		LET tmp_1 = IF (y <= (372705412054814616519807377301979582364609675264)) THEN (x * (1)) ELSE t_0 ENDIF IN
        		LET tmp = IF (y <= (-4449698141561543378010010345383822612506954265860180386393807339118211530148983001708984375e-107)) THEN t_0 ELSE tmp_1 ENDIF IN
        	tmp
        END code
        \begin{array}{l}
        t_0 := \frac{x \cdot y}{z}\\
        \mathbf{if}\;y \leq -4.4496981415615434 \cdot 10^{-17}:\\
        \;\;\;\;t\_0\\
        
        \mathbf{elif}\;y \leq 3.727054120548146 \cdot 10^{+47}:\\
        \;\;\;\;x \cdot 1\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if y < -4.4496981415615434e-17 or 3.7270541205481462e47 < y

          1. Initial program 84.8%

            \[\frac{x \cdot \left(y + z\right)}{z} \]
          2. Taylor expanded in y around inf

            \[\leadsto \frac{x \cdot y}{z} \]
          3. Step-by-step derivation
            1. Applied rewrites46.9%

              \[\leadsto \frac{x \cdot y}{z} \]

            if -4.4496981415615434e-17 < y < 3.7270541205481462e47

            1. Initial program 84.8%

              \[\frac{x \cdot \left(y + z\right)}{z} \]
            2. Taylor expanded in y around 0

              \[\leadsto \frac{x \cdot z}{z} \]
            3. Step-by-step derivation
              1. Applied rewrites40.7%

                \[\leadsto \frac{x \cdot z}{z} \]
              2. Step-by-step derivation
                1. Applied rewrites51.4%

                  \[\leadsto x \cdot \frac{z}{z} \]
                2. Taylor expanded in y around 0

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

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

                Alternative 4: 71.0% accurate, 0.7× speedup?

                \[\begin{array}{l} t_0 := x \cdot \frac{y}{z}\\ \mathbf{if}\;y \leq -4.4496981415615434 \cdot 10^{-17}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 3.727054120548146 \cdot 10^{+47}:\\ \;\;\;\;x \cdot 1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
                (FPCore (x y z)
                  :precision binary64
                  :pre TRUE
                  (let* ((t_0 (* x (/ y z))))
                  (if (<= y -4.4496981415615434e-17)
                    t_0
                    (if (<= y 3.727054120548146e+47) (* x 1.0) t_0))))
                double code(double x, double y, double z) {
                	double t_0 = x * (y / z);
                	double tmp;
                	if (y <= -4.4496981415615434e-17) {
                		tmp = t_0;
                	} else if (y <= 3.727054120548146e+47) {
                		tmp = x * 1.0;
                	} else {
                		tmp = t_0;
                	}
                	return tmp;
                }
                
                real(8) function code(x, y, z)
                use fmin_fmax_functions
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    real(8), intent (in) :: z
                    real(8) :: t_0
                    real(8) :: tmp
                    t_0 = x * (y / z)
                    if (y <= (-4.4496981415615434d-17)) then
                        tmp = t_0
                    else if (y <= 3.727054120548146d+47) then
                        tmp = x * 1.0d0
                    else
                        tmp = t_0
                    end if
                    code = tmp
                end function
                
                public static double code(double x, double y, double z) {
                	double t_0 = x * (y / z);
                	double tmp;
                	if (y <= -4.4496981415615434e-17) {
                		tmp = t_0;
                	} else if (y <= 3.727054120548146e+47) {
                		tmp = x * 1.0;
                	} else {
                		tmp = t_0;
                	}
                	return tmp;
                }
                
                def code(x, y, z):
                	t_0 = x * (y / z)
                	tmp = 0
                	if y <= -4.4496981415615434e-17:
                		tmp = t_0
                	elif y <= 3.727054120548146e+47:
                		tmp = x * 1.0
                	else:
                		tmp = t_0
                	return tmp
                
                function code(x, y, z)
                	t_0 = Float64(x * Float64(y / z))
                	tmp = 0.0
                	if (y <= -4.4496981415615434e-17)
                		tmp = t_0;
                	elseif (y <= 3.727054120548146e+47)
                		tmp = Float64(x * 1.0);
                	else
                		tmp = t_0;
                	end
                	return tmp
                end
                
                function tmp_2 = code(x, y, z)
                	t_0 = x * (y / z);
                	tmp = 0.0;
                	if (y <= -4.4496981415615434e-17)
                		tmp = t_0;
                	elseif (y <= 3.727054120548146e+47)
                		tmp = x * 1.0;
                	else
                		tmp = t_0;
                	end
                	tmp_2 = tmp;
                end
                
                code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(y / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -4.4496981415615434e-17], t$95$0, If[LessEqual[y, 3.727054120548146e+47], N[(x * 1.0), $MachinePrecision], t$95$0]]]
                
                f(x, y, z):
                	x in [-inf, +inf],
                	y in [-inf, +inf],
                	z in [-inf, +inf]
                code: THEORY
                BEGIN
                f(x, y, z: real): real =
                	LET t_0 = (x * (y / z)) IN
                		LET tmp_1 = IF (y <= (372705412054814616519807377301979582364609675264)) THEN (x * (1)) ELSE t_0 ENDIF IN
                		LET tmp = IF (y <= (-4449698141561543378010010345383822612506954265860180386393807339118211530148983001708984375e-107)) THEN t_0 ELSE tmp_1 ENDIF IN
                	tmp
                END code
                \begin{array}{l}
                t_0 := x \cdot \frac{y}{z}\\
                \mathbf{if}\;y \leq -4.4496981415615434 \cdot 10^{-17}:\\
                \;\;\;\;t\_0\\
                
                \mathbf{elif}\;y \leq 3.727054120548146 \cdot 10^{+47}:\\
                \;\;\;\;x \cdot 1\\
                
                \mathbf{else}:\\
                \;\;\;\;t\_0\\
                
                
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if y < -4.4496981415615434e-17 or 3.7270541205481462e47 < y

                  1. Initial program 84.8%

                    \[\frac{x \cdot \left(y + z\right)}{z} \]
                  2. Taylor expanded in y around 0

                    \[\leadsto \frac{x \cdot z}{z} \]
                  3. Step-by-step derivation
                    1. Applied rewrites40.7%

                      \[\leadsto \frac{x \cdot z}{z} \]
                    2. Step-by-step derivation
                      1. Applied rewrites51.4%

                        \[\leadsto x \cdot \frac{z}{z} \]
                      2. Taylor expanded in y around 0

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

                          \[\leadsto x \cdot 1 \]
                        2. Taylor expanded in y around inf

                          \[\leadsto x \cdot \frac{y}{z} \]
                        3. Step-by-step derivation
                          1. Applied rewrites46.0%

                            \[\leadsto x \cdot \frac{y}{z} \]

                          if -4.4496981415615434e-17 < y < 3.7270541205481462e47

                          1. Initial program 84.8%

                            \[\frac{x \cdot \left(y + z\right)}{z} \]
                          2. Taylor expanded in y around 0

                            \[\leadsto \frac{x \cdot z}{z} \]
                          3. Step-by-step derivation
                            1. Applied rewrites40.7%

                              \[\leadsto \frac{x \cdot z}{z} \]
                            2. Step-by-step derivation
                              1. Applied rewrites51.4%

                                \[\leadsto x \cdot \frac{z}{z} \]
                              2. Taylor expanded in y around 0

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

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

                              Alternative 5: 51.4% accurate, 2.6× speedup?

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

                                \[\frac{x \cdot \left(y + z\right)}{z} \]
                              2. Taylor expanded in y around 0

                                \[\leadsto \frac{x \cdot z}{z} \]
                              3. Step-by-step derivation
                                1. Applied rewrites40.7%

                                  \[\leadsto \frac{x \cdot z}{z} \]
                                2. Step-by-step derivation
                                  1. Applied rewrites51.4%

                                    \[\leadsto x \cdot \frac{z}{z} \]
                                  2. Taylor expanded in y around 0

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

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

                                    Reproduce

                                    ?
                                    herbie shell --seed 2026092 
                                    (FPCore (x y z)
                                      :name "Numeric.SpecFunctions:choose from math-functions-0.1.5.2"
                                      :precision binary64
                                      (/ (* x (+ y z)) z))