stan-dev / stan-dev/math

is_integer incorrect for infinity inputs

Open
#1,239 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stan/math/prim/scal/fun/is_integer.hpp and reproduce the reported infinity case using the example in the issue. Check the existing is_integer coverage if available, then verify that positive and negative infinity produce the expected false result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.