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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
maniA
  • 1,437
  • 2
  • 21
  • 42

2 Answers2

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

jfly
  • 7,715
  • 3
  • 35
  • 65
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.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • It means: printf("enter 3 values for s, t and mt:\n"); scanf("%c %c %c", &s, &t, &mt); and then s= __float128 strtoflt128 ("s", NULL); and so on? – maniA Feb 18 '14 at 08:37
  • @user3305448 not `%c', use `%s`, and you should decalare `s`, `t`, `mt` as char array which is big enough to hold what you input. – jfly Feb 18 '14 at 08:40
  • 1
    @user3305448: Almost. String is `%s`, `%c` is just a single character and that's not enough. And you should really include the length limit, so it will be more like `%63s` (if you declare `char s[64]` and pass `s` as argument) or `%ms` (if you declare `char *s`, pass `&s` as argument and call `free(s)` afterwards). Beware that `%ms` is a GNU extension, but than quadmath is not portable either. – Jan Hudec Feb 18 '14 at 08:43