tcpdump mailing list archives
Re: POSIX and float rounding
From: Guy Harris <gharris () sonic net>
Date: Thu, 19 Mar 2026 10:23:17 -0700
On Mar 19, 2026, at 9:45 AM, Guy Harris <gharris () sonic net> wrote:
On Mar 19, 2026, at 9:30 AM, Denis Ovsienko <denis () ovsienko info> wrote:On Thu, 19 Mar 2026 09:16:47 -0700 Guy Harris <gharris () sonic net> wrote:Try that on the platforms in question and see what it reports.$ ./roundingmode Rounding mode: to nearest fenv.h defines exactly the four FE_ rounding modes.So that's true on *all* of the platforms, including QNX? That's annoying. It appears that QNX's definition of "to nearest", in the "the two choices are equidistant" case, is different from that of all other platforms (and from what I guess I was taught in school, as I've always thought you were supposed to round 0.5 up to 1). Perhaps it's time to see what IEEE 754 has to say on the topic.
The Wikipedia says:
https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules
Tl;dr: there are *five* rounding rules:
• Round to nearest, ties to even – rounds to the nearest value; if the number falls midway, it is rounded to the
nearest value with an even least significant digit.
• Round to nearest, ties away from zero (or ties to away) – rounds to the nearest value; if the number falls
midway, it is rounded to the nearest value above (for positive numbers) or below (for negative numbers).
• Round toward 0 – directed rounding towards zero (also known as truncation).
• Round toward +∞ – directed rounding towards positive infinity (also known as rounding up or ceiling).
• Round toward −∞ – directed rounding towards negative infinity (also known as rounding down or floor).
and in the two "to nearest" cases, you get:
+11.5 +12.5 −11.5 −12.5
to nearest, ties to even +12.0 +12.0 −12.0 −12.0
to nearest, ties away from zero +12.0 +13.0 −12.0 −13.0
Your Linux examples appear to be "to nearest, ties to even", as "Format %.0f rounds 0.50 to 0." and "Format %.0f rounds
1.50 to 2.", and your QNX examples appear to be "to nearest, ties away from zero", as "Format %.0f rounds 0.50 to 1."
and "Format %.0f rounds 1.50 to 2.".
This Stack Overflow answer:
https://stackoverflow.com/a/37000031
claims that
Its implementation defined. Assuming IEEE 754:
The standard defines 5 rounding modes, which might be used. The two modes of interest are:
• Round to nearest, ties to even (*this is the default for binary floating-point and the recommended
default for decimal*)
• Round to nearest, ties away from zero
so, for whatever reason, QNX isn't implementing what's claimed to be the default.
And the answer appears to be correct; IEEE 754-2019 says, in 4.3.3 "Rounding attribute requirements":
An implementation of this standard shall provide roundTiesToEven and the three directed rounding attributes. A
decimal format implementation of this standard shall provide roundTiesToAway as a user-selectable rounding-direction
attribute. The rounding attribute roundTiesToAway is not required for a binary format implementation.
The roundTiesToEven rounding-direction attribute shall be the default rounding-direction attribute for results
in binary formats. The default rounding-direction attribute for results in decimal formats is language-defined, but
should be roundTiesToEven.
So, in the environment on your QNX box, the IEEE 754-2019 default appears *not* to be implemented, as it's doing
roundTiesToAway. Unfortunately, I don't have the phone number for the IEEE Police Department. :-)
_______________________________________________
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:
- POSIX and float rounding Denis Ovsienko (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Denis Ovsienko (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Rick Jones via tcpdump-workers (Mar 19)
- Re: POSIX and float rounding Denis Ovsienko (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
- Re: POSIX and float rounding Guy Harris (Mar 19)
