bytesize._util.misc module

Miscellaneous utilities.

bytesize._util.misc.convert_magnitude(value, places=2)

Convert magnitude to a decimal string.

Parameters:
  • value (a numeric value, not a float) – any value
  • places (an integer type or NoneType) – number of decimal places to use, default is 2
Returns:

a string representation of value

Return type:

str

Since a rational number may be a non-terminating decimal quantity, this representation is not guaranteed to be exact, regardless of the value of places.

Even in the case of a terminating decimal representation, the representation may be inexact if the number of significant digits is too large for the precision of the Decimal operations as specified by the context.

bytesize._util.misc.decimal_magnitude(value)

Get value as a possibly repeating decimal.

Parameters:value (any precise numerical quantity) – the value
Returns:a precise decimal representation of the number
Return type:tuple of int * list of int * int

The parts represent the non-fractional part, the fractional part including the first repeating part, the length of the repeating part.

bytesize._util.misc.get_repeating_fraction(numerator, denominator)

Get the repeating decimal number corresponding to the ratio of numerator and denominator.

Parameters:
  • numerator (int) – the numerator
  • denominator (int) – the denominator
Returns:

a list of decimal digits and a number indicating length of repeat

Return type:

tuple of (list of int) * int

Prereq: numerator < denominator, denominator > 0, numerator >= 0

bytesize._util.misc.get_string_info(magnitude, places)

Get information about the string that represents this magnitude.

Parameters:
  • magnitude (Fraction) – the magnitude
  • places (int) – the number of places after the decimal pt
Returns:

a pair, indicating whether the value is exact and the value

Rtypes:

tuple of bool * str

bytesize._util.misc.long_decimal_division(divisor, dividend)

Precise division of two precise quantities.

Parameters:
  • divisor (any precise numeric quantity) – the divisor
  • dividend (any precise numeric quantity) – the dividend
Returns:

the result of long division

Return type:

a tuple of int * list * list

:raises SizeValueError: on bad input

The result is the number to the left of the decimal point, a list of the non-repeating digits to the right of the decimal point, and a list of the repeating digits.

bytesize._util.misc.round_fraction(value, rounding)

Round a fraction to an integer according to rounding method.

Parameters:
  • value (Fraction) – value to round
  • rounding (a member of RoundingMethods) – rounding method
Returns:

a rounded integer

Return type:

int

Previous topic

bytesize._util.bases module

Next topic

bytesize._config module

This Page