6 releases
0.2.3 | Apr 30, 2023 |
---|---|
0.2.2 | Apr 26, 2023 |
0.1.1 | Apr 17, 2023 |
#875 in Algorithms
55 downloads per month
48KB
925 lines
Number-Based: Tool for working with number bases
Number-Based is an attempt of mine to make working with number bases simple.
Overview
There are currently two types around which this crate is built: uBase and iBase (hopefully others will come in the near future). These are pretty much equivalent to u32/u64 and i32/i64 respectively, but with the caveat that they can be in and be converted to any base, $B \in \mathbb{N}$.
As the base increases, the numbers will begin to look a bit crazy (because western number systems have no representation for numbers in bases, $B > 10$). For bases 10 through 36, numbers are represented as letters. There is no general consensus on what symbols should be used for number bases above 36, so this crate follows the series of utf8-encodings for numbers in bases greater than 36. Do note that not all utf8-encodings are used because some are not valid figures to be used in a string for representing a number. Also note that "-" is not a valid grapheme to use in any number as it represents a negative number (see example below). In order specify graphemes different from what's included in this crate, use custom_graphemes feature (see documentation). Reference for using custom graphemes can be found in the examples folder Github.
Usage
Examples (these can be found in the examples folder Github):
Converting a uBase/iBase instance
// converting down
// create uBase instance
let mut number = uBase::from_string(7863, "NUMBERBASED");
// convert number to base 10
number.convert(10);
assert_eq!(number.display(), String::from("20781774882369576414324066149192513674771"));
// Do aslso note that number.conver(10).display() is equivalent to number.as_decimal() with the
// important distinction that as_decimal() returns a u128 and not a string
// converting up
// create uBase instance
let mut other_number = uBase::from_string(10, "120387517860123746975");
// convert number to base 10000
other_number.convert(10000);
assert_eq!(other_number.display(), String::from("1ࡒᶹ⇵ঢᮛ"));
Performing operations with uBase/iBase instances
// create the iBase instances
let number1 = iBase::from_string(30000, "-ð䧈炙㞈榻");
assert_eq!(number1.as_decimal(), -120387517860123746975);
let number2 = iBase::from_string(30000, "20");
assert_eq!(number2.as_decimal(), 60000);
// divide the numbers
let quotient = number1 / number2;
// other operations such as addition, subtraction, and multiplication are also available with
// the operators "+", "-", and "*" respectively
assert_eq!(quotient.display(), String::from("-¦┒㡺嚊"));
assert_eq!(quotient.as_decimal(), -120387517860123746975 / 60000);
LICENSE
Apache-2.0
See LICENSE document for complete license.
Dependencies
~47KB