LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2018, 07:19 AM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
windows python: quadratic formula


Hi,
I am trying to write the quadratic formula program:
Code:
# program to evaluate roots of quadratic eq using python
import math
a=1; b=1; c=100;
x1 = (-b + math.sqrt(b**2 - 4*a*c))/ (2 * a)
x2 = (-b - math.sqrt(b**2 - 4*a*c))/ (2 * a)
print (x1)
print (x2)
I am getting following errors:
Traceback (most recent call last):
File "quadraticeq.py", line 4, in <module>
x1 = (-b + math.sqrt(b**2 - 4*a*c))/ (2 * a)
ValueError: math domain error

Can somebody please guide me whats the error?

Zulfi.
 
Old 06-06-2018, 07:29 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
insert two more lines
Code:
# program to evaluate roots of quadratic eq using python
import math
a=1; b=1; c=100;
d=b**2 - 4*a*c
print (d)
x1 = (-b + math.sqrt(b**2 - 4*a*c))/ (2 * a)
x2 = (-b - math.sqrt(b**2 - 4*a*c))/ (2 * a)
print (x1)
print (x2)
 
Old 06-06-2018, 07:29 AM   #3
Delcaran
Member
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 44

Rep: Reputation: 27
With your values of a, b and c you are trying to calculate sqrt(-399), which of course is not a valid operation.
You should first calculate b**2 - 4*a*c, check if it's positive and only then apply the quadratic formula.
 
2 members found this post helpful.
Old 06-06-2018, 07:33 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
b^2 - 4ac is negative. So you are trying to take the root from Z- instead of N. Which is the wrong domain.

jlinkels
 
Old 06-06-2018, 07:43 AM   #5
Delcaran
Member
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 44

Rep: Reputation: 27
Quote:
Originally Posted by jlinkels View Post
So you are trying to take the root from Z- instead of N.
Just to be annoying, the domain of square root function is not only N but R+U{0}, all non-negative real numbers.

Last edited by Delcaran; 06-06-2018 at 07:45 AM.
 
Old 06-06-2018, 07:59 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by Delcaran View Post
Just to be annoying, the domain of square root function is not only N but R+U{0}, all non-negative real numbers.
No you are not annoying, I am just plain downright stupid. I have been playing around too much with integers the last few months.

But I was close as to why the expression "domain" was used in the error message

jlinkels

Last edited by jlinkels; 06-06-2018 at 08:00 AM.
 
Old 06-06-2018, 08:38 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
or use:
Code:
import cmath as math
....

$python /tmp/a.py
(-0.5+9.98749217772j)
(-0.5-9.98749217772j)
$
 
2 members found this post helpful.
Old 06-06-2018, 10:05 PM   #8
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Thanks all of you. God bless you.

Zulfi.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem: Using C++ to solve Quadratic Equation lckwswws Programming 20 02-17-2015 06:03 PM
quadratic equation for python NEW JAPANBOY Programming 3 06-26-2011 11:59 PM
fomula for coefficient for quadratic equation JAPANBOY Programming 4 06-25-2011 01:21 PM
windows & Linux the perfect formula linker General 7 08-23-2003 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration