Struct number_based::signed_integer::ibase::iBase
source · pub struct iBase { /* private fields */ }Implementations§
Trait Implementations§
source§impl Add<iBase> for iBase
impl Add<iBase> for iBase
iBase instances can be added together just like regular numbers unsing the ‘+’ operator. Do note however that the resulting iBase instance will be of the same base as the first term. iBase instances of different bases can be added together without issue.
Example:
// create the iBase instances
let number1 = iBase::from_string(20, "-1287");
assert_eq!(number1.as_decimal(), -8967);
let number2 = iBase::from_string(15, "1238970");
assert_eq!(number2.as_decimal(), 13090380);
// add the numbers
let res = number1 + number2;
assert_eq!(res.base(), 20); // because number1 is of base 20, res will also be of base 20
assert_eq!(res.as_decimal(), -8967 + 13090380);source§impl Div<iBase> for iBase
impl Div<iBase> for iBase
iBase instances can be divided just like normal numbers using the ‘/’ operator. Do note however that the resulting iBase instance will be of the same base as the numerator. iBase instances of different bases can be divided without issue.
This operation works just like regular integer division; that is, the quotient is rounded down to the nearest whole number.
Example:
// create the iBase instances
let number1 = iBase::from_string(17, "-AB123");
assert_eq!(number1.as_decimal(), -889579);
let number2 = iBase::from_string(7, "456");
assert_eq!(number2.as_decimal(), 237);
// divide the numbers
let res = number1 / number2;
assert_eq!(res.as_decimal(), -889579 / 237);Do also note that if the absolute valut of the demomenator is greater than the absolute value of the numerator, the quotient will always be 0 (in whatever base the operation is performed)
Example:
// --- snip ---
// divide the numbers (|denomenator| is greater than |numerator|)
let res = number2 / number1;
// No matter what base res is, res.display will be "0".
// Hence res.as_decimal(), res.as_binary(), res.as_octal() etc. will all be 0.
assert_eq!(res.display(), String::from("0"));source§impl Mul<iBase> for iBase
impl Mul<iBase> for iBase
iBase instances can be multiplied just like normal numbers using the ‘*’ operator. Do note however that the resulting iBase instance will be of the same base as the first factor. iBase instances of different bases can be multiplied without issue.
Example:
// create the iBase instances
let number1 = iBase::from_string(2, "1001011101101011");
assert_eq!(number1.as_decimal(), 38763);
let number2 = iBase::from_string(27, "-ABGL");
assert_eq!(number2.as_decimal(), -205302);
// multiply the numbers
let res = number1 * number2;
assert_eq!(res.as_decimal(), 38763 * -205302);source§impl NumberBase for iBase
impl NumberBase for iBase
§type DecimalType = i128
type DecimalType = i128
source§fn from_string(base: u16, number: &str) -> Self
fn from_string(base: u16, number: &str) -> Self
source§fn greater_than(&self, number: Self) -> bool
fn greater_than(&self, number: Self) -> bool
source§fn as_binary(&self) -> Self::DecimalType
fn as_binary(&self) -> Self::DecimalType
source§fn as_octal(&self) -> Self::DecimalType
fn as_octal(&self) -> Self::DecimalType
source§fn as_decimal(&self) -> Self::DecimalType
fn as_decimal(&self) -> Self::DecimalType
source§impl Sub<iBase> for iBase
impl Sub<iBase> for iBase
iBase instances can be subtracted just like regular nubers using the ‘-’ operator. Do note however that the resulting iBase instance will be of the same base as the first term. iBase instances of different bases can be subtracted without issue.
Do also note that the iBase struct does not support negative numbers, so subtracting a larger number from a smaller one will result in a panic!
Example:
// create the iBase instances
let number1 = iBase::from_string(20, "1234876");
assert_eq!(number1.as_decimal(), 70915346);
let number2 = iBase::from_string(15, "-3245");
assert_eq!(number2.as_decimal(), -10640);
// subtract the numbers
let res = number1 - number2;
assert_eq!(res.as_decimal(), 70915346 - -10640);