Re: Is there a formula for overflow?

2022-11-30 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 30, 2022 at 03:07:44AM +, thebluepandabear via Digitalmars-d-learn wrote: > I am reading through Ali's book about D, and he gives the following > examples for an overflow: [...] > The result overflows and is 1705032704. > > Also for the second example, it overflows and comes up

Re: Is there a formula for overflow?

2022-11-30 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 11:40:36 UTC, thebluepandabear wrote: then the narrower value is converted to the wider type **C.** If the signed type is wider than the unsigned type, then the unsigned value is converted to the signed type **D.** Otherwise the signed type is converted to the

Re: Is there a formula for overflow?

2022-11-30 Thread Ali Çehreli via Digitalmars-d-learn
On 11/29/22 19:07, thebluepandabear wrote: > But the book doesn't talk about why the D compiler came up with these > results The compiler doesn't do anything special. It's all about the lack of bits to store the value. If the result needs 33 bits but the type has only 32 bits, the

Re: Is there a formula for overflow?

2022-11-30 Thread thebluepandabear via Digitalmars-d-learn
then the narrower value is converted to the wider type **C.** If the signed type is wider than the unsigned type, then the unsigned value is converted to the signed type **D.** Otherwise the signed type is converted to the unsigned type SDB@79 I didn't ask about casting...

Re: Is there a formula for overflow?

2022-11-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 03:07:44 UTC, thebluepandabear wrote: I am curious as to what formula the D compiler uses for calculating 'overflowed' values, if such thing exists? :) Regards, thebluepandabear **Source:**

Re: Is there a formula for overflow?

2022-11-29 Thread Siarhei Siamashka via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 03:19:49 UTC, Basile B. wrote: writeln((30LU + 30LU) % uint.max); It's actually writeln((30LU + 30LU) % (uint.max.to!ulong + 1)); or writeln((30LU + 30LU) & uint.max);

Re: Is there a formula for overflow?

2022-11-29 Thread Basile B. via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 03:19:49 UTC, Basile B. wrote: [...] It's always a wraparound (think modulo) but your examples with negative number can be explained because there are hidden unsigned to signed implicit convertions. That the only special things D does. forgot to say, you

Re: Is there a formula for overflow?

2022-11-29 Thread Basile B. via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 03:07:44 UTC, thebluepandabear wrote: I am reading through Ali's book about D, and he gives the following examples for an overflow: ```D import std.stdio; void main() { // 3 billion each uint number_1 = 30; uint number_2 = 30; }

Is there a formula for overflow?

2022-11-29 Thread thebluepandabear via Digitalmars-d-learn
I am reading through Ali's book about D, and he gives the following examples for an overflow: ```D import std.stdio; void main() { // 3 billion each uint number_1 = 30; uint number_2 = 30; } writeln("maximum value of uint: ", uint.max); writeln(" number_1: ", number_1);