Fix calculate_cents - use simple math.log2 instead of broken bit_length logic

This commit is contained in:
Michael Winter 2026-04-01 16:47:45 +02:00
parent 8f332fac52
commit 5b92e4ff83

View file

@ -64,19 +64,11 @@ def parse_fraction(frac_str):
def calculate_cents(fraction_str):
"""Calculate cents from fraction string."""
import math
fr = parse_fraction(fraction_str)
if fr <= 0:
return 0
return 1200 * (
float(fr).bit_length()
- 1
+ (fr / (1 << (fr.bit_length() - 1)) - 1) / 0.6931471805599453
if hasattr(fr, "bit_length")
else 1200 * (float(fr).bit_length() - 1)
)
# Simpler: use log2
import math
return 1200 * math.log2(fr)