27 lines
762 B
Python
27 lines
762 B
Python
from typing import Union
|
|
|
|
Number = Union[int, float, complex]
|
|
|
|
def divide(divisor: Number, dividend: Number) -> float:
|
|
"""
|
|
Divide the dividend by the divisor with robust error handling.
|
|
|
|
Args:
|
|
divisor: The number to divide by
|
|
dividend: The number to be divided
|
|
|
|
Returns:
|
|
float: The result of the division
|
|
|
|
Raises:
|
|
TypeError: If inputs are not numeric
|
|
ZeroDivisionError: If divisor is zero
|
|
"""
|
|
if divisor == 0:
|
|
raise ZeroDivisionError("Cannot divide by zero")
|
|
|
|
try:
|
|
return dividend / divisor
|
|
except TypeError:
|
|
raise TypeError(f"Both divisor and dividend must be numeric types. Got: {type(divisor).__name__} and {type(dividend).__name__}")
|