[comp.lang.c] TC floating point

ding@unix.cis.pitt.edu (Yijun Ding) (02/22/90)

After reading TC sizeof() in the news group, here is a program to show
bugs in TC 2.0:

#include <math.h>
char buf[80];
double x, *f;

main()
{
  test1();
  test2();
}

test2()
{
  /* error: floating point format not linked */
  /* solution: x=sin(1) */
  f = (double *)malloc(sizeof(double)*10);
  scanf("%lf",f);
  printf("%lf\n",*f);
}

test1()
{
  /* number of digits after decimal incorrect */
  x = 3.14;
  sprintf(buf,"%8.0e %8.1e %8.2e %8.3e\n",x,x,x,x);
  puts(buf);
}

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.

bright@Data-IO.COM (Walter Bright) (02/24/90)

In article <22449@unix.cis.pitt.edu> ding@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!