pub struct Radix { /* private fields */ }
Expand description
Radix number, that is usually written as numberbase (4448 for
example). Base can be only in range 2..=10
.
Implementations§
Source§impl Radix
impl Radix
Sourcepub const fn new(base: u8) -> Result<Self, RadixError>
pub const fn new(base: u8) -> Result<Self, RadixError>
Creates a new Radix
.
§Errors
Returns a BaseError
when base isn’t correct.
§Examples
use ognlib::num::radix::Radix;
let n = Radix::new(2).unwrap();
assert_eq!(n.number(), 0);
assert_eq!(n.base(), 2);
let e1 = Radix::new(1).unwrap_err();
assert_eq!(e1.to_string(), "Expected base in range `2..=10`, found 1");
let e2 = Radix::new(33).unwrap_err();
assert_eq!(e2.to_string(), "Expected base in range `2..=10`, found 33");
Sourcepub fn from_radix(number: usize, base: u8) -> Result<Self, RadixError>
pub fn from_radix(number: usize, base: u8) -> Result<Self, RadixError>
Creates a new Radix
with given number and base.
§Panics
Panics if a char from RADIX
is somehow not parsed
§Errors
Returns a BaseError
if base isn’t correct; NumberError
if number isn’t correct.
§Examples
use ognlib::num::radix::Radix;
let n = Radix::from_radix(11010000, 2).unwrap();
assert_eq!(n.number(), 11010000);
assert_eq!(n.base(), 2);
let e = Radix::from_radix(444, 3).unwrap_err();
assert_eq!(e.to_string(), "Number contains a digit (4) that is more or equal than base (3)",);
Sourcepub fn to_radix(self, base: u8) -> Result<Self, RadixError>
pub fn to_radix(self, base: u8) -> Result<Self, RadixError>
Translate Radix
to another Radix
.
§Panics
Panics if k is less than 2 or k more than 36.
§Errors
Returns a BaseError
when base isn’t correct; ParseError
if there was error with
parse functions.
§Examples
use ognlib::num::radix::Radix;
let n1 = Radix::from(123);
let new1 = n1.to_radix(8).unwrap();
let n2 = Radix::from_radix(173, 8).unwrap();
let new2 = n2.to_radix(10).unwrap();
assert_eq!(new1.radix(), (173, 8));
assert_eq!(new2.radix(), (123, 10));
let e = new2.to_radix(1).unwrap_err();
assert_eq!(e.to_string(), "Expected base in range `2..=10`, found 1");
Sourcepub fn to_str_radix(self, base: u8) -> Result<StringRadix, RadixError>
pub fn to_str_radix(self, base: u8) -> Result<StringRadix, RadixError>
Translate Radix
to another StringRadix
.
§Panics
Panics if k is less than 2 or k more than 36.
§Errors
Returns a BaseError
when base isn’t correct; ParseError
if there was error with
parse functions.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n = Radix::from_radix(11010000, 2).unwrap();
let res = n.to_str_radix(16).unwrap();
assert_eq!(res.radix(), ("D0", 16));
let e = n.to_str_radix(42).unwrap_err();
assert_eq!(e.to_string(), "Expected base in range `2..=36`, found 42");
Sourcepub fn add_to_str(
self,
other: Self,
base: u8,
) -> Result<StringRadix, RadixError>
pub fn add_to_str( self, other: Self, base: u8, ) -> Result<StringRadix, RadixError>
Sum 2 Radix
to new StringRadix
.
§Errors
Same as to_str_radix
.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let res = Radix::add_to_str(n1, n2, 16).unwrap();
assert_eq!(res.radix(), ("97", 16));
Sourcepub fn sub_to_str(
self,
other: Self,
base: u8,
) -> Result<StringRadix, RadixError>
pub fn sub_to_str( self, other: Self, base: u8, ) -> Result<StringRadix, RadixError>
Sub 2 Radix
to new StringRadix
.
§Errors
Same as to_str_radix
.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let res = Radix::sub_to_str(n2, n1, 16).unwrap();
assert_eq!(res.radix(), ("61", 16));
Sourcepub fn mul_to_str(
self,
other: Self,
base: u8,
) -> Result<StringRadix, RadixError>
pub fn mul_to_str( self, other: Self, base: u8, ) -> Result<StringRadix, RadixError>
Mul 2 Radix
to new StringRadix
.
§Errors
Same as to_str_radix
.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let res = Radix::mul_to_str(n1, n2, 16).unwrap();
assert_eq!(res.radix(), ("D14", 16));
Trait Implementations§
Source§impl Add<usize> for Radix
impl Add<usize> for Radix
Source§fn add(self, other: usize) -> Self::Output
fn add(self, other: usize) -> Self::Output
Performs a +
operation (usize
as rhs
).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n = Radix::from_radix(123, 4).unwrap();
let n_str = StringRadix::from_radix("123", 4).unwrap();
let res = (n + 124).to_radix(8).unwrap();
let res_str = (n_str + 124).to_radix(8).unwrap();
assert_eq!(res.radix(), (227, 8));
assert_eq!(res_str.radix(), ("227", 8));
Source§impl Add for Radix
impl Add for Radix
Source§fn add(self, other: Self) -> Self::Output
fn add(self, other: Self) -> Self::Output
Performs a +
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
let res = (n1 + n2).to_radix(8).unwrap();
let res_str = (n_str1 + n_str2).to_radix(8).unwrap();
assert_eq!(res.radix(), (227, 8));
assert_eq!(res_str.radix(), ("227", 8));
Source§impl AddAssign<usize> for Radix
impl AddAssign<usize> for Radix
Source§fn add_assign(&mut self, other: usize)
fn add_assign(&mut self, other: usize)
Performs a +=
operation (usize
is rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n = Radix::from_radix(123, 4).unwrap();
let mut n_str = StringRadix::from_radix("123", 4).unwrap();
n += 124;
n_str += 124;
n = n.to_radix(8).unwrap();
n_str = n_str.to_radix(8).unwrap();
assert_eq!(n.radix(), (227, 8));
assert_eq!(n_str.radix(), ("227", 8));
Source§impl AddAssign for Radix
impl AddAssign for Radix
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Performs a +=
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let mut n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
n1 += n2;
n_str1 += n_str2;
n1 = n1.to_radix(8).unwrap();
n_str1 = n_str1.to_radix(8).unwrap();
assert_eq!(n1.radix(), (227, 8));
assert_eq!(n_str1.radix(), ("227", 8));
Source§impl Div<usize> for Radix
impl Div<usize> for Radix
Source§fn div(self, other: usize) -> Self::Output
fn div(self, other: usize) -> Self::Output
Performs a /
operation (usize
as rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n = Radix::from_radix(444, 5).unwrap();
let n_str = StringRadix::from_radix("444", 5).unwrap();
let res = (n / 27).to_radix(8).unwrap();
let res_str = (n_str / 27).to_radix(8).unwrap();
assert_eq!(res.radix(), (4, 8));
assert_eq!(res_str.radix(), ("4", 8));
Source§impl Div for Radix
impl Div for Radix
Source§fn div(self, other: Self) -> Self::Output
fn div(self, other: Self) -> Self::Output
Performs a /
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
let res = (n2 / n1).to_radix(8).unwrap();
let res_str = (n_str2 / n_str1).to_radix(8).unwrap();
assert_eq!(res.radix(), (4, 8));
assert_eq!(res_str.radix(), ("4", 8));
Source§impl DivAssign<usize> for Radix
impl DivAssign<usize> for Radix
Source§fn div_assign(&mut self, other: usize)
fn div_assign(&mut self, other: usize)
Performs a /=
operation (usize
as rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n = Radix::from_radix(444, 5).unwrap();
let mut n_str = StringRadix::from_radix("444", 5).unwrap();
n /= 27;
n_str /= 27;
n = n.to_radix(8).unwrap();
n_str = n_str.to_radix(8).unwrap();
assert_eq!(n.radix(), (4, 8));
assert_eq!(n_str.radix(), ("4", 8));
Source§impl DivAssign for Radix
impl DivAssign for Radix
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
Performs a /=
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let mut n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let mut n_str2 = StringRadix::from_radix("444", 5).unwrap();
n2 /= n1;
n_str2 /= n_str1;
n2 = n2.to_radix(8).unwrap();
n_str2 = n_str2.to_radix(8).unwrap();
assert_eq!(n2.radix(), (4, 8));
assert_eq!(n_str2.radix(), ("4", 8));
Source§impl FromStr for Radix
impl FromStr for Radix
Source§fn from_str(number: &str) -> Result<Self, Self::Err>
fn from_str(number: &str) -> Result<Self, Self::Err>
Creates a new Radix
with base 10 and given str
number.
§Errors
Returns ParseIntError
if number contains invalid digits.
§Examples
use {core::str::FromStr, ognlib::num::radix::Radix};
let n = Radix::from_str("123").unwrap();
assert_eq!(n.radix(), (123, 10));
let e = Radix::from_str("12A").unwrap_err();
assert_eq!(e.to_string(), "invalid digit found in string");
Source§type Err = ParseIntError
type Err = ParseIntError
Source§impl Mul<usize> for Radix
impl Mul<usize> for Radix
Source§fn mul(self, other: usize) -> Self::Output
fn mul(self, other: usize) -> Self::Output
Performs a *
operation (usize
as rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n = Radix::from_radix(123, 4).unwrap();
let n_str = StringRadix::from_radix("123", 4).unwrap();
let res = (n * 124).to_radix(8).unwrap();
let res_str = (n_str * 124).to_radix(8).unwrap();
assert_eq!(res.radix(), (6424, 8));
assert_eq!(res_str.radix(), ("6424", 8));
Source§impl Mul for Radix
impl Mul for Radix
Source§fn mul(self, other: Self) -> Self::Output
fn mul(self, other: Self) -> Self::Output
Performs a *
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
let res = (n1 * n2).to_radix(8).unwrap();
let res_str = (n_str1 * n_str2).to_radix(8).unwrap();
assert_eq!(res.radix(), (6424, 8));
assert_eq!(res_str.radix(), ("6424", 8));
Source§impl MulAssign<usize> for Radix
impl MulAssign<usize> for Radix
Source§fn mul_assign(&mut self, other: usize)
fn mul_assign(&mut self, other: usize)
Performs a *=
operation (usize
as rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n = Radix::from_radix(123, 4).unwrap();
let mut n_str = StringRadix::from_radix("123", 4).unwrap();
n *= 124;
n_str *= 124;
n = n.to_radix(8).unwrap();
n_str = n_str.to_radix(8).unwrap();
assert_eq!(n.radix(), (6424, 8));
assert_eq!(n_str.radix(), ("6424", 8));
Source§impl MulAssign for Radix
impl MulAssign for Radix
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
Performs a *=
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let mut n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
n1 *= n2;
n_str1 *= n_str2;
n1 = n1.to_radix(8).unwrap();
n_str1 = n_str1.to_radix(8).unwrap();
assert_eq!(n1.radix(), (6424, 8));
assert_eq!(n_str1.radix(), ("6424", 8));
Source§impl PartialOrd for Radix
impl PartialOrd for Radix
Source§impl Rem<usize> for Radix
impl Rem<usize> for Radix
Source§fn rem(self, other: usize) -> Self::Output
fn rem(self, other: usize) -> Self::Output
Performs a %
operation (usize
as rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n = Radix::from_radix(444, 5).unwrap();
let n_str = StringRadix::from_radix("444", 5).unwrap();
let res = (n % 27).to_radix(8).unwrap();
let res_str = (n_str % 27).to_radix(8).unwrap();
assert_eq!(res.radix(), (20, 8));
assert_eq!(res_str.radix(), ("20", 8));
Source§impl Rem for Radix
impl Rem for Radix
Source§fn rem(self, other: Self) -> Self::Output
fn rem(self, other: Self) -> Self::Output
Performs a %
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
let res = (n2 % n1).to_radix(8).unwrap();
let res_str = (n_str2 % n_str1).to_radix(8).unwrap();
assert_eq!(res.radix(), (20, 8));
assert_eq!(res_str.radix(), ("20", 8));
Source§impl RemAssign<usize> for Radix
impl RemAssign<usize> for Radix
Source§fn rem_assign(&mut self, other: usize)
fn rem_assign(&mut self, other: usize)
Performs a %=
operation (usize
as rhs).
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n = Radix::from_radix(444, 5).unwrap();
let mut n_str = StringRadix::from_radix("444", 5).unwrap();
n %= 27;
n_str %= 27;
n = n.to_radix(8).unwrap();
n_str = n_str.to_radix(8).unwrap();
assert_eq!(n.radix(), (20, 8));
assert_eq!(n_str.radix(), ("20", 8));
Source§impl RemAssign for Radix
impl RemAssign for Radix
Source§fn rem_assign(&mut self, other: Self)
fn rem_assign(&mut self, other: Self)
Performs a %=
operation.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let mut n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let mut n_str2 = StringRadix::from_radix("444", 5).unwrap();
n2 %= n1;
n_str2 %= n_str1;
n2 = n2.to_radix(8).unwrap();
n_str2 = n_str2.to_radix(8).unwrap();
assert_eq!(n2.radix(), (20, 8));
assert_eq!(n_str2.radix(), ("20", 8));
Source§impl Sub<usize> for Radix
impl Sub<usize> for Radix
Source§fn sub(self, other: usize) -> Self::Output
fn sub(self, other: usize) -> Self::Output
Performs a -
operation (usize
is rhs). Result of operation is an absolute
value. Base of resulting number is the base of greater number.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n = Radix::from_radix(123, 4).unwrap();
let n_str = StringRadix::from_radix("123", 4).unwrap();
let res = (n - 124).to_radix(8).unwrap();
let res_str = (n_str - 124).to_radix(8).unwrap();
assert_eq!(res.radix(), (141, 8));
assert_eq!(res_str.radix(), ("141", 8));
Source§impl Sub for Radix
impl Sub for Radix
Source§fn sub(self, other: Self) -> Self::Output
fn sub(self, other: Self) -> Self::Output
Performs a -
operation. Result of the operation is an absolute value. Base of the
resulting number is the base of the greater number.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
let res = (n1 - n2).to_radix(8).unwrap();
let res_str = (n_str1 - n_str2).to_radix(8).unwrap();
assert_eq!(res.radix(), (141, 8));
assert_eq!(res_str.radix(), ("141", 8));
Source§impl SubAssign<usize> for Radix
impl SubAssign<usize> for Radix
Source§fn sub_assign(&mut self, other: usize)
fn sub_assign(&mut self, other: usize)
Performs a -=
operation (usize
as rhs).
The same rules as for common sub are applied.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n = Radix::from_radix(123, 4).unwrap();
let mut n_str = StringRadix::from_radix("123", 4).unwrap();
n -= 124;
n_str -= 124;
n = n.to_radix(8).unwrap();
n_str = n_str.to_radix(8).unwrap();
assert_eq!(n.radix(), (141, 8));
assert_eq!(n_str.radix(), ("141", 8));
Source§impl SubAssign for Radix
impl SubAssign for Radix
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Performs a -=
operation. The same rules as for common sub are applied.
§Examples
use ognlib::num::radix::{Radix, StringRadix};
let mut n1 = Radix::from_radix(123, 4).unwrap();
let n2 = Radix::from_radix(444, 5).unwrap();
let mut n_str1 = StringRadix::from_radix("123", 4).unwrap();
let n_str2 = StringRadix::from_radix("444", 5).unwrap();
n1 -= n2;
n_str1 -= n_str2;
n1 = n1.to_radix(8).unwrap();
n_str1 = n_str1.to_radix(8).unwrap();
assert_eq!(n1.radix(), (141, 8));
assert_eq!(n_str1.radix(), ("141", 8));
impl Copy for Radix
impl Eq for Radix
impl StructuralPartialEq for Radix
Auto Trait Implementations§
impl Freeze for Radix
impl RefUnwindSafe for Radix
impl Send for Radix
impl Sync for Radix
impl Unpin for Radix
impl UnwindSafe for Radix
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more