is_integer incorrect for infinity inputs
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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