Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #5659
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Hartmut Goebel <h.goebel@goebel-consult.de> |
| 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 | <mailman.603.1597740490.9580.python-de@python.org> (permalink) |
| References | <hq1glnF175jU1@mid.individual.net> <db3df76f-9ef4-c5f3-0341-19adcc0998a4@goebel-consult.de> |
| 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 | <h.goebel@goebel-consult.de> |
| 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 | <hq1glnF175jU1@mid.individual.net> |
| 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 <python-de.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-de>, <mailto:python-de-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-de/> |
| List-Post | <mailto:python-de@python.org> |
| List-Help | <mailto:python-de-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-de>, <mailto:python-de-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <db3df76f-9ef4-c5f3-0341-19adcc0998a4@goebel-consult.de> |
| X-Mailman-Original-References | <hq1glnF175jU1@mid.individual.net> |
| Xref | csiph.com de.comp.lang.python:5659 |
Show key headers only | View raw
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
Back to de.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Python2 auf 3: Matheberechnungen Stephan Seitz <stse+usenet@rootsland.net> - 2020-08-18 08:09 +0000
Re: [Python-de] Python2 auf 3: Matheberechnungen Hartmut Goebel <h.goebel@goebel-consult.de> - 2020-08-18 10:38 +0200
Re: [Python-de] Python2 auf 3: Matheberechnungen Stephan Seitz <stse+usenet@rootsland.net> - 2020-08-18 11:00 +0000
Re: [Python-de] Python2 auf 3: Matheberechnungen Peter Otten <__peter__@web.de> - 2020-08-18 10:56 +0200
csiph-web