[Cfp-interest 2586] Re: Floating point rounding modes

Hans Boehm boehm at acm.org
Wed Jan 4 11:08:22 PST 2023


[I'm not sure whether this will reach cfp-interest; feel free to forward if
not. ]

Disclaimer: I have no confidence in my ability to design an API here that
would actually make it through the various C++ subcommittees. Hence the
vague nature of the initial proposal. My plan is to have the next revision
contain a more specific syntax proposal. But:

The simplest acceptable (to me) proposal would be to change the above to:

   double x, y;
   y = cr_sin_explicit(x, FE_TOWARDZERO);

(This uses the atomics-inspired _explicit syntax to get around the absence
of overloading. That's not great, but there is a precedent.)

At least in C++, I think there are reasons to do something a bit more
elaborate. There may also be reasons not to, especially based on C
compatibility.

Does C23 have a way to specify rounding of decimal constants to binary
floating point? My impression was no. I think that's another usability
obstacle that should be addressed if we really want to make directed
rounding usable to get a bound on the true result.

My concern with this kind of syntax in a C++ context is that it's rather
easy to accidentally mix in an operation or conversion that does not obey
the intended rounding rules. That's why the current strawman C++ proposal
instead introduces a new type. With that approach. the C syntax might look
more like

   cr_double x, y;
   y = sin_explicit(x, FE_TOWARDZERO);

or perhaps just

   cr_double x, y;
   y = sin(x, FE_TOWARDZERO);

A major goal here is really to make the value of floating point expressions
independent of the context in which they appear. That problem showed up in
the constexpr discussions this morning.

Hans

On Tue, Jan 3, 2023 at 11:45 PM Paul Zimmermann <Paul.Zimmermann at inria.fr>
wrote:

>        Hi Hans,
>
> I have read your proposal. Currently in C if we want correct rounding of
> the
> sine function towards zero we write (assuming the math library supports
> correct rounding, or using the new C23 cr_sin function):
>
>    double x, y;
>    fesetround (FE_TOWARDZERO);
>    y = sin (x); /* or y = cr_sin(x) */
>
> What would be the corresponding lines in your proposal in the *C* language?
> (As far as I know, this mailing list only covers C, not C++.)
>
> Best regards,
> Paul
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.oakapple.net/pipermail/cfp-interest/attachments/20230104/33c33c19/attachment.htm>


More information about the Cfp-interest mailing list