www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to detect overflow

reply Namal <sotis22 mail.ru> writes:
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
parent reply BBasile <bb.temp gmx.com> writes:
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);

 -> 1073741824
You can use core.checkedint [1] --- http://dlang.org/phobos/core_checkedint.html
Nov 03 2015
next sibling parent reply Namal <sotis22 mail.ru> writes:
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 there a way to detect overflow for example for:

 	int i = 2_000_000_000;
 	
 	int a = i*i*i;
 	
 	writeln(a);

 -> 1073741824
You can use core.checkedint [1] --- http://dlang.org/phobos/core_checkedint.html
Is it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Nov 03 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/03/2015 10:34 PM, Namal wrote:

 http://dlang.org/phobos/core_checkedint.html
Is it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Yeah, looks like classic copy-paste errors. :) Ali
Nov 03 2015
parent reply BBasile <bb.temp gmx.com> writes:
On Wednesday, 4 November 2015 at 07:19:09 UTC, Ali Çehreli wrote:
 On 11/03/2015 10:34 PM, Namal wrote:

 http://dlang.org/phobos/core_checkedint.html
Is it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Yeah, looks like classic copy-paste errors. :) Ali
I take the token for this ddoc fix: https://github.com/D-Programming-Language/druntime/pull/1429
Nov 03 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/03/2015 11:34 PM, BBasile wrote:
 On Wednesday, 4 November 2015 at 07:19:09 UTC, Ali Çehreli wrote:
 On 11/03/2015 10:34 PM, Namal wrote:

 http://dlang.org/phobos/core_checkedint.html
Is it just an error in the documentation that the return value is stated as sum for the multiplication functions?
Yeah, looks like classic copy-paste errors. :) Ali
I take the token for this ddoc fix: https://github.com/D-Programming-Language/druntime/pull/1429
Thanks. I've noticed that the parameter of the subtraction functions should be named 'underflow', no? Ali
Nov 04 2015
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
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
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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. 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.
Thanks. It looks like I've been making stuff up on this page: :( http://ddili.org/ders/d.en/arithmetic.html Ali
Nov 04 2015
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
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.html
It's a common source for confusion, the word "underflow" is a bit misleading. Maybe better to use the term "zero-flushed".
Nov 04 2015
prev sibling parent reply Namal <sotis22 mail.ru> writes:
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 there a way to detect overflow for example for:

 	int i = 2_000_000_000;
 	
 	int a = i*i*i;
 	
 	writeln(a);

 -> 1073741824
You can use core.checkedint [1] --- http://dlang.org/phobos/core_checkedint.html
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.
Nov 03 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/03/2015 11:52 PM, Namal wrote:

 http://dlang.org/phobos/core_checkedint.html
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.
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); } Ali
Nov 03 2015
parent reply Namal <sotis22 mail.ru> writes:
On Wednesday, 4 November 2015 at 07:59:44 UTC, Ali Çehreli wrote:
 On 11/03/2015 11:52 PM, Namal wrote:

 http://dlang.org/phobos/core_checkedint.html
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.
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); } Ali
wow, this I don't understand at all, how do those two operations connected to each other? By the bool value?
Nov 04 2015
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/04/2015 12:11 AM, 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);
 }

 Ali
wow, this I don't understand at all, how do those two operations connected to each other? By the bool value?
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. :) ) Ali
Nov 04 2015