Re: Floating point calculation problem

Discussion in 'Python' started by Steven D'Aprano, Feb 2, 2013.

  1. Schizoid Man wrote:

    > I have a program that performs some calculations that runs perfectly on
    > Python 2.7.3. However, when I try to execute it on Python 3.3.0 I get the
    > following error:
    > numer = math.log(s)
    > TypeError: a float is required
    >
    > The quantity s is input with the following line: s = input("Enter s: ")
    >
    > To get rid of the compile error, I can cast this as a float:
    > s = float(input("Enter s: "))
    >
    > However, then the result returned by the method is wrong. Why does this
    > error occur in version 3.3.0 but not in 2.7.3? Why is the result incorrect
    > when s is cast as a float (the casting is not required in 2.7.3)?



    Others have already discussed the differences between input() in Python 2
    and 3, but there's another difference that could be causing you to get the
    wrong results: division in Python 2 defaults to *integer* division.

    If you type a value like "3" (with no decimal point) into input, you will
    get the int 3, not the float 3.0. Then if you divide by another integer, by
    default you will get truncating integer division instead of what you
    probably expect:


    >>> num = input('Enter a value: ')

    Enter a value: 3
    >>> print num/2

    1


    Whereas if you type it with a decimal point, input() will turn it into a
    float, and you will get float division:

    >>> num = input('Enter a value: ')

    Enter a value: 3.0
    >>> print num/2

    1.5

    This does not happen in Python 3.x -- you always get floating point
    division, even if both the numerator and denominator are ints.


    You can fix this, and get the proper calculator-style floating point
    division, in Python 2 by putting this line at the very top of your script:

    from __future__ import division


    This must appear before any other line of code; it can follow comments and
    blank lines, but not code.



    --
    Steven
     
    Steven D'Aprano, Feb 2, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bei
    Replies:
    0
    Views:
    242
  2. bei
    Replies:
    4
    Views:
    392
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,369
    Saraswati lakki
    Jan 6, 2012
  4. Hyun chul Park

    Floating point calculation problem

    Hyun chul Park, Jul 8, 2008, in forum: Ruby
    Replies:
    3
    Views:
    111
    Axel Etzold
    Jul 8, 2008
  5. Chris Angelico

    Re: Floating point calculation problem

    Chris Angelico, Feb 2, 2013, in forum: Python
    Replies:
    16
    Views:
    239
    Michael Torrie
    Feb 3, 2013
Loading...

Share This Page