Path: utzoo!censor!geac!torsqnt!lethe!yunexus!ists!helios.physics.utoronto.ca!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: Single and double precision math functions, are same names possible? Keywords: numerical analysis, math.h, single precision,double Message-ID: <552@taumet.com> Date: 14 Jan 91 16:35:08 GMT References: <1991Jan13.050236.11634@ux1.cso.uiuc.edu> <1991Jan13.061801.29334@portia.Stanford.EDU> Organization: Taumetric Corporation, San Diego Lines: 52 fangchin@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@taumet.com