Re: BASIC "sgn" function equivalent

2022-07-28 Thread pascal111 via Digitalmars-d-learn
On Thursday, 28 July 2022 at 15:38:18 UTC, H. S. Teoh wrote: On Thu, Jul 28, 2022 at 03:10:19PM +, 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

Re: BASIC "sgn" function equivalent

2022-07-28 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jul 28, 2022 at 03:10:19PM +, 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 +, pascal111 via > > Digitalmars-d-learn wrote: [...] > > > Of-course D has a built in "sgn" function, but I

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Antonio via Digitalmars-d-learn
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 ```

Re: BASIC "sgn" function equivalent

2022-07-28 Thread pascal111 via Digitalmars-d-learn
On Thursday, 28 July 2022 at 15:03:34 UTC, H. S. Teoh wrote: On Thu, Jul 28, 2022 at 02:30:38PM +, 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.

Re: BASIC "sgn" function equivalent

2022-07-28 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jul 28, 2022 at 02:30:38PM +, 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 re

Re: BASIC "sgn" function equivalent

2022-07-28 Thread pascal111 via Digitalmars-d-learn
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 ca

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Adam D Ruppe via Digitalmars-d-learn
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

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Antonio via Digitalmars-d-learn
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 (isFloat

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Antonio via Digitalmars-d-learn
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.

BASIC "sgn" function equivalent

2022-07-28 Thread pascal111 via Digitalmars-d-learn
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 retu