K
Keith Thompson
lcw1964 said:jacob navia wrote: [...]Obviously this is a bug. You could report it to them, maybe they
are interested in knowing about it. There must be some mailing
list in the cygwin docs. I was subscribed ages ago.
Under linux:
[root@gateway tmp]# cat texpl.c
#include <stdio.h>
#include <math.h>
int main(void)
{
long double n = expl(1.0L);
printf("%Lg\n",n);
return 0;
}
[root@gateway tmp]# gcc texpl.c -lm
[root@gateway tmp]# ./a.out
2.71828
[root@gateway tmp]#
this works, so it must be a bug in the cygwin environment/library.
Messr. Navia, that worked for me.
The key is the addition of the -lm parameter to the command line, which
I did not have before.
I suspect that I have made an embarassing beginner's error and have
stirred up a lot of hubbub unnecessarily!
I have no idea what those three little characters mean (-lm), but
something tells me that before I post my next newbie question I do a
little more homework first.
That's a reasonable conclusion, but I'm afraid it happens to be wrong.
The "-lm" option, in some (mostly Unix-like) implementations, tells
the linker to link in the math library. This is question 14.3 in the
comp.lang.c FAQ, <http://www.c-faq.com/>.
But that's not what's going on here. The following is mostly specific
to Cygwin, and therefore only marginally topical.
Cygwin's implementation happens to be smart enough that it doesn't
need the "-lm" option; it links the math library without being asked
if it needs it.
jacob's program works for me under Cygwin, but only because the
compiler itself is smart enough to replace the expression expl(1.0L)
with its value during compilation. If you change the line
long double n = expl(1.0L);
to
long double one = 1.0L;
long double n = expl(one)
then it fails. If you declare one as "const" and compile with "-O1"
or higher, then it works again.
The compiler is capable of evaluating expl() in some very limited
circumstances. If that fails, it generates a call to expl(), which
doesn't exist in the runtime library (regardless of whether you use
"-lm").
The point is that the compiler and the runtime libraries are two
different part of the implementation. If they're provided separately,
you can see odd behavior if one of them supports a given feature and
the other doesn't.
If "double" precision is good enough, use exp(). If not you'll need
to find another solution. (You might be able to use exp() to create a
close approximation to the correct long double result, but I don't
know how to refine that to the required precision.)
There are open-source C libraries including glibc. You might be able
to extract an expl() implementation from one of them. Google is your
friend.