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