TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: KURT J. TISCHER
from: BOB STOUT
date: 1997-07-08 12:26:00
subject: Signs of numbers

On , Kurt J. Tischer (1:157/438@fidonet) wrote: 
 > I am trying to put together a windows DLL file using Visual C++ and one of
 > my old VB functions uses a function internal to VB called Sgn, which
 > returns an integer (1, 0, or -1) corresponding to the sign of a number.
 > (e.g.   numSign = Sgn(numSign)   )
 > Does Visual C++ have a similar function?  If so, in what header file is it
 > located and what is the name of the function?  If not, would someone 
lease
 > show me a function to determine the sign of a number which would return 1
 > if positive, 0 if zero, and -1 if negative...?
  Drop in the following - it will be in the next SNIPPETS release...
/*
**  SGN.H - C equivalent to BASIC's SGN. Returns 1 if argument is positive,
**          -1 if argument is negative, 0 if argument is zero.
**
**  public domain by Bob Stout
*/
#if defined(__cplusplus) && __cplusplus
 #if  (defined(__SC__) && __SC__ >= 0x700) || \
      (defined(_MSC_VER) && _MSC_VER > 800) || \
      (defined(__WATCOMC__) && __WATCOMC__ >= 1000) || \
      (defined(__BORLANDC__) && __BORLANDC__ >= 0x450)
  template
  inline int sgn(const T n)
  {
        if (n > 0)
              return 1;
        if (n < 0)
              return -1;
        return 0;
  }
 #else /* no templates */
  /*
  ** prototypes for overloaded sgn() functions
  */
  inline int sgn(const char n)
  {
        if (n > 0)
              return 1;
        if (n < 0)
              return -1;
        return 0;
  }
  inline int sgn(const unsigned char n)
  {
        if (n > 0)
              return 1;
        return 0;
  }
  inline int sgn(const int n)
  {
        if (n > 0)
              return 1;
        if (n < 0)
              return -1;
        return 0;
  }
  inline int sgn(const unsigned int n)
  {
        if (n > 0)
              return 1;
        return 0;
  }
  inline int sgn(const long n)
  {
        if (n > 0)
              return 1;
        if (n < 0)
              return -1;
        return 0;
  }
  inline int sgn(const unsigned long n)
  {
        if (n > 0)
              return 1;
        return 0;
  }
  inline int sgn(const double n)
  {
        if (n > 0)
              return 1;
        if (n < 0)
              return -1;
        return 0;
  }
  inline int sgn(const float n)
  {
        if (n > 0)
              return 1;
        if (n < 0)
              return -1;
        return 0;
  }
 #endif
#else /* C, not C++ */
 /*
 **  Note that the C version is an unsafe macro since it may evaluate its
 **  argument more than once.
 */
 #define sgn(x) (((x)>0)?1:((x)<0)?-1:0)
#endif
/*
**  Define the macro TEST to compile stand-alone test harness.
*/
#ifdef TEST
#include 
#include 
int main(int argc, char *argv[])
{
      size_t i;
      puts("Using longs");
      for (i = 1; i < argc; ++i)
      {
            long l = atol(argv[i]);
            printf("sgn(%ld) = %d\n", l, sgn(l));
      }
      puts("\nUsing doubles");
      for (i = 1; i < argc; ++i)
      {
            double l = atof(argv[i]);
            printf("sgn(%f) = %d\n", l, sgn(l));
      }
      return EXIT_SUCCESS;
}
#endif /* TEST */
--- QM v1.00
---------------
* Origin: MicroFirm : Down to the C in chips (1:106/2000.6)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.