Re: Question about ubyte x overflow, any safe way?
On 08/04/2019 11:12 AM, matheus wrote: Hi, The snippet below will produce an "infinite loop" because obviously "ubyte u" will overflow after 255: import std.stdio; void main(){ ubyte u = 250; for(;u<256;++u){ writeln(u); } } Question: Is there a way (Flag) to prevent this? Matheus. Two examples with foreach and ranges. The 'ubyte.max + 1' expression is int. The compiler casts to ubyte (because we typed ubyte) in the foreach and we cast to ubyte in the range: void main() { int count = 0; // (Explicit request for) implicit conversion to ubyte foreach (ubyte u; ubyte.min .. ubyte.max + 1) { ++count; } assert(count == 256); import std.range; import std.algorithm; import std.conv; int count2 = 0; // Explicit conversion with to!ubyte iota(ubyte.max + 1).map!(to!ubyte).each!(_ => ++count2); assert(count2 == 256); } Ali
Re: Question about ubyte x overflow, any safe way?
On Sunday, 4 August 2019 at 18:38:34 UTC, Paul Backus wrote: ... Use std.experimental.checkedint: import std.stdio; import std.experimental.checkedint; void main() { for(Checked!(ubyte, Throw) u = ubyte(250); u < 256; ++u) { writeln(u.get); } } An exception will be thrown when you attempt to increment u above 255. Unfortunately I'm using DMD 2.072 which doesn't support this, but I'll upgrade soon as I can and will check this out. Thanks, Matheus.
Re: Question about ubyte x overflow, any safe way?
On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote: On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when overflow or even an warning (Only if "some" flag was active). If you want to prevent overflow you must either use BigInt or wrap ubyte in a struct that doesn't allow overflow Could you please elaborate about this struct wrapping? Do you mean manually check on change? Matheus. Std.experimental.checkedint maybe exactly what you are looking for
Re: Question about ubyte x overflow, any safe way?
On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote: On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when overflow or even an warning (Only if "some" flag was active). Use std.experimental.checkedint: import std.stdio; import std.experimental.checkedint; void main() { for(Checked!(ubyte, Throw) u = ubyte(250); u < 256; ++u) { writeln(u.get); } } An exception will be thrown when you attempt to increment u above 255.
Re: Question about ubyte x overflow, any safe way?
On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when overflow or even an warning (Only if "some" flag was active). If you want to prevent overflow you must either use BigInt or wrap ubyte in a struct that doesn't allow overflow Could you please elaborate about this struct wrapping? Do you mean manually check on change? Matheus.
Re: Question about ubyte x overflow, any safe way?
On Sunday, 4 August 2019 at 18:12:48 UTC, matheus wrote: Hi, The snippet below will produce an "infinite loop" because obviously "ubyte u" will overflow after 255: import std.stdio; void main(){ ubyte u = 250; for(;u<256;++u){ writeln(u); } } Question: Is there a way (Flag) to prevent this? Matheus. What do you want to do? If you just want to count to 255 then use a foreach If you want to prevent overflow you must either use BigInt or wrap ubyte in a struct that doesn't allow overflow
Question about ubyte x overflow, any safe way?
Hi, The snippet below will produce an "infinite loop" because obviously "ubyte u" will overflow after 255: import std.stdio; void main(){ ubyte u = 250; for(;u<256;++u){ writeln(u); } } Question: Is there a way (Flag) to prevent this? Matheus.
Re: Help me decide D or C
On Saturday, 3 August 2019 at 12:29:18 UTC, Russel Winder wrote: Knowing many paradigms well is proven experimentally (see the work by Petre, Green, Gilmore, and others) to improve capability in any given language. So knowing Java, Prolog, Lisp, Python, SQL, C, Go, Rust, D, Kotlin, Groovy, Ruby to a goodly level of competence makes you a better programmer in any one of them. Thank you Russel Winder. Thanks to your comment, I was able to find the book you spoke of and ordered it immediately. For those who may be interested, here it is: Psychology of Programming (Computers and People Series), written by J-M Hoc, T.R.G. Green, R. samurcay, & D. Gilmore, published in 1991 by Academic Press. As for me, it is interesting to notice that the authors of this book work for Inria, the same French Institute who created F* language in partnership with Microsoft Research, along with Low*, a subset of F* language and its librairies, focused on C features (e.g. the C memory model, stack and heap-allocated arrays, machine integers, C string literals, etc.). See https://fstarlang.github.io/lowstar/html/Introduction.html for more details. It is really refreshing for the community to have someone like you who keep connections with other languages, technologies and paradigms. I wish you the best!
Re: Speed of Random Numbers
On Sun, Aug 4, 2019 at 11:49 AM Daniel Kozak wrote: > > You can try http://code.dlang.org/packages/mir-random > > I am using theme here: > https://github.com/TechEmpower/FrameworkBenchmarks/blob/b9cc153dcd1c20e78197b0191536f0d11b8ca554/frameworks/D/vibed/source/postgresql.d#L49 > > On Sun, Aug 4, 2019 at 12:20 AM Giovanni Di Maria via > Digitalmars-d-learn wrote: > > > > Thank you very much to Everybody! > > Giovanni > > You can try http://code.dlang.org/packages/mir-random I am using theme here: https://github.com/TechEmpower/FrameworkBenchmarks/blob/b9cc153dcd1c20e78197b0191536f0d11b8ca554/frameworks/D/vibed/source/postgresql.d#L49 And compile it with ldc
Re: Speed of Random Numbers
You can try http://code.dlang.org/packages/mir-random I am using theme here: https://github.com/TechEmpower/FrameworkBenchmarks/blob/b9cc153dcd1c20e78197b0191536f0d11b8ca554/frameworks/D/vibed/source/postgresql.d#L49 On Sun, Aug 4, 2019 at 12:20 AM Giovanni Di Maria via Digitalmars-d-learn wrote: > > Thank you very much to Everybody! > Giovanni >