Re: strange behavior of math.sqrt() in new 3.0 version

2009-01-03 Thread Eric Kemp

Tim Roberts wrote:

Scott David Daniels scott.dani...@acm.org wrote:

I avoid using single-letter variables except where I know the types
from the name (so I use i, j, k, l, m, n as integers, s as string,
and w, x, y, and z I am a little looser with (but usually float or
complex).


It's amazing to me that Fortran continues to live on in the hearts and
minds of today's programmers.


Fortran is alive and well.  If you can find a Fortran 2003 compiler, you 
can even use classes.

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


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-28 Thread Steve Holden
Tim Roberts wrote:
 Scott David Daniels scott.dani...@acm.org wrote:
 I avoid using single-letter variables except where I know the types
from the name (so I use i, j, k, l, m, n as integers, s as string,
 and w, x, y, and z I am a little looser with (but usually float or
 complex).
 
 It's amazing to me that Fortran continues to live on in the hearts and
 minds of today's programmers.

Well I think it's more that the original Fortran programmers were
applied mathematicians, who have always tended to use i through m as
integer variables, and that usage continues both in programming and
mathematics today.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-27 Thread Tim Roberts
Scott David Daniels scott.dani...@acm.org wrote:

I avoid using single-letter variables except where I know the types
from the name (so I use i, j, k, l, m, n as integers, s as string,
and w, x, y, and z I am a little looser with (but usually float or
complex).

It's amazing to me that Fortran continues to live on in the hearts and
minds of today's programmers.
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


strange behavior of math.sqrt() in new 3.0 version

2008-12-26 Thread David
I'm a newbee trying 3.0   Please help with  math.sqrt() 

At the command line this function works correctly 
   import math
  n = input(enter a number  )
  s = math.sqrt(n)
 An entry of 9 or 9.0  will yield 3.0

Yet the same code in a script gives an error message
 Script1
   import math
   n = input(enter a number  )
   s = math.sqrt(n)
  
   Traceback (most recent call last) :
  File stdin, line 1, in module
  File script1.py line 3 in module
 s = math.sqrt(n)
   TypeError : a float is required
 Entering 9 or 9.0 gives same error message.

   According to the math module the results of all
   functions are floats.  However it says nothing about
   inputs.

Strangely the above code runs fine in version 2.5  ( ? )
and will handle large integers.

I've read the documentation for 3.0 including the section
Floating Point Arithmetic: Issues  Limitations and it
helps nada.
--
http://mail.python.org/mailman/listinfo/python-list


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-26 Thread Scott David Daniels

David Lemper wrote:
I'm a newbee trying 3.0   Please help with  math.sqrt() 

At the command line this function works correctly 
   import math

  n = input(enter a number  )
  s = math.sqrt(n)
 An entry of 9 or 9.0  will yield 3.0



Yet the same code in a script gives an error message
 Script1
   import math
   n = input(enter a number  )
   s = math.sqrt(n)
 ... TypeError : a float is required



Strangely the above code runs fine in version 2.5  ( ? ) ...


OK, here's what's going on:
at the command line in 2.X, the builtin function input reads a string
and returns the eval of that string.  This is a bit of a safety issue.
I suspect when it worked from the command line, you were using a 2.X
command line inadvertently.  in 2.X, you'll get similar errors if you
use raw_input instead of input.

The input function in 3.0 is the same as the raw_input function
in 2.X.  I would suggest using:
import math
value = float(input(enter a number  ))
root = math.sqrt(value)
print('root(%s) == %s' % (value, root))

I avoid using single-letter variables except where I know the types
from the name (so I use i, j, k, l, m, n as integers, s as string,
and w, x, y, and z I am a little looser with (but usually float or
complex).

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-26 Thread Chris Rebert
On Fri, Dec 26, 2008 at 1:52 PM,  da...@bag.python.org wrote:
 I'm a newbee trying 3.0   Please help with  math.sqrt()

 At the command line this function works correctly
   import math
  n = input(enter a number  )

raw_input() was renamed input() in Python 3.0, and it returns a
*string*, not a *number*.
Therefore, you need to convert the string to an int or float.

Change the line to:

n = float(input(enter a number  ))

And it should work just fine.

  s = math.sqrt(n)
 An entry of 9 or 9.0  will yield 3.0

 Yet the same code in a script gives an error message
 Script1
   import math
   n = input(enter a number  )
   s = math.sqrt(n)

   Traceback (most recent call last) :
  File stdin, line 1, in module
  File script1.py line 3 in module
 s = math.sqrt(n)
   TypeError : a float is required

You're currently giving it a string, not a number, which is
nonsensical, hence the TypeError. I presume ints would be coerced to
floats by the function.

 Entering 9 or 9.0 gives same error message.

   According to the math module the results of all
   functions are floats.  However it says nothing about
   inputs.

 Strangely the above code runs fine in version 2.5  ( ? )
 and will handle large integers.

 I've read the documentation for 3.0 including the section
 Floating Point Arithmetic: Issues  Limitations and it
 helps nada.

You should read What's New in Python 3.0, it covers changes such as
the one you've encountered -
http://docs.python.org/3.0/whatsnew/3.0.html

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-26 Thread Gabriel Genellina

En Fri, 26 Dec 2008 19:52:24 -0200, da...@bag.python.org escribió:


I'm a newbee trying 3.0   Please help with  math.sqrt()

At the command line this function works correctly
   import math
  n = input(enter a number  )
  s = math.sqrt(n)
 An entry of 9 or 9.0  will yield 3.0

Yet the same code in a script gives an error message
 Script1
   import math
   n = input(enter a number  )
   s = math.sqrt(n)
  Traceback (most recent call last) :
  File stdin, line 1, in module
  File script1.py line 3 in module
 s = math.sqrt(n)
   TypeError : a float is required
 Entering 9 or 9.0 gives same error message.

   According to the math module the results of all
   functions are floats.  However it says nothing about
   inputs.

Strangely the above code runs fine in version 2.5  ( ? )
and will handle large integers.

I've read the documentation for 3.0 including the section
Floating Point Arithmetic: Issues  Limitations and it
helps nada.


And you won't find nothing - the change is in input behavior, not in the  
math functions.

For versions prior to 3.0, there are:

raw_input(message) - string typed
input(message) - result of evaluating the string typed

raw_input just returns whatever you type, as a string. Using the input  
function, Python evaluates whatever you type to obtain a result: if you  
type the three characters nine dot zero the result is the double  
9.0; you can even type (17+1)/2.0 to get the same value (try it with your  
Python 2.5)


Since version 3.0, input behaves as raw_input in the older versions, and  
there is no builtin function equivalent to the old input function.

Use this instead:

n = float(input(enter a number  ))


--
Gabriel Genellina

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


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-26 Thread John Machin
On Dec 27, 8:52 am, David Lemper wrote:
 I'm a newbee trying 3.0   Please help with  math.sqrt()

math.sqrt() is not the problem.

 At the command line this function works correctly
        import math
               n = input(enter a number  )
               s = math.sqrt(n)
      An entry of 9 or 9.0  will yield 3.0

I can't reproduce this. See below. Are you sure that you weren't using
Python 2.x? Start with a fresh Python 3.0, and please copy/paste
*exactly* what is on the screen, like I've done:

C:\junk\python30\python
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
Type help, copyright, credits or license for more information.
 n = input(float- )
float- 9
 type(n)
class 'str'
 repr(n)
'9'
 import math
 s = math.sqrt(n)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: a float is required

All of the above is exactly as expected. input() returns a string.
math.sqrt expects a float.


 Yet the same code in a script gives an error message
      Script1
                    import math
                    n = input(enter a number  )
                    s = math.sqrt(n)

                Traceback (most recent call last) :
                   File stdin, line 1, in module
                   File script1.py line 3 in module
                      s = math.sqrt(n)
                TypeError : a float is required
      Entering 9 or 9.0 gives same error message.

Again, as expected.


    According to the math module the results of all
    functions are floats.  However it says nothing about
    inputs.

 Strangely the above code runs fine in version 2.5  ( ? )
 and will handle large integers.

Suggestion: either choose ONE of 2.6 and 3.0 to learn Python, or plan
to spend a lot of time being confused, or reading What's new in
Python 3.0 -- a relevant snippet of which is:

PEP 3111: raw_input() was renamed to input(). That is, the new input()
function reads a line from sys.stdin and returns it with the trailing
newline stripped. It raises EOFError if the input is terminated
prematurely. To get the old behavior of input(), use eval(input()).


HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list