signum function

B

Bernhard Reinhardt

Hi,

is there a "standard"-library like cmath that contains a signum
function? Searching the web I only found "make your own implementation!".

If I make my own subroutine, I have to decide whether it computes signum
of a int or a float or a double. While the functions containig in cmath
do not care what type the argument is. Besides I can not belief that no
professional implementaion exists.

Regards

Bernhard
 
G

Guest

Hi,

is there a "standard"-library like cmath that contains a signum
function? Searching the web I only found "make your own implementation!".

If I make my own subroutine, I have to decide whether it computes signum
of a int or a float or a double. While the functions containig in cmath
do not care what type the argument is. Besides I can not belief that no
professional implementaion exists.

You are wrong about the functions in cmath not caring about the type,
either there is one function for each type of they rely on up-casting. A
C++ approach might be to overload the function for all types, or using
templates.

Something like this might work

template<typename T>
T signum(T n)
{
if (n < 0) return -1;
if (n > 0) return 1;
return 0;
}
 
A

Adam Badura

If I make my own subroutine, I have to decide whether it computes signum
of a int or a float or a double. While the functions containig in cmath
do not care what type the argument is. Besides I can not belief that no
professional implementaion exists.

No. You don't have to. You can use overloading to have a version
for every type you want or templates to have a version for any type
that supports operations you used to implement the function or both
templates and overloading.

Adam Badura
 
B

Bernhard Reinhardt

Bernhard said:
Hi,

is there a "standard"-library like cmath that contains a signum
function? Searching the web I only found "make your own implementation!".

If I make my own subroutine, I have to decide whether it computes signum
of a int or a float or a double. While the functions containig in cmath
do not care what type the argument is. Besides I can not belief that no
professional implementaion exists.

The conclusion of the posts from Erik and Adam is again: Do it yourself!

I´m not only not familiar with the priciple of overloading (since I´m
quite a newbe) but I can´t belive that there´s no established library
which can do the thing.

If I have to create the code myself, I see two way´s to do:

1) Write the code in every program in which I need it. Or
2) create my own library (which I don´t know how to do, too).


I think option 1) is unhandy.
With option 2) my code won´t compile on different computers.

So please tell me that´s not all you got for me.

Regard

Bernhard
 
Z

Zeppe

Bernhard said:
The conclusion of the posts from Erik and Adam is again: Do it yourself!

I´m not only not familiar with the priciple of overloading (since I´m
quite a newbe) but I can´t belive that there´s no established library
which can do the thing.

If I have to create the code myself, I see two way´s to do:

1) Write the code in every program in which I need it. Or
2) create my own library (which I don´t know how to do, too).


I think option 1) is unhandy.
With option 2) my code won´t compile on different computers.

So please tell me that´s not all you got for me.

the simplest choice is probably:

template <typename T>
T sign(T t) { return (t < 0) : T(-1) : T(1); }

It's not always the best, but it's a good start.

Regards,

Zeppe
 
A

Adam Badura

I´m not only not familiar with the priciple of overloading (since I´m
quite a newbe) but I can´t belive that there´s no established library
which can do the thing.

1) Overloading is quite simple. You just write a few functions with
the same name but parameters of different types, like:

sgn(char val);
sgn(short val);
sgn(int val);

And so on...
Then when you call sgn the compiler will match arguments and choose
the best function.

2) I sometimes also think that standard library is to small. However
in C++ it seems to be philosophy of giving only realy basic
functionality. All the rest should be supported by thid-part
libraries. This is much different then it is for C# or Java. Or at
least I see it so.
2) create my own library (which I don´t know how to do, too).

This depends much on the system and compiler you use.

Adam Badura
 

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
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top