#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
constexprandstd::format()support - arbitrary precision and compact algebraic data types
natural/integer/rational/real<>/decimalclasses behave similarly to built-inintandfloattypes (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() realallows more compact and efficient representation thanrational, but requires roundingreal<2>is similar to built-infloatanddouble, but with arbitrary long mantissa, and 32-bit exponentdecimalalias 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
0value.
- 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
natural::str(16)
- Initializes 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.numand.denfields, to remove common factors from them. - It throws exception if
denis zero. - Note that
rationalis automatically simplified after all arithmetic operations.
- Swap
numanddenin-place. Throws exception ifnumis 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
aandbare in[0, m-1]range - updates
ato(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.
roundsis an input parameter that determines accuracy level.- Higher value of
roundsindicates more accuracy.
- https://en.wikipedia.org/wiki/Chudnovsky_algorithm for computing PI