how to convert an integer to a float?

Discussion in 'Python' started by yinglcs@gmail.com, Feb 28, 2007.

  1. Guest

    Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    i1)' always return 0, can you please tell me how can i convert it from
    an integer to float?


    def compareValue(n1, n2):
    i1 = int(n1)
    i2 = int(n2)

    dx = abs(i2 - i1)/min(i2, i1)
    print dx
    return dx < 0.05
     
    , Feb 28, 2007
    #1
    1. Advertising

  2. jeff Guest

    On Feb 27, 7:05 pm, "" <> wrote:
    > Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    > i1)' always return 0, can you please tell me how can i convert it from
    > an integer to float?
    >
    > def compareValue(n1, n2):
    > i1 = int(n1)
    > i2 = int(n2)
    >
    > dx = abs(i2 - i1)/min(i2, i1)
    > print dx
    > return dx < 0.05


    x = x + 0.0
     
    jeff, Feb 28, 2007
    #2
    1. Advertising

  3. wrote:
    > Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    > i1)' always return 0, can you please tell me how can i convert it from
    > an integer to float?


    When two integers are involved in a division, the result will also be a
    division. If one of the operands is a float, then the result will be a
    float instead. So try casting one of the values to a float before
    performing the division:

    dx = float(abs(i2 - i1))/min(i2, i1)

    -Farshid
     
    Farshid Lashkari, Feb 28, 2007
    #3
  4. Farshid Lashkari wrote:
    > When two integers are involved in a division, the result will also be a
    > division.


    My bad, I meant the result will also be an *integer*

    -Farshid
     
    Farshid Lashkari, Feb 28, 2007
    #4
  5. Matimus Guest

    On Feb 27, 4:05 pm, "" <> wrote:
    > Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    > i1)' always return 0, can you please tell me how can i convert it from
    > an integer to float?
    >
    > def compareValue(n1, n2):
    > i1 = int(n1)
    > i2 = int(n2)
    >
    > dx = abs(i2 - i1)/min(i2, i1)
    > print dx
    > return dx < 0.05


    dx = float(abs(i2 -i1))/min(i2, i1)

    Or you could just put "from __future__ import division" at the top of
    your file.

    see http://www.python.org/dev/peps/pep-0238/ for details.

    -Matt
     
    Matimus, Feb 28, 2007
    #5
  6. Guest

    yinglcs, you can use float() or the new division:

    >>> 1 / 2

    0
    >>> 1 / float(2)

    0.5
    >>> from __future__ import division
    >>> 1 / 2

    0.5

    Bye,
    bearophile
     
    , Feb 28, 2007
    #6
  7. wrote:

    > def compareValue(n1, n2):
    > i1 = int(n1)
    > i2 = int(n2)
    >
    > dx = abs(i2 - i1)/min(i2, i1)
    > print dx
    > return dx < 0.05


    You could also prepend

    from __future__ import division

    Regards,


    Björn

    --
    BOFH excuse #237:

    Plate voltage too low on demodulator tube
     
    Bjoern Schliessmann, Feb 28, 2007
    #7
  8. On 2007-02-28, jeff <> wrote:
    > On Feb 27, 7:05 pm, "" <> wrote:
    >> Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    >> i1)' always return 0, can you please tell me how can i convert it from
    >> an integer to float?
    >>
    >> def compareValue(n1, n2):
    >> i1 = int(n1)
    >> i2 = int(n2)
    >>
    >> dx = abs(i2 - i1)/min(i2, i1)
    >> print dx
    >> return dx < 0.05

    >
    > x = x + 0.0


    How, um, perlesque.

    I rather think that this is a bit more pythonic:

    x = float(x)

    --
    Grant Edwards grante Yow! I'm a fuschia bowling
    at ball somewhere in Brittany
    visi.com
     
    Grant Edwards, Feb 28, 2007
    #8
  9. Ben Finney Guest

    Please don't top-post; instead, reply below the lines you're
    responding to, and trim any irrelevant lines from the original.

    Subscriber123 <> writes:

    > How did the new division ever get approved?!


    By being introduced as a PEP, which is now numbered PEP 238.

    <URL:http://www.python.org/dev/peps/pep-0238/>

    > That's not pythonic! What if you then need to divide two integers
    > and find an element in a list or dict?


    Then your code is dependent on ambiguous behaviour which has changed
    in newer Python versions.

    As described in the above document, the '//' operator will
    unambiguously request floor division, even in older versions of
    Python.

    > I know that at the moment it is not implemented unless imported from
    > __future__, but I expect that it eventually might be.


    Please read PEP 238 to see the implementation plan.

    > That would be a problem with backwards compatibility.


    The older Python versions aren't going away. Anyone who wants their
    old code to work with new versions of Python has a responsibility to
    see what parts of their code need to be updated.

    --
    \ "My roommate got a pet elephant. Then it got lost. It's in the |
    `\ apartment somewhere." -- Steven Wright |
    _o__) |
    Ben Finney
     
    Ben Finney, Feb 28, 2007
    #9
  10. On 27 Feb 2007 16:05:40 -0800, "" <>
    declaimed the following in comp.lang.python:

    > Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    > i1)' always return 0, can you please tell me how can i convert it from
    > an integer to float?
    >

    Being cynical: the same way you converted the arguments to
    integer... use float()

    >
    > def compareValue(n1, n2):
    > i1 = int(n1)
    > i2 = int(n2)
    >

    But WHY are you converting to integer in the first place.

    > dx = abs(i2 - i1)/min(i2, i1)


    Note that, by converting to integer, you run the risk of a division
    by zero

    n1 = 0.2
    n2 = 0.3

    i1 <- 0
    i2 <- 0

    min(0, 0)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 28, 2007
    #10
  11. <> wrote in message
    news:...
    > Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    > i1)' always return 0, can you please tell me how can i convert it from
    > an integer to float?


    I don't think that's what you really want to do.

    What you really want is for dx to be a float rather than being truncated to
    an integer. Division is going to behave that way in the future, so if you
    want it to behave that way now, you can write

    from __future__ import division

    at the beginning of your program.

    If for some reason you don't want to rely on using a version of Python that
    knows about this future behavior, you might consider

    dx = float(abs(i2 - i1))/min(i2, i1)

    as an alternative.
     
    Andrew Koenig, Mar 5, 2007
    #11
  12. On 2007-03-05, Andrew Koenig <> wrote:
    ><> wrote in message
    > news:...
    >> Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
    >> i1)' always return 0, can you please tell me how can i convert it from
    >> an integer to float?

    >
    > I don't think that's what you really want to do.
    >
    > What you really want is for dx to be a float rather than being truncated to
    > an integer. Division is going to behave that way in the future, so if you
    > want it to behave that way now, you can write
    >
    > from __future__ import division
    >
    > at the beginning of your program.
    >
    > If for some reason you don't want to rely on using a version of Python that
    > knows about this future behavior, you might consider
    >
    > dx = float(abs(i2 - i1))/min(i2, i1)


    I prefer to multiply by 1.0 That has the advantage it continues to work
    if your numbers happen to be complex.

    --
    Antoon Pardon
     
    Antoon Pardon, Mar 8, 2007
    #12
    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. Andy
    Replies:
    7
    Views:
    6,332
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    671
  3. shellon
    Replies:
    7
    Views:
    3,185
    Gabriel Genellina
    Jan 17, 2007
  4. Replies:
    2
    Views:
    555
    John Machin
    Apr 30, 2007
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,658
    James Kanze
    Oct 8, 2009
Loading...

Share This Page