C++ Logo

liaison

Advanced search

Re: [wg14/wg21 liaison] [isocpp-ext] Report from the recent C/C++ liaison meeting (SG22); includes new floating point types(!)

From: Joseph Myers <joseph_at_[hidden]>
Date: Fri, 17 Sep 2021 18:17:58 +0000
On Fri, 17 Sep 2021, Jorg Brown via Liaison wrote:

> Something that would make me incredibly happy is if c++23 said that I could
> overload on std::float16, std::float32, std::float64, std::float80, and
> std::float128, and that the basic functions (+-*/, abs, frexp, ldexp) all
> work on those types, regardless of what the CPU natively supports.
> Alternatively, if there were macros I could check that would tell me if
> each of these had support - but it would still have to be guaranteed that
> "float", "long", and "long double" could be passed to my overload, without
> compile error.

In C:

* The types, where they exist, are always distinct, so _Float64, _Float32x
and double are always different types, even if they have the same format.
In C++ terms, that should mean you can overload on the types that exist
without needing to be concerned about the possibility of two of them being
the same.

* Any of the <float.h> macros can be used to check whether a type exists;
you can use "#ifdef FLT16_MANT_DIG", after including <float.h> to see if
_Float16 is supported, for example. (Note the special case in N2601 about
non-arithmetic interchange format support: FLTN_DIG and FLTN_DECIMAL_DIG
are defined there for if binaryN is supported by the encoding-conversion
functions, even if the _FloatN type isn't supported.)

> Looking at the actual details of
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1312.pdf , I find one
> thing new and intriguing: "Type-generic macros". Specifically, it says
> that if you include <tgmath.h>, you can write "pow(2, 3.0)" and get the
> double version of pow, and you can write "pow(2, 3.0DD)" and get the
> _Decimal64 version. This is of course common practice in C++, but how does
> C do this without overloads?

C11 and later provide _Generic to allow users to write that sort of macro
themselves (before, it could be done using implementation extensions;
tgmath.h was added in C99).

> Notably, the most glaring need for this is printf. If they are going to
> introduce a pow that does the right thing, how about a type-generic

In C, the strfrom functions (e.g. strfromf128) are provided for the new
types, instead of having any corresponding printf format length modifiers.
Likewise, strtof128 exists instead of any scanf support.

I'd expect C++ also to provide C++-style I/O and string-numeric
conversions for the new types, instead of doing anything with printf/scanf
for them.

-- 
Joseph S. Myers
joseph_at_[hidden]

Received on 2021-09-17 13:18:06