473,414 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

X root Operator help

I'm currently new to Python and I haven't been able to find the
operator/math function to find the square root or even the x root of a
number. I'm rewriting a program that I wrote in BASIC that does the
math of a quadratic equation (user puts in a, b, and c values) and
tells the user whether it has 1 root, 2 roots, or no real roots and
displays the roots if it has real roots.

Thanks in advance,
Dan

Apr 18 '07 #1
11 2295
lucidparadox wrote:
I'm currently new to Python and I haven't been able to find the
operator/math function to find the square root or even the x root of a
number.
Without ever having used it, I would guess
it's the sqrt function in the math module.

(Possibly also of interest: the pow function
in that same module)
TJG
Apr 18 '07 #2
lucidparadox wrote:
I'm currently new to Python and I haven't been able to find the
operator/math function to find the square root or even the x root of a
number.
For square root, use math.sqrt(y)

For x root use y**(1/x)
I'm rewriting a program that I wrote in BASIC that does the
math of a quadratic equation (user puts in a, b, and c values) and
tells the user whether it has 1 root, 2 roots, or no real roots and
displays the roots if it has real roots.
In floating point arithmetic, the naive way of calculating both roots
always using the formula (-b +/- sqrt(b**2 - 4*a*c))/2*a will give you
inaccurate results sometimes. See
<http://www.cse.uiuc.edu/eot/modules/floating_point/quadratic_formula/>.

For a better formula, see how r_1 and r_2 are defined in
<http://en.wikipedia.org/wiki/Quadratic_equation#Alternative_formula>.
--
Michael Hoffman
Apr 18 '07 #3
On Apr 18, 8:52 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
lucidparadox wrote:
I'm currently new to Python and I haven't been able to find the
operator/math function to find the square root or even the x root of a
number.

For square root, use math.sqrt(y)

For x root use y**(1/x)
I'm rewriting a program that I wrote in BASIC that does the
math of a quadratic equation (user puts in a, b, and c values) and
tells the user whether it has 1 root, 2 roots, or no real roots and
displays the roots if it has real roots.

In floating point arithmetic, the naive way of calculating both roots
always using the formula (-b +/- sqrt(b**2 - 4*a*c))/2*a will give you
inaccurate results sometimes. See
<http://www.cse.uiuc.edu/eot/modules/floating_point/quadratic_formula/>.

For a better formula, see how r_1 and r_2 are defined in
<http://en.wikipedia.org/wiki/Quadratic_equation#Alternative_formula>.
--
Michael Hoffman
Thanks. As of right now I'm just trying to familiarize myself with
the syntax of Python. Actually I should have thought of y**(1/x).
I'm currently a freshman in college so the alternative formula hasn't
been introduced to me yet. Thanks for the pointers though.

Dan Collins

Apr 18 '07 #4
[Michael Hoffman]
>In floating point arithmetic, the naive way of calculating both roots
always using the formula (-b +/- sqrt(b**2 - 4*a*c))/2*a will give you
inaccurate results sometimes. See
<http://www.cse.uiuc.edu/eot/modules/floating_point/quadratic_formula/>.
[lucidparadox]
Thanks. As of right now I'm just trying to familiarize myself with
the syntax of Python. Actually I should have thought of y**(1/x).
math.sqrt(y) might be faster or even more accurate (depending on
platform) than y**0.5.
I'm currently a freshman in college so the alternative formula hasn't
been introduced to me yet.
It probably won't be unless you take a class in numerical analysis. Many
a person has been burned by not thinking about these sorts of things
until it is too late (myself included).
--
Michael Hoffman
Apr 18 '07 #5
Michael Hoffman wrote:
lucidparadox wrote:
>I'm currently new to Python and I haven't been able to find the
operator/math function to find the square root or even the x root of a
number.

For square root, use math.sqrt(y)

For x root use y**(1/x)
[...]
>>1/3
0
>>3.14159 ** (1/3)
1.0
>>>
So the cube root of pi is 1? I don't think so.

For generic roots use y ** (1.0 / x)

until future versions of Python produce floating point results from all
divisions as necessary.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 18 '07 #6
[Michael Hoffman]
>For x root use y**(1/x)
[Steve Holden]
>>3.14159 ** (1/3)
1.0
>>>

So the cube root of pi is 1? I don't think so.

For generic roots use y ** (1.0 / x)
Yes, good point. :)
--
Michael Hoffman
Apr 18 '07 #7
python help,

A client is using win xp home.

my program contains;
shutil.copyfile(n, 'prn')

This runs fine on win xp pro but they are getting
the following traceback.

File "LOP_PRT_10.pyc", line 170, in __init__
* File "LOP_PRT_10.pyc", line 188, in Fprint1
* File "shutil.pyc", line 47, in copyfile
IOError: [Errno 2] No such file or directory:
'prn'

Since this runs ok on win xp pro, does this have
something to do with the home version of xp.

I'm thinking of changeing 'prn' to 'lpt1' and
trying again but I don't want to use the client
as a testor. Or is there some other explaination
for the problem.

jim-on-linux
Apr 18 '07 #8
jim-on-linux wrote:
python help,

A client is using win xp home.

my program contains;
shutil.copyfile(n, 'prn')

This runs fine on win xp pro but they are getting
the following traceback.

File "LOP_PRT_10.pyc", line 170, in __init__
File "LOP_PRT_10.pyc", line 188, in Fprint1
File "shutil.pyc", line 47, in copyfile
IOError: [Errno 2] No such file or directory:
'prn'

Since this runs ok on win xp pro, does this have
something to do with the home version of xp.

I'm thinking of changeing 'prn' to 'lpt1' and
trying again but I don't want to use the client
as a testor. Or is there some other explaination
for the problem.
Not that this is your question, but if you're
trying to print under Windows have you looked
at:

http://tgolden.sc.sabren.com/python/...o_i/print.html

for alternatives?

TJG
Apr 18 '07 #9
On Wednesday 18 April 2007 17:02, Tim Golden
wrote:
jim-on-linux wrote:
python help,

A client is using win xp home.

my program contains;
shutil.copyfile(n, 'prn')

This runs fine on win xp pro but they are
getting the following traceback.

File "LOP_PRT_10.pyc", line 170, in __init__
File "LOP_PRT_10.pyc", line 188, in Fprint1
File "shutil.pyc", line 47, in copyfile
IOError: [Errno 2] No such file or directory:
'prn'

Since this runs ok on win xp pro, does this
have something to do with the home version of
xp.

I'm thinking of changeing 'prn' to 'lpt1'
and trying again but I don't want to use the
client as a testor. Or is there some other
explaination for the problem.

Not that this is your question, but if you're
trying to print under Windows have you looked
at:
http://tgolden.sc.sabren.com/python/win32_how_d
o_i/print.html

for alternatives?

TJG
Thanks for the response,

I got the following to work on windows.

win32api.ShellExecute (
0, 'print',
filename, None, ".", 0
)

However it prints the name of the file at the top
and adds a page number on the bottom. Is there
some way of eliminating the filename and page
number.
jim-on-linux






Apr 20 '07 #10
jim-on-linux wrote:
On Wednesday 18 April 2007 17:02, Tim Golden
wrote:
>jim-on-linux wrote:
>>python help,

A client is using win xp home.

my program contains;
shutil.copyfile(n, 'prn')

This runs fine on win xp pro but they are
getting the following traceback.

File "LOP_PRT_10.pyc", line 170, in __init__
File "LOP_PRT_10.pyc", line 188, in Fprint1
File "shutil.pyc", line 47, in copyfile
IOError: [Errno 2] No such file or directory:
'prn'

Since this runs ok on win xp pro, does this
have something to do with the home version of
xp.

I'm thinking of changeing 'prn' to 'lpt1'
and trying again but I don't want to use the
client as a testor. Or is there some other
explaination for the problem.
Not that this is your question, but if you're
trying to print under Windows have you looked
at:
http://tgolden.sc.sabren.com/python/win32_how_d
o_i/print.html

for alternatives?

TJG

Thanks for the response,

I got the following to work on windows.

win32api.ShellExecute (
0, 'print',
filename, None, ".", 0
)

However it prints the name of the file at the top
and adds a page number on the bottom. Is there
some way of eliminating the filename and page
number.
Unfortunately, this approach is quick-and-dirty and
you're at the mercy of whatever the "print" verb
does to "filename" on your box. (Although you
can configure that if you try hard enough).

I'm afraid if you want control, you'll have to
go the PDF route or to automate Word / OpenOffice
etc.

TJG
Apr 20 '07 #11

Thanks Tim for resopnding,
I appreciate the help.

I convinced the client to install Linux on 4
machines rather than upgrade from xp home to XP
Pro, and more machines to come if the like it.

jim-on-linux
On Friday 20 April 2007 03:22, you wrote:
jim-on-linux wrote:
On Wednesday 18 April 2007 17:02, Tim Golden

wrote:
jim-on-linux wrote:
python help,

A client is using win xp home.

my program contains;
shutil.copyfile(n, 'prn')

This runs fine on win xp pro but they are
getting the following traceback.

File "LOP_PRT_10.pyc", line 170, in
__init__ File "LOP_PRT_10.pyc", line 188,
in Fprint1 File "shutil.pyc", line 47, in
copyfile IOError: [Errno 2] No such file or
directory: 'prn'

Since this runs ok on win xp pro, does this
have something to do with the home version
of xp.

I'm thinking of changeing 'prn' to 'lpt1'
and trying again but I don't want to use
the client as a testor. Or is there some
other explaination for the problem.

Not that this is your question, but if
you're trying to print under Windows have
you looked at:
http://tgolden.sc.sabren.com/python/win32_ho
w_d o_i/print.html

for alternatives?

TJG
Thanks for the response,

I got the following to work on windows.

win32api.ShellExecute (
0, 'print',
filename, None, ".", 0
)

However it prints the name of the file at the
top and adds a page number on the bottom. Is
there some way of eliminating the filename
and page number.

Unfortunately, this approach is quick-and-dirty
and you're at the mercy of whatever the "print"
verb does to "filename" on your box. (Although
you can configure that if you try hard enough).

I'm afraid if you want control, you'll have to
go the PDF route or to automate Word /
OpenOffice etc.

TJG
Apr 20 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
0
by: ./Rob & | last post by:
Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it...
5
by: MLH | last post by:
I'm supposed to set a password for the MySQL root user. The output of mysql_install_db instructed me to run the following commands... /usr/bin/mysqladmin -u root -h appserver password mynwewpasswd...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
13
by: Kishor | last post by:
Hi Friends Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32 Output : 5th root of 32 is 2 Ex:(1) Input : 243 Output : 5th root of 243 is...
19
by: Steve Franks | last post by:
I am using VS.NET 2005 beta 2. When I run my project locally using the default ASP.NET Development Web Server it runs using a root address like this: http://localhost:11243/testsite/ However...
3
by: Nalaka | last post by:
Hi, I have an asp.net web application (www.myWebSite), and a subweb application (www.myWebSite/subSite). How do I set it so that, subweb application (www.myWebSite/subSite) be the root...
7
by: rajbala.3399 | last post by:
Hi , I want to download sql in my linux system........... # rpm -ivh MySQL-server-5.0.24a-0.glibc23.i386.rpm MySQL-cl ient-5.0.24a-0.glibc23.i386.rpm Preparing... ...
5
by: ildam | last post by:
Hello. I'm slowly coming to terms with the SQL Server Management Studio 2005. I've really tried, but cannot figure out how to restrict the SSMS to only look at one database. Ideally what I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.