Re: abs and minimum values

2021-10-31 Thread Siarhei Siamashka via Digitalmars-d-learn
On Thursday, 28 October 2021 at 21:23:15 UTC, kyle wrote: ``` void main() { import std.math : abs, sgn; alias n_type = short; //or int, long, byte, whatever assert(n_type.min == abs(n_type.min)); assert(sgn(abs(n_type.min)) == -1); } ``` I stumbled into this fun today. I

Re: abs and minimum values

2021-10-31 Thread Imperatorn via Digitalmars-d-learn
On Friday, 29 October 2021 at 08:05:35 UTC, Dom DiSc wrote: On Thursday, 28 October 2021 at 21:26:04 UTC, kyle wrote: Okay I checked the phobos docs and it does say "Limitations Does not work correctly for signed intergal types and value Num.min." Should have looked there first, I know. Still

Re: Does associative array change the location of values?

2021-10-31 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 30, 2021 at 07:56:35PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 10/30/21 3:47 PM, Elronnd wrote: > > > If the GC were moving, it would also have to move the pointers you > > took to AA elements. > > I doubt D's GC can ever change pointer values because the values may >

Re: abs and minimum values

2021-10-31 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 31 October 2021 at 10:12:49 UTC, Siarhei Siamashka wrote: On Sunday, 31 October 2021 at 05:04:33 UTC, Dom DiSc wrote: On Friday, 29 October 2021 at 14:20:09 UTC, Ali Çehreli wrote: [...] This should be no surprise. You need to know what the resulting type of int + uint should be.

The type inference everywhere

2021-10-31 Thread Salih Dincer via Digitalmars-d-learn
```d auto foo(int value, auto s = Section(2, 60)) { int max; /* ^--- ? ...*/ return Section (0, max) } ``` Is possible something like above pointed. OK, I know it isn't because I tried! Well, wouldn't it be nice if it did? Why shouldn't the inference be

Re: abs and minimum values

2021-10-31 Thread Siarhei Siamashka via Digitalmars-d-learn
On Sunday, 31 October 2021 at 05:04:33 UTC, Dom DiSc wrote: On Friday, 29 October 2021 at 14:20:09 UTC, Ali Çehreli wrote: Unsigned!T abs(T)(const(T) x) if(isIntegral!T) { static if(isSigned!T) if(x < 0) return cast(Unsigned!T)-x; return x; } void main() { int a = -5; int b = -4;

Re: Does associative array change the location of values?

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 9:49 AM, H. S. Teoh wrote: > The current spec explicitly states that masking pointers this way is UB. Ok. :) What about unions? union U { ulong u; void* p; } Can the GC deal with that? Or other smart ways where I store the pointer in two parts as page+offset? (Perhaps that's

srand in D

2021-10-31 Thread pascal111 via Digitalmars-d-learn
Hi! I'm C learner and found the high similarity between C and D, and was converting C code into D but couldn't get the equivalent of this C statement "srand(time(NULL));".

Re: abs and minimum values

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/30/21 10:04 PM, Dom DiSc wrote: > On Friday, 29 October 2021 at 14:20:09 UTC, Ali Çehreli wrote: >> Unsigned!T abs(T)(const(T) x) if(isIntegral!T) >> { >>static if(isSigned!T) if(x < 0) return cast(Unsigned!T)-x; >>return x; >> } >> >> void main() { >> int a = -5; >> int b = -4;

Re: abs and minimum values

2021-10-31 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Sunday, 31 October 2021 at 05:04:33 UTC, Dom DiSc wrote: This should be no surprise. You need to know what the resulting type of int + uint should be. And it is .. uint! which is one of the stupit integer-promotion rules inherited from C. In C++ it is undefined behaviour to take the

Re: abs and minimum values

2021-10-31 Thread Elronnd via Digitalmars-d-learn
On Sunday, 31 October 2021 at 10:32:50 UTC, Imperatorn wrote: What I would like is for it to mirror math. Use bigints.

Re: srand in D

2021-10-31 Thread pascal111 via Digitalmars-d-learn
On Sunday, 31 October 2021 at 17:02:00 UTC, Ali Çehreli wrote: On 10/31/21 9:54 AM, pascal111 wrote: Hi! I'm C learner and found the high similarity between C and D, and was converting C code into D Welcome! :) In case it makes to your use cases, check out -betterC as well:

Re: The type inference everywhere

2021-10-31 Thread data pulverizer via Digitalmars-d-learn
On Sunday, 31 October 2021 at 18:51:09 UTC, user1234 wrote: To me it is the right answer. Maybe that OP wanted the TemplateType parameter to be implicitly added (and that's why Ali interpreted the question as a language proposal)? Ah, I see. Interesting proposal.

Re: srand in D

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 9:54 AM, pascal111 wrote: Hi! I'm C learner and found the high similarity between C and D, and was converting C code into D Welcome! :) In case it makes to your use cases, check out -betterC as well: https://dlang.org/spec/betterc.html > but couldn't get the equivalent of this

Re: Does associative array change the location of values?

2021-10-31 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Oct 31, 2021 at 09:54:23AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 10/31/21 9:49 AM, H. S. Teoh wrote: > > > The current spec explicitly states that masking pointers this way is UB. > > Ok. :) What about unions? > > union U { > ulong u; > void* p; > } > > Can the GC

Re: The type inference everywhere

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 7:07 AM, Salih Dincer wrote: > ```d > auto foo(int value, auto s = Section(2, 60)) { > int max; /* ^--- ? > ...*/ > return Section (0, max) > } > ``` > Is possible something like above pointed. OK, I know it isn't because I > tried! Well, wouldn't

Re: The type inference everywhere

2021-10-31 Thread data pulverizer via Digitalmars-d-learn
On Sunday, 31 October 2021 at 17:35:35 UTC, Ali Çehreli wrote: On 10/31/21 7:07 AM, Salih Dincer wrote: > ```d > auto foo(int value, auto s = Section(2, 60)) { > int max; /* ^--- ? > ...*/ > return Section (0, max) > } > ``` > Is possible something like above

Re: Does associative array change the location of values?

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 9:54 AM, Ali Çehreli wrote: Or other smart ways where I store the pointer in two parts as page+offset? Ok, that was silly. There would be no reference to the GC memory if I chopped off a pointer. :/ Ali

Re: srand in D

2021-10-31 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 31 October 2021 at 16:54:35 UTC, pascal111 wrote: Hi! I'm C learner and found the high similarity between C and D, and was converting C code into D but couldn't get the equivalent of this C statement "srand(time(NULL));". Since D gives you access to the C standard library, you can

Re: The type inference everywhere

2021-10-31 Thread user1234 via Digitalmars-d-learn
On Sunday, 31 October 2021 at 17:51:45 UTC, data pulverizer wrote: On Sunday, 31 October 2021 at 17:35:35 UTC, Ali Çehreli wrote: On 10/31/21 7:07 AM, Salih Dincer wrote: > [...] because I > [...] Makes sense because e.g. the following works: struct S { auto i = 42; } I bet the problem

Re: The type inference everywhere

2021-10-31 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 31 October 2021 at 17:35:35 UTC, Ali Çehreli wrote: On 10/31/21 7:07 AM, Salih Dincer wrote: > ```d > auto foo(int value, auto s = Section(2, 60)) { > int max; /* ^--- ? > ...*/ > return Section (0, max) > } > ``` > Is possible something like above