namespace check on compile time

L

Lodewijk Smit

Hi,

In the C standard of 1999 additional mathematical functions are added.

For example, beside the traditional
double sin(double x);

there are also the functions:
float sinf(float x);
long double sinl(long double x);

to compute the sin for respectively float and long double types.

However, most compilers do not implement all defined mathematical functions
in the standard. For example, the Intel 7.1 C compiler implements the float
version of sin (sinf), but has not the atan2f function. The gcc compiler
even not implements the sinf function.

It is simple to implement an own function for floats, e.g.

float mysinf(float x) {(float) return sin ((double) x);}

However, it is clear that this typecasted function is less efficient than a
dedicated sinf function. And maybe also not functional equivalent, because I
use a higher precision during the computation. Therefore, I would like to
use the dedicated sinf function if this is implemented by the compiler and
to use my own (not optimal) function otherwise. It is possible to implement
this behaviour by using #ifdef statements, but this is not prefered. In this
way I have to check all the mathematical functions that are used in my
program for every C compiler that can be used to compile my "standard" C
program.

Is it possible to check on compile time the namespace whether a sinf
function is included?
If this is possible, I can define my own sinf function if it is not in the
namespace and otherwise use the dedicated sinf function of the compiler.

Maybe there are also other known solutions to avoid this problem...

Any help is appreciated.

With kind regards,

Lodewijk
 
A

av

Lodewijk said:
Hi,

In the C standard of 1999 additional mathematical functions are added.

For example, beside the traditional
double sin(double x);

there are also the functions:
float sinf(float x);
long double sinl(long double x);

to compute the sin for respectively float and long double types.

However, most compilers do not implement all defined mathematical
functions in the standard. For example, the Intel 7.1 C compiler
implements the float version of sin (sinf), but has not the atan2f
function. The gcc compiler even not implements the sinf function.

It is simple to implement an own function for floats, e.g.

float mysinf(float x) {(float) return sin ((double) x);}
....

Hi,

I think that this is something that is more of a linker issue rather than a
compiler issue. What you can do, is try and link your version of the
function you want to modify after the standard C libraries. This will cause
the standard library function to be picked up in the cases where it is
supported and use your custom function in the case where it is not.

There again like you mentioned, it introduces inconsistencies between the
values that you will get back from different implementations.

If someone else has a better suggestion, I am all eyes (ears won't help in
this case).

A./
 
S

Sam Dennis

Lodewijk said:
In the C standard of 1999 additional mathematical functions are added.

However, most compilers do not implement all defined mathematical functions
in the standard. The gcc compiler even not implements the sinf function.

GCC doesn't implement much of the standard library; glibc, however, its
companion, most certainly does implement such -l and -f functions; they
aren't defined unless you're compiling for C99 (or other modes that are
significantly off-topic), which is not the default.
Is it possible to check on compile time the namespace whether a sinf
function is included?

Sort of: you can compare __STDC_VERSION__ against 199901; it's not your
problem if the implementation lies about its conformance status.
 
T

those who know me have no need of my name

in comp.lang.c i read:
then those compilers don't conform for a hosted implementation. a free-
standing implementation need not implement them. whether a specific
compiler claims conformance as hosted and/or free-standing isn't topical
for this newsgroup.
Sort of: you can compare __STDC_VERSION__ against 199901; it's not your
problem if the implementation lies about its conformance status.

a conforming c89 compiler is free to define __STDC_VERSION__ to anything it
likes. qoi alone would argue that you won't find it set to anything too
odd, but it might easily be a value larger than 199901L or non-numeric.
 
K

Keith Thompson

those who know me have no need of my name said:
a conforming c89 compiler is free to define __STDC_VERSION__ to anything it
likes. qoi alone would argue that you won't find it set to anything too
odd, but it might easily be a value larger than 199901L or non-numeric.

Theoretically, yes, but I seriously doubt that any conforming C89/C90
compiler ever has, or ever will, define __STDC_VERSION__ either as
anything larger than 199901L or as anything non-numeric. (Any
compiler that did so would qualify for immediate defenestration.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top