# How to define signum function

**URL:** <https://community.freefem.org/t/how-to-define-signum-function/2727>\
**Category:** General Discussion\
**Created:** [September 27, 2023, 9:49am UTC](https://community.freefem.org/t/how-to-define-signum-function/2727 "2023-09-27T09:49:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaghunathBandha](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/raghunathbandha/32/1843_2.png) [@RaghunathBandha](https://community.freefem.org/u/RaghunathBandha)\
**Post date:** [September 27, 2023, 9:49am UTC](https://community.freefem.org/t/how-to-define-signum-function/2727/1 "2023-09-27T09:49:54Z")

</div>

I want to define sgn(x)=1 for x\>=0 and sgn(x)=-1 for x\<0. How can I define this at starting of my program in freefem.

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [September 27, 2023, 10:15am UTC](https://community.freefem.org/t/how-to-define-signum-function/2727/2 "2023-09-27T10:15:03Z")

</div>

Many ways to do so. for example

> func sgn = (x \>=0 ? 1.0 : -1.0);

or also

> func real sgn(real xx) = (xx \>=0 ? 1.0 : -1.0);

or via macros

> macro sgn(xx) (( xx \>=0 ? 1.0 : -1.0 )) // EOM

---

<div class="post-metadata">

**Author:** ![RaghunathBandha](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/raghunathbandha/32/1843_2.png) [@RaghunathBandha](https://community.freefem.org/u/RaghunathBandha)\
**Post date:** [September 27, 2023, 11:42am UTC](https://community.freefem.org/t/how-to-define-signum-function/2727/3 "2023-09-27T11:42:04Z")

</div>

Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [September 28, 2023, 8:27am UTC](https://community.freefem.org/t/how-to-define-signum-function/2727/4 "2023-09-28T08:27:38Z")

</div>

the freefem++ sign do this in c++  
// c++ code …  
double sign(double x){return (x\>0.)-(x\<0.); }// Add FH jan 2018

real s= sign(1);
