www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - It is possible to substract 5 from 3 unsigned integer

reply Alaindevos <devosalain ymail.com> writes:
Is that the expected behavior of the programmer?
Opinions can differ. Feel free to elaborate.
Oct 06 2020
next sibling parent Alaindevos <devosalain ymail.com> writes:
On Tuesday, 6 October 2020 at 12:24:56 UTC, Alaindevos wrote:
 Is that the expected behavior of the programmer?
 Opinions can differ. Feel free to elaborate.
E.g. length of a string unsigned long.
Oct 06 2020
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 6 October 2020 at 12:24:56 UTC, Alaindevos wrote:
 Is that the expected behavior of the programmer?
 Opinions can differ. Feel free to elaborate.
Elaborate please. Are you really asking if one can do subtraction in D.
Oct 06 2020
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On Tuesday, 6 October 2020 at 12:24:56 UTC, Alaindevos wrote:
 Is that the expected behavior of the programmer?
 Opinions can differ. Feel free to elaborate.
It's expected behavior: "If both operands are of integral types and an overflow or underflow occurs in the computation, wrapping will happen. For example, uint.max + 1 == uint.min, uint.min - 1 == uint.max, int.max + 1 == int.min, and int.min - 1 == int.max." https://dlang.org/spec/expression.html#add_expressions
Oct 06 2020
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/6/20 5:24 AM, Alaindevos wrote:
 Is that the expected behavior of the programmer?
 Opinions can differ. Feel free to elaborate.
The following is even more "expected". ;) Subtract zero from -1 and you get size_t.max. void main() { int[] arr; int i = -1; auto u = (i - arr.length); // -1 - 0 assert(u == size_t.max); // the surprise static assert (is (typeof(u) == size_t)); // the reason } Ali
Oct 06 2020
parent reply Alaindevos <devosalain ymail.com> writes:
There are two subtractions possible.
A machine-one which can be architecture dependent, does not have 
the same results on all computers, and behaves like a modulus in 
mathematics.
A logical one. For the last one higher classes might be needed.
Oct 06 2020
next sibling parent ikod <igor.khasilev gmail.com> writes:
On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:
 There are two subtractions possible.
 A machine-one which can be architecture dependent, does not 
 have the same results on all computers, and behaves like a 
 modulus in mathematics.
 A logical one. For the last one higher classes might be needed.
Hello, You may try https://dlang.org/phobos/std_experimental_checkedint.html in this case. Regards,
Oct 06 2020
prev sibling next sibling parent Johan <j j.nl> writes:
On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:
 There are two subtractions possible.
 A machine-one which can be architecture dependent, does not 
 have the same results on all computers, and behaves like a 
 modulus in mathematics.
 A logical one. For the last one higher classes might be needed.
The (signed and unsigned) integer wrap around is well-defined in D, and is architecture independent. That means for example that if a specific architecture does not have a subtract instruction that wraps around according to what D specifies, then the compiler will generate the necessary instructions such that still int.min - 1 == int.max. That's similar to how integer multiplication 'just works' on architectures that do not have a multiply instruction. -Johan
Oct 06 2020
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:
 There are two subtractions possible.
 A machine-one which can be architecture dependent, does not 
 have the same results on all computers, and behaves like a 
 modulus in mathematics.
 A logical one. For the last one higher classes might be needed.
Or use BigInt
Oct 08 2020
prev sibling parent Kagamin <spam here.lot> writes:
On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:
 A logical one. For the last one higher classes might be needed.
Also assert(5/3==1);
Oct 08 2020