Skip to content

Commit 55f5ee8

Browse files
committed
bigint abs
1 parent 9fe25eb commit 55f5ee8

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

include/jsoncons/utility/bigint.hpp

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,51 +1569,6 @@ class basic_bigint
15691569
return x &= y;
15701570
}
15711571

1572-
friend basic_bigint<Allocator> abs( const basic_bigint& a )
1573-
{
1574-
if ( a.is_negative())
1575-
{
1576-
return -a;
1577-
}
1578-
return a;
1579-
}
1580-
1581-
friend basic_bigint<Allocator> power( basic_bigint<Allocator> x, unsigned n )
1582-
{
1583-
basic_bigint<Allocator> y = 1;
1584-
1585-
while ( n )
1586-
{
1587-
if ( n & 1 )
1588-
{
1589-
y *= x;
1590-
}
1591-
x *= x;
1592-
n >>= 1;
1593-
}
1594-
1595-
return y;
1596-
}
1597-
1598-
friend basic_bigint<Allocator> sqrt( const basic_bigint& a )
1599-
{
1600-
basic_bigint<Allocator> x = a;
1601-
basic_bigint<Allocator> b = a;
1602-
basic_bigint<Allocator> q;
1603-
1604-
b <<= 1;
1605-
while ( (void)(b >>= 2), b > 0 )
1606-
{
1607-
x >>= 1;
1608-
}
1609-
while ( x > (q = a/x) + 1 || x < q - 1 )
1610-
{
1611-
x += q;
1612-
x >>= 1;
1613-
}
1614-
return x < q ? x : q;
1615-
}
1616-
16171572
template <typename CharT>
16181573
friend std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os, const basic_bigint& v)
16191574
{
@@ -1629,12 +1584,12 @@ class basic_bigint
16291584
auto this_view = get_storage_view();
16301585
auto y_view = y.get_storage_view();
16311586

1587+
if ( this_view.size() == 0 && y_view.size() == 0 )
1588+
return 0;
16321589
if ( is_negative() != y.is_negative())
16331590
return y.is_negative() - is_negative();
16341591
int code = 0;
1635-
if ( this_view.size() == 0 && y_view.size() == 0 )
1636-
code = 0;
1637-
else if ( this_view.size() < y_view.size())
1592+
if ( this_view.size() < y_view.size())
16381593
code = -1;
16391594
else if ( this_view.size() > y_view.size())
16401595
code = +1;
@@ -1673,6 +1628,7 @@ class basic_bigint
16731628
if ( num < denom )
16741629
{
16751630
quot = value_type(0);
1631+
quot.set_negative(quot_neg);
16761632
rem = num;
16771633
rem.set_negative(rem_neg);
16781634
return;
@@ -1849,7 +1805,7 @@ class basic_bigint
18491805
a[n] = 0;
18501806
}
18511807
}
1852-
public:
1808+
18531809
bool normalize(basic_bigint& denom, basic_bigint& num, int& x) const
18541810
{
18551811
auto denom_view = denom.get_storage_view();
@@ -1894,7 +1850,7 @@ class basic_bigint
18941850
rem.reduce();
18951851
}
18961852
}
1897-
private:
1853+
18981854
size_type round_up(size_type i) const // Find suitable new block size
18991855
{
19001856
return (i/word_length + 1) * word_length;
@@ -1978,6 +1934,54 @@ class basic_bigint
19781934
}
19791935
};
19801936

1937+
template <typename Allocator>
1938+
basic_bigint<Allocator> abs( const basic_bigint<Allocator>& a )
1939+
{
1940+
if ( a.is_negative())
1941+
{
1942+
return -a;
1943+
}
1944+
return a;
1945+
}
1946+
1947+
template <typename Allocator>
1948+
basic_bigint<Allocator> pow(const basic_bigint<Allocator>& x, unsigned n)
1949+
{
1950+
basic_bigint<Allocator> y = 1;
1951+
1952+
while ( n )
1953+
{
1954+
if ( n & 1 )
1955+
{
1956+
y *= x;
1957+
}
1958+
x *= x;
1959+
n >>= 1;
1960+
}
1961+
1962+
return y;
1963+
}
1964+
1965+
template <typename Allocator>
1966+
basic_bigint<Allocator> sqrt(const basic_bigint<Allocator>& a)
1967+
{
1968+
basic_bigint<Allocator> x = a;
1969+
basic_bigint<Allocator> b = a;
1970+
basic_bigint<Allocator> q;
1971+
1972+
b <<= 1;
1973+
while ( (void)(b >>= 2), b > 0 )
1974+
{
1975+
x >>= 1;
1976+
}
1977+
while ( x > (q = a/x) + 1 || x < q - 1 )
1978+
{
1979+
x += q;
1980+
x >>= 1;
1981+
}
1982+
return x < q ? x : q;
1983+
}
1984+
19811985
using bigint = basic_bigint<std::allocator<uint8_t>>;
19821986

19831987
} // namespace jsoncons

test/corelib/src/utility/bigint_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ TEST_CASE("https://github.com/rgroshanrg/bigint SampleTest.cpp")
577577
}
578578
SECTION("abs")
579579
{
580-
bigint c = abs(b);
580+
bigint c = jsoncons::abs(b);
581581
CHECK(bigint::parse("60820564691661355463515465564664568") == c);
582582
}
583583
}
584+

0 commit comments

Comments
 (0)