Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mathematical Signum function
03-12-2010, 02:44 AM (This post was last modified: 03-18-2010 04:03 AM by drdebcol.)
Post: #1
Mathematical Signum function
This is an interesting and simple function which i haven't found in any of programming languages. It is easy to make so i decided to make it.
In mathematics, the sign function is an odd mathematical function that extracts the sign of a real number.
Here is the mathematical definition :
[Image: 45159_sigumfunctiondefinition.jpg]
And you can find signum by using this formula :
[Image: 45160_sigumfunctiondefinition2.jpg]
As you can see it is defined for every x from set of real numbers.
And you can see that on this graphic :
[Image: 45161_signumfunctiongraphic.jpg]
So it is mathematically simple, and in programming it is also easy to make.
So here is the code in Pascal :
Code:
function sgn(x:real):shortint;
begin
  if x=0 then
    sgn:=0
  else
    if x>0 then
      sgn:=1
    else
      sgn:=-1;
end;
And C & C++ code :
Code:
short sgn(float x)
{
  if (x==0)
    return 0;
  else
    if (x>0)
      return 1;
    else
      return -1;    
}
I used shortint type in both of languages, just to save memory.
Basically it returns only 3 states (1,0,-1), so you can't use bool, but you can save memory by using shortint.
I hope that you learned something from this, it can't be more simple !
And yeah, you can find more about it here on Wikipedia :
http://en.wikipedia.org/wiki/Sign_function

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
03-12-2010, 09:39 PM
Post: #2
RE: Mathematical Signum function
hehe... interesting issue... I think i used something similar for one task of class all negative and positive numbers from one array in 2 other... If I find source, i will post it Smile Good point DrDebcol! Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
03-12-2010, 09:52 PM
Post: #3
RE: Mathematical Signum function
(03-12-2010 09:39 PM)l3g1sl4tor Wrote:  hehe... interesting issue... I think i used something similar for one task of class all negative and positive numbers from one array in 2 other... If I find source, i will post it Smile Good point DrDebcol! Smile
Yeah this can be useful.
I noticed that i forgot to mark 1 and -1 on graphic, but it is obvious !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
03-13-2010, 07:13 AM
Post: #4
RE: Mathematical Signum function
Oh well... first i have looked is formula and program, so i didnt noticed also Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: