xlohi (overflows)

Percentage Accurate: 3.1% → 19.4%
Time: 17.4s
Alternatives: 5
Speedup: 18.0×

Specification

?
\[lo < -1 \cdot 10^{+308} \land hi > 10^{+308}\]
\[\begin{array}{l} \\ \frac{x - lo}{hi - lo} \end{array} \]
(FPCore (lo hi x) :precision binary64 (/ (- x lo) (- hi lo)))
double code(double lo, double hi, double x) {
	return (x - lo) / (hi - lo);
}
real(8) function code(lo, hi, x)
    real(8), intent (in) :: lo
    real(8), intent (in) :: hi
    real(8), intent (in) :: x
    code = (x - lo) / (hi - lo)
end function
public static double code(double lo, double hi, double x) {
	return (x - lo) / (hi - lo);
}
def code(lo, hi, x):
	return (x - lo) / (hi - lo)
function code(lo, hi, x)
	return Float64(Float64(x - lo) / Float64(hi - lo))
end
function tmp = code(lo, hi, x)
	tmp = (x - lo) / (hi - lo);
end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / N[(hi - lo), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x - lo}{hi - lo}
\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 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: 3.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x - lo}{hi - lo} \end{array} \]
(FPCore (lo hi x) :precision binary64 (/ (- x lo) (- hi lo)))
double code(double lo, double hi, double x) {
	return (x - lo) / (hi - lo);
}
real(8) function code(lo, hi, x)
    real(8), intent (in) :: lo
    real(8), intent (in) :: hi
    real(8), intent (in) :: x
    code = (x - lo) / (hi - lo)
end function
public static double code(double lo, double hi, double x) {
	return (x - lo) / (hi - lo);
}
def code(lo, hi, x):
	return (x - lo) / (hi - lo)
function code(lo, hi, x)
	return Float64(Float64(x - lo) / Float64(hi - lo))
end
function tmp = code(lo, hi, x)
	tmp = (x - lo) / (hi - lo);
end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / N[(hi - lo), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x - lo}{hi - lo}
\end{array}

Alternative 1: 19.4% accurate, 0.1× speedup?

\[\begin{array}{l} \\ {\left(\frac{lo}{hi - x} \cdot \frac{lo}{hi}\right)}^{-1} \end{array} \]
(FPCore (lo hi x) :precision binary64 (pow (* (/ lo (- hi x)) (/ lo hi)) -1.0))
double code(double lo, double hi, double x) {
	return pow(((lo / (hi - x)) * (lo / hi)), -1.0);
}
real(8) function code(lo, hi, x)
    real(8), intent (in) :: lo
    real(8), intent (in) :: hi
    real(8), intent (in) :: x
    code = ((lo / (hi - x)) * (lo / hi)) ** (-1.0d0)
end function
public static double code(double lo, double hi, double x) {
	return Math.pow(((lo / (hi - x)) * (lo / hi)), -1.0);
}
def code(lo, hi, x):
	return math.pow(((lo / (hi - x)) * (lo / hi)), -1.0)
function code(lo, hi, x)
	return Float64(Float64(lo / Float64(hi - x)) * Float64(lo / hi)) ^ -1.0
end
function tmp = code(lo, hi, x)
	tmp = ((lo / (hi - x)) * (lo / hi)) ^ -1.0;
end
code[lo_, hi_, x_] := N[Power[N[(N[(lo / N[(hi - x), $MachinePrecision]), $MachinePrecision] * N[(lo / hi), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]
\begin{array}{l}

\\
{\left(\frac{lo}{hi - x} \cdot \frac{lo}{hi}\right)}^{-1}
\end{array}
Derivation
  1. Initial program 3.1%

    \[\frac{x - lo}{hi - lo} \]
  2. Add Preprocessing
  3. Taylor expanded in lo around -inf

    \[\leadsto \color{blue}{1 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
    2. unsub-negN/A

      \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
    3. lower--.f64N/A

      \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
    4. lower-/.f64N/A

      \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
    5. +-commutativeN/A

      \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
    6. associate--l+N/A

      \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
    7. associate-/l*N/A

      \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
    8. *-commutativeN/A

      \[\leadsto 1 - \frac{\color{blue}{\frac{x - hi}{lo} \cdot hi} + \left(x - hi\right)}{lo} \]
    9. lower-fma.f64N/A

      \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, x - hi\right)}}{lo} \]
    10. lower-/.f64N/A

      \[\leadsto 1 - \frac{\mathsf{fma}\left(\color{blue}{\frac{x - hi}{lo}}, hi, x - hi\right)}{lo} \]
    11. lower--.f64N/A

      \[\leadsto 1 - \frac{\mathsf{fma}\left(\frac{\color{blue}{x - hi}}{lo}, hi, x - hi\right)}{lo} \]
    12. lower--.f6419.0

      \[\leadsto 1 - \frac{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, \color{blue}{x - hi}\right)}{lo} \]
  5. Applied rewrites19.0%

    \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, x - hi\right)}{lo}} \]
  6. Taylor expanded in lo around 0

    \[\leadsto -1 \cdot \color{blue}{\frac{hi \cdot \left(x - hi\right)}{{lo}^{2}}} \]
  7. Step-by-step derivation
    1. Applied rewrites19.7%

      \[\leadsto \frac{\frac{hi - x}{lo}}{lo} \cdot \color{blue}{hi} \]
    2. Step-by-step derivation
      1. Applied rewrites19.7%

        \[\leadsto {\left(\frac{lo}{hi - x} \cdot \frac{lo}{hi}\right)}^{-1} \]
      2. Add Preprocessing

      Alternative 2: 19.4% accurate, 0.6× speedup?

      \[\begin{array}{l} \\ \frac{hi - x}{lo} \cdot \frac{hi}{lo} \end{array} \]
      (FPCore (lo hi x) :precision binary64 (* (/ (- hi x) lo) (/ hi lo)))
      double code(double lo, double hi, double x) {
      	return ((hi - x) / lo) * (hi / lo);
      }
      
      real(8) function code(lo, hi, x)
          real(8), intent (in) :: lo
          real(8), intent (in) :: hi
          real(8), intent (in) :: x
          code = ((hi - x) / lo) * (hi / lo)
      end function
      
      public static double code(double lo, double hi, double x) {
      	return ((hi - x) / lo) * (hi / lo);
      }
      
      def code(lo, hi, x):
      	return ((hi - x) / lo) * (hi / lo)
      
      function code(lo, hi, x)
      	return Float64(Float64(Float64(hi - x) / lo) * Float64(hi / lo))
      end
      
      function tmp = code(lo, hi, x)
      	tmp = ((hi - x) / lo) * (hi / lo);
      end
      
      code[lo_, hi_, x_] := N[(N[(N[(hi - x), $MachinePrecision] / lo), $MachinePrecision] * N[(hi / lo), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \frac{hi - x}{lo} \cdot \frac{hi}{lo}
      \end{array}
      
      Derivation
      1. Initial program 3.1%

        \[\frac{x - lo}{hi - lo} \]
      2. Add Preprocessing
      3. Taylor expanded in lo around -inf

        \[\leadsto \color{blue}{1 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
        2. unsub-negN/A

          \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
        3. lower--.f64N/A

          \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
        4. lower-/.f64N/A

          \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
        5. +-commutativeN/A

          \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
        6. associate--l+N/A

          \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
        7. associate-/l*N/A

          \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
        8. *-commutativeN/A

          \[\leadsto 1 - \frac{\color{blue}{\frac{x - hi}{lo} \cdot hi} + \left(x - hi\right)}{lo} \]
        9. lower-fma.f64N/A

          \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, x - hi\right)}}{lo} \]
        10. lower-/.f64N/A

          \[\leadsto 1 - \frac{\mathsf{fma}\left(\color{blue}{\frac{x - hi}{lo}}, hi, x - hi\right)}{lo} \]
        11. lower--.f64N/A

          \[\leadsto 1 - \frac{\mathsf{fma}\left(\frac{\color{blue}{x - hi}}{lo}, hi, x - hi\right)}{lo} \]
        12. lower--.f6419.0

          \[\leadsto 1 - \frac{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, \color{blue}{x - hi}\right)}{lo} \]
      5. Applied rewrites19.0%

        \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, x - hi\right)}{lo}} \]
      6. Taylor expanded in lo around 0

        \[\leadsto -1 \cdot \color{blue}{\frac{hi \cdot \left(x - hi\right)}{{lo}^{2}}} \]
      7. Step-by-step derivation
        1. Applied rewrites19.7%

          \[\leadsto \frac{\frac{hi - x}{lo}}{lo} \cdot \color{blue}{hi} \]
        2. Step-by-step derivation
          1. Applied rewrites19.7%

            \[\leadsto \frac{hi - x}{lo} \cdot \frac{hi}{\color{blue}{lo}} \]
          2. Add Preprocessing

          Alternative 3: 19.4% accurate, 0.6× speedup?

          \[\begin{array}{l} \\ hi \cdot \frac{\frac{hi}{lo}}{lo} \end{array} \]
          (FPCore (lo hi x) :precision binary64 (* hi (/ (/ hi lo) lo)))
          double code(double lo, double hi, double x) {
          	return hi * ((hi / lo) / lo);
          }
          
          real(8) function code(lo, hi, x)
              real(8), intent (in) :: lo
              real(8), intent (in) :: hi
              real(8), intent (in) :: x
              code = hi * ((hi / lo) / lo)
          end function
          
          public static double code(double lo, double hi, double x) {
          	return hi * ((hi / lo) / lo);
          }
          
          def code(lo, hi, x):
          	return hi * ((hi / lo) / lo)
          
          function code(lo, hi, x)
          	return Float64(hi * Float64(Float64(hi / lo) / lo))
          end
          
          function tmp = code(lo, hi, x)
          	tmp = hi * ((hi / lo) / lo);
          end
          
          code[lo_, hi_, x_] := N[(hi * N[(N[(hi / lo), $MachinePrecision] / lo), $MachinePrecision]), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          hi \cdot \frac{\frac{hi}{lo}}{lo}
          \end{array}
          
          Derivation
          1. Initial program 3.1%

            \[\frac{x - lo}{hi - lo} \]
          2. Add Preprocessing
          3. Taylor expanded in lo around -inf

            \[\leadsto \color{blue}{1 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
          4. Step-by-step derivation
            1. mul-1-negN/A

              \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
            2. unsub-negN/A

              \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
            3. lower--.f64N/A

              \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
            4. lower-/.f64N/A

              \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
            5. +-commutativeN/A

              \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
            6. associate--l+N/A

              \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
            7. associate-/l*N/A

              \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
            8. *-commutativeN/A

              \[\leadsto 1 - \frac{\color{blue}{\frac{x - hi}{lo} \cdot hi} + \left(x - hi\right)}{lo} \]
            9. lower-fma.f64N/A

              \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, x - hi\right)}}{lo} \]
            10. lower-/.f64N/A

              \[\leadsto 1 - \frac{\mathsf{fma}\left(\color{blue}{\frac{x - hi}{lo}}, hi, x - hi\right)}{lo} \]
            11. lower--.f64N/A

              \[\leadsto 1 - \frac{\mathsf{fma}\left(\frac{\color{blue}{x - hi}}{lo}, hi, x - hi\right)}{lo} \]
            12. lower--.f6419.0

              \[\leadsto 1 - \frac{\mathsf{fma}\left(\frac{x - hi}{lo}, hi, \color{blue}{x - hi}\right)}{lo} \]
          5. Applied rewrites19.0%

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

            \[\leadsto \frac{{hi}^{2}}{\color{blue}{{lo}^{2}}} \]
          7. Step-by-step derivation
            1. Applied rewrites19.7%

              \[\leadsto hi \cdot \color{blue}{\frac{\frac{hi}{lo}}{lo}} \]
            2. Add Preprocessing

            Alternative 4: 18.8% accurate, 1.3× speedup?

            \[\begin{array}{l} \\ \frac{-lo}{hi} \end{array} \]
            (FPCore (lo hi x) :precision binary64 (/ (- lo) hi))
            double code(double lo, double hi, double x) {
            	return -lo / hi;
            }
            
            real(8) function code(lo, hi, x)
                real(8), intent (in) :: lo
                real(8), intent (in) :: hi
                real(8), intent (in) :: x
                code = -lo / hi
            end function
            
            public static double code(double lo, double hi, double x) {
            	return -lo / hi;
            }
            
            def code(lo, hi, x):
            	return -lo / hi
            
            function code(lo, hi, x)
            	return Float64(Float64(-lo) / hi)
            end
            
            function tmp = code(lo, hi, x)
            	tmp = -lo / hi;
            end
            
            code[lo_, hi_, x_] := N[((-lo) / hi), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \frac{-lo}{hi}
            \end{array}
            
            Derivation
            1. Initial program 3.1%

              \[\frac{x - lo}{hi - lo} \]
            2. Add Preprocessing
            3. Taylor expanded in hi around inf

              \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
            4. Step-by-step derivation
              1. lower-/.f64N/A

                \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
              2. lower--.f6418.8

                \[\leadsto \frac{\color{blue}{x - lo}}{hi} \]
            5. Applied rewrites18.8%

              \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
            6. Taylor expanded in lo around inf

              \[\leadsto \frac{-1 \cdot lo}{hi} \]
            7. Step-by-step derivation
              1. Applied rewrites18.8%

                \[\leadsto \frac{-lo}{hi} \]
              2. Add Preprocessing

              Alternative 5: 18.7% accurate, 18.0× speedup?

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

                \[\frac{x - lo}{hi - lo} \]
              2. Add Preprocessing
              3. Taylor expanded in lo around inf

                \[\leadsto \color{blue}{1} \]
              4. Step-by-step derivation
                1. Applied rewrites18.7%

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

                Reproduce

                ?
                herbie shell --seed 2024323 
                (FPCore (lo hi x)
                  :name "xlohi (overflows)"
                  :precision binary64
                  :pre (and (< lo -1e+308) (> hi 1e+308))
                  (/ (- x lo) (- hi lo)))