2

I use the <quadmath.h>. With which argument type can I read my input correctly? If I use double it looks like:

printf("enter 3 values for s, t and mt:\n");
scanf("%lf %lf %lf", &s, &t, &mt);
printf("%lf %lf %lf\n", s, t, mt);

I tried different possibilities instead of "l" for example:

scanf("%Qf %Qf %Qf", &s, &t, &mt);

or even without

 scanf("%f %f %f", &s, &t, &mt);

however I get an error.

4

2 回答 2

3

You can't use %Qf specifier in the scanf, you should input the high precision numbers as string, then use strtoflt128 to convert. You also can't use %Qf specifier in the printf, use quadmath_snprintf to converts a __float128 floating-point number into a string, then use printf with %s specifier to display it. Here is an example.

#include <stdio.h>
#include <quadmath.h>
int main()
{
        char s[256], t[256], mt[256];
        printf("enter 3 values for s, t and mt:\n");
        scanf("%s %s %s", s, t, mt);
        __float128 qs = strtoflt128(s, NULL);

        __float128 qt = strtoflt128(t, NULL);

        __float128 qmt = strtoflt128(mt, NULL);

        quadmath_snprintf(s, 256, "%Qf", qs);

        quadmath_snprintf(t, 256, "%Qf", qt);

        quadmath_snprintf(mt, 256, "%Qf", qmt);

        printf("%s %s %s", s, t, mt);
        return 0;
}

run the program:

enter 3 values for s, t and mt:
3.4 4.5 5.6(enter)
3.400000 4.500000 5.600000

于 2014-02-18T08:26:29.130 回答
2

scanf (and related functions and printf and related functions) is not extensible. It can only parse what the standard library knows about. The C standard library comes with the operating system, not the compiler. It does not know about libquadmath, which is a compiler extension.

So you'll have to read strings and convert them separately using strtoflt128.

Note that C++ streams can be extended to extract __float128, but I don't see C++ interface in the quadmath library.

于 2014-02-18T08:25:34.183 回答