tcpdump mailing list archives

Re: POSIX and float rounding


From: Guy Harris <gharris () sonic net>
Date: Thu, 19 Mar 2026 09:16:47 -0700

On Mar 19, 2026, at 6:40 AM, Denis Ovsienko <denis () ovsienko info> wrote:

Does POSIX define which method printf(3) is supposed to use for
rounding a float?

The first question to ask is "does *the C standard* define which method printf() is supposed to use for rounding a 
float?"

C99 says, in 7.19.6.1 "The fprintf function":

        f,F     A double argument representing a floating-point number is converted to decimal notation in the style 
[−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If 
the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point 
character appears. If a decimal-point character appears, at least one digit appears before it. *The value is rounded to 
the appropriate number of digits.*

(emphasis mine) so the specification for printf-style functions punts on that.

7.6 "Floating-point environment <fenv.h>" says:

        Each of the macros

                FE_DOWNWARD
                FE_TONEAREST
                FE_TOWARDZERO
                FE_UPWARD

        is defined if and only if the implementation supports getting and setting the represented rounding direction by 
means of the fegetround and fesetround functions. Additional implementation-defined rounding directions, with macro 
definitions beginning with FE_ and an uppercase letter, may also be specified by the implementation. The defined macros 
expand to integer constant expressions whose values are distinct nonnegative values.

7.6.3.1 "The fegetround function" says:

        Synopsis

                #include <fenv.h>
                int fegetround(void);

        Description

                The fegetround function gets the current rounding direction.

        Returns

                The fegetround function returns the value of the rounding direction macro representing the current 
rounding direction or a negative value if there is no such rounding direction macro or the current rounding direction 
is not determinable.

So it might be interesting to do something such what the attached program does. When compiled and run on macOS, it 
prints "Rounding mode: to nearest".

Try that on the platforms in question and see what it reports.

_______________________________________________
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: