#include "algebra/algebra.h"
#include <print>
using namespace algebra;
using namespace algebra::literals;
int main(int argc, char* argv[]) {
int e = 2;
integer i = 7_i + e;
rational r = 5/6_q;
rational a = r * i;
std::print("{} | {:.2f}\n", a, a); // prints 15/2 7.50
std::print("{:.20}\n", sqrt(2_q, 8)); // prints 1.41421356237309504880
decimal d = 1.1_d; // stored exactly (unlike float and double which can't represent this value)
std::print("{}\n", d); // prints 1.1
return 0;
}
- header-only and no dependencies
- full
constexpr
andstd::format()
support - arbitrary precision and compact algebraic data types
natural
/integer
/rational
/real<>
/decimal
classes behave similarly to built-inint
andfloat
types (except for overflow)- no heap allocation for integer values in
[-UINT64, UINT64]
range - all types support casting to and from all built-in integer and floating point types
- no silent overflow / failures (std::runtime_error is thrown)
- output using
std::format()
/std::print()
/std::ostream
/.str()
real
allows more compact and efficient representation thanrational
, but requires roundingreal<2>
is similar to built-infloat
anddouble
, but with arbitrary long mantissa, and 32-bit exponentdecimal
alias forreal<10>
sizeof(integer)
is 16 bytes,sizeof(rational)
is 32 bytes, whilestd::vector<>
is 24 bytes
- multiplication and division currently use
O(N^2)
algorithms where N is number of 64-bit words used
Overloaded operators:
- arithmetic
+
-
*
/
%
+=
-=
*=
/=
%=
++
--
- relational
<
>
<=
>=
==
!=
- shift
<<
>>
<<=
>>=
- bitwise
~
|
&
^
|=
&=
^=
- Allows low level access to vector of individual words of this number.
- Initializes to
0
value.
- Returns number of trailing zeros in binary representation.
- Same as
(a & 1) == 0
, but avoids temporary allocation for result of&
- Same as
(a & 1) == 1
, but avoids temporary allocation for result of&
- Same as
natual::str(16)
- Ininializes rational as a/b, and simplifies by removing common divisor.
- Same as
rational::rational(integer a, integer b)
, but assuming a and b are already simplified.
- You can use
.simplify()
after directly modifying.num
and.den
fields, to remove common factors from them. - It throws exception if
den
is zero. - Note that
rational
is automatically simplified after all arithmetic operations.
- Swap
num
andden
in-place. Throws exception ifnum
is zero.
- Same as
a = -a
, but performed in-place without memory allocation.
- Alias for
std::shared_ptr<expr>
Overloaded operators:
- arithmetic
+
-
*
/
%
- relational
<
>
<=
>=
==
!=
- returns
static_cast<unsigned __int128>(a >> e)
without memory allocation (except for result itself)
- returns
static_cast<uint64_t>(a >> e)
without memory allocation
- uniformly sample from [0, count-1]
- assumes that
a
andb
are in[0, m-1]
range - updates
a
to(a * b) % m
- returns
(a**n) % p
- Miller-Rabin algorithm
- It returns false if n is composite and returns true if n is probably prime.
rounds
is an input parameter that determines accuracy level.- Higher value of
rounds
indicates more accuracy.
- https://en.wikipedia.org/wiki/Chudnovsky_algorithm for computing PI