Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ def power(base: float, exponent: float) -> float:
>>> power(5, 0)
1
"""

if base < 0 and exponent != 0:
inv_exp = 1 / exponent
if abs(inv_exp - round(inv_exp)) < 1e-10:
if round(inv_exp) % 2 == 0:
raise ValueError("Raíz negativa")

return base ** exponent

def valor_maximo(a: float, b: float) -> float:
Expand Down
9 changes: 7 additions & 2 deletions src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def operation_click(self, operation):
>>> # Con paréntesis: (2+3)*4
>>> # expression = "(2+3)*4"
"""
# Si estamos en modo expresión (con paréntesis)
if self.use_expression_mode:
self.expression += operation
self.display.delete(0, tk.END)
Expand Down Expand Up @@ -462,9 +463,13 @@ def equals_click(self):
self.first_number = None
self.operator = None

except ValueError:
self.show_error("Entrada inválida")
except ValueError as e:
if str(e) == "Raíz negativa":
self.show_error("Raíz negativa")
else:
self.show_error("Entrada inválida")
return

except ZeroDivisionError:
self.show_error("No se puede dividir por 0")
return
Expand Down
3 changes: 2 additions & 1 deletion tests/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def test_power():
assert power(-2, 3) == -8
assert power(-2, 0) == 1
assert power(-2, -3) == -0.125
# assert power(-4, 0.5) == -2.0, "Error: la raíz cuadrada de un número negativo no es real"
with pytest.raises(ValueError, match="Raíz negativa"):
power(-4, 0.5)


def test_valor_maximo():
Expand Down