Skip to content

evaluate_rational should check for abs(x) <= 1 instead of x <= 1 when deciding to use 1/x instead of x. #1139

Closed
@WarrenWeckesser

Description

@WarrenWeckesser

In the detail functions for evaluate_rational defined in the files rational_horner#_#.hpp in https://github.com/boostorg/math/tree/develop/include/boost/math/tools/detail, there is code such as this:

template <class T, class U, class V>
inline V evaluate_rational_c_imp(const T* a, const U* b, const V& x, const std::integral_constant<int, 6>*) BOOST_MATH_NOEXCEPT(V)
{
   if(x <= 1)
     return static_cast<V>((((((a[5] * x + a[4]) * x + a[3]) * x + a[2]) * x + a[1]) * x + a[0]) / (((((b[5] * x + b[4]) * x + b[3]) * x + b[2]) * x + b[1]) * x + b[0]));
   else
   {
      V z = 1 / x;
      return static_cast<V>((((((a[0] * z + a[1]) * z + a[2]) * z + a[3]) * z + a[4]) * z + a[5]) / (((((b[0] * z + b[1]) * z + b[2]) * z + b[3]) * z + b[4]) * z + b[5]));
   }
}

I think the condition x <= 1 should be changed to (-1 <= x) && (x <= 1) (i.e. abs(x) <= 1). Currently, the following program prints 1 nan, but I expect it to print 1 1.

#include <iostream>
#include "boost/math/tools/rational.hpp"

int main(int argc, char *argv[])
{
    const double a[] = {1.0, 1.0, 1.0, 1.0, 1.0};
    const double b[] = {1.0, 1.0, 1.0, 1.0, 1.0};

    double x1 = 1e80;
    double y1 = boost::math::tools::evaluate_rational(a, b, x1);

    double x2 = -1e80;
    double y2 = boost::math::tools::evaluate_rational(a, b, x2);
    
    std::cout << y1 << " " << y2 << std::endl;

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions