Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Hartmut Goebel Newsgroups: de.comp.lang.python Subject: Re: [Python-de] Python2 auf 3: Matheberechnungen Date: Tue, 18 Aug 2020 10:38:46 +0200 Organization: Goebel Consult Lines: 57 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de kftUC/QKnRnhts9lS0ANHA/SOHoq907xyK1G2z2h14jg== Return-Path: X-Original-To: python-de@python.org Delivered-To: python-de@mail.python.org Authentication-Results: mail.python.org; dkim=none reason="no signature"; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Virus-Scanned: amavisd-new at mnet-online.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: Content-Language: de-DE X-Content-Filtered-By: Mailman/MimeDel 2.1.34 X-BeenThere: python-de@python.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Die Deutsche Python Mailingliste List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com de.comp.lang.python:5659 Am 18.08.20 um 10:09 schrieb Stephan Seitz: > Eine Funktion berechnet z.B. aus Jahr, Monat, Tag und Stunde den folgenden > Wert: > return 367*year - 7 * ( year + (month+9)/12 ) / 4 + 275*month/9 + day - 730530 + float(hour)/float(24) > > Python2 kommt aktuell auf 7536.33333333, Python3 auf 7535.298611111201. > Da dieser Wert für weitere Berechnungen verwendet wird, komme ich nie > auf identische Ergebnisse. > > Woran liegt das? Und wie portiert man dann so ein Script? Der Operator / hat sich geändert (siehe https://www.python.org/dev/peps/pep-0238/) In Python 2 hat / Ganzzahlen so geteilt, dass nur der ganzzahlige Teil geliefert wurde ("floor operator) >>> (month+9)/12 1 In Python 3 teilt / auch Ganzzahlen "echt": >>> (month+9)/12 1.4166666666666667 Um nur den ganzzahligen Teil zu bekommen, verwende //: >>> (month+9)//12 1 Deine Formal muss also lauten 367*year - 7 * ( year + (month+9)//12 ) // 4 + 275*month//9 + day - 730530 + float(hour)/float(24) >>> (month+9)//12 BTW 1: "float(24) kannst Du klarere als "24." schreiben BTW 2: Da Du durch "24." oder "float(24)" teilst, kannst Du Dir das "float" bei "hour" sparen BTW 3: Da "/" in Python 3 sowieso "Reste" liefert, genügt auch "hour/24" -- Schönen Gruß Hartmut Goebel Dipl.-Informatiker (univ), CISSP, CSSLP, ISO 27001 Lead Implementer Information Security Management, Security Governance, Secure Software Development Goebel Consult, Landshut http://www.goebel-consult.de Blog: https://www.goe-con.de/blog/chatsecure-ist-tot-lang-lebe-chatsecure Kolumne: https://www.goe-con.de/hartmut-goebel/cissp-gefluester/2011-08-horrorszenario-bring-your-own-device