continued_fraction uses float division, producing invalid terms for large integers and raising OverflowError
Los mantenedores suelen responder en 2 días
Evaluación
- Dificultad
- 1/5
- Tiempo estimado
- Menos de una hora
- Aptitud para principiantes
- 88/100
Línea de trabajo
Abre maths/continued_fraction.py y observa el bucle que calcula cada término: el cociente se toma con floor(numerator / denominator), lo que convierte primero a float. Reprodúcelo con el snippet del issue, sustituye ese paso por división entera con floor para que funcionen fracciones exactas de cualquier tamaño, y comprueba que continued_fraction(Fraction(2**53 + 3)) devuelve [9007199254740995] y que el caso 10**400 / 3 ya no desborda. Hecho significa que ambos ejemplos se comportan correctamente y que las fracciones negativas siguen dando una fracción continua correcta.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Repository commit
6e03a48579ca212a08b36be00378f887dc41a003
Python version (python --version)
Python 3.11.0, macOS
Dependencies version (pip freeze)
The reproduction uses only Python's standard library; no third-party dependencies are involved.
Expected behavior
continued_fraction(Fraction(2**53 + 3)) should return [9007199254740995], the continued fraction of that integer. Large exact fractions should not fail merely because they exceed floating-point range.
Actual behavior
Run from the repository root:
from fractions import Fraction
from maths.continued_fraction import continued_fraction
print(continued_fraction(Fraction(2**53 + 3)))
try:
print(continued_fraction(Fraction(10**400, 3)))
except OverflowError as exc:
print(type(exc).__name__ + ':', exc)
Output:
[9007199254740996, -1]
OverflowError: integer division result too large for a float
The first result represents the same integer numerically, but is not the regular continued fraction: the trailing term is negative because the first quotient was rounded upward.
In maths/continued_fraction.py, floor(numerator / denominator) first converts the integer ratio to a float. This loses precision for sufficiently large values and can overflow. Computing the quotient directly with integer floor division (numerator // denominator) preserves exact arithmetic and also handles negative fractions.
I searched existing issues and PRs for continued fraction and continued_fraction; I did not find an existing report of this problem.
- Lenguaje dominante
- Python
- Estrellas
- 226k
- Forks
- 51.3k
- Merge medio
- 1 d 13 h
- PR fusionados (30 d)
- 32
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de TheAlgorithms/Python
-
binomial_distribution overflows at 171 trials even when the result is representablePosiblemente ocupada @landfvit la tomó hace 4 días. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
TheAlgorithms/Python#15656 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
ciphers/base32.py and ciphers/base85.py encode Unicode code points instead of bytes: output diverges from the spec for non-ASCII, round-trips corrupt chars >= U+0400, arbitrary bytes crashPosiblemente ocupada @Nate-br la tomó hace 5 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
TheAlgorithms/Python#15636 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
ciphers/des_ecb.py: encrypt/decrypt silently corrupts any character above U+00FF (round-trip fails)Posiblemente ocupada @mohammedanasnakhuda la tomó hace 6 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
TheAlgorithms/Python#15634 ·
Los mantenedores suelen responder en 2 días
-
other/password.py: alternative_password_generator ignores the requested length when the required characters exceed itPosiblemente ocupada @raaj-2222 la tomó hace 6 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
TheAlgorithms/Python#15633 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
ciphers/base58.py is a copy of base64_cipher.py — no Base58 implementation exists in the filePosiblemente ocupada @SakshiYeole la tomó hace 7 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 45/100
TheAlgorithms/Python#15637 ·
Los mantenedores suelen responder en 2 días
Todos los issues de TheAlgorithms/Python
Issues similares
-
PluginProvider.storeInstance races and clobbers the dedup cache for concurrent loadsPosiblemente ocupada @Wilson0511 la tomó hoy. Abiertoneeds-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 2 días
-
community documentation good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
kagent-dev/kagent#3462 ·
Los mantenedores suelen responder en 1 día