www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - BASIC "sgn" function equivalent

reply pascal111 <judas.the.messiah.111 gmail.com> writes:
I'm making an equivalent of "sgn" function of BASIC language, and 
I used "(T)" in its definition, but the function can receive 
wrong data by passing string data to it, how we can solve it?

int sgn(T)(T x)
{

     if(x<0)
         return -1;
     else if(x>0)
         return 1;
     else
         return 0;

}
Jul 28 2022
next sibling parent reply Antonio <antonio abrevia.net> writes:
On Thursday, 28 July 2022 at 12:02:54 UTC, pascal111 wrote:
 I'm making an equivalent of "sgn" function of BASIC language, 
 and I used "(T)" in its definition, but the function can 
 receive wrong data by passing string data to it, how we can 
 solve it?
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
Jul 28 2022
next sibling parent Antonio <antonio abrevia.net> writes:
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; } ```
Jul 28 2022
prev sibling parent reply pascal111 <judas.the.messiah.111 gmail.com> writes:
On Thursday, 28 July 2022 at 12:13:31 UTC, Antonio wrote:
 On Thursday, 28 July 2022 at 12:02:54 UTC, pascal111 wrote:
 I'm making an equivalent of "sgn" function of BASIC language, 
 and I used "(T)" in its definition, but the function can 
 receive wrong data by passing string data to it, how we can 
 solve it?
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
Of-course D has a built in "sgn" function, but I like to do it with my hands to be sure of code, I'm some doubtful and don't trust alien works.
Jul 28 2022
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Thu, Jul 28, 2022 at 02:30:38PM +0000, pascal111 via Digitalmars-d-learn
wrote:
[...]
 Of-course D has a built in "sgn" function, but I like to do it with my
 hands to be sure of code, I'm some doubtful and don't trust alien
 works.
Just use the source, Luke! Phobos is open source for a reason. https://github.com/dlang/phobos/blob/8280b1e7de6cca4dc9a593431591054a5b3aa288/std/math/traits.d#L694 T -- Sometimes the best solution to morale problems is just to fire all of the unhappy people. -- despair.com
Jul 28 2022
next sibling parent reply pascal111 <judas.the.messiah.111 gmail.com> writes:
On Thursday, 28 July 2022 at 15:03:34 UTC, H. S. Teoh wrote:
 On Thu, Jul 28, 2022 at 02:30:38PM +0000, pascal111 via 
 Digitalmars-d-learn wrote: [...]
 Of-course D has a built in "sgn" function, but I like to do it 
 with my hands to be sure of code, I'm some doubtful and don't 
 trust alien works.
Just use the source, Luke! Phobos is open source for a reason. https://github.com/dlang/phobos/blob/8280b1e7de6cca4dc9a593431591054a5b3aa288/std/math/traits.d#L694 T
I think Phobos is the big library of D, is it the same one with gdc compiler too? I'm using gdc compiler.
Jul 28 2022
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Thu, Jul 28, 2022 at 03:10:19PM +0000, pascal111 via Digitalmars-d-learn
wrote:
 On Thursday, 28 July 2022 at 15:03:34 UTC, H. S. Teoh wrote:
 On Thu, Jul 28, 2022 at 02:30:38PM +0000, pascal111 via
 Digitalmars-d-learn wrote: [...]
 Of-course D has a built in "sgn" function, but I like to do it
 with my hands to be sure of code, I'm some doubtful and don't
 trust alien works.
Just use the source, Luke! Phobos is open source for a reason. https://github.com/dlang/phobos/blob/8280b1e7de6cca4dc9a593431591054a5b3aa288/std/math/traits.d#L694
[...]
 I think Phobos is the big library of D, is it the same one with gdc
 compiler too? I'm using gdc compiler.
AFAIK, all D compilers ship with full Phobos source code. On my installation, it's in /usr/lib/gcc/x86_64-linux-gnu/11/include/d/*. You can probably find yours in a similar (or the same) location. Under that directory, just look into std/math/*.d for the std.math functions. For example, .sgn is in std/math/traits.d. In general, all 3 D compilers ship with the same Phobos sources, except perhaps in a few places where some compiler-specific fixes were needed. Generally, though, I think the idea is to merge all such changes upstream under version() blocks so that we can use the same Phobos sources for all compilers. T -- What's the difference between a 4D tube and an overweight Dutchman? One is a hollow spherinder, and the other is a spherical Hollander.
Jul 28 2022
parent pascal111 <judas.the.messiah.111 gmail.com> writes:
On Thursday, 28 July 2022 at 15:38:18 UTC, H. S. Teoh wrote:
 On Thu, Jul 28, 2022 at 03:10:19PM +0000, pascal111 via 
 Digitalmars-d-learn wrote:
 [...]
[...]
 [...]
AFAIK, all D compilers ship with full Phobos source code. On my installation, it's in /usr/lib/gcc/x86_64-linux-gnu/11/include/d/*. You can probably find yours in a similar (or the same) location. Under that directory, just look into std/math/*.d for the std.math functions. For example, .sgn is in std/math/traits.d. In general, all 3 D compilers ship with the same Phobos sources, except perhaps in a few places where some compiler-specific fixes were needed. Generally, though, I think the idea is to merge all such changes upstream under version() blocks so that we can use the same Phobos sources for all compilers. T
I checked this path "/usr/lib/gcc/x86_64-linux-gnu/11/include/d/*" yesterday in my Ubuntu. I think you are right. Thanks!
Jul 28 2022
prev sibling parent Antonio <antonio abrevia.net> writes:
On Thursday, 28 July 2022 at 15:03:34 UTC, H. S. Teoh wrote:
 Just use the source, Luke!  Phobos is open source for a reason.

 	https://github.com/dlang/phobos/blob/8280b1e7de6cca4dc9a593431591054a5b3aa288/std/math/traits.d#L694


 T
:-) ```d // TODO : make this faster ```
Jul 28 2022
prev sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 28 July 2022 at 12:02:54 UTC, pascal111 wrote:
 I'm making an equivalent of "sgn" function of BASIC language, 
 and I used "(T)" in its definition, but the function can 
 receive wrong data by passing string data to it, how we can 
 solve it?
There's no need to do the complication of a template, just do a normal function taking a `double`. You can add overloads if you want like one taking a `long` and/or `int` but those would cover all types that have signs.
Jul 28 2022