[Cfp-interest] outstanding issues

Fred J. Tydeman tydeman at tybor.com
Wed Apr 11 10:23:16 PDT 2012


On Tue, 10 Apr 2012 16:35:30 -0700 Jim Thomas wrote:
>
>ISSUE 3: Would it be better to restrict the format specifiers 
>that the strfrom functions have to support?


I recently discovered that gcc has similar support.
>From the info pages on libquadmath:

3.2 `quadmath_snprintf' -- Convert to string
============================================

The function `quadmath_snprintf' converts a `__float128' floating-point
number into a string.  It is a specialized alternative to `snprintf',
where the format string is restricted to a single conversion specifier
with `Q' modifier and conversion specifier `e', `E', `f', `F', `g',
`G', `a' or `A', with no extra characters before or after the
conversion specifier.  The `%m$' or `*m$' style must not be used in the
format.

Syntax
     `int quadmath_snprintf (char *s, size_t size, const char *format,
     ...)'

_Arguments_:
     S          output string
     SIZE       byte size of the string, including tailing NUL
     FORMAT     conversion specifier string

Example
          #include <quadmath.h>
          #include <stdlib.h>
          #include <stdio.h>

          int main ()
          {
            __float128 r;
            int prec = 20;
            int width = 46;
            char buf[128];

            r = 2.0q;
            r = sqrtq (r);
            int n = quadmath_snprintf (buf, sizeof buf, "%+-#*.20Qe", width, r);
            if ((size_t) n < sizeof buf)
              printf ("%s\n", buf);
              /* Prints: +1.41421356237309504880e+00 */
            quadmath_snprintf (buf, sizeof buf, "%Qa", r);
            if ((size_t) n < sizeof buf)
              printf ("%s\n", buf);
              /* Prints: 0x1.6a09e667f3bcc908b2fb1366ea96p+0 */
            n = quadmath_snprintf (NULL, 0, "%+-#46.*Qe", prec, r);
            if (n > -1)
              {
                char *str = malloc (n + 1);
                if (str)
                  {
                    quadmath_snprintf (str, n + 1, "%+-#46.*Qe", prec, r);
                    printf ("%s\n", str);
                    /* Prints: +1.41421356237309504880e+00 */
                  }
                free (str);
              }
            return 0;
          }


   On some targets when supported by the C library hooks are installed
for `printf' family of functions, so that `printf ("%Qe", 1.2Q);' etc.
works too.





---
Fred J. Tydeman        Tydeman Consulting
tydeman at tybor.com      Testing, numerics, programming
+1 (775) 358-9748      Vice-chair of PL22.11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.



More information about the Cfp-interest mailing list