I tried to find the square root of a negative number while writing a function to solve quadratic equations but the interpreter said something , it said math domain error, what does this mean. Even though I've been able to solve the quadratic equation using complex numbers and i will be posting it very soon, i want to know when to expect such errors

Responses please, geeks in the house.

Maybe you understand from this interactive session by 'no comments' style:

>>> from math import *
>>> sqrt(-1)

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    sqrt(-1)
ValueError: math domain error
>>> sqrt(-1+0j)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    sqrt(-1+0j)
TypeError: can't convert complex to float

>>> a = -1+0j
>>> a
(-1+0j)
>>> sqrt(a)

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    sqrt(a)
TypeError: can't convert complex to float
>>> log(-3)

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    log(-3)
ValueError: math domain error
>>> import cmath
>>> help(cmath)
Help on built-in module cmath:

NAME
    cmath

FILE
    (built-in)

DESCRIPTION
    This module is always available. It provides access to mathematical
    functions for complex numbers.

FUNCTIONS
    acos(...)
        acos(x)
        
        Return the arc cosine of x.
    
    acosh(...)
        acosh(x)
        
        Return the hyperbolic arccosine of x.
    
    asin(...)
        asin(x)
        
        Return the arc sine of x.
    
    asinh(...)
        asinh(x)
        
        Return the hyperbolic arc sine of x.
    
    atan(...)
        atan(x)
        
        Return the arc tangent of x.
    
    atanh(...)
        atanh(x)
        
        Return the hyperbolic arc tangent of x.
    
    cos(...)
        cos(x)
        
        Return the cosine of x.
    
    cosh(...)
        cosh(x)
        
        Return the hyperbolic cosine of x.
    
    exp(...)
        exp(x)
        
        Return the exponential value e**x.
    
    isinf(...)
        isinf(z) -> bool
        Checks if the real or imaginary part of z is infinite.
    
    isnan(...)
        isnan(z) -> bool
        Checks if the real or imaginary part of z not a number (NaN)
    
    log(...)
        log(x[, base]) -> the logarithm of x to the given base.
        If the base not specified, returns the natural logarithm (base e) of x.
    
    log10(...)
        log10(x)
        
        Return the base-10 logarithm of x.
    
    phase(...)
        phase(z) -> float
        
        Return argument, also known as the phase angle, of a complex.
    
    polar(...)
        polar(z) -> r: float, phi: float
        
        Convert a complex from rectangular coordinates to polar coordinates. r is
        the distance from 0 and phi the phase angle.
    
    rect(...)
        rect(r, phi) -> z: complex
        
        Convert from polar coordinates to rectangular coordinates.
    
    sin(...)
        sin(x)
        
        Return the sine of x.
    
    sinh(...)
        sinh(x)
        
        Return the hyperbolic sine of x.
    
    sqrt(...)
        sqrt(x)
        
        Return the square root of x.
    
    tan(...)
        tan(x)
        
        Return the tangent of x.
    
    tanh(...)
        tanh(x)
        
        Return the hyperbolic tangent of x.

DATA
    e = 2.7182818284590451
    pi = 3.1415926535897931


>>> cmath.sqrt(-1)
1j
>>> cmath.log(-3)
(1.0986122886681098+3.1415926535897931j)
>>>

Thanks now i understand why my quadratic function is not working, thanks a lot tonyjv for the honest criticism, will work on it and it will come back better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.