Add isInteger() to APFloat.

Useful utility function; this wasn't too hard to do before, but also wasn't
obviously discoverable.  Make it explicit.  Reviewed offline by Michael
Gottesman.

llvm-svn: 253254
This commit is contained in:
Stephen Canon 2015-11-16 21:52:48 +00:00
parent 927bdba29d
commit 1bfc89baac
3 changed files with 27 additions and 1 deletions

View File

@ -449,6 +449,9 @@ public:
/// magnitude in the current semantics.
bool isLargest() const;
/// Returns true if and only if the number is an exact integer.
bool isInteger() const;
/// @}
APFloat &operator=(const APFloat &);

View File

@ -767,6 +767,15 @@ APFloat::isLargest() const {
&& isSignificandAllOnes();
}
bool
APFloat::isInteger() const {
// This could be made more efficient; I'm going for obviously correct.
if (!isFinite()) return false;
APFloat truncated = *this;
truncated.roundToIntegral(rmTowardZero);
return compare(truncated) == cmpEqual;
}
bool
APFloat::bitwiseIsEqual(const APFloat &rhs) const {
if (this == &rhs)

View File

@ -1313,7 +1313,21 @@ TEST(APFloatTest, roundToIntegral) {
P = APFloat::getInf(APFloat::IEEEdouble, true);
P.roundToIntegral(APFloat::rmTowardZero);
EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0);
}
TEST(APFloatTest, isInteger) {
APFloat T(-0.0);
EXPECT_TRUE(T.isInteger());
T = APFloat(3.14159);
EXPECT_FALSE(T.isInteger());
T = APFloat::getNaN(APFloat::IEEEdouble);
EXPECT_FALSE(T.isInteger());
T = APFloat::getInf(APFloat::IEEEdouble);
EXPECT_FALSE(T.isInteger());
T = APFloat::getInf(APFloat::IEEEdouble, true);
EXPECT_FALSE(T.isInteger());
T = APFloat::getLargest(APFloat::IEEEdouble);
EXPECT_TRUE(T.isInteger());
}
TEST(APFloatTest, getLargest) {