Home > Mobile >  How many floating-point types in c?
How many floating-point types in c?

Time:08-28

As many programmers know there are several floating-point types in C. So far I know float, double and long double but I'm not quite sure they are all of them, cause I found several definitions like __DEC32_MAX__. At first I thought that is another name for __FLT_MAX__ but when I tried to print it, I realized that it's different (as in below):

#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
*/

What are __DEC__* s?
Is there any other floating-point type that I don't know?

CodePudding user response:

There are only 3 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

C23 added 3 more 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 underscore are reserved for implementations

CodePudding user response:

Some specific uses require small floats like: float8 and float16 and bifloat It is used mainly in the DSP, AI and graphics applications. Many hardware architectures support at least some of them (for example bifloat AVX-512 BF16, ARM-V8.8, AMD ROCm, CUDA and some more).

There are also big float formats like float128. Example hardware: VSX in PowerPC.

Those floats are not supported directly by the standard C. Some compilers support it as an extension.

CodePudding user response:

The decimal types are very special, very interesting, and very much not standard (yet). They use an internal fraction representation which is based on base 10, not base 2. See in particular IEEE 754-2008.

So all of the important, unfortunate, tiresome arguments about 0.1 not being exactly representable, about 0.1 0.2 not exactly equaling 0.3, go away, and almost miraculously computer floating point can behave just like the decimal floating point we're all used to! But these types, and the printf support they'd require, are not standard, popular, or well known — yet.

In IEEE 754-2008, the two principal decimal types are decimal32 and decimal64, which are the decimal counterparts to our familiar binary float and double. There is also decimal128 analogous to long double.

  • Related