justbases._nats module

Methods dealing exclusively with natural numbers.

class justbases._nats.Nats

Bases: object

Methods to convert non-negative ints.

static carry_in(value, carry, base)

Add a carry digit to a number represented by value.

Parameters:
  • value (list of int) – the value
  • carry (int) – the carry digit (>= 0)
  • base (int) – the base (>= 2)
Returns:

carry-out and result

Return type:

tuple of int * (list of int)

Complexity: O(len(value))

classmethod convert(value, from_base, to_base)

Convert value from a base to a base.

Parameters:
  • value (sequence of int) – the value to convert
  • from_base (int) – base of value
  • to_base (int) – base of result
Returns:

the conversion result

Return type:

list of int

Raises:
  • ConvertError – if from_base is less than 2
  • ConvertError – if to_base is less than 2
  • ConvertError – if elements in value outside bounds
Preconditions:
  • all integers in value must be no less than 0
  • from_base, to_base must be at least 2

Complexity: O(len(value))

static convert_from_int(value, to_base)

Convert int value to a base.

Parameters:
  • value (int) – the value to convert, must be at least 0
  • to_base (int) – base of result, must be at least 2
Returns:

the conversion result

Return type:

list of int

Raises:
  • BasesValueError – if value is less than 0
  • BasesValueError – if to_base is less than 2
Preconditions:
  • to_base must be at least 2

Complexity: O(log_{to_base}(value))

static convert_to_int(value, from_base)

Convert value to an int.

Parameters:
  • value (sequence of int) – the value to convert
  • from_base (int) – base of value
Returns:

the conversion result

Return type:

int

Raises:
  • ConvertError – if from_base is less than 2
  • ConvertError – if elements in value outside bounds
Preconditions:
  • all integers in value must be at least 0
  • all integers in value must be less than from_base
  • from_base must be at least 2

Complexity: O(len(value))

Previous topic

justbases._errors module

Next topic

justbases._rationals module

This Page