BASIC "sgn" function equivalent

Antonio antonio at abrevia.net
Thu Jul 28 12:26:31 UTC 2022


On Thursday, 28 July 2022 at 12:13:31 UTC, Antonio wrote:

>
>
> Use isFloating!T and isIntegral!T traits.
>
> The standard library **sng** function is a good example:
>
> https://dlang.org/library/std/math/traits/sgn.html

```d
import std.traits : isFloatingPoint, isIntegral;

int sgn(T)(T x) if (isFloatingPoint!T || isIntegral!T)
{
   if(x<0)
     return -1;
   else if(x>0)
     return 1;
   else
     return 0;
}

void main()
{
    import std.stdio;
	
    writeln(sgn(-1234));
    sgn(-1213.32).writeln;
    1234.sgn.writeln;
}
```


More information about the Digitalmars-d-learn mailing list