digitalmars.D.learn - Why does this not work?
- Shachar (12/12) Sep 17 2014 void func( int c )
- flamencofantasy (7/7) Sep 17 2014 the result of ubyte + ubyte is int I believe.
- Shachar (8/15) Sep 17 2014 From http://dlang.org/type, under Usual Arithmetic Conversions:
- ketmar via Digitalmars-d-learn (5/9) Sep 17 2014 On Wed, 17 Sep 2014 13:20:13 +0000
- Shachar Shemesh (5/14) Sep 17 2014 I don't understand. Why is this behavior preferrable to the one outlined...
- flamencofantasy (3/24) Sep 17 2014 Because of overflow.
- ketmar via Digitalmars-d-learn (5/7) Sep 17 2014 On Wed, 17 Sep 2014 16:36:41 +0300
- anonymous (7/26) Sep 17 2014 You missed that "integer promotions" are done first.
- flamencofantasy (1/1) Sep 17 2014 http://www.drdobbs.com/tools/value-range-propagation/229300211
void func( int c ) { ubyte a; a = cast(ubyte)c + cast(ubyte)c; } dmd v2.065 complains about: test.d(5): Error: cannot implicitly convert expression (cast(int)cast(ubyte)c + cast(int)cast(ubyte)c) of type int to ubyte As far as I can tell, adding two values of the same type should result in the same type. Shachar
Sep 17 2014
the result of ubyte + ubyte is int I believe. Try; void func( int c ) { ubyte a; a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c); }
Sep 17 2014
On Wednesday, 17 September 2014 at 13:03:05 UTC, flamencofantasy wrote:the result of ubyte + ubyte is int I believe. Try; void func( int c ) { ubyte a; a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c); }From http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done. So, as far as I can see, the specs disagree with you. Shachar
Sep 17 2014
On Wed, 17 Sep 2014 13:20:13 +0000 Shachar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:From http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed=20 by: 1. If both are the same type, no more conversions are done.it's bug in specs, i believe, 'cause compiler promotes smaller types to int/uint.
Sep 17 2014
On 17/09/14 16:32, ketmar via Digitalmars-d-learn wrote:On Wed, 17 Sep 2014 13:20:13 +0000 Shachar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I don't understand. Why is this behavior preferrable to the one outlined by the specs? Thanks, ShacharFrom http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done.it's bug in specs, i believe, 'cause compiler promotes smaller types to int/uint.
Sep 17 2014
Because of overflow. On Wednesday, 17 September 2014 at 13:36:42 UTC, Shachar Shemesh wrote:On 17/09/14 16:32, ketmar via Digitalmars-d-learn wrote:On Wed, 17 Sep 2014 13:20:13 +0000 Shachar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I don't understand. Why is this behavior preferrable to the one outlined by the specs? Thanks, ShacharFrom http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done.it's bug in specs, i believe, 'cause compiler promotes smaller types to int/uint.
Sep 17 2014
On Wed, 17 Sep 2014 16:36:41 +0300 Shachar Shemesh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I don't understand. Why is this behavior preferrable to the one outlined by the specs?'cause C does exactly this. there is no reason to confuse people with C background by silently removing such feature.
Sep 17 2014
On Wednesday, 17 September 2014 at 13:20:15 UTC, Shachar wrote:On Wednesday, 17 September 2014 at 13:03:05 UTC, flamencofantasy wrote:You missed that "integer promotions" are done first. <http://dlang.org/type.html#Integer%20Promotions>:the result of ubyte + ubyte is int I believe. Try; void func( int c ) { ubyte a; a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c); }From http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done. So, as far as I can see, the specs disagree with you.Integer Promotions are conversions of the following types:[...]from to[...]ubyte int[...] So, integer promotions turn ubyte + ubyte into int + int.
Sep 17 2014
http://www.drdobbs.com/tools/value-range-propagation/229300211
Sep 17 2014