Skip to content

is_integer incorrect for infinity inputs  #1239

Open
@andrjohns

Description

@andrjohns

Description

The checking function is_integer is defined using floor:

template <typename T>
inline bool is_integer(T x) {
  return floor(x) == x;
}

However, when floor is passed infinity, it returns it unmodified.

  • If arg is ±∞, it is returned, unmodified

So floor(double inf) returns double inf, which int turn means that is_integer(double inf) returns true.

Example

#include <stan/math/prim/scal/fun/is_integer.hpp>
#include <iostream>
#include <limits>

int main() {
  using stan::math::is_integer;

  double dbl_inf = std::numeric_limits<double>::infinity();

  std::cout << std::boolalpha
            << "is_integer(double inf): " << is_integer(dbl_inf) 
            << std::endl;
}

andrew@Lappy:~/math$ g++ -std=c++1y  -I . is_integer_test.cpp 
andrew@Lappy:~/math$ ./a.out 
is_integer(double inf): true

Expected Output

is_integer(double inf): false

Current Version:

v2.19.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions