Skip to main content

Module numbertools

Module numbertools 

Source
Expand description

music21’s common.numberTools: quarter lengths kept exact, numbers rounded and written the way music21 writes them, Roman numerals and ordinals.

Structs§

NearestMultiple
The multiple of a unit nearest a value, as nearest_multiple finds it.

Enums§

IntOrFloat
A whole number or a float, which is what music21’s numToIntOrFloat and roundToHalfInteger give back.
OffsetQL
A quarter length or an offset as music21 keeps one: a float where a float says it exactly, a fraction where none does.

Constants§

DENOM_LIMIT
music21’s defaults.limitOffsetDenominator: the largest denominator op_frac keeps a quarter length as a fraction over.
MUSIC_ORDINAL_NAMES
music21’s musicOrdinals: ORDINAL_NAMES with an interval’s own names for the first, the eighth, the fifteenth and the twenty-second.
ORDINAL_NAMES
music21’s ordinals: the names of the numbers from nought to twenty-two, in English.

Functions§

add_float_precision
The third or sixth a value is within grain of, as op_frac gives it, or nothing for a value near neither: music21’s addFloatPrecision, which leaves such a value alone.
approximate_gcd
The greatest length that goes a whole number of times, to within grain, into every one of values: music21’s approximateGCD, which tries the smallest value, then each value divided by one to sixteen.
contiguous_list
Whether each value is one more than the one before: music21’s contiguousList, which wants them sorted first. An empty list is contiguous here; music21 cannot look at its first value.
contiguous_runs
The runs of sorted values each one more than the one before, as ranges of their positions: what music21’s groupContiguousIntegers groups.
decimal_to_tuplet
The tuplet ratio, in lowest terms, that a simple decimal stands for: music21’s decimalToTuplet. Below one, the denominator is the larger.
dot_multiplier
What dots dots multiply a note’s length by: music21’s dotMultiplier. It is always exact, since its denominator is a power of two.
float_ratio
The exact fraction a float stands for, in lowest terms, as Python’s float.as_integer_ratio gives it.
float_repr
A float written as Python’s repr writes it: the shortest digits that read back as the same float, 1.0 for a whole number, and an exponent below a ten-thousandth or from 1e16 up.
from_roman
A Roman numeral, upper or lower case, read as a number: music21’s fromRoman. The medieval IIII and IC are read too, unless strict_modern refuses a subtraction of less than a tenth.
is_binary_denominator
Whether denominator is a power of two, which is when music21’s opFrac turns a fraction into a float.
is_close
Python’s math.isclose(a, b, abs_tol=abs_tol), with its relative tolerance of one in a billion.
limit_denominator
The fraction nearest numerator / denominator whose denominator is at most limit: Python’s Fraction.limit_denominator, which music21’s _preFracLimitDenominator copies. A fraction already within the limit comes back as it is.
mixed_numeral
A number as a whole number and a fraction, 1 1/3, with the fraction’s denominator limited to limit: music21’s mixedNumeral for a float.
mixed_numeral_fraction
mixed_numeral for an exact fraction, whose denominator is not limited: -4 3/7 for -31/7.
nearest_multiple
The multiple of unit nearest a value that is not negative, how far off it is and which way: music21’s nearestMultiple. Half way rounds down.
num_to_int_or_float
A value close enough to a whole number, within a millionth, as that whole number, and anything else as it is: music21’s numToIntOrFloat.
op_frac
music21’s opFrac for a float: the float itself where its exact fraction has a denominator up to DENOM_LIMIT, and otherwise the nearest fraction within the limit, written as a float again where its denominator is a power of two.
ordinal_abbreviation
The letters written after a number to make it an ordinal, st, nd, rd or th, with an s after all but st for plural: music21’s ordinalAbbreviation.
ordinals_to_numbers
music21’s ordinalsToNumbers, in the order it fills it: each ordinal name, lowered and not, and abbreviated, 1st, and each interval name that differs from it, mapped to its number.
round_to_half_integer
A number rounded to the nearest half, a quarter rounding up, as music21’s roundToHalfInteger does: a whole number where it lands on one, and a float on a half.
str_trim_float
A float written with max_digits decimals and its trailing zeros dropped, one decimal always kept: music21’s strTrimFloat.
to_roman
A number from 1 to 3999 as a Roman numeral: music21’s toRoman.
unit_boundary_proportion
The span of the unit interval each value’s share of their sum takes, one after another, the last ending at exactly one: music21’s unitBoundaryProportion.
unit_normalize_proportion
Each value as its share of their sum: music21’s unitNormalizeProportion.
weighted_index
Which of the weighted choices a draw draw from the unit interval lands on, as music21’s weightedSelection picks one: the last where it lands on none.