Fix fraction representation in output

Fix to_fraction() in src/pitch.py to handle negative exponents
correctly without floating point precision loss.

Before: 2573485501354569/2251799813685248
After:  8/7
This commit is contained in:
Michael Winter 2026-03-17 15:30:05 +01:00
parent e4b6d2cfdc
commit e687087e76

View file

@ -42,11 +42,17 @@ class Pitch:
def to_fraction(self) -> Fraction: def to_fraction(self) -> Fraction:
"""Convert to frequency ratio (e.g., 3/2).""" """Convert to frequency ratio (e.g., 3/2)."""
from math import prod from fractions import Fraction
return Fraction( numerator = 1
prod(pow(self.dims[d], self.hs_array[d]) for d in range(len(self.dims))) denominator = 1
) for d in range(len(self.dims)):
exp = self.hs_array[d]
if exp >= 0:
numerator *= self.dims[d] ** exp
else:
denominator *= self.dims[d] ** (-exp)
return Fraction(numerator, denominator)
def to_cents(self) -> float: def to_cents(self) -> float:
"""Convert to cents (relative to 1/1 = 0 cents).""" """Convert to cents (relative to 1/1 = 0 cents)."""