Single and double precision math functions, are same names possible?

Stephen Clamage steve at taumet.com
Tue Jan 15 03:35:08 AEST 1991


fangchin at portia.Stanford.EDU (Chin Fang) writes:

>precision, libm.a, UNIX.

>Not too long ago, Glenn Geers of The Univ. of Sydney published a
>alternative math libs containing both single precision and double 
>precision lib functions and a header file for Intel i486/386 boxes. 
...
>Put it the other way, can you have both precisions using the same name
>and just a single header file as though I were still using the standard
>libm.a and math.h?  (So transparent that I would have no need to do
>extra editing of function names for porting except perhaps a few -D
>switches for compiliations?)

1.  C method:  The header file looks like this:

	#ifdef USE_FLOATS
		float sin(float);
		float cos(float);
		...etc
	#else
		double sin(double);
		double cos(double);
		...etc
	#endif

You then need two floating-point libraries, one with the float versions,
one with the double versions.  At link time, you select the appropriate
library.  You can do this automatically in a Make file.


2.  C++ method:  The header file looks like this:

	float  sin(float);
	double sin(double);
	float  cos(float);
	double cos(double);
	...etc

To oversimplify a bit, C++ allows multiple versions of a function, as long
as their parameter lists are not the same.  Both versions of each function
reside in the same library, and the correct one will get linked.  No
special compile- or link-time attention is needed.  You can even mix calls
to the float and double versions of the same function within one program
(in which case both get linked).  If the library functions are written in
assembler, the C++ naming conventions must be used for the external names.

A program which conforms to ANSI C standards (including prototypes) will
generally also be a legal and correct C++ program.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list