


Warum rundet die Funktion „round()' halbe Gleitkommazahlen in Python auf die nächste gerade Zahl?
Understanding Half Float Number Rounding Behavior in Python
When dealing with half float numbers in Python, the round() function can exhibit an unexpected behavior, as illustrated by the following code snippet:
<code class="python">for i in range(1, 15, 2): n = i / 2 print(n, "=>", round(n))</code>
This code prints:
0.5 => 0 1.5 => 2 2.5 => 2 3.5 => 4 4.5 => 4 5.5 => 6 6.5 => 6
Instead of rounding up all floating values, round() rounds them to the nearest even number. This peculiar behavior is attributed to "rounding half to even," also known as "bankers rounding."
Cause of Bankers Rounding
According to the Python documentation, round() rounds half towards even to prevent compounding rounding errors. By rounding to the nearest even number, the overall rounding effect is averaged out.
Correcting the Rounding Behavior
To obtain the desired rounding behavior, specifically rounding up from half, the decimal module can be utilized. By defining a local context and setting the rounding parameter to ROUND_HALF_UP, Python's rounding function can be customized.
Here's an example demonstrating how to round up from half using the decimal module:
<code class="python">from decimal import localcontext, Decimal, ROUND_HALF_UP with localcontext() as ctx: ctx.rounding = ROUND_HALF_UP for i in range(1, 15, 2): n = Decimal(i) / 2 print(n, '=>', n.to_integral_value())</code>
The output of this code is:
0.5 => 1 1.5 => 2 2.5 => 3 3.5 => 4 4.5 => 5 5.5 => 6 6.5 => 7
Conclusion
To achieve correct rounding behavior for half float numbers, the decimal module in Python provides the necessary tools for specifying the desired rounding strategy. By utilizing the ROUND_HALF_UP option, half float numbers can be rounded up consistently, as shown in the provided example.
Das obige ist der detaillierte Inhalt vonWarum rundet die Funktion „round()' halbe Gleitkommazahlen in Python auf die nächste gerade Zahl?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

So verwenden Sie Python, um die ZiPF -Verteilung einer Textdatei zu finden

So herunterladen Sie Dateien in Python

Wie benutze ich eine schöne Suppe, um HTML zu analysieren?

Wie man mit PDF -Dokumenten mit Python arbeitet

Wie kann man mit Redis in Django -Anwendungen zwischenstrichen

Einführung des natürlichen Sprach -Toolkits (NLTK)

Wie führe ich ein tiefes Lernen mit Tensorflow oder Pytorch durch?
