How to printf long doubles

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

How to printf long doubles

sisyphus1
Hi,

With what, in the below program, should I replace "%Lf\n",  in order that
the desired value (0.032258) is printed out.
Currently, I'm getting -0.000000.
-----------------------
#include <stdio.h>

int main (void)
{
  long double d = 1. / 31.;
  printf ("%Lf\n", d);
  return 0;
}

----------------------

Google will only tell me that "%Lf" is broken on MinGW (as of a few years
ago), and the wiki has been removed.

Cheers,
Rob




------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

JonY-2
On 12/13/2008 10:05, Sisyphus wrote:

> Hi,
>
> With what, in the below program, should I replace "%Lf\n",  in order that
> the desired value (0.032258) is printed out.
> Currently, I'm getting -0.000000.
> -----------------------
> #include<stdio.h>
>
> int main (void)
> {
>    long double d = 1. / 31.;
>    printf ("%Lf\n", d);
>    return 0;
> }
>
> ----------------------
>
> Google will only tell me that "%Lf" is broken on MinGW (as of a few years
> ago), and the wiki has been removed.
>
> Cheers,
> Rob
>
>

Hi,
Try adding "-D__USE_MINGW_ANSI_STDIO" to gcc when compiling it.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

Keith Marshall
On Saturday 13 December 2008 02:22:02 JonY wrote:
> > Google will only tell me that "%Lf" is broken on MinGW (as of a
> > few years ago), and the wiki has been removed.

It isn't so much that "%Lf" is broken, as that MSVCRT doesn't
differentiate between `long double' and `double'.  Both are treated
as being 64-bit; however, MinGW has 80-bit `long double'.

> Try adding "-D__USE_MINGW_ANSI_STDIO" to gcc when compiling it.

mingwrt-3.15 onwards[*] provides a fully ANSI conformant printf();
however, it isn't used by default, unless you compile with `-ansi',
`-posix' or, as JonY suggests, `-D__USE_MINGW_ANSI_STDIO'.

Regards,
Keith.

[*] Earlier mingw-runtime versions didn't support this; you could,
however, use snprintf() to get 80-bit `long double' interpretation.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

sisyphus1

----- Original Message -----
From: "Keith Marshall" <[hidden email]>
To: <[hidden email]>
Sent: Saturday, December 13, 2008 9:51 PM
Subject: Re: [Mingw-users] How to printf long doubles


> On Saturday 13 December 2008 02:22:02 JonY wrote:
>> > Google will only tell me that "%Lf" is broken on MinGW (as of a
>> > few years ago), and the wiki has been removed.
>
> It isn't so much that "%Lf" is broken, as that MSVCRT doesn't
> differentiate between `long double' and `double'.  Both are treated
> as being 64-bit; however, MinGW has 80-bit `long double'.
>
>> Try adding "-D__USE_MINGW_ANSI_STDIO" to gcc when compiling it.
>
> mingwrt-3.15 onwards[*] provides a fully ANSI conformant printf();
> however, it isn't used by default, unless you compile with `-ansi',
> `-posix' or, as JonY suggests, `-D__USE_MINGW_ANSI_STDIO'.
>
> Regards,
> Keith.
>
> [*] Earlier mingw-runtime versions didn't support this; you could,
> however, use snprintf() to get 80-bit `long double' interpretation.
>

Aaah ... excellent !! Thanks JohnY, Keith.

I haven't tried the snprintf() approach, but I've updated my mingw runtime
(to 3.15.1) and '-posix', '-ansi' and '-D__USE_MINGW_ANSI_STDIO' all work
fine.

I've struck a glitch with stdlib.h and '-ansi'. (No problem with either
'-posix', or '-D__USE_MINGW_ANSI_STDIO' but.)

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

int main (void)
{
  long double d = 1. / 31.;
  printf ("%Lf\n", d);
  return 0;
}


C:\_32\C>gcc -ansi -o try.exe try.c
In file included from try.c:2:
C:/home/rob/mingw_vista/i686-pc-mingw32/bin/../lib/gcc/i686-pc-mingw32/3.4.5/../../../../include/stdlib.h:317:
error: syntax error before "double"

C:\_32\C>
######################################

Line 317 is:

inline double __cdecl __MINGW_NOTHROW strtod (const char* __restrict__
__nptr, char** __restrict__ __endptr)

Is that syntax error to be expected when using the -ansi switch and
including stdlib.h ?

Cheers,
Rob


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

JonY-2
On 12/14/2008 10:27, Sisyphus wrote:

> ----- Original Message -----
> From: "Keith Marshall"<[hidden email]>
> To:<[hidden email]>
> Sent: Saturday, December 13, 2008 9:51 PM
> Subject: Re: [Mingw-users] How to printf long doubles
>
>
>> On Saturday 13 December 2008 02:22:02 JonY wrote:
>>>> Google will only tell me that "%Lf" is broken on MinGW (as of a
>>>> few years ago), and the wiki has been removed.
>> It isn't so much that "%Lf" is broken, as that MSVCRT doesn't
>> differentiate between `long double' and `double'.  Both are treated
>> as being 64-bit; however, MinGW has 80-bit `long double'.
>>
>>> Try adding "-D__USE_MINGW_ANSI_STDIO" to gcc when compiling it.
>> mingwrt-3.15 onwards[*] provides a fully ANSI conformant printf();
>> however, it isn't used by default, unless you compile with `-ansi',
>> `-posix' or, as JonY suggests, `-D__USE_MINGW_ANSI_STDIO'.
>>
>> Regards,
>> Keith.
>>
>> [*] Earlier mingw-runtime versions didn't support this; you could,
>> however, use snprintf() to get 80-bit `long double' interpretation.
>>
>
> Aaah ... excellent !! Thanks JohnY, Keith.
>
> I haven't tried the snprintf() approach, but I've updated my mingw runtime
> (to 3.15.1) and '-posix', '-ansi' and '-D__USE_MINGW_ANSI_STDIO' all work
> fine.
>
> I've struck a glitch with stdlib.h and '-ansi'. (No problem with either
> '-posix', or '-D__USE_MINGW_ANSI_STDIO' but.)
>
> ######################################
> C:\_32\C>type try.c
> #include<stdio.h>
> #include<stdlib.h>
>
> int main (void)
> {
>    long double d = 1. / 31.;
>    printf ("%Lf\n", d);
>    return 0;
> }
>
>
> C:\_32\C>gcc -ansi -o try.exe try.c
> In file included from try.c:2:
> C:/home/rob/mingw_vista/i686-pc-mingw32/bin/../lib/gcc/i686-pc-mingw32/3.4.5/../../../../include/stdlib.h:317:
> error: syntax error before "double"
>
> C:\_32\C>
> ######################################
>
> Line 317 is:
>
> inline double __cdecl __MINGW_NOTHROW strtod (const char* __restrict__
> __nptr, char** __restrict__ __endptr)
>
> Is that syntax error to be expected when using the -ansi switch and
> including stdlib.h ?
>
> Cheers,
> Rob
>

Hi,
it looks like a bug in stdlib.h, there is no "inline" keyword in the
ANSI C standard.

Fixing it seems trivial by checking for "__STRICT_ANSI__". gcc sets it
to 1 when invoked with "-ansi".

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

Keith Marshall
On Sunday 14 December 2008 02:37:41 JonY wrote:
> > inline double __cdecl __MINGW_NOTHROW strtod (const char*
> > __restrict__ __nptr, char** __restrict__ __endptr)
> >
> > Is that syntax error to be expected when using the -ansi switch
> > and including stdlib.h ?
>
> it looks like a bug in stdlib.h,

It is; already known, and fixed in CVS, but it was reported just too
late to make it into mingwrt-3.15.1.  Chris, I presume you  will see
this; IMO, this is sufficiently critical to warrant a 3.15.2 release.

> there is no "inline" keyword in the ANSI C standard.
>
> Fixing it seems trivial by checking for "__STRICT_ANSI__". gcc sets
> it to 1 when invoked with "-ansi".

A better way to fix it is to correct that errant line in stdlib.h;
either `s/inline/__inline__/', or replace MinGW's include/stdlib.h
with the already corrected version, from:
http://cygwin.com/cgi-bin/cvsweb.cgi/~checkout~/src/winsup/mingw/include/stdlib.h?rev=1.37&content-type=text/plain&cvsroot=src

Regards,
Keith.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

sisyphus1

----- Original Message -----
From: "Keith Marshall" <[hidden email]>
.
.
>>
>> Fixing it seems trivial by checking for "__STRICT_ANSI__". gcc sets
>> it to 1 when invoked with "-ansi".
>
> A better way to fix it is to correct that errant line in stdlib.h;
> either `s/inline/__inline__/', or replace MinGW's include/stdlib.h
> with the already corrected version, from:
> http://cygwin.com/cgi-bin/cvsweb.cgi/~checkout~/src/winsup/mingw/include/stdlib.h?rev=1.37&content-type=text/plain&cvsroot=src

Thanks again JonY, Keith - I've now fixed stdlib.h.

One thing that I'm struggling to achieve is to have an ansi-compliant
printf() in a static library. For testing, I created a 'libfoo.a' as
follows:

#################################
C:\_32\C>type foolib.c
#include <stdio.h>

void my_print(void) {
     long double d = 1. / 31.;
     printf("%Lf\n", d);
}


C:\_32\C>gcc -D__USE_MINGW_ANSI_STDIO -c foolib.c

C:\_32\C>ar rc libfoo.a foolib.o

C:\_32\C>ranlib libfoo.a

C:\_32\C>
#################################

And I also wrote, built and ran a little app to test the ansi-compliance of
my_print():

#################################
C:\_32\C>type foo.c
#include <stdio.h>

int main(void) {
    long double d = 1. /31.;
    printf("%Lf\n", d);
    my_print();
    return 0;
}


C:\_32\C>gcc -D__USE_MINGW_ANSI_STDIO -o foo.exe foo.c -L. -lfoo

C:\_32\C>foo
0.032258
-0.000000

C:\_32\C>
#################################

Is there any way to have my_print() print out 0.032258 ?

Cheers,
Rob





------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

Keith Marshall
On Monday 15 December 2008 04:25:03 Sisyphus wrote:
> One thing that I'm struggling to achieve is to have an
> ansi-compliant printf() in a static library.

FTR, the ansi-compliant printf() is provided by a MinGW specific
function, __mingw_printf(), (and similarly for __mingw_fprintf(),
etc.).  These all live in libmingwex.a, which is *always* linked
statically.

> For testing, I created a 'libfoo.a' as follows:
>
> [...snip...]
>
> C:\_32\C>gcc -D__USE_MINGW_ANSI_STDIO -o foo.exe foo.c -L. -lfoo

FWIW, __USE_MINGW_ANSI_STDIO is an internal symbol, (as is hinted by
the double underscore prefix).  It is defined internally, as a side
effect of specifying either `-posix' or `-ansi', (or as a side effect
of defining any one of a number of other publicly documented symbols,
as specified in _mingw.h).  In general, you should prefer to specify
`-ansi' or `-posix', rather than defining __USE_MINGW_ANSI_STDIO
directly, as you do here.

> C:\_32\C>foo
> 0.032258
> -0.000000

Using *exactly* your sample code, copied and pasted from your message,
I am unable to reproduce this...

> Is there any way to have my_print() print out 0.032258 ?

For me, this is exactly what I see.

--

Regards,
Keith.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: How to printf long doubles

sisyphus1

----- Original Message -----
From: "Keith Marshall" <[hidden email]>
.
.
>
>> Is there any way to have my_print() print out 0.032258 ?
>
> For me, this is exactly what I see.
>

It's now also what I see.
I went carefully through that entire process at least 6 times - probably
more like 10 or a dozen times.
Each time my_print() spat out -0.000000.

When I came back to it this morning, eventually, instead of building the
test app with:

gcc -D__USE_MINGW_ANSI_STDIO -o foo.exe foo.c -L. -lfoo

I did:

gcc -D__USE_MINGW_ANSI_STDIO -o foo.exe foo.c foolib.o

and my_print() output the correct 0.032258

So I tried another variant on the build of the test app:

gcc -D__USE_MINGW_ANSI_STDIO -o foo.exe foo.c libfoo.a

but my_print() spat out -0.000000 again. At that point, I deleted libfoo.a,
re-built it, and everything has been fine ever since. Annoyingly, I'm unable
to determine what the problem was - I can no longer reproduce it. I guess I
must have been doing *something* stupid (again and again and again), but I
can't work out what it was. I know at one stage early on I forgot to include
the '-D__USE_MINGW_ANSI_STDIO' when building foolib.o. I hypothesised that
that particular (incorrect) build of foolib.o stayed in libfoo.a, despite
repeatedly re-running 'ar rc libfoo.a foolib.o' (where foolib.o had been
re-built correctly). But I've tested that hypothesis out, and can't get it
to hold water.

The problem went away a couple of hours before you posted - my apologies for
not updating the thread a little earlier. It might have saved you some time
and effort.

Thanks again for your help (and JonY's), and for the additional info re
'-D__USE_MINGW_ANSI_STDIO' that you've just provided. It's much appreciated.

Cheers,
Rob


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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
|

Posting lessons learned by a first time poster

Patrick Lengel
In reply to this post by Keith Marshall

These are the lessons learned from a first time poster to this
board about proper posting.  I posted a question and made some newbie
errors and I am trying to abide by the common practices for this
community.  Keith Marshall was kind enough to spend time to teach
me about proper posting practices and indicated that posting my
experience may be useful to others.  This is my attempt to give
back a little for the courtesy he has shown me by bringing me
up to speed with preferred posting practices.  I have copied
some of my correspondence with Keith with responses that I hope
some may find to be useful.


> So, what do you mean by "top post"?

Keith provided some great links about "Top Posting".  The following
is a great link that explains what this means.  I hope my e-mail
follows proper "top posting" now.
  http://www.redballoon.net/~snorwood/quote-rant.shtml
  http://www.caliburn.nl/topposting.html
  http://www.google.com/search?q=top+posting


> Also, what do you mean by "plain text".

I personally use my Hotmail account and found that at the top
of my e-mail window that there is a button to select one of
several e-mail formatting styles.  One style indicates "Plain text"
and this is the option you want.  Do not use the "Rich Text" or
"HTML" options.  The button is right next to the "Spell check"
button.  I never knew this was an option until Keith kindly
explained what was needed.  Maybe your e-mail package has this
option hidden somewhere else.  The "Rich text" and "HTML" formats
add extra formatting information to the text material that may make
it difficult for the recipients to read.


> Try to use  after about 75 characters.

I found that after selecting the "Plain text" option that
my text keeps going to the right forever and that my email
package doesn't automatically wrap my text when I
reach the right margin of the window.  When properly selecting
the "Plain text" option you are forced to enter a  just
so you can see what you are typing.  If your program
automatically wraps the line of text you may not be using the
correct formatting option.  Someone who doesn't use an email
package that decodes the non-"Plain text" format may see one
continuous line of text that is 1k characters long.
Very hard to read.


> however, if you don't object, there may be merit in copying it back
> to the list, for the benefit of others who ask the same questions.

I hope that I did well to reflect the useful information and advice
that I have learned from Keith back to the community.  You have
provided help to me and I only hope that by capturing my new
experiences that I can provide something back to you.  It may not
seem like much but it may be all I have to offer at this time.

Kind regards,
Patrick

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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: Posting lessons learned by a first time poster

Tuomo Latto
Patrick Lengel wrote:

> These are the lessons learned from a first time poster to this
> board about proper posting.  I posted a question and made some newbie
> errors and I am trying to abide by the common practices for this
> community.  Keith Marshall was kind enough to spend time to teach
> me about proper posting practices and indicated that posting my
> experience may be useful to others.  This is my attempt to give
> back a little for the courtesy he has shown me by bringing me
> up to speed with preferred posting practices.  I have copied
> some of my correspondence with Keith with responses that I hope
> some may find to be useful.

There's at least one more to be learned. You just hijacked a thread
by replying to an existing message instead of writing a new one.

E-mail messages include a Message-Id header.
When replying to a message, the contents of said header field
are copied to References (for context, I think)
and, more importantly, to In-Reply-To headers in the reply.
The latter header is used by e-mail clients and list archive
software to construct a threaded (tree-like) view of the messages.

For proof, see:
http://n2.nabble.com/How-to-printf-long-doubles-tt1650580.html#a1666296
(and http://n2.nabble.com/MinGW-users-f1079350.html)

Notice how your message is now neatly shown as belonging to
a thread started by "How to printf long doubles".


--
Tuomo

... Do NOT look into laser with remaining eyeball!


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
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.