Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38082

Re: Floating point calculation problem

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'type,': 0.07; 'python': 0.09; 'directive.': 0.09; 'lines:': 0.09; 'of)': 0.09; 'operator,': 0.09; 'runtime': 0.09; 'tuple': 0.09; 'def': 0.10; 'result.': 0.15; '3.3.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inputs': 0.16; 'integers,': 0.16; 'object()': 0.16; 'threw': 0.16; 'string': 0.17; 'wrote:': 0.17; 'typing': 0.17; 'input': 0.18; 'feb': 0.19; 'sort': 0.21; '(on': 0.22; 'install,': 0.22; 'subject:problem': 0.22; "python's": 0.23; 'this:': 0.23; 'idea': 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'am,': 0.27; 'strongly': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'fixed': 0.28; 'division': 0.29; 'necessary,': 0.29; 'steven': 0.29; 'version,': 0.30; 'error': 0.30; 'code': 0.31; 'getting': 0.33; 'int': 0.33; 'zero': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'doing': 0.35; 'received:209.85': 0.35; 'but': 0.36; "didn't": 0.36; 'does': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'first': 0.61; 'back': 0.62; 'cast': 0.65; '2013': 0.84; 'divide': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=1cFMHKwrxpTwiPR7R7KIBC80uLAg3E9+UA1UuKeTYbU=; b=tDG7TJUj36MTNlbAB/+i268jbz/9lo1ejZ9KaTqGcnYz/8uLC3IPn9XZmxmibjCy1B MP2At/XkVKCSZbyHNAIhGSvyUcMWqBYVW90P+IrZim+YedGnA6kcDClFAKgChiPm7DJ5 4Z8pv6wE2DpujVE82eQlMBggj4xQx6QN6Zl/hONq9cnK3nke5AOkEts+dXRgvOkYCqmG fW6M8dKc9b0KqiAHBE50+1U7bDCO3VEos9JZZEmsDkIapY77w8l/pDjdbJwyFh1cOe71 +nZOL6XkWxcCz7J1tVvq0PQaf2VcLuzDdKdvdd8eHIcXKP9kOtZR8ZkVKyKQjeh8Qn/z mSnw==
MIME-Version 1.0
X-Received by 10.52.176.202 with SMTP id ck10mr12796524vdc.42.1359820830814; Sat, 02 Feb 2013 08:00:30 -0800 (PST)
In-Reply-To <kejcfi$s70$1@dont-email.me>
References <keipl5$kif$1@dont-email.me> <mailman.1289.1359801291.2939.python-list@python.org> <keisev$7v1$1@dont-email.me> <mailman.1291.1359804020.2939.python-list@python.org> <keitk2$hln$1@dont-email.me> <mailman.1292.1359805138.2939.python-list@python.org> <keiujd$o71$1@dont-email.me> <mailman.1293.1359806334.2939.python-list@python.org> <kejcfi$s70$1@dont-email.me>
Date Sun, 3 Feb 2013 03:00:30 +1100
Subject Re: Floating point calculation problem
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1295.1359820833.2939.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359820833 news.xs4all.nl 6854 [2001:888:2000:d::a6]:41359
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38082

Show key headers only | View raw


On Sun, Feb 3, 2013 at 2:48 AM, Schizoid Man <schiz_man@21stcentury.com> wrote:
> def Numer(s, k):
>    return math.log(s / k)
>
> s = float(input("Enter s:   "))
> k = float(input("Enter k:   "))
> print("Result: ", Numer(s, k))
>
> For the v2.7 version, the only difference is the input lines:
>
> s = input("Enter s:   ")
> k = input("Enter k:   ")
>
> So for values of s=60 and k=50, the first code returns 0.1823215567939546
> (on the PC), whereas the second returns 0.0 (on the Mac). This this
> expression is evaluated in the numerator, it never returns a divide by zero
> error, and the result of 0 is treated as legitimate.

So on your Python 2 install, you're working with integers, dividing
one by another, and getting back a value of 1 - and log(1) is 0. The
problem is your division operator, which can be fixed as Steven
suggests, with the future directive.

> However, if I cast the v2.7 inputs as float() then it does indeed return the
> right result. But what threw me was that no cast didn't result in a runtime
> error in 2.7, but did in 3.3.
>
> Also, if the cast is necessary, then now exactly does the dynamic typing
> work?

It's not quite a cast. Python's type system is broadly this: Every
object (value) has a type, every name (variable, sort of) doesn't. So
I can do this:

spam = "hello, world" # spam is a string
spam = 5 # spam is now an int
spam = (1,2,3) # spam is now a tuple
spam = object() # you get the idea

I strongly recommend you either go with Python 3 or raw_input, but NOT
with float(input()) in Py2. Go with float(raw_input()) and you're
doing exactly one translation, and the correct one.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 10:27 +0000
  Re: Floating point calculation problem Chris Angelico <rosuav@gmail.com> - 2013-02-02 21:34 +1100
    Re: Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 11:14 +0000
      Re: Floating point calculation problem Chris Angelico <rosuav@gmail.com> - 2013-02-02 22:20 +1100
        Re: Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 11:34 +0000
          Re: Floating point calculation problem Chris Angelico <rosuav@gmail.com> - 2013-02-02 22:38 +1100
            Re: Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 11:51 +0000
              Re: Floating point calculation problem Chris Angelico <rosuav@gmail.com> - 2013-02-02 22:58 +1100
                Re: Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 15:48 +0000
                Re: Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 15:54 +0000
                Re: Floating point calculation problem Chris Angelico <rosuav@gmail.com> - 2013-02-03 03:00 +1100
                Re: Floating point calculation problem Michael Torrie <torriem@gmail.com> - 2013-02-02 11:19 -0700
                Re: Floating point calculation problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-03 12:25 +1100
                Re: Floating point calculation problem Dave Angel <d@davea.name> - 2013-02-02 21:20 -0500
                Re: Floating point calculation problem Michael Torrie <torriem@gmail.com> - 2013-02-02 21:22 -0700
              Re: Floating point calculation problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-02 23:11 +1100
      Re: Floating point calculation problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-02 22:45 +1100
        Re: Floating point calculation problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-02 11:50 +0000
  Re: Floating point calculation problem Chris Rebert <clp2@rebertia.com> - 2013-02-02 02:47 -0800
  Re: Floating point calculation problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-02 23:05 +1100

csiph-web