TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JERRY COFFIN
from: BOB STOUT
date: 1997-07-15 01:10:00
subject: Signs of numbers

On , Jerry Coffin (1:128/166.5@fidonet) wrote: 
 RS>   The bottom line is that a single function...
 RS> int sgn(double x)
 RS> {
 RS>       if (x > 0.0)
 RS>             return 1;
 RS>       if (x < 0.0)
 RS>             return -1;
 RS>       else  return 0;
 RS> }
 RS> ...would work just fine in C, but in C++, either a template
 RS> implementation or overladed functions would be best.
 > I think in this case a templated function is preferrable to an
 > overloaded function:
 > template
 > int sign(const T &x) {
 >     if ( x > 0)
 >         return 1;
 >     else if ( x < 0)
 >         return -1;
 >     return 0;
 > }
Jerry...
  The original code I posted in this thread began:
#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 */
  /* Overloaded C++ functions and C version follow */
  The message you quoted specifically referred to the C version (which used a 
macro to control whether to use an unsafe macro for the C implementation). 
Sorry, we should have taken it to the C_Echo instead.
 > This should work for any type for which comparison to 0 and having a
 > sign makes sense.  The only problem that might arise would be things
 > like smart pointers that define comparison to 0 only in terms of equal
 > and not equal, rather than less than/greater than.  In particular, I've
 > seen a few that would do things like saying a pointer was both less than
 > AND greater than 0, as long as it wasn't equal to 0.  In this case,
 > the "sign" you obtained for that pointer type wouldn't depend on the
 > pointer itself, but on the order in which you did the comparisons.
 > These might handle the comparison by defining a conversion of int to the
 > smart pointer, then compare the two, in which case the comparison might
 > not work very well.  Strongly preferrable is for the smart pointer to
 > explicitly define the comparison operators it supports (==(int) and
 > !=(int)) and make the conversion from int to smart pointer explicit,
 > meaning you'd have to use `T(0)' to get 0 converted to a pointer.
 > Unfortunately, there are still quite a few compilers around that don't
 > support the explicit keyword.
  Good point. I suppose it should be noted in the code that anyone wishing to 
use the template version to compare such pointers should try it before 
trusting it. 
--- 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™.