# signum function - sign(x)

**URL:** <https://forum.arduino.cc/t/signum-function-sign-x/37901>\
**Category:** Development\
**Created:** [June 17, 2009, 8:38pm UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901 "2009-06-17T20:38:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 17, 2009, 8:38pm UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/1 "2009-06-17T20:38:20Z")

</div>

Hi everyone,

can you please submit your most optimized functions for signum?

If anybody wonder it's defined this way:  
if x\>0 return 1  
if x\<0 return -1  
if x=0 return 0

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 17, 2009, 9:03pm UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/2 "2009-06-17T21:03:06Z")

</div>

I do:

> template   
> type sign(type value) {  
> return type((value\>0)-(value\<0));  
> }

It is part of my [Utility](http://www.arduino.cc/playground/uploads/Code/Utility.zip) library.

Example:

> #include \<Utility.h\>
> 
> void **setup** (){  
> Serial.begin(9600);
> 
> int i = 0;  
> Serial.println( sign(i), DEC);
> 
> char j = -1;  
> Serial.println( sign(j), DEC);
> 
> double k = 3.14;  
> Serial.println( sign(k), DEC);  
> }
> 
> void **loop** (){  
> }

This is probably not the most efficient way? But it is a way 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 17, 2009, 9:12pm UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/3 "2009-06-17T21:12:24Z")

</div>

Minor gotcha with that implementation because AFAIK the C language does not guarantee that expressions return 1 for "true" (they can return any non-zero value).

My favorite implementation is the one that's the clearest to read and lets the compiler do its own optimization (which is usually darned good):

```cpp
static inline int8_t sgn(int val) {
  if (val < 0) return -1;
  if (val==0) return 0;
  return 1;
}

```

I bet this will compile to something as efficient as any other method given the quality of GCC today!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 17, 2009, 9:34pm UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/4 "2009-06-17T21:34:50Z")

</div>

> Minor gotcha with that implementation because AFAIK the C language does not guarantee that expressions return 1 for "true" (they can return any non-zero value).

This might be the case, but Arduino is c++, yes?

In c++ I'm pretty sure a **true** will be explicitly promoted to the integral value 1.

I agree the version you present is cleaner though.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [June 18, 2009, 3:10am UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/5 "2009-06-18T03:10:07Z")

</div>

> In c++ I'm pretty sure a true will be explicitly promoted to the integral value 1.

Yup (assuming the compiler is conformant). From [conv](http://www.open-std.org/jtc1/sc22/open/n2356/conv.html)...

4 An rvalue of type bool can be converted to an rvalue of type int, with  
false becoming zero and true becoming one.

4 If the destination type is bool, see _conv.bool_. If the source type  
is bool, the value false is converted to zero and the value true is  
converted to one.

Personally, I would (and have) used both constructs regardless of the standard but it's good to know there shouldn't be any future problems.

- Brian

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 18, 2009, 3:47am UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/6 "2009-06-18T03:47:31Z")

</div>

Cool...learned something.

I think I was wrong about the compiler being super-optimal too. Here's what it came up with (version 4.4.0):

```cpp
      movw r18,r24
      sbrc r25,7
      rjmp .L7
      ldi r24,lo8(0)
      cp r18, __zero_reg__
      cpc r19, __zero_reg__
      brne .L8
      ret
.L8:
      ldi r24,lo8(1)
      ret
.L7:
      ldi r24,lo8(-1)
      ret

```

And here's what I came up with, smaller and faster:

```cpp
signed char sgn(int val) __attribute__ ((naked));
signed char sgn(int val)
{
  asm (
"      sbrc r25,7\n"
"      rjmp sgn_isNegative\n"

"  sbiw r24,1\n"
"  brpl sgn_isPositive\n"

"      clr  r24\n"
"      ret\n"

"sgn_isPositive:\n"
"      ldi r24,lo8(1)\n"
"      ret\n"

"sgn_isNegative:\n"
"      ldi r24,lo8(-1)\n"
"      ret\n"
  );
}

```

Warning! Not tested....

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 27, 2010, 8:18am UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/7 "2010-09-27T08:18:29Z")

</div>

Hi ,

can you please submit your functions for signum?

it's defined this way:

if ((x1 != 0) && (x2 != 0) && ( sign(x1) != sign(x2)))  
{  
p = x1 + x2 ;

}

else if

{  
p = sign(x1) \* min ( abs(v1), abs(v2));

}

else  
{  
i want repete until (x1 or / and x2 )!=0

thank you.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 27, 2010, 8:21am UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/8 "2010-09-27T08:21:51Z")

</div>

oups sorry it's p = sign(x1) \* min ( abs(x1), abs(x2));

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 7:54pm UTC](https://forum.arduino.cc/t/signum-function-sign-x/37901/9 "2021-05-06T19:54:02Z")

</div>

Closed on May 6, 2021, 7:54pm UTC


