digitalmars.D.learn - BASIC "sgn" function equivalent
- pascal111 (12/12) Jul 28 2022 I'm making an equivalent of "sgn" function of BASIC language, and
- Antonio (4/8) Jul 28 2022 Use isFloating!T and isIntegral!T traits.
- Antonio (21/24) Jul 28 2022 ```d
- pascal111 (4/12) Jul 28 2022 Of-course D has a built in "sgn" function, but I like to do it
- H. S. Teoh (7/10) Jul 28 2022 Just use the source, Luke! Phobos is open source for a reason.
- pascal111 (3/11) Jul 28 2022 I think Phobos is the big library of D, is it the same one with
- H. S. Teoh (15/27) Jul 28 2022 AFAIK, all D compilers ship with full Phobos source code. On my
- pascal111 (4/22) Jul 28 2022 I checked this path
- Antonio (5/8) Jul 28 2022 :-)
- Adam D Ruppe (5/9) Jul 28 2022 There's no need to do the complication of a template, just do a
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
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
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
On Thursday, 28 July 2022 at 12:13:31 UTC, Antonio wrote:On Thursday, 28 July 2022 at 12:02:54 UTC, pascal111 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.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
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
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: [...]I think Phobos is the big library of D, is it the same one with gdc compiler too? I'm using gdc compiler.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
Jul 28 2022
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#L694I 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
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:I checked this path "/usr/lib/gcc/x86_64-linux-gnu/11/include/d/*" yesterday in my Ubuntu. I think you are right. Thanks![...][...][...]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
Jul 28 2022
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
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