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


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

Re: [Python-de] Warum liefert // ein float?

From Florian Lindner <mailinglists@xgm.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] Warum liefert // ein float?
Date 2017-04-06 11:02 +0200
Message-ID <mailman.172.1491469415.2961.python-de@python.org> (permalink)
References <ea8deb7d-44d8-4f77-3922-90e41b49a439@xgm.de> <a5f1d6f1-89bf-b1f6-b688-c932620ce23f@chrisarndt.de> <afbefdbe-7546-6912-7732-d68a9f78bf46@xgm.de>

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Am 06.04.2017 um 10:12 schrieb Christopher Arndt:
> Am 06.04.2017 um 09:52 schrieb Florian Lindner:
>> Warum liefert // ein float zurück, obwohl das Ergebnis, immer eine eine Ganzzahl ist (in dem Sinne, dass es keine
>> Nachkommestellen hat)?
> 
> Der Grund ist die Python Type Coercion. Bei Operationen mit
> unterschiedlichen Typen der Argumente, werden beide vor der Operation zu
> einem gemeinsamen, kompatiblen Typ konvertiert, der dann auch den Typ
> des Ergebnisses bestimmt.
> 
> Daher auch:
> 
> 10 // 3 == 3

aber:

>>> type(6 / 3)
<class 'float'>

siehe https://www.python.org/dev/peps/pep-0238/

Finde es so auch logisch, also 6 / 3 -> float.

Fände es aber umgedreht auch logischer, wenn 6.0 // 3.0 -> int, da (. // .) prinzipiell eine Ganzzahl zurückgibt.

Grüße,
Florian

Back to de.comp.lang.python | Previous | Next | Find similar


Thread

Re: [Python-de] Warum liefert // ein float? Florian Lindner <mailinglists@xgm.de> - 2017-04-06 11:02 +0200

csiph-web