digitalmars.D.learn - How to detect overflow
- Namal (7/7) Nov 03 2015 Is there a way to detect overflow for example for:
- BBasile (4/11) Nov 03 2015 You can use core.checkedint [1]
- Namal (3/16) Nov 03 2015 Is it just an error in the documentation that the return value is
- =?UTF-8?Q?Ali_=c3=87ehreli?= (3/6) Nov 03 2015 Yeah, looks like classic copy-paste errors. :)
- BBasile (3/11) Nov 03 2015 I take the token for this ddoc fix:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/17) Nov 04 2015 Thanks. I've noticed that the parameter of the subtraction functions
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (7/9) Nov 04 2015 Integer math cannot underflow, unless you define division to be
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/13) Nov 04 2015 Thanks. It looks like I've been making stuff up on this page: :(
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (3/5) Nov 04 2015 It's a common source for confusion, the word "underflow" is a bit
- Namal (6/19) Nov 03 2015 It says:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (9/15) Nov 03 2015 import core.checkedint;
- Namal (3/25) Nov 04 2015 wow, this I don't understand at all, how do those two operations
- =?UTF-8?Q?Ali_=c3=87ehreli?= (15/28) Nov 04 2015 The 'overflow' parameter is a 'ref' parameter, meaning that these
Is there a way to detect overflow for example for: int i = 2_000_000_000; int a = i*i*i; writeln(a); -> 1073741824
Nov 03 2015
On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:Is there a way to detect overflow for example for: int i = 2_000_000_000; int a = i*i*i; writeln(a); -> 1073741824You can use core.checkedint [1] --- http://dlang.org/phobos/core_checkedint.html
Nov 03 2015
On Wednesday, 4 November 2015 at 04:22:03 UTC, BBasile wrote:On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:Is it just an error in the documentation that the return value is stated as sum for the multiplication functions?Is there a way to detect overflow for example for: int i = 2_000_000_000; int a = i*i*i; writeln(a); -> 1073741824You can use core.checkedint [1] --- http://dlang.org/phobos/core_checkedint.html
Nov 03 2015
On 11/03/2015 10:34 PM, Namal wrote:Yeah, looks like classic copy-paste errors. :) Alihttp://dlang.org/phobos/core_checkedint.htmlIs it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Nov 03 2015
On Wednesday, 4 November 2015 at 07:19:09 UTC, Ali Çehreli wrote:On 11/03/2015 10:34 PM, Namal wrote:I take the token for this ddoc fix: https://github.com/D-Programming-Language/druntime/pull/1429Yeah, looks like classic copy-paste errors. :) Alihttp://dlang.org/phobos/core_checkedint.htmlIs it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Nov 03 2015
On 11/03/2015 11:34 PM, BBasile wrote:On Wednesday, 4 November 2015 at 07:19:09 UTC, Ali Çehreli wrote:Thanks. I've noticed that the parameter of the subtraction functions should be named 'underflow', no? AliOn 11/03/2015 10:34 PM, Namal wrote:I take the token for this ddoc fix: https://github.com/D-Programming-Language/druntime/pull/1429Yeah, looks like classic copy-paste errors. :) Alihttp://dlang.org/phobos/core_checkedint.htmlIs it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Nov 04 2015
On Wednesday, 4 November 2015 at 08:18:00 UTC, Ali Çehreli wrote:Thanks. I've noticed that the parameter of the subtraction functions should be named 'underflow', no?Integer math cannot underflow, unless you define division to be equivalent to division over reals. overflow => higher/lower than max/min underflow => wrongly rounded to zero (smaller than epsilon) Underflow can lead to unexpected division by zero errors in float expressions.
Nov 04 2015
On 11/04/2015 02:01 AM, Ola Fosheim Grøstad wrote:On Wednesday, 4 November 2015 at 08:18:00 UTC, Ali Çehreli wrote:Thanks. It looks like I've been making stuff up on this page: :( http://ddili.org/ders/d.en/arithmetic.html AliThanks. I've noticed that the parameter of the subtraction functions should be named 'underflow', no?Integer math cannot underflow, unless you define division to be equivalent to division over reals. overflow => higher/lower than max/min underflow => wrongly rounded to zero (smaller than epsilon) Underflow can lead to unexpected division by zero errors in float expressions.
Nov 04 2015
On Wednesday, 4 November 2015 at 10:06:47 UTC, Ali Çehreli wrote:Thanks. It looks like I've been making stuff up on this page: :( http://ddili.org/ders/d.en/arithmetic.htmlIt's a common source for confusion, the word "underflow" is a bit misleading. Maybe better to use the term "zero-flushed".
Nov 04 2015
On Wednesday, 4 November 2015 at 04:22:03 UTC, BBasile wrote:On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:It says: "The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end." But how can I make multiple operations? I can only put 2 values in the function.Is there a way to detect overflow for example for: int i = 2_000_000_000; int a = i*i*i; writeln(a); -> 1073741824You can use core.checkedint [1] --- http://dlang.org/phobos/core_checkedint.html
Nov 03 2015
On 11/03/2015 11:52 PM, Namal wrote:import core.checkedint; void main() { bool overflowed; auto result = adds(int.max, 1, overflowed); // this overflows adds(1, 2, overflowed); // this does not reset the flag assert(overflowed); } Alihttp://dlang.org/phobos/core_checkedint.htmlIt says: "The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end." But how can I make multiple operations? I can only put 2 values in the function.
Nov 03 2015
On Wednesday, 4 November 2015 at 07:59:44 UTC, Ali Çehreli wrote:On 11/03/2015 11:52 PM, Namal wrote:wow, this I don't understand at all, how do those two operations connected to each other? By the bool value?import core.checkedint; void main() { bool overflowed; auto result = adds(int.max, 1, overflowed); // this overflows adds(1, 2, overflowed); // this does not reset the flag assert(overflowed); } Alihttp://dlang.org/phobos/core_checkedint.htmlIt says: "The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end." But how can I make multiple operations? I can only put 2 values in the function.
Nov 04 2015
On 11/04/2015 12:11 AM, Namal wrote:The 'overflow' parameter is a 'ref' parameter, meaning that these functions modify the actual 'overflowed' variable above. You can imagine that they do not set it to 'false' ever: Imagine that adds() is implemented like the following: pure nothrow nogc safe int adds(int x, int y, ref bool overflow) { // ... if (over_flow_detected) { overflow = true; } } So, the caller's variable can only go from 'false' to 'true', never the other way around. (All of this is just a guess. :) ) Aliimport core.checkedint; void main() { bool overflowed; auto result = adds(int.max, 1, overflowed); // this overflows adds(1, 2, overflowed); // this does not reset the flag assert(overflowed); } Aliwow, this I don't understand at all, how do those two operations connected to each other? By the bool value?
Nov 04 2015