Jump to content

What is wrong with my python?

Go to solution Solved by Glenwing,

(b² - 4ac) = (25 - 4•25) = -75, you're trying to take the square root of -75. You can't take the square root of a negative number, so you'll need a special case for that, which outputs "no solution" or something like that.

Can't figure this out and I need a quick answer... been looking at it for about 2 hours now haha so I thought it was time to post. It's part of a larger amount of code, but I narrowed the problem down to "math.sqrt()" so I made a short version and brought it here.. Why do I get a "math domain error" everytime i run this??

 

Spoiler

import math

a = 5
b = 5
c = 5

quad = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

print(quad)

any solutions?

 

"If a Lobster is a fish because it moves by jumping, then a kangaroo is a bird" - Admiral Paulo de Castro Moreira da Silva

"There is nothing more difficult than fixing something that isn't all the way broken yet." - Author Unknown

Spoiler

Intel Core i7-3960X @ 4.6 GHz - Asus P9X79WS/IPMI - 12GB DDR3-1600 quad-channel - EVGA GTX 1080ti SC - Fractal Design Define R5 - 500GB Crucial MX200 - NH-D15 - Logitech G710+ - Mionix Naos 7000 - Sennheiser PC350 w/Topping VX-1

Link to comment
https://linustechtips.com/topic/569809-what-is-wrong-with-my-python/
Share on other sites

Link to post
Share on other sites

Just now, bcredeur97 said:

Can't figure this out and I need a quick answer... been looking at it for about 2 hours now haha so I thought it was time to post. It's part of a larger amount of code, but I narrowed the problem down to "math.sqrt()" so I made a short version and brought it here.. Why do I get a "math domain error" everytime i run this??

 

  Hide contents

import math

a = 5
b = 5
c = 5

quad = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

print(quad)

any solutions?

 

maybe try the ^ sign for powers?

Link to post
Share on other sites

Just now, mikat said:

maybe try the ^ sign for powers?

nope. same error. Not sure if that works in python anyway.

Like I said, the problem is because of "math.sqrt()" as whenever I remove it, the code runs fine.

"If a Lobster is a fish because it moves by jumping, then a kangaroo is a bird" - Admiral Paulo de Castro Moreira da Silva

"There is nothing more difficult than fixing something that isn't all the way broken yet." - Author Unknown

Spoiler

Intel Core i7-3960X @ 4.6 GHz - Asus P9X79WS/IPMI - 12GB DDR3-1600 quad-channel - EVGA GTX 1080ti SC - Fractal Design Define R5 - 500GB Crucial MX200 - NH-D15 - Logitech G710+ - Mionix Naos 7000 - Sennheiser PC350 w/Topping VX-1

Link to post
Share on other sites

1 minute ago, NJHourigan said:

I've never used the math functions, so I don't know about that, but could it be to do with how a quadratic formula will give two answers and python doesn't know how to deal with that?

i was doing both answers seperately, I made a quick short program with only one(+) so I could show you what my problem is.

"If a Lobster is a fish because it moves by jumping, then a kangaroo is a bird" - Admiral Paulo de Castro Moreira da Silva

"There is nothing more difficult than fixing something that isn't all the way broken yet." - Author Unknown

Spoiler

Intel Core i7-3960X @ 4.6 GHz - Asus P9X79WS/IPMI - 12GB DDR3-1600 quad-channel - EVGA GTX 1080ti SC - Fractal Design Define R5 - 500GB Crucial MX200 - NH-D15 - Logitech G710+ - Mionix Naos 7000 - Sennheiser PC350 w/Topping VX-1

Link to post
Share on other sites

Try using a different means of evaluating B raised to the second. If needed square B as a fourth variable and use it

 

Capture2.PNG

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to post
Share on other sites

Just now, RedWulf said:

Try using a different means of evaluating B raised to the second. If needed square B as a fourth variable and use it

 

Capture2.PNG

yes and you can also use the trick of raising the whole function to the power 0.5 :)

math.sqrt(2) = 2^0.5

Link to post
Share on other sites

(b² - 4ac) = (25 - 4•25) = -75, you're trying to take the square root of -75. You can't take the square root of a negative number, so you'll need a special case for that, which outputs "no solution" or something like that.

Link to post
Share on other sites

Just now, Glenwing said:

Probably the term under the radical is coming out negative. You can't take the square root of a negative number, so you'll need a special case for that, which outputs "no solution".

Well B^2 = 25 and 4xAxC = 100 so its the sqrt of -75. I think we all over looked that

 

Just so you're not mislead, sqrt of a negative will involve the non-real number i which I don't think python.math is loaded with.

 

Try 

a=5

b=9

c=3

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to post
Share on other sites

6 minutes ago, Glenwing said:

(b² - 4ac) = (25 - 4•25) = -75, you're trying to take the square root of -75. You can't take the square root of a negative number, so you'll need a special case for that, which outputs "no solution" or something like that.

yeah I was just putting in random numbers from user input  thinking anything would work. my mistake!

"If a Lobster is a fish because it moves by jumping, then a kangaroo is a bird" - Admiral Paulo de Castro Moreira da Silva

"There is nothing more difficult than fixing something that isn't all the way broken yet." - Author Unknown

Spoiler

Intel Core i7-3960X @ 4.6 GHz - Asus P9X79WS/IPMI - 12GB DDR3-1600 quad-channel - EVGA GTX 1080ti SC - Fractal Design Define R5 - 500GB Crucial MX200 - NH-D15 - Logitech G710+ - Mionix Naos 7000 - Sennheiser PC350 w/Topping VX-1

Link to post
Share on other sites

int a , b , c;
cin >> a >> b >> c;
int delta = b*b-4*a*c;
if(delta<0)
            cout << "No real solutions\n";
if(delta==0)
            cout << -b/(2*a) << '\n';
if(delta>1)
            cout << (-b+sqrt(delta))/(2*a) << '\n' << (-b-sqrt(delta))/(2*a) << '\n';

 

Or , in Python :

import math
a = int(input())
b = int(input())
c = int(input())
delta = b**2 -4*a*c
if delta < 0:
            print("No real solutions")
elif delta == 0:
            print(-b/(2*a))
else :
            print((-b+sqrt(delta))/(2*a))
            print((-b-sqrt(delta))/(2*a))

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

y

34 minutes ago, Nineshadow said:

import math
a = int(input())
b = int(input())
c = int(input())
delta = b**2 -4*a*c
if delta < 0:
            print("No real solutions")
elif delta == 0:
            print(-b/(2*a))
else :
            print((-b+sqrt(delta))/(2*a))
            print((-b-sqrt(delta))/(2*a))

 

for that matter just leave it since all roots of 0 are 0 

if (delta <0):

   quad=( "no real sol")

else:

   quad = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

print(quad)

 

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×