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


Groups > de.comp.lang.python > #5659

Re: [Python-de] Python2 auf 3: Matheberechnungen

From Hartmut Goebel <h.goebel@goebel-consult.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] Python2 auf 3: Matheberechnungen
Date 2020-08-18 10:38 +0200
Organization Goebel Consult
Message-ID <mailman.603.1597740490.9580.python-de@python.org> (permalink)
References <hq1glnF175jU1@mid.individual.net> <db3df76f-9ef4-c5f3-0341-19adcc0998a4@goebel-consult.de>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar


Thread

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