TC floating point

Walter Bright bright at Data-IO.COM
Sat Feb 24 07:21:24 AEST 1990


In article <22449 at unix.cis.pitt.edu> ding at unix.cis.pitt.edu (Yijun  Ding) writes:
<test2()
<{
<  /* error: floating point format not linked */
<  /* solution: x=sin(1) */
<  f = (double *)malloc(sizeof(double)*10);
<  scanf("%lf",f);
<  printf("%lf\n",*f);
<}
<The bug in test1() in not important, but I am still looking best way to avoid
<the problem in test2(). TC will load floating point routine 'automatically' for
<you. It is fooled when malloc() is used, even the program involves lengthy
<numerical calculations.

I don't use Turbo C, but I suspect that the way it works is that if the
*compiler* sees a floating point operation in the code, it inserts something
into the OBJ file to tell the linker to link in the floating point library.
The problem with test2() is that nowhere is there a floating point operation.
There are two versions of printf/scanf, one that's integer only and one
that can do floating point formatting. Since they are library routines,
the compiler cannot be reasonably expected to notice the %lf in the format
string and load the floating point versions of printf/scanf. The integer
versions get loaded, and then fail when they see the %lf.

The solution is to do some floating point operation in the source code.
x=sin(1); will work, but it will pull in a lot of code to handle trig
functions. Try something like:

#ifdef __TURBOC__
static void float_dummy()
{	static double d;
	d *= 2;			/* cause floating point code to be loaded */
}
#endif

Note that you never even need to call float_dummy(), it just has to *be* there.

Disclaimer: I haven't tried this, but I bet it'll work!



More information about the Comp.lang.c mailing list