Jen*_*nny 3 c floating-point types decimal
正如许多程序员都知道 C 中有几种浮点类型。到目前为止,我知道float,double但long double我不太确定它们是全部,因为我发现了几个定义,例如__DEC32_MAX__。起初我以为这是另一个名称,__FLT_MAX__但当我尝试打印它时,我意识到它是不同的(如下所示):
#include <stdio.h>
void main(void)
{
__mingw_printf("flt value: %e, size: %d\n", __FLT_MAX__, sizeof(__FLT_MAX__));
__mingw_printf("dbl value: %e, size: %d\n", __DBL_MAX__, sizeof(__DBL_MAX__));
__mingw_printf("ldbl value: %e, size: %d\n", __LDBL_MAX__, sizeof(__LDBL_MAX__));
__mingw_printf("dec32 value: %e, size: %d\n", __DEC32_MAX__, sizeof(__DEC32_MAX__));
__mingw_printf("dec64 value: %e, size: %d\n", __DEC64_MAX__, sizeof(__DEC64_MAX__));
__mingw_printf("dec128 value: %e, size: %d\n", __DEC128_MAX__, sizeof(__DEC128_MAX__));
}
/* output:
flt value: 3.402823e+038, size: 4
dbl value: 1.797693e+308, size: 8
ldbl value: 3.237664e-317, size: 16
dec32 value: 9.944455e-315, size: 4
dec64 value: 9.089022e+269, size: 8
dec128 value: 3.237656e-317, size: 16
*/
Run Code Online (Sandbox Code Playgroud)
什么是__DEC__*?
还有其他我不知道的浮点类型吗?
There are only 3 real floating-point types in C before C23: float, double and long double, which can use arbitrary base for the significand like decimal or octal although nowadays it's most likely binary. Some older systems use hexadecimal floating-point types. Each of the real floating types can be combined with the _Complex or _Imaginary keyword to make complex floating types
C23 added 3 more real floating-point types: _Decimal32, _Decimal64, and _Decimal128, all are decimal types
Anything else if you see would be compiler extensions or internal compiler things. Those would be prefixed with __ because identifiers beginning with double underscores are reserved for implementations. __mingw_printf, __DBL_MAX__, __DEC64_MAX__... are all internal to the implementation and you aren't supposed to use them directly. Use FLT_MAX instead of __FLT_MAX__
In fact before C23 there's a decimal floating extension in GCC
As an extension, GNU C supports decimal floating types as defined in the N1312 draft of ISO/IEC WDTR24732. Support for decimal floating types in GCC will evolve as the draft technical report changes. Calling conventions for any target might also change. Not all targets support decimal floating types.
The decimal floating types are
_Decimal32,_Decimal64, and_Decimal128. They use a radix of ten, unlike the floating typesfloat,double, andlong doublewhose radix is not specified by the C standard but is usually two.6.14 Decimal Floating Types
Since mingw is a GCC port, it also supports that decimal floating extension. Typically in float.h the standard constants would be defined as the implementation-specific constants, for example
#define LDBL_MAX __LDBL_MAX__
#define DEC128_MAX __DEC128_MAX__
Run Code Online (Sandbox Code Playgroud)