inf_add
Create, add, subtract, multiply, divide, shift, compare, and display infinitely long positive numbers.
WARNING
This library is in an early stage of development. Breaking changes may occur in future versions.
Documentation is still in progress.
Usage Notes
The recommended way to perform operations on InfInt objects is to use *_destroy functions,
such as obj.add_destroy(other).
You can also use default operators, like so:
let obj a = new;
let obj b = new;
let obj c = a + b; // c is now InfInt(3)
However, the default operators will perform a clone to create a new object,
which is not very performant.
The *_destroy functions will not clone the object,
but will modify the original object in place.
let obj a = new;
let obj b = new;
a.add_destroy; // a is now InfInt(3)
These functions also return a mutable reference to the modified object, allowing you to chain multiple operations together.
let mut obj a = new;
a.add_destroy.multiply_destroy; // a is now InfInt(9)
The full list of operations is as follows:
add_destroy(&other): Addsotherto the current objectsubtract_destroy(&other): Subtractsotherfrom the current objectmultiply_destroy(&other): Multiplies the current object byotherbitshift_left_destroy(&other): Performs a left bitwise shift byotherbitsbitshift_right_destroy(&other): Performs a right bitwise shift byotherbitsbitand_destroy(&other): Performs a bitwise AND operation withotherbitor_destroy(&other): Performs a bitwise OR operation withotherdivide_destroy(&other): Divides the current object byother
You cannot create an InfInt object with no value.
If you perform an operation on an empty InfInt, it will panic.
Displaying the object
You can print the object using the fmt trait:
use InfInt;
let obj = new;
println!; // prints "123456789012345678901234567890"
You can also get a formatted decimal string representation of the object
using the to_formatted_string method:
use InfInt;
let obj = new;
let formatted = obj.to_formatted_string;
println!; // prints "1,234,567,890,123,456,789,012,345,678.90"
Keep in mind that the fmt trait will convert the number to a base 10 string representation,
which can be expensive for very large numbers.
Notes on bitshift and division
Although bitshifts can be performed on an arbitrarily large number,
the amount shifted by must be a u32 value, limiting the maximum shift to 232 bits.
This should cover the vast majority of use cases, and
any larger shifts would be extremely expensive in terms of computation.
Keep in mind that shifting 1 by 232 bits will result in 2^4294967296,
a number too large to fit on any screen.
Because division uses bitshifts internally, the maximum value resulting from a division operation is limited to 2232 bits. Once again, this should cover most use cases, and division can be done on two arbitrarily large numbers.
Implementation details
Data is stored in a vector of u128 values. The longer the vector, the higher the computation cost.
The most expensive operation is fmt,
as it requires constructing a base 10 vector representation of the number.
Future versions may cache the resulting string representation
to improve performance.