llvm.org GIT mirror llvm / 643edab
Fix APFloat from string conversion for Inf The method IEEEFloat::convertFromStringSpecials() does not recognize the "+Inf" and "-Inf" strings but these strings are printed for the double Infinities by the IEEEFloat::toString(). This patch adds the "+Inf" and "-Inf" strings to the list of recognized patterns in IEEEFloat::convertFromStringSpecials(). Re-landing after fix. Reviewers: sberg, bogner, majnemer, timshen, rnk, skatkov, gottesmm, bkramer, scanon, anna Reviewed By: anna Subscribers: mkazantsev, FlameTop, llvm-commits, reames, apilipenko Differential Revision: https://reviews.llvm.org/D38030 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321054 91177308-0d34-0410-b5e6-96231b3b80d8 Serguei Katkov 2 years ago
4 changed file(s) with 26 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
25452545 }
25462546
25472547 bool IEEEFloat::convertFromStringSpecials(StringRef str) {
2548 if (str.equals("inf") || str.equals("INFINITY")) {
2548 if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) {
25492549 makeInf(false);
25502550 return true;
25512551 }
25522552
2553 if (str.equals("-inf") || str.equals("-INFINITY")) {
2553 if (str.equals("-inf") || str.equals("-INFINITY") || str.equals("-Inf")) {
25542554 makeInf(true);
25552555 return true;
25562556 }
585585 APFloat::opStatus Status =
586586 F.convertFromString(*this, APFloat::rmNearestTiesToEven);
587587 if (Status != APFloat::opOK) {
588 if (!AllowInexact || Status != APFloat::opInexact)
588 if (!AllowInexact || !(Status & APFloat::opInexact))
589589 return true;
590590 }
591591
846846 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-1e-99999").isNegZero());
847847
848848 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
849 }
850
851 TEST(APFloatTest, fromToStringSpecials) {
852 auto expects = [] (const char *first, const char *second) {
853 std::string roundtrip = convertToString(convertToDoubleFromString(second), 0, 3);
854 EXPECT_STREQ(first, roundtrip.c_str());
855 };
856 expects("+Inf", "+Inf");
857 expects("+Inf", "INFINITY");
858 expects("+Inf", "inf");
859 expects("-Inf", "-Inf");
860 expects("-Inf", "-INFINITY");
861 expects("-Inf", "-inf");
862 expects("NaN", "NaN");
863 expects("NaN", "nan");
864 expects("NaN", "-NaN");
865 expects("NaN", "-nan");
849866 }
850867
851868 TEST(APFloatTest, fromHexadecimalString) {
874874 {"0.0", false, false, 0.0},
875875 {"-0.0", false, false, -0.0},
876876 {"123.45", false, true, 123.45},
877 {"123.45", true, false, 123.45}};
877 {"123.45", true, false, 123.45},
878 {"1.8e308", true, false, std::numeric_limits::infinity()},
879 {"1.8e308", false, true, std::numeric_limits::infinity()},
880 {"0x0.0000000000001P-1023", false, true, 0.0},
881 {"0x0.0000000000001P-1023", true, false, 0.0},
882 };
878883
879884 TEST(StringRefTest, getAsDouble) {
880885 for (const auto &Entry : DoubleStrings) {