tcpdump mailing list archives

Re: POSIX and float rounding


From: Guy Harris <gharris () sonic net>
Date: Thu, 19 Mar 2026 12:03:20 -0700

As for a rationale for QNX's behavior, for what it's worth, Google's Artificial Brain says:

        Key Details regarding QNX roundTiesToAway Behavior:

            • Behavior: For a number like 
0.5, round(0.5) returns 
1.0, and round(-0.5) returns 
−1.0.

            • Library: Part of libm (general-purpose math library) and libm-sve (optimized for ARMv8).

            • Context: Unlike some IEEE 754 default behaviors that round to the nearest even number, the QNX round() 
function specifically implements the "away from zero" method for ties.

            • Use Case: This rounding method is often used in financial calculations to match standard commercial 
rounding practices.

        This behavior ensures that numbers such as 1.5 and 2.5 become 2 and 3 respectively, rather than rounding ties 
towards the nearest even integer, and is crucial for maintaining consistent rounding in embedded applications.

The rationale for roundTiesToEven is, as I understand it, that it means that, on average, the roundup errors will be 
zero, with half of all X.XX5 rounding up to X.XX{X+1} and half rounding down to X.XX0, so there there's no *systematic* 
bias from rounding; Google's Artificial Brain cites

        https://medium.com/@codingguy/why-python-round-6-5000000000000001-6-and-why-its-perfectly-logical-2b7a8e7dbde3

        https://mathematica.stackexchange.com/questions/2116/why-round-to-even-integers

        https://stackoverflow.com/questions/45129046/why-floats-rounding-use-round-to-even

for this.

Amusingly, an answer (the only current one) to

        
https://stackoverflow.com/questions/49500029/why-is-round-nearest-ties-away-rounding-mode-optional-in-the-ieee754-standard-fo

says

        roundTiesToAway is also called commercial rounding.

        Muller et al.: "Handbook of Floating-Point Arithmetic" explains:

        roundTiesToAway is useful for some accounting calculations. This is why it is required for radix-10 
implementations only, the main use of radix 10 being financial calculations. For instance, the European Council 
Regulation No. 1103/97 of 17 June 1997 on certain provisions relating to the introduction of the Euro sets out a number 
of rounding and conversion rules. Among them, If the application of the conversion rate gives a result which is exactly 
half-way, the sum shall be rounded up.

        X86 and ARM microprocessors only have 2 bits in the control word for setting rounding mode. They don't have 
space for adding a 5th rounding mode. That's a reason for not making roundTiesToAway mandatory for binary floating 
point representations.

but roundTiesToEven is called "Banker's Rounding":

        https://www.personalinvesting.jpmorgan.com/bankers-rounding

and, last I heard, bankers worked in finance, so I guess the financial profession is not of one mind on this - 
accountants like roundTiesToAway and bankers like roundTiesToEven.

See

        https://en.wikipedia.org/wiki/Rounding

for more than you ever wanted to know about rounding rules.

(Fun fact: the query that got Google Artificial Brain's first response cited, for some unknown reason, an earlier email 
of mine in this thread, even though I didn't say anything about roundTiesToAway often being "used in financial 
calculations to match standard commercial rounding practices." I'm not sure either of the *other* citations said 
anything about it either.)
_______________________________________________
tcpdump-workers mailing list -- tcpdump-workers () lists tcpdump org
To unsubscribe send an email to tcpdump-workers-leave () lists tcpdump org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Current thread: