Re: X root Operator help

2007-04-18 Thread Michael Hoffman
[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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X root Operator help

2007-04-18 Thread Steve Holden
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X root Operator help

2007-04-18 Thread Michael Hoffman
[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
>> .

[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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X root Operator help

2007-04-18 Thread lucidparadox
On Apr 18, 8:52 am, Michael Hoffman <[EMAIL PROTECTED]> 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)
>
>  > 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
> .
>
> For a better formula, see how r_1 and r_2 are defined in
> .
> --
> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X root Operator help

2007-04-18 Thread Michael Hoffman
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 
.

For a better formula, see how r_1 and r_2 are defined in 
.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X root Operator help

2007-04-18 Thread Tim Golden
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
-- 
http://mail.python.org/mailman/listinfo/python-list


X root Operator help

2007-04-18 Thread lucidparadox
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

-- 
http://mail.python.org/mailman/listinfo/python-list