Jump to page: 1 2
Thread overview
How to detect overflow
Nov 04, 2015
Namal
Nov 04, 2015
BBasile
Nov 04, 2015
Namal
Nov 04, 2015
Ali Çehreli
Nov 04, 2015
BBasile
Nov 04, 2015
Ali Çehreli
Nov 04, 2015
Ali Çehreli
Nov 04, 2015
Namal
Nov 04, 2015
Ali Çehreli
Nov 04, 2015
Namal
Nov 04, 2015
Ali Çehreli
November 04, 2015
Is there a way to detect overflow for example for:

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

-> 1073741824
November 04, 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);
>
> -> 1073741824

You can use core.checkedint [1]

---

http://dlang.org/phobos/core_checkedint.html
November 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:
>> 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?
November 04, 2015
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

November 04, 2015
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
November 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:
>> 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.


November 04, 2015
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

November 04, 2015
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?

November 04, 2015
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

November 04, 2015
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

« First   ‹ Prev
1 2