Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r83103 - in trunk: boost/multiprecision/cpp_int libs/multiprecision/test
From: john_at_[hidden]
Date: 2013-02-23 12:22:19


Author: johnmaddock
Date: 2013-02-23 12:22:18 EST (Sat, 23 Feb 2013)
New Revision: 83103
URL: http://svn.boost.org/trac/boost/changeset/83103

Log:
Fix bug in subtraction routine that leads to -0 rather than 0.
Add test cases.
Fixes #8145.
Text files modified:
   trunk/boost/multiprecision/cpp_int/add.hpp | 4 ++++
   trunk/libs/multiprecision/test/test_cpp_int.cpp | 40 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 44 insertions(+), 0 deletions(-)

Modified: trunk/boost/multiprecision/cpp_int/add.hpp
==============================================================================
--- trunk/boost/multiprecision/cpp_int/add.hpp (original)
+++ trunk/boost/multiprecision/cpp_int/add.hpp 2013-02-23 12:22:18 EST (Sat, 23 Feb 2013)
@@ -223,6 +223,10 @@
 #endif
          result.sign(a.sign());
       }
+ else if((result.size() == 1) && (*pr == 0))
+ {
+ result.sign(false); // zero is unsigned.
+ }
    }
    else if(result.size() == 1)
    {

Modified: trunk/libs/multiprecision/test/test_cpp_int.cpp
==============================================================================
--- trunk/libs/multiprecision/test/test_cpp_int.cpp (original)
+++ trunk/libs/multiprecision/test/test_cpp_int.cpp 2013-02-23 12:22:18 EST (Sat, 23 Feb 2013)
@@ -418,6 +418,46 @@
       a = a - ull;
       BOOST_CHECK_EQUAL(a, test_type("0x12345600012434ffffffff0000000000000000"));
 #endif
+ //
+ // Now check that things which should be zero really are
+ // https://svn.boost.org/trac/boost/ticket/8145:
+ //
+ a = -1;
+ a += 1;
+ BOOST_CHECK_EQUAL(a, 0);
+ a = 1;
+ a += -1;
+ BOOST_CHECK_EQUAL(a, 0);
+ a = -1;
+ a += test_type(1);
+ BOOST_CHECK_EQUAL(a, 0);
+ a = 1;
+ a += test_type(-1);
+ BOOST_CHECK_EQUAL(a, 0);
+ a = test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ a -= test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ BOOST_CHECK_EQUAL(a, 0);
+ a = -test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ a += test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ BOOST_CHECK_EQUAL(a, 0);
+ a = 2;
+ a *= 0;
+ BOOST_CHECK_EQUAL(a, 0);
+ a = -2;
+ a *= 0;
+ BOOST_CHECK_EQUAL(a, 0);
+ a = 2;
+ a *= test_type(0);
+ BOOST_CHECK_EQUAL(a, 0);
+ a = -2;
+ a *= test_type(0);
+ BOOST_CHECK_EQUAL(a, 0);
+ a = -2;
+ a /= 50;
+ BOOST_CHECK_EQUAL(a, 0);
+ a = -test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ a /= (1 + test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
+ BOOST_CHECK_EQUAL(a, 0);
    }
 
    void test()


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk