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: 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: References: Date: Sun, 3 Feb 2013 03:00:30 +1100 Subject: Re: Floating point calculation problem From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sun, Feb 3, 2013 at 2:48 AM, Schizoid Man 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