Thousands grouping in printf()

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Thousands grouping in printf()

sisyphus1
Hi,

This relates to the last of the failures that the mpfr-2.4.0-rc1 test suite
throws up. (Which should mean that, after this, I can stop annoying you
all.)

The demo:
###########################
C:\_32\C>type try.c
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
 double d = 5.587555123e-01;
 _putenv( "PRINTF_EXPONENT_DIGITS=2" );
 printf("%.10f\n", d);
 printf("%'.10f\n", d);
 printf("%'.*f\n", 10, d);

 return 0;
}


C:\_32\C>gcc -ansi -o try.exe try.c

C:\_32\C>try
0.5587555123
0.558756
0.000000
###########################

Clearly, the first line of output (where no thousands grouping has been
requested) is correct. As I understand it, on an ansi-compliant system, the
second and third lines would, in this example, be identical to the first.

It would be nice (but not greatly important) if there was some way to
achieve that ansi-compliant
behaviour with MinGW. However, I couldn't find a way of doing that.

If compiled without the -ansi switch, the output is:

0.5587555123
'.10f
'.*f

Cheers,
Rob


------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Thousands grouping in printf()

Keith Marshall
On Saturday 20 December 2008 00:39:35 Sisyphus wrote:
>  printf("%.10f\n", d);
>  printf("%'.10f\n", d);
>  printf("%'.*f\n", 10, d);

AFAIK, the "'f" formatting spec is not an ISO C (i.e. ANSI) provision,
nor even a standard POSIX provision; it is part of the XSI extension
to POSIX.  Thus...

>  0.5587555123

would be correct output for all of the above, so it is disturbing that
you see it only for the first, and...

>  0.558756
>  0.000000

for the second and third respectively.  While I have no immediate
plans to support the POSIX-XSI extension, I did think I'd set things
up so the thousands grouping flag would simply be ignored.  Please
file a bug report, on the SF tracker, and I'll follow it up.

> If compiled without the -ansi switch, the output is:
>
>  0.5587555123
>  '.10f
>  '.*f

I'm not at all disturbed by this.  Here, you are seeing the output
from Microsoft's printf() implementation, which clearly doesn't have
any provision to accommodate the thousands grouping flag -- good luck
with trying to get *them* to fix that!

--

Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Thousands grouping in printf()

Greg Chicares
In reply to this post by sisyphus1
On 2008-12-20 00:39Z, Sisyphus wrote:
>
>  printf("%.10f\n", d);
>  printf("%'.10f\n", d);
>  printf("%'.*f\n", 10, d);
[...]
> Clearly, the first line of output (where no thousands grouping has been
> requested) is correct. As I understand it, on an ansi-compliant system, the
> second and third lines would, in this example, be identical to the first.

There's no "'" flag in C99 [7.19.6.1/6]. That's a POSIX extension:
  http://opengroup.org/onlinepubs/007908799/xsh/fprintf.html

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Thousands grouping in printf()

Keith Marshall
On Saturday 20 December 2008 10:33:00 Greg Chicares wrote:

> On 2008-12-20 00:39Z, Sisyphus wrote:
> >  printf("%.10f\n", d);
> >  printf("%'.10f\n", d);
> >  printf("%'.*f\n", 10, d);
>
> [...]
>
> > Clearly, the first line of output (where no thousands grouping
> > has been requested) is correct. As I understand it, on an
> > ansi-compliant system, the second and third lines would, in this
> > example, be identical to the first.
>
> There's no "'" flag in C99 [7.19.6.1/6]. That's a POSIX extension:
>   http://opengroup.org/onlinepubs/007908799/xsh/fprintf.html

And not even a basic POSIX extension; it is only purportedly available
to applications which support the XSI extension to POSIX itself.

Furthermore, even with XSI support, the grouping should only affect
digits to the left of the radix point; there is nothing in the OP's
sample case, to demonstrate the grouping effect.

--

Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Thousands grouping in printf()

sisyphus1
In reply to this post by Keith Marshall

----- Original Message -----
From: "Keith Marshall" <[hidden email]>

[snip]

>
>  While I have no immediate
> plans to support the POSIX-XSI extension, I did think I'd set things
> up so the thousands grouping flag would simply be ignored.

Excellent. (That is the expectation of those failing tests.)

>  Please
> file a bug report, on the SF tracker, and I'll follow it up.
>

I'll do that tomorrow.

There was one other small bug that came to light, for which I'll also submit
a report to the SF tracker.

The demo program:

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

int main (void)
{
 double d = 3.72e-01;
 _putenv( "PRINTF_EXPONENT_DIGITS=2" );
 printf("%.*f\n", 0, d);
 printf("%.0f\n", d);
 return 0;
}

#############################

When compiled without the -ansi switch, that program correctly outputs:

0
0

But when compiled *with* the -ansi switch, it incorrectly outputs:

0.
0.

Thanks to all for your responses, your patience, and above all, for the
continuing work you do in making MinGW such a wonderful compiler.

Cheers,
Rob




------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Thousands grouping in printf()

Keith Marshall
On Sunday 21 December 2008 01:39:04 Sisyphus wrote:

> There was one other small bug that came to light, for which I'll
> also submit a report to the SF tracker.
>
> The demo program [relevant part]:
>   :
>  printf("%.*f\n", 0, d);
>  printf("%.0f\n", d);
>   :
>
> [...] when compiled *with* the -ansi switch, it incorrectly outputs:
>
> 0.
> 0.

Looks like:
https://sourceforge.net/tracker/index.php?func=detail&aid=2136252&group_id=2435&atid=102435

which is already fixed in CVS, and slated for inclusion in the next
release.

--

Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.